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 | /* For psa_can_do_hash() */ |
| 19 | #include "psa_crypto_core.h" |
| 20 | |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 21 | #include "test/asn1_helpers.h" |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 22 | #include "test/psa_crypto_helpers.h" |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 23 | #include "test/psa_exercise_key.h" |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 24 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 25 | #include "test/drivers/test_driver.h" |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 26 | #define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION |
| 27 | #else |
| 28 | #define TEST_DRIVER_LOCATION 0x7fffff |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 29 | #endif |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 30 | |
Ryan | 3a1b786 | 2024-03-01 17:24:04 +0000 | [diff] [blame] | 31 | #if defined(MBEDTLS_THREADING_PTHREAD) |
| 32 | #include "mbedtls/threading.h" |
| 33 | #endif |
| 34 | |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 35 | /* If this comes up, it's a bug in the test code or in the test data. */ |
| 36 | #define UNUSED 0xdeadbeef |
| 37 | |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 38 | /* Assert that an operation is (not) active. |
| 39 | * This serves as a proxy for checking if the operation is aborted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 40 | #define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0) |
| 41 | #define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 42 | |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 43 | #if defined(PSA_WANT_ALG_JPAKE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 44 | int ecjpake_operation_setup(psa_pake_operation_t *operation, |
| 45 | psa_pake_cipher_suite_t *cipher_suite, |
| 46 | psa_pake_role_t role, |
| 47 | mbedtls_svc_key_id_t key, |
| 48 | size_t key_available) |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 49 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 50 | PSA_ASSERT(psa_pake_abort(operation)); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 51 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 52 | PSA_ASSERT(psa_pake_setup(operation, cipher_suite)); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 53 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 54 | PSA_ASSERT(psa_pake_set_role(operation, role)); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 55 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 56 | if (key_available) { |
| 57 | PSA_ASSERT(psa_pake_set_password_key(operation, key)); |
| 58 | } |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 59 | return 0; |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 60 | exit: |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 61 | return 1; |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 62 | } |
| 63 | #endif |
| 64 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 65 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 66 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 67 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 68 | /** Test if a buffer contains a constant byte value. |
| 69 | * |
| 70 | * `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] | 71 | * |
| 72 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 73 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 74 | * \param size Size of the buffer in bytes. |
| 75 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 76 | * \return 1 if the buffer is all-bits-zero. |
| 77 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 78 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 79 | 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] | 80 | { |
| 81 | size_t i; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 82 | for (i = 0; i < size; i++) { |
| 83 | if (((unsigned char *) buffer)[i] != c) { |
| 84 | return 0; |
| 85 | } |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 86 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 87 | return 1; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 88 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 89 | #if defined(MBEDTLS_ASN1_WRITE_C) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 90 | /* 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] | 91 | static int asn1_write_10x(unsigned char **p, |
| 92 | unsigned char *start, |
| 93 | size_t bits, |
| 94 | unsigned char x) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 95 | { |
| 96 | int ret; |
| 97 | int len = bits / 8 + 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 98 | if (bits == 0) { |
| 99 | return MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 100 | } |
| 101 | if (bits <= 8 && x >= 1 << (bits - 1)) { |
| 102 | return MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 103 | } |
| 104 | if (*p < start || *p - start < (ptrdiff_t) len) { |
| 105 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 106 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 107 | *p -= len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 108 | (*p)[len-1] = x; |
| 109 | if (bits % 8 == 0) { |
| 110 | (*p)[1] |= 1; |
| 111 | } else { |
| 112 | (*p)[0] |= 1 << (bits % 8); |
| 113 | } |
| 114 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 115 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, |
| 116 | MBEDTLS_ASN1_INTEGER)); |
| 117 | return len; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 118 | } |
| 119 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 120 | static int construct_fake_rsa_key(unsigned char *buffer, |
| 121 | size_t buffer_size, |
| 122 | unsigned char **p, |
| 123 | size_t bits, |
| 124 | int keypair) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 125 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 126 | size_t half_bits = (bits + 1) / 2; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 127 | int ret; |
| 128 | int len = 0; |
| 129 | /* Construct something that looks like a DER encoding of |
| 130 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 131 | * RSAPrivateKey ::= SEQUENCE { |
| 132 | * version Version, |
| 133 | * modulus INTEGER, -- n |
| 134 | * publicExponent INTEGER, -- e |
| 135 | * privateExponent INTEGER, -- d |
| 136 | * prime1 INTEGER, -- p |
| 137 | * prime2 INTEGER, -- q |
| 138 | * exponent1 INTEGER, -- d mod (p-1) |
| 139 | * exponent2 INTEGER, -- d mod (q-1) |
| 140 | * coefficient INTEGER, -- (inverse of q) mod p |
| 141 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 142 | * } |
| 143 | * Or, for a public key, the same structure with only |
| 144 | * version, modulus and publicExponent. |
| 145 | */ |
| 146 | *p = buffer + buffer_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 147 | if (keypair) { |
| 148 | MBEDTLS_ASN1_CHK_ADD(len, /* pq */ |
| 149 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 150 | MBEDTLS_ASN1_CHK_ADD(len, /* dq */ |
| 151 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 152 | MBEDTLS_ASN1_CHK_ADD(len, /* dp */ |
| 153 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 154 | MBEDTLS_ASN1_CHK_ADD(len, /* q */ |
| 155 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 156 | MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */ |
| 157 | asn1_write_10x(p, buffer, half_bits, 3)); |
| 158 | MBEDTLS_ASN1_CHK_ADD(len, /* d */ |
| 159 | asn1_write_10x(p, buffer, bits, 1)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 160 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 161 | MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */ |
| 162 | asn1_write_10x(p, buffer, 17, 1)); |
| 163 | MBEDTLS_ASN1_CHK_ADD(len, /* n */ |
| 164 | asn1_write_10x(p, buffer, bits, 1)); |
| 165 | if (keypair) { |
| 166 | MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */ |
| 167 | mbedtls_asn1_write_int(p, buffer, 0)); |
| 168 | } |
| 169 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 170 | { |
| 171 | const unsigned char tag = |
| 172 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 173 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 174 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 175 | return len; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 176 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 177 | #endif /* MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 178 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 179 | int exercise_mac_setup(psa_key_type_t key_type, |
| 180 | const unsigned char *key_bytes, |
| 181 | size_t key_length, |
| 182 | psa_algorithm_t alg, |
| 183 | psa_mac_operation_t *operation, |
| 184 | psa_status_t *status) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 185 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 186 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 187 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 188 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 189 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 190 | psa_set_key_algorithm(&attributes, alg); |
| 191 | psa_set_key_type(&attributes, key_type); |
| 192 | PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 193 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 194 | *status = psa_mac_sign_setup(operation, key, alg); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 195 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 196 | PSA_ASSERT(psa_mac_abort(operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 197 | /* If setup failed, reproduce the failure, so that the caller can |
| 198 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 199 | if (*status != PSA_SUCCESS) { |
| 200 | TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 201 | } |
| 202 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 203 | psa_destroy_key(key); |
| 204 | return 1; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 205 | |
| 206 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 207 | psa_destroy_key(key); |
| 208 | return 0; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 209 | } |
| 210 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 211 | int exercise_cipher_setup(psa_key_type_t key_type, |
| 212 | const unsigned char *key_bytes, |
| 213 | size_t key_length, |
| 214 | psa_algorithm_t alg, |
| 215 | psa_cipher_operation_t *operation, |
| 216 | psa_status_t *status) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 217 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 218 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 219 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 221 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 222 | psa_set_key_algorithm(&attributes, alg); |
| 223 | psa_set_key_type(&attributes, key_type); |
| 224 | PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 225 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 226 | *status = psa_cipher_encrypt_setup(operation, key, alg); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 227 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 228 | PSA_ASSERT(psa_cipher_abort(operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 229 | /* If setup failed, reproduce the failure, so that the caller can |
| 230 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 231 | if (*status != PSA_SUCCESS) { |
| 232 | TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg), |
| 233 | *status); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 234 | } |
| 235 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 236 | psa_destroy_key(key); |
| 237 | return 1; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 238 | |
| 239 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 240 | psa_destroy_key(key); |
| 241 | return 0; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 242 | } |
| 243 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 244 | 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] | 245 | { |
| 246 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 247 | 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] | 248 | uint8_t buffer[1]; |
| 249 | size_t length; |
| 250 | int ok = 0; |
| 251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 252 | psa_set_key_id(&attributes, key_id); |
| 253 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 254 | psa_set_key_algorithm(&attributes, PSA_ALG_CTR); |
| 255 | psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); |
| 256 | TEST_EQUAL(psa_get_key_attributes(key, &attributes), |
| 257 | PSA_ERROR_INVALID_HANDLE); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 258 | TEST_EQUAL( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 259 | 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] | 260 | TEST_EQUAL( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 261 | MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0); |
| 262 | TEST_EQUAL(psa_get_key_lifetime(&attributes), 0); |
| 263 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0); |
| 264 | TEST_EQUAL(psa_get_key_algorithm(&attributes), 0); |
| 265 | TEST_EQUAL(psa_get_key_type(&attributes), 0); |
| 266 | TEST_EQUAL(psa_get_key_bits(&attributes), 0); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 267 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 268 | TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length), |
| 269 | PSA_ERROR_INVALID_HANDLE); |
| 270 | TEST_EQUAL(psa_export_public_key(key, |
| 271 | buffer, sizeof(buffer), &length), |
| 272 | PSA_ERROR_INVALID_HANDLE); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 273 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 274 | ok = 1; |
| 275 | |
| 276 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 277 | /* |
| 278 | * Key attributes may have been returned by psa_get_key_attributes() |
| 279 | * thus reset them as required. |
| 280 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 281 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 282 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 283 | return ok; |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 284 | } |
| 285 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 286 | /* Assert that a key isn't reported as having a slot number. */ |
| 287 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 288 | #define ASSERT_NO_SLOT_NUMBER(attributes) \ |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 289 | do \ |
| 290 | { \ |
| 291 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 292 | TEST_EQUAL(psa_get_key_slot_number( \ |
| 293 | attributes, \ |
| 294 | &ASSERT_NO_SLOT_NUMBER_slot_number), \ |
| 295 | PSA_ERROR_INVALID_ARGUMENT); \ |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 296 | } \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 297 | while (0) |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 298 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 299 | #define ASSERT_NO_SLOT_NUMBER(attributes) \ |
| 300 | ((void) 0) |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 301 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 302 | |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 303 | #define INPUT_INTEGER 0x10000 /* Out of range of psa_key_type_t */ |
| 304 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 305 | /* An overapproximation of the amount of storage needed for a key of the |
| 306 | * given type and with the given content. The API doesn't make it easy |
| 307 | * to find a good value for the size. The current implementation doesn't |
| 308 | * care about the value anyway. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 309 | #define KEY_BITS_FROM_DATA(type, data) \ |
| 310 | (data)->len |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 311 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 312 | typedef enum { |
| 313 | IMPORT_KEY = 0, |
| 314 | GENERATE_KEY = 1, |
| 315 | DERIVE_KEY = 2 |
| 316 | } generate_method; |
| 317 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 318 | typedef enum { |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 319 | DO_NOT_SET_LENGTHS = 0, |
| 320 | SET_LENGTHS_BEFORE_NONCE = 1, |
| 321 | SET_LENGTHS_AFTER_NONCE = 2 |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 322 | } set_lengths_method_t; |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 323 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 324 | typedef enum { |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 325 | USE_NULL_TAG = 0, |
| 326 | USE_GIVEN_TAG = 1, |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 327 | } tag_usage_method_t; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 328 | |
Kusumit Ghoderao | a14ae5a | 2023-04-19 14:16:26 +0530 | [diff] [blame] | 329 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 330 | /*! |
| 331 | * \brief Internal Function for AEAD multipart tests. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 332 | * \param key_type_arg Type of key passed in |
| 333 | * \param key_data The encryption / decryption key data |
| 334 | * \param alg_arg The type of algorithm used |
| 335 | * \param nonce Nonce data |
| 336 | * \param additional_data Additional data |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 337 | * \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] | 338 | * feed additional data in to be encrypted / |
| 339 | * decrypted. If -1, no chunking. |
| 340 | * \param input_data Data to encrypt / decrypt |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 341 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 342 | * the data in to be encrypted / decrypted. If |
| 343 | * -1, no chunking |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 344 | * \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] | 345 | * expected here, this controls whether or not |
| 346 | * to set lengths, and in what order with |
| 347 | * respect to set nonce. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 348 | * \param expected_output Expected output |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 349 | * \param is_encrypt If non-zero this is an encryption operation. |
Paul Elliott | 41ffae1 | 2021-07-22 21:52:01 +0100 | [diff] [blame] | 350 | * \param do_zero_parts If non-zero, interleave zero length chunks |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 351 | * with normal length chunks. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 352 | * \return int Zero on failure, non-zero on success. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 353 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 354 | static int aead_multipart_internal_func(int key_type_arg, data_t *key_data, |
| 355 | int alg_arg, |
| 356 | data_t *nonce, |
| 357 | data_t *additional_data, |
| 358 | int ad_part_len_arg, |
| 359 | data_t *input_data, |
| 360 | int data_part_len_arg, |
| 361 | set_lengths_method_t set_lengths_method, |
| 362 | data_t *expected_output, |
| 363 | int is_encrypt, |
| 364 | int do_zero_parts) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 365 | { |
| 366 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 367 | psa_key_type_t key_type = key_type_arg; |
| 368 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 369 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 370 | unsigned char *output_data = NULL; |
| 371 | unsigned char *part_data = NULL; |
| 372 | unsigned char *final_data = NULL; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 373 | size_t data_true_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 374 | size_t part_data_size = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 375 | size_t output_size = 0; |
| 376 | size_t final_output_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 377 | size_t output_length = 0; |
| 378 | size_t key_bits = 0; |
| 379 | size_t tag_length = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 380 | size_t part_offset = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 381 | size_t part_length = 0; |
| 382 | size_t output_part_length = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 383 | size_t tag_size = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 384 | size_t ad_part_len = 0; |
| 385 | size_t data_part_len = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 386 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 387 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 388 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 389 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 390 | int test_ok = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 391 | size_t part_count = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 392 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 393 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 394 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 395 | if (is_encrypt) { |
| 396 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 397 | } else { |
| 398 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 399 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 400 | |
| 401 | psa_set_key_algorithm(&attributes, alg); |
| 402 | psa_set_key_type(&attributes, key_type); |
| 403 | |
| 404 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 405 | &key)); |
| 406 | |
| 407 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 408 | key_bits = psa_get_key_bits(&attributes); |
| 409 | |
| 410 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); |
| 411 | |
| 412 | if (is_encrypt) { |
| 413 | /* Tag gets written at end of buffer. */ |
| 414 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 415 | (input_data->len + |
| 416 | tag_length)); |
| 417 | data_true_size = input_data->len; |
| 418 | } else { |
| 419 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 420 | (input_data->len - |
| 421 | tag_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 422 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 423 | /* Do not want to attempt to decrypt tag. */ |
| 424 | data_true_size = input_data->len - tag_length; |
| 425 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 426 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 427 | TEST_CALLOC(output_data, output_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 428 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 429 | if (is_encrypt) { |
| 430 | final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
| 431 | TEST_LE_U(final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
| 432 | } else { |
| 433 | final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); |
| 434 | TEST_LE_U(final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 435 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 436 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 437 | TEST_CALLOC(final_data, final_output_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 438 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 439 | if (is_encrypt) { |
| 440 | status = psa_aead_encrypt_setup(&operation, key, alg); |
| 441 | } else { |
| 442 | status = psa_aead_decrypt_setup(&operation, key, alg); |
| 443 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 444 | |
| 445 | /* If the operation is not supported, just skip and not fail in case the |
| 446 | * encryption involves a common limitation of cryptography hardwares and |
| 447 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 448 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 449 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 450 | 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] | 451 | } |
| 452 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 453 | PSA_ASSERT(status); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 454 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 455 | if (set_lengths_method == DO_NOT_SET_LENGTHS) { |
| 456 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
| 457 | } else if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { |
| 458 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 459 | data_true_size)); |
| 460 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
| 461 | } else if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) { |
| 462 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 463 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 464 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 465 | data_true_size)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 466 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 467 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 468 | if (ad_part_len_arg != -1) { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 469 | /* Pass additional data in parts */ |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 470 | ad_part_len = (size_t) ad_part_len_arg; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 471 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 472 | for (part_offset = 0, part_count = 0; |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 473 | part_offset < additional_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 474 | part_offset += part_length, part_count++) { |
| 475 | if (do_zero_parts && (part_count & 0x01)) { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 476 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 477 | } else if (additional_data->len - part_offset < ad_part_len) { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 478 | part_length = additional_data->len - part_offset; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 479 | } else { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 480 | part_length = ad_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 481 | } |
| 482 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 483 | PSA_ASSERT(psa_aead_update_ad(&operation, |
| 484 | additional_data->x + part_offset, |
| 485 | part_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 486 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 487 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 488 | } else { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 489 | /* Pass additional data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 490 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 491 | additional_data->len)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 492 | } |
| 493 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 494 | if (data_part_len_arg != -1) { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 495 | /* Pass data in parts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 496 | data_part_len = (size_t) data_part_len_arg; |
| 497 | part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 498 | (size_t) data_part_len); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 499 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 500 | TEST_CALLOC(part_data, part_data_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 501 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 502 | for (part_offset = 0, part_count = 0; |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 503 | part_offset < data_true_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 504 | part_offset += part_length, part_count++) { |
| 505 | if (do_zero_parts && (part_count & 0x01)) { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 506 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 507 | } else if ((data_true_size - part_offset) < data_part_len) { |
| 508 | part_length = (data_true_size - part_offset); |
| 509 | } else { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 510 | part_length = data_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 511 | } |
| 512 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 513 | PSA_ASSERT(psa_aead_update(&operation, |
| 514 | (input_data->x + part_offset), |
| 515 | part_length, part_data, |
| 516 | part_data_size, |
| 517 | &output_part_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 518 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 519 | if (output_data && output_part_length) { |
| 520 | memcpy((output_data + output_length), part_data, |
| 521 | output_part_length); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 522 | } |
| 523 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 524 | output_length += output_part_length; |
| 525 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 526 | } else { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 527 | /* Pass all data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 528 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 529 | data_true_size, output_data, |
| 530 | output_size, &output_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 531 | } |
| 532 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 533 | if (is_encrypt) { |
| 534 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 535 | final_output_size, |
| 536 | &output_part_length, |
| 537 | tag_buffer, tag_length, |
| 538 | &tag_size)); |
| 539 | } else { |
| 540 | PSA_ASSERT(psa_aead_verify(&operation, final_data, |
| 541 | final_output_size, |
| 542 | &output_part_length, |
| 543 | (input_data->x + data_true_size), |
| 544 | tag_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 545 | } |
| 546 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 547 | if (output_data && output_part_length) { |
| 548 | memcpy((output_data + output_length), final_data, |
| 549 | output_part_length); |
| 550 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 551 | |
| 552 | output_length += output_part_length; |
| 553 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 554 | |
| 555 | /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE |
| 556 | * should be exact.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 557 | if (is_encrypt) { |
| 558 | TEST_EQUAL(tag_length, tag_size); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 559 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 560 | if (output_data && tag_length) { |
| 561 | memcpy((output_data + output_length), tag_buffer, |
| 562 | tag_length); |
| 563 | } |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 564 | |
| 565 | output_length += tag_length; |
| 566 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 567 | TEST_EQUAL(output_length, |
| 568 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 569 | input_data->len)); |
| 570 | TEST_LE_U(output_length, |
| 571 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
| 572 | } else { |
| 573 | TEST_EQUAL(output_length, |
| 574 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, |
| 575 | input_data->len)); |
| 576 | TEST_LE_U(output_length, |
| 577 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 578 | } |
| 579 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 580 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 581 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 582 | output_data, output_length); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 583 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 584 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 585 | test_ok = 1; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 586 | |
| 587 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 588 | psa_destroy_key(key); |
| 589 | psa_aead_abort(&operation); |
| 590 | mbedtls_free(output_data); |
| 591 | mbedtls_free(part_data); |
| 592 | mbedtls_free(final_data); |
| 593 | PSA_DONE(); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 594 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 595 | return test_ok; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 596 | } |
| 597 | |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 598 | /*! |
| 599 | * \brief Internal Function for MAC multipart tests. |
| 600 | * \param key_type_arg Type of key passed in |
| 601 | * \param key_data The encryption / decryption key data |
| 602 | * \param alg_arg The type of algorithm used |
| 603 | * \param input_data Data to encrypt / decrypt |
| 604 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 605 | * the data in to be encrypted / decrypted. If |
| 606 | * -1, no chunking |
| 607 | * \param expected_output Expected output |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 608 | * \param is_verify If non-zero this is a verify operation. |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 609 | * \param do_zero_parts If non-zero, interleave zero length chunks |
| 610 | * with normal length chunks. |
| 611 | * \return int Zero on failure, non-zero on success. |
| 612 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 613 | static int mac_multipart_internal_func(int key_type_arg, data_t *key_data, |
| 614 | int alg_arg, |
| 615 | data_t *input_data, |
| 616 | int data_part_len_arg, |
| 617 | data_t *expected_output, |
| 618 | int is_verify, |
| 619 | int do_zero_parts) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 620 | { |
| 621 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 622 | psa_key_type_t key_type = key_type_arg; |
| 623 | psa_algorithm_t alg = alg_arg; |
| 624 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 625 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
| 626 | size_t part_offset = 0; |
| 627 | size_t part_length = 0; |
| 628 | size_t data_part_len = 0; |
| 629 | size_t mac_len = 0; |
| 630 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 631 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 632 | |
| 633 | int test_ok = 0; |
| 634 | size_t part_count = 0; |
| 635 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 636 | PSA_INIT(); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 637 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 638 | if (is_verify) { |
| 639 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 640 | } else { |
| 641 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 642 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 643 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 644 | psa_set_key_algorithm(&attributes, alg); |
| 645 | psa_set_key_type(&attributes, key_type); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 646 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 647 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 648 | &key)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 649 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 650 | if (is_verify) { |
| 651 | status = psa_mac_verify_setup(&operation, key, alg); |
| 652 | } else { |
| 653 | status = psa_mac_sign_setup(&operation, key, alg); |
| 654 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 655 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 656 | PSA_ASSERT(status); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 657 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 658 | if (data_part_len_arg != -1) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 659 | /* Pass data in parts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 660 | data_part_len = (size_t) data_part_len_arg; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 661 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 662 | for (part_offset = 0, part_count = 0; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 663 | part_offset < input_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 664 | part_offset += part_length, part_count++) { |
| 665 | if (do_zero_parts && (part_count & 0x01)) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 666 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 667 | } else if ((input_data->len - part_offset) < data_part_len) { |
| 668 | part_length = (input_data->len - part_offset); |
| 669 | } else { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 670 | part_length = data_part_len; |
| 671 | } |
| 672 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 673 | PSA_ASSERT(psa_mac_update(&operation, |
| 674 | (input_data->x + part_offset), |
| 675 | part_length)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 676 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 677 | } else { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 678 | /* Pass all data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 679 | PSA_ASSERT(psa_mac_update(&operation, input_data->x, |
| 680 | input_data->len)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 681 | } |
| 682 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 683 | if (is_verify) { |
| 684 | PSA_ASSERT(psa_mac_verify_finish(&operation, expected_output->x, |
| 685 | expected_output->len)); |
| 686 | } else { |
| 687 | PSA_ASSERT(psa_mac_sign_finish(&operation, mac, |
| 688 | PSA_MAC_MAX_SIZE, &mac_len)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 689 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 690 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 691 | mac, mac_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | test_ok = 1; |
| 695 | |
| 696 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 697 | psa_destroy_key(key); |
| 698 | psa_mac_abort(&operation); |
| 699 | PSA_DONE(); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 700 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 701 | return test_ok; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 702 | } |
| 703 | |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 704 | #if defined(PSA_WANT_ALG_JPAKE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 705 | static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive, |
| 706 | psa_pake_operation_t *server, |
| 707 | psa_pake_operation_t *client, |
| 708 | int client_input_first, |
| 709 | int round, int inject_error) |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 710 | { |
| 711 | unsigned char *buffer0 = NULL, *buffer1 = NULL; |
| 712 | size_t buffer_length = ( |
| 713 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) + |
| 714 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) + |
| 715 | 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] | 716 | /* The output should be exactly this size according to the spec */ |
| 717 | const size_t expected_size_key_share = |
| 718 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE); |
| 719 | /* The output should be exactly this size according to the spec */ |
| 720 | const size_t expected_size_zk_public = |
| 721 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC); |
| 722 | /* The output can be smaller: the spec allows stripping leading zeroes */ |
| 723 | const size_t max_expected_size_zk_proof = |
| 724 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 725 | size_t buffer0_off = 0; |
| 726 | size_t buffer1_off = 0; |
| 727 | size_t s_g1_len, s_g2_len, s_a_len; |
| 728 | size_t s_g1_off, s_g2_off, s_a_off; |
| 729 | size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len; |
| 730 | size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off; |
| 731 | size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len; |
| 732 | size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off; |
| 733 | size_t c_g1_len, c_g2_len, c_a_len; |
| 734 | size_t c_g1_off, c_g2_off, c_a_off; |
| 735 | size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len; |
| 736 | size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off; |
| 737 | size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len; |
| 738 | size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off; |
| 739 | psa_status_t expected_status = PSA_SUCCESS; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 740 | psa_status_t status; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 741 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 742 | TEST_CALLOC(buffer0, buffer_length); |
| 743 | TEST_CALLOC(buffer1, buffer_length); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 744 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 745 | switch (round) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 746 | case 1: |
| 747 | /* Server first round Output */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 748 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 749 | buffer0 + buffer0_off, |
| 750 | 512 - buffer0_off, &s_g1_len)); |
| 751 | TEST_EQUAL(s_g1_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 752 | s_g1_off = buffer0_off; |
| 753 | buffer0_off += s_g1_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 754 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 755 | buffer0 + buffer0_off, |
| 756 | 512 - buffer0_off, &s_x1_pk_len)); |
| 757 | TEST_EQUAL(s_x1_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 758 | s_x1_pk_off = buffer0_off; |
| 759 | buffer0_off += s_x1_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 760 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 761 | buffer0 + buffer0_off, |
| 762 | 512 - buffer0_off, &s_x1_pr_len)); |
| 763 | TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 764 | s_x1_pr_off = buffer0_off; |
| 765 | buffer0_off += s_x1_pr_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 766 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 767 | buffer0 + buffer0_off, |
| 768 | 512 - buffer0_off, &s_g2_len)); |
| 769 | TEST_EQUAL(s_g2_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 770 | s_g2_off = buffer0_off; |
| 771 | buffer0_off += s_g2_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 772 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 773 | buffer0 + buffer0_off, |
| 774 | 512 - buffer0_off, &s_x2_pk_len)); |
| 775 | TEST_EQUAL(s_x2_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 776 | s_x2_pk_off = buffer0_off; |
| 777 | buffer0_off += s_x2_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 778 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 779 | buffer0 + buffer0_off, |
| 780 | 512 - buffer0_off, &s_x2_pr_len)); |
| 781 | TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 782 | s_x2_pr_off = buffer0_off; |
| 783 | buffer0_off += s_x2_pr_len; |
| 784 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 785 | if (inject_error == 1) { |
Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 786 | buffer0[s_x1_pr_off + 8] ^= 1; |
| 787 | buffer0[s_x2_pr_off + 7] ^= 1; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 788 | expected_status = PSA_ERROR_DATA_INVALID; |
| 789 | } |
| 790 | |
Neil Armstrong | 51009d7 | 2022-09-05 17:59:54 +0200 | [diff] [blame] | 791 | /* |
| 792 | * When injecting errors in inputs, the implementation is |
| 793 | * free to detect it right away of with a delay. |
| 794 | * This permits delaying the error until the end of the input |
| 795 | * sequence, if no error appears then, this will be treated |
| 796 | * as an error. |
| 797 | */ |
| 798 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 799 | if (client_input_first == 1) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 800 | /* Client first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 801 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 802 | buffer0 + s_g1_off, s_g1_len); |
| 803 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 804 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 805 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 806 | } else { |
| 807 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 808 | } |
| 809 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 810 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 811 | buffer0 + s_x1_pk_off, |
| 812 | s_x1_pk_len); |
| 813 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 814 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 815 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 816 | } else { |
| 817 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 818 | } |
| 819 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 820 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 821 | buffer0 + s_x1_pr_off, |
| 822 | s_x1_pr_len); |
| 823 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 824 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 825 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 826 | } else { |
| 827 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 828 | } |
| 829 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 830 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 831 | buffer0 + s_g2_off, |
| 832 | s_g2_len); |
| 833 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 834 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 835 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 836 | } else { |
| 837 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 838 | } |
| 839 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 840 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 841 | buffer0 + s_x2_pk_off, |
| 842 | s_x2_pk_len); |
| 843 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 844 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 845 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 846 | } else { |
| 847 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 848 | } |
| 849 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 850 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 851 | buffer0 + s_x2_pr_off, |
| 852 | s_x2_pr_len); |
| 853 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 854 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 855 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 856 | } else { |
| 857 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 858 | } |
| 859 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 860 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 861 | if (inject_error == 1) { |
| 862 | TEST_ASSERT( |
| 863 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 864 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | /* Client first round Output */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 868 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 869 | buffer1 + buffer1_off, |
| 870 | 512 - buffer1_off, &c_g1_len)); |
| 871 | TEST_EQUAL(c_g1_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 872 | c_g1_off = buffer1_off; |
| 873 | buffer1_off += c_g1_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 874 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 875 | buffer1 + buffer1_off, |
| 876 | 512 - buffer1_off, &c_x1_pk_len)); |
| 877 | TEST_EQUAL(c_x1_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 878 | c_x1_pk_off = buffer1_off; |
| 879 | buffer1_off += c_x1_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 880 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 881 | buffer1 + buffer1_off, |
| 882 | 512 - buffer1_off, &c_x1_pr_len)); |
| 883 | TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 884 | c_x1_pr_off = buffer1_off; |
| 885 | buffer1_off += c_x1_pr_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 886 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 887 | buffer1 + buffer1_off, |
| 888 | 512 - buffer1_off, &c_g2_len)); |
| 889 | TEST_EQUAL(c_g2_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 890 | c_g2_off = buffer1_off; |
| 891 | buffer1_off += c_g2_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 892 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 893 | buffer1 + buffer1_off, |
| 894 | 512 - buffer1_off, &c_x2_pk_len)); |
| 895 | TEST_EQUAL(c_x2_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 896 | c_x2_pk_off = buffer1_off; |
| 897 | buffer1_off += c_x2_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 898 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 899 | buffer1 + buffer1_off, |
| 900 | 512 - buffer1_off, &c_x2_pr_len)); |
| 901 | TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 902 | c_x2_pr_off = buffer1_off; |
| 903 | buffer1_off += c_x2_pr_len; |
| 904 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 905 | if (client_input_first == 0) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 906 | /* Client first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 907 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 908 | buffer0 + s_g1_off, s_g1_len); |
| 909 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 910 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 911 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 912 | } else { |
| 913 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 914 | } |
| 915 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 916 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 917 | buffer0 + s_x1_pk_off, |
| 918 | s_x1_pk_len); |
| 919 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 920 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 921 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 922 | } else { |
| 923 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 924 | } |
| 925 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 926 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 927 | buffer0 + s_x1_pr_off, |
| 928 | s_x1_pr_len); |
| 929 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 930 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 931 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 932 | } else { |
| 933 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 934 | } |
| 935 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 936 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 937 | buffer0 + s_g2_off, |
| 938 | s_g2_len); |
| 939 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 940 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 941 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 942 | } else { |
| 943 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 944 | } |
| 945 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 946 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 947 | buffer0 + s_x2_pk_off, |
| 948 | s_x2_pk_len); |
| 949 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 950 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 951 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 952 | } else { |
| 953 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 954 | } |
| 955 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 956 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 957 | buffer0 + s_x2_pr_off, |
| 958 | s_x2_pr_len); |
| 959 | if (inject_error == 1 && 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 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 966 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 967 | if (inject_error == 1) { |
| 968 | TEST_ASSERT( |
| 969 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 970 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 971 | } |
| 972 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 973 | if (inject_error == 2) { |
Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 974 | buffer1[c_x1_pr_off + 12] ^= 1; |
| 975 | buffer1[c_x2_pr_off + 7] ^= 1; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 976 | expected_status = PSA_ERROR_DATA_INVALID; |
| 977 | } |
| 978 | |
| 979 | /* Server first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 980 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 981 | buffer1 + c_g1_off, c_g1_len); |
| 982 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 983 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 984 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 985 | } else { |
| 986 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 987 | } |
| 988 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 989 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 990 | buffer1 + c_x1_pk_off, c_x1_pk_len); |
| 991 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 992 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 993 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 994 | } else { |
| 995 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 996 | } |
| 997 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 998 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 999 | buffer1 + c_x1_pr_off, c_x1_pr_len); |
| 1000 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1001 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1002 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1003 | } else { |
| 1004 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1005 | } |
| 1006 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1007 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 1008 | buffer1 + c_g2_off, c_g2_len); |
| 1009 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1010 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1011 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1012 | } else { |
| 1013 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1014 | } |
| 1015 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1016 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1017 | buffer1 + c_x2_pk_off, c_x2_pk_len); |
| 1018 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1019 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1020 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1021 | } else { |
| 1022 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1023 | } |
| 1024 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1025 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1026 | buffer1 + c_x2_pr_off, c_x2_pr_len); |
| 1027 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1028 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1029 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1030 | } else { |
| 1031 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1032 | } |
| 1033 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1034 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1035 | if (inject_error == 2) { |
| 1036 | TEST_ASSERT( |
| 1037 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1038 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1039 | |
| 1040 | break; |
| 1041 | |
| 1042 | case 2: |
| 1043 | /* Server second round Output */ |
| 1044 | buffer0_off = 0; |
| 1045 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1046 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 1047 | buffer0 + buffer0_off, |
| 1048 | 512 - buffer0_off, &s_a_len)); |
| 1049 | TEST_EQUAL(s_a_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1050 | s_a_off = buffer0_off; |
| 1051 | buffer0_off += s_a_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1052 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1053 | buffer0 + buffer0_off, |
| 1054 | 512 - buffer0_off, &s_x2s_pk_len)); |
| 1055 | TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1056 | s_x2s_pk_off = buffer0_off; |
| 1057 | buffer0_off += s_x2s_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1058 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1059 | buffer0 + buffer0_off, |
| 1060 | 512 - buffer0_off, &s_x2s_pr_len)); |
| 1061 | TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1062 | s_x2s_pr_off = buffer0_off; |
| 1063 | buffer0_off += s_x2s_pr_len; |
| 1064 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1065 | if (inject_error == 3) { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1066 | buffer0[s_x2s_pk_off + 12] += 0x33; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1067 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1068 | } |
| 1069 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1070 | if (client_input_first == 1) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1071 | /* Client second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1072 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1073 | buffer0 + s_a_off, s_a_len); |
| 1074 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1075 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1076 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1077 | } else { |
| 1078 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1079 | } |
| 1080 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1081 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1082 | buffer0 + s_x2s_pk_off, |
| 1083 | s_x2s_pk_len); |
| 1084 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1085 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1086 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1087 | } else { |
| 1088 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1089 | } |
| 1090 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1091 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1092 | buffer0 + s_x2s_pr_off, |
| 1093 | s_x2s_pr_len); |
| 1094 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1095 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1096 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1097 | } else { |
| 1098 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1099 | } |
| 1100 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1101 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1102 | if (inject_error == 3) { |
| 1103 | TEST_ASSERT( |
| 1104 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1105 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | /* Client second round Output */ |
| 1109 | buffer1_off = 0; |
| 1110 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1111 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1112 | buffer1 + buffer1_off, |
| 1113 | 512 - buffer1_off, &c_a_len)); |
| 1114 | TEST_EQUAL(c_a_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1115 | c_a_off = buffer1_off; |
| 1116 | buffer1_off += c_a_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1117 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1118 | buffer1 + buffer1_off, |
| 1119 | 512 - buffer1_off, &c_x2s_pk_len)); |
| 1120 | TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1121 | c_x2s_pk_off = buffer1_off; |
| 1122 | buffer1_off += c_x2s_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1123 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1124 | buffer1 + buffer1_off, |
| 1125 | 512 - buffer1_off, &c_x2s_pr_len)); |
| 1126 | TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1127 | c_x2s_pr_off = buffer1_off; |
| 1128 | buffer1_off += c_x2s_pr_len; |
| 1129 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1130 | if (client_input_first == 0) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1131 | /* Client second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1132 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1133 | buffer0 + s_a_off, s_a_len); |
| 1134 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1135 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1136 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1137 | } else { |
| 1138 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1139 | } |
| 1140 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1141 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1142 | buffer0 + s_x2s_pk_off, |
| 1143 | s_x2s_pk_len); |
| 1144 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1145 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1146 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1147 | } else { |
| 1148 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1149 | } |
| 1150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1151 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1152 | buffer0 + s_x2s_pr_off, |
| 1153 | s_x2s_pr_len); |
| 1154 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1155 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1156 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1157 | } else { |
| 1158 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1159 | } |
| 1160 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1161 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1162 | if (inject_error == 3) { |
| 1163 | TEST_ASSERT( |
| 1164 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1165 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1166 | } |
| 1167 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1168 | if (inject_error == 4) { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1169 | buffer1[c_x2s_pk_off + 7] += 0x28; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1170 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1171 | } |
| 1172 | |
| 1173 | /* Server second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1174 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 1175 | buffer1 + c_a_off, c_a_len); |
| 1176 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1177 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1178 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1179 | } else { |
| 1180 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1181 | } |
| 1182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1183 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1184 | buffer1 + c_x2s_pk_off, c_x2s_pk_len); |
| 1185 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1186 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1187 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1188 | } else { |
| 1189 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1190 | } |
| 1191 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1192 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1193 | buffer1 + c_x2s_pr_off, c_x2s_pr_len); |
| 1194 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1195 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1196 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1197 | } else { |
| 1198 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1199 | } |
| 1200 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1201 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1202 | if (inject_error == 4) { |
| 1203 | TEST_ASSERT( |
| 1204 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1205 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1206 | |
| 1207 | break; |
| 1208 | |
| 1209 | } |
| 1210 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1211 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1212 | mbedtls_free(buffer0); |
| 1213 | mbedtls_free(buffer1); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1214 | } |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 1215 | #endif /* PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1216 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1217 | typedef enum { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 1218 | INJECT_ERR_NONE = 0, |
| 1219 | INJECT_ERR_UNINITIALIZED_ACCESS, |
| 1220 | INJECT_ERR_DUPLICATE_SETUP, |
| 1221 | INJECT_ERR_INVALID_USER, |
| 1222 | INJECT_ERR_INVALID_PEER, |
| 1223 | INJECT_ERR_SET_USER, |
| 1224 | INJECT_ERR_SET_PEER, |
| 1225 | INJECT_EMPTY_IO_BUFFER, |
| 1226 | INJECT_UNKNOWN_STEP, |
| 1227 | INJECT_INVALID_FIRST_STEP, |
| 1228 | INJECT_WRONG_BUFFER_SIZE, |
| 1229 | INJECT_VALID_OPERATION_AFTER_FAILURE, |
| 1230 | INJECT_ANTICIPATE_KEY_DERIVATION_1, |
| 1231 | INJECT_ANTICIPATE_KEY_DERIVATION_2, |
| 1232 | } ecjpake_injected_failure_t; |
| 1233 | |
Paul Elliott | 01885fa | 2023-02-09 12:07:30 +0000 | [diff] [blame] | 1234 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Paul Elliott | 1243f93 | 2023-02-07 11:21:10 +0000 | [diff] [blame] | 1235 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1236 | static void interruptible_signverify_get_minmax_completes(uint32_t max_ops, |
| 1237 | psa_status_t expected_status, |
| 1238 | size_t *min_completes, |
| 1239 | size_t *max_completes) |
| 1240 | { |
| 1241 | |
| 1242 | /* This is slightly contrived, but we only really know that with a minimum |
| 1243 | value of max_ops that a successful operation should take more than one op |
| 1244 | to complete, and likewise that with a max_ops of |
| 1245 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */ |
| 1246 | if (max_ops == 0 || max_ops == 1) { |
Paul Elliott | c86d45e | 2023-02-15 17:38:05 +0000 | [diff] [blame] | 1247 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1248 | if (expected_status == PSA_SUCCESS) { |
| 1249 | *min_completes = 2; |
| 1250 | } else { |
| 1251 | *min_completes = 1; |
| 1252 | } |
| 1253 | |
| 1254 | *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED; |
| 1255 | } else { |
| 1256 | *min_completes = 1; |
| 1257 | *max_completes = 1; |
| 1258 | } |
| 1259 | } |
Paul Elliott | 01885fa | 2023-02-09 12:07:30 +0000 | [diff] [blame] | 1260 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1261 | |
Gilles Peskine | 1d25a0a | 2024-02-12 16:40:04 +0100 | [diff] [blame] | 1262 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) |
| 1263 | static int rsa_test_e(mbedtls_svc_key_id_t key, |
| 1264 | size_t bits, |
| 1265 | const data_t *e_arg) |
| 1266 | { |
| 1267 | uint8_t *exported = NULL; |
| 1268 | size_t exported_size = |
| 1269 | PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits); |
| 1270 | size_t exported_length = SIZE_MAX; |
| 1271 | int ok = 0; |
| 1272 | |
| 1273 | TEST_CALLOC(exported, exported_size); |
| 1274 | PSA_ASSERT(psa_export_public_key(key, |
| 1275 | exported, exported_size, |
| 1276 | &exported_length)); |
| 1277 | uint8_t *p = exported; |
| 1278 | uint8_t *end = exported + exported_length; |
| 1279 | size_t len; |
| 1280 | /* RSAPublicKey ::= SEQUENCE { |
| 1281 | * modulus INTEGER, -- n |
| 1282 | * publicExponent INTEGER } -- e |
| 1283 | */ |
| 1284 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 1285 | MBEDTLS_ASN1_SEQUENCE | |
| 1286 | MBEDTLS_ASN1_CONSTRUCTED)); |
| 1287 | TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1)); |
| 1288 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 1289 | MBEDTLS_ASN1_INTEGER)); |
| 1290 | if (len >= 1 && p[0] == 0) { |
| 1291 | ++p; |
| 1292 | --len; |
| 1293 | } |
| 1294 | if (e_arg->len == 0) { |
| 1295 | TEST_EQUAL(len, 3); |
| 1296 | TEST_EQUAL(p[0], 1); |
| 1297 | TEST_EQUAL(p[1], 0); |
| 1298 | TEST_EQUAL(p[2], 1); |
| 1299 | } else { |
Gilles Peskine | 7a18f96 | 2024-02-12 16:48:11 +0100 | [diff] [blame] | 1300 | const uint8_t *expected = e_arg->x; |
| 1301 | size_t expected_len = e_arg->len; |
| 1302 | while (expected_len > 0 && *expected == 0) { |
| 1303 | ++expected; |
| 1304 | --expected_len; |
| 1305 | } |
| 1306 | TEST_MEMORY_COMPARE(p, len, expected, expected_len); |
Gilles Peskine | 1d25a0a | 2024-02-12 16:40:04 +0100 | [diff] [blame] | 1307 | } |
| 1308 | ok = 1; |
| 1309 | |
| 1310 | exit: |
| 1311 | mbedtls_free(exported); |
| 1312 | return ok; |
| 1313 | } |
| 1314 | #endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE */ |
| 1315 | |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 1316 | static int setup_key_production_parameters( |
| 1317 | psa_key_production_parameters_t **params, size_t *params_data_length, |
| 1318 | int flags_arg, const data_t *params_data) |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 1319 | { |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 1320 | *params_data_length = params_data->len; |
Gilles Peskine | c81393b | 2024-02-14 20:51:28 +0100 | [diff] [blame] | 1321 | /* If there are N bytes of padding at the end of |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 1322 | * psa_key_production_parameters_t, then it's enough to allocate |
| 1323 | * MIN(sizeof(psa_key_production_parameters_t), |
| 1324 | * offsetof(psa_key_production_parameters_t, data) + params_data_length). |
Gilles Peskine | c81393b | 2024-02-14 20:51:28 +0100 | [diff] [blame] | 1325 | * |
| 1326 | * For simplicity, here, we allocate up to N more bytes than necessary. |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 1327 | * In practice, the current layout of psa_key_production_parameters_t |
Gilles Peskine | c81393b | 2024-02-14 20:51:28 +0100 | [diff] [blame] | 1328 | * makes padding extremely unlikely, so we don't worry about testing |
| 1329 | * that the library code doesn't try to access these extra N bytes. |
| 1330 | */ |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 1331 | *params = mbedtls_calloc(1, sizeof(**params) + *params_data_length); |
| 1332 | TEST_ASSERT(*params != NULL); |
| 1333 | (*params)->flags = (uint32_t) flags_arg; |
| 1334 | memcpy((*params)->data, params_data->x, params_data->len); |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 1335 | return 1; |
| 1336 | exit: |
| 1337 | return 0; |
| 1338 | } |
| 1339 | |
Ryan | 3a1b786 | 2024-03-01 17:24:04 +0000 | [diff] [blame] | 1340 | #if defined(MBEDTLS_THREADING_PTHREAD) |
Ryan Everett | 5061999 | 2024-03-12 16:55:14 +0000 | [diff] [blame] | 1341 | |
| 1342 | typedef struct same_key_context { |
| 1343 | data_t *data; |
| 1344 | mbedtls_svc_key_id_t key; |
| 1345 | psa_key_attributes_t *attributes; |
| 1346 | int type; |
| 1347 | int bits; |
| 1348 | /* The following two parameters are used to ensure that when multiple |
| 1349 | * threads attempt to load/destroy the key, exactly one thread succeeds. */ |
| 1350 | int key_loaded; |
| 1351 | mbedtls_threading_mutex_t MBEDTLS_PRIVATE(key_loaded_mutex); |
| 1352 | } |
| 1353 | same_key_context; |
| 1354 | |
| 1355 | /* Attempt to import the key in ctx. This handles any valid error codes |
| 1356 | * and reports an error for any invalid codes. This function also insures |
| 1357 | * that once imported by some thread, all threads can use the key. */ |
| 1358 | void *thread_import_key(void *ctx) |
| 1359 | { |
| 1360 | mbedtls_svc_key_id_t returned_key_id; |
| 1361 | same_key_context *skc = (struct same_key_context *) ctx; |
| 1362 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1363 | |
Ryan Everett | 6c48870 | 2024-03-14 17:49:44 +0000 | [diff] [blame] | 1364 | /* Import the key, exactly one thread must succeed. */ |
Ryan Everett | 5061999 | 2024-03-12 16:55:14 +0000 | [diff] [blame] | 1365 | psa_status_t status = psa_import_key(skc->attributes, skc->data->x, |
| 1366 | skc->data->len, &returned_key_id); |
| 1367 | switch (status) { |
| 1368 | case PSA_SUCCESS: |
| 1369 | if (mbedtls_mutex_lock(&skc->key_loaded_mutex) == 0) { |
| 1370 | if (skc->key_loaded) { |
| 1371 | mbedtls_mutex_unlock(&skc->key_loaded_mutex); |
| 1372 | /* More than one thread has succeeded, report a failure. */ |
Ryan Everett | 3de040f | 2024-03-14 17:50:06 +0000 | [diff] [blame^] | 1373 | 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] | 1374 | } |
| 1375 | skc->key_loaded = 1; |
| 1376 | mbedtls_mutex_unlock(&skc->key_loaded_mutex); |
| 1377 | } |
| 1378 | break; |
| 1379 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
| 1380 | /* If all of the key slots are reserved when a thread |
| 1381 | * locks the mutex to reserve a new slot, it will return |
| 1382 | * PSA_ERROR_INSUFFICIENT_MEMORY; this is correct behaviour. |
| 1383 | * There is a chance for this to occur here when the number of |
| 1384 | * threads running this function is larger than the number of |
| 1385 | * free key slots. Each thread reserves an empty key slot, |
| 1386 | * unlocks the mutex, then relocks it to finalize key creation. |
| 1387 | * It is at that point where the thread sees that the key |
| 1388 | * already exists, releases the reserved slot, |
| 1389 | * and returns PSA_ERROR_ALREADY_EXISTS. |
| 1390 | * There is no guarantee that the key is loaded upon this return |
| 1391 | * code, so we can't test the key information. Just stop this |
| 1392 | * thread from executing, note that this is not an error. */ |
| 1393 | goto exit; |
| 1394 | break; |
| 1395 | case PSA_ERROR_ALREADY_EXISTS: |
| 1396 | /* The key has been loaded by a different thread. */ |
| 1397 | break; |
| 1398 | default: |
| 1399 | PSA_ASSERT(status); |
| 1400 | } |
| 1401 | /* At this point the key must exist, test the key information. */ |
| 1402 | status = psa_get_key_attributes(skc->key, &got_attributes); |
| 1403 | if (status == PSA_ERROR_INSUFFICIENT_MEMORY) { |
| 1404 | /* This is not a test failure. The following sequence of events |
| 1405 | * causes this to occur: |
| 1406 | * 1: This thread successfuly imports a persistent key skc->key. |
| 1407 | * 2: N threads reserve an empty key slot in psa_import_key, |
| 1408 | * where N is equal to the number of free key slots. |
| 1409 | * 3: A final thread attempts to reserve an empty key slot, kicking |
| 1410 | * skc->key (which has no registered readers) out of its slot. |
| 1411 | * 4: This thread calls psa_get_key_attributes(skc->key,...): |
| 1412 | * it sees that skc->key is not in a slot, attempts to load it and |
| 1413 | * finds that there are no free slots. |
| 1414 | * This thread returns PSA_ERROR_INSUFFICIENT_MEMORY. |
| 1415 | * |
| 1416 | * The PSA spec allows this behaviour, it is an unavoidable consequence |
| 1417 | * of allowing persistent keys to be kicked out of the key store while |
| 1418 | * they are still valid. */ |
| 1419 | goto exit; |
| 1420 | } |
| 1421 | PSA_ASSERT(status); |
| 1422 | TEST_EQUAL(psa_get_key_type(&got_attributes), skc->type); |
| 1423 | TEST_EQUAL(psa_get_key_bits(&got_attributes), skc->bits); |
| 1424 | |
| 1425 | exit: |
| 1426 | /* Key attributes may have been returned by psa_get_key_attributes(), |
| 1427 | * reset them as required. */ |
| 1428 | psa_reset_key_attributes(&got_attributes); |
| 1429 | return NULL; |
| 1430 | } |
| 1431 | |
| 1432 | void *thread_use_and_destroy_key(void *ctx) |
| 1433 | { |
| 1434 | same_key_context *skc = (struct same_key_context *) ctx; |
| 1435 | |
| 1436 | /* Do something with the key according |
| 1437 | * to its type and permitted usage. */ |
| 1438 | TEST_ASSERT(mbedtls_test_psa_exercise_key(skc->key, |
| 1439 | skc->attributes->policy.usage, |
| 1440 | skc->attributes->policy.alg, 1)); |
| 1441 | |
| 1442 | psa_status_t status = psa_destroy_key(skc->key); |
| 1443 | if (status == PSA_SUCCESS) { |
| 1444 | if (mbedtls_mutex_lock(&skc->key_loaded_mutex) == 0) { |
| 1445 | /* Ensure that we are the only thread to succeed. */ |
| 1446 | if (skc->key_loaded != 1) { |
| 1447 | mbedtls_mutex_unlock(&skc->key_loaded_mutex); |
Ryan Everett | 3de040f | 2024-03-14 17:50:06 +0000 | [diff] [blame^] | 1448 | TEST_FAIL("The same key has been destroyed multiple times."); |
Ryan Everett | 5061999 | 2024-03-12 16:55:14 +0000 | [diff] [blame] | 1449 | } |
| 1450 | skc->key_loaded = 0; |
| 1451 | mbedtls_mutex_unlock(&skc->key_loaded_mutex); |
| 1452 | } |
| 1453 | } else { |
| 1454 | TEST_EQUAL(status, PSA_ERROR_INVALID_HANDLE); |
| 1455 | } |
| 1456 | |
| 1457 | exit: |
| 1458 | return NULL; |
| 1459 | } |
| 1460 | |
Ryan | 3a1b786 | 2024-03-01 17:24:04 +0000 | [diff] [blame] | 1461 | typedef struct generate_key_context { |
| 1462 | psa_key_type_t type; |
| 1463 | psa_key_usage_t usage; |
| 1464 | size_t bits; |
| 1465 | psa_algorithm_t alg; |
| 1466 | psa_status_t expected_status; |
| 1467 | psa_key_attributes_t *attributes; |
| 1468 | int is_large_key; |
| 1469 | int reps; |
| 1470 | } |
| 1471 | generate_key_context; |
| 1472 | void *thread_generate_key(void *ctx) |
| 1473 | { |
| 1474 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1475 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1476 | generate_key_context *gkc = (struct generate_key_context *) ctx; |
| 1477 | |
| 1478 | /* If there are race conditions, it is likely the case that they do not |
| 1479 | * arise every time the code runs. We repeat the code to increase the |
| 1480 | * chance that any race conditions will be hit. */ |
| 1481 | for (int n = 0; n < gkc->reps; n++) { |
| 1482 | /* Generate a key */ |
| 1483 | psa_status_t status = psa_generate_key(gkc->attributes, &key); |
| 1484 | |
| 1485 | if (gkc->is_large_key > 0) { |
| 1486 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 1487 | } |
| 1488 | |
| 1489 | TEST_EQUAL(status, gkc->expected_status); |
| 1490 | if (gkc->expected_status != PSA_SUCCESS) { |
| 1491 | PSA_ASSERT(psa_destroy_key(key)); |
| 1492 | goto exit; |
| 1493 | } |
| 1494 | |
| 1495 | /* Test the key information */ |
| 1496 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1497 | TEST_EQUAL(psa_get_key_type(&got_attributes), gkc->type); |
| 1498 | TEST_EQUAL(psa_get_key_bits(&got_attributes), gkc->bits); |
| 1499 | |
| 1500 | /* Do something with the key according |
| 1501 | * to its type and permitted usage. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1502 | if (!mbedtls_test_psa_exercise_key(key, gkc->usage, gkc->alg, 0)) { |
Ryan | 3a1b786 | 2024-03-01 17:24:04 +0000 | [diff] [blame] | 1503 | psa_destroy_key(key); |
| 1504 | goto exit; |
| 1505 | } |
| 1506 | psa_reset_key_attributes(&got_attributes); |
| 1507 | |
| 1508 | PSA_ASSERT(psa_destroy_key(key)); |
| 1509 | } |
| 1510 | exit: |
| 1511 | /* |
| 1512 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1513 | * thus reset them as required. |
| 1514 | */ |
| 1515 | psa_reset_key_attributes(&got_attributes); |
| 1516 | return NULL; |
| 1517 | } |
| 1518 | #endif /* MBEDTLS_THREADING_PTHREAD */ |
| 1519 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1520 | /* END_HEADER */ |
| 1521 | |
| 1522 | /* BEGIN_DEPENDENCIES |
| 1523 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1524 | * END_DEPENDENCIES |
| 1525 | */ |
| 1526 | |
| 1527 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 7abdf7e | 2023-03-09 11:17:43 +0100 | [diff] [blame] | 1528 | void psa_can_do_hash() |
| 1529 | { |
| 1530 | /* We can't test that this is specific to drivers until partial init has |
| 1531 | * been implemented, but we can at least test before/after full init. */ |
| 1532 | TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE)); |
| 1533 | PSA_INIT(); |
| 1534 | TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE)); |
| 1535 | PSA_DONE(); |
| 1536 | } |
| 1537 | /* END_CASE */ |
| 1538 | |
| 1539 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1540 | void static_checks() |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1541 | { |
| 1542 | size_t max_truncated_mac_size = |
| 1543 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1544 | |
| 1545 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1546 | * encoding. The shifted mask is the maximum truncated value. The |
| 1547 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1548 | TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size); |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1549 | } |
| 1550 | /* END_CASE */ |
| 1551 | |
| 1552 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1553 | void import_with_policy(int type_arg, |
| 1554 | int usage_arg, int alg_arg, |
| 1555 | int expected_status_arg) |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1556 | { |
| 1557 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1558 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1559 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1560 | psa_key_type_t type = type_arg; |
| 1561 | psa_key_usage_t usage = usage_arg; |
| 1562 | psa_algorithm_t alg = alg_arg; |
| 1563 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1564 | const uint8_t key_material[16] = { 0 }; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1565 | psa_status_t status; |
| 1566 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1567 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1568 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1569 | psa_set_key_type(&attributes, type); |
| 1570 | psa_set_key_usage_flags(&attributes, usage); |
| 1571 | psa_set_key_algorithm(&attributes, alg); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1572 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1573 | status = psa_import_key(&attributes, |
| 1574 | key_material, sizeof(key_material), |
| 1575 | &key); |
| 1576 | TEST_EQUAL(status, expected_status); |
| 1577 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1578 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1579 | } |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1580 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1581 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1582 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1583 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), |
| 1584 | mbedtls_test_update_key_usage_flags(usage)); |
| 1585 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 1586 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1587 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1588 | PSA_ASSERT(psa_destroy_key(key)); |
| 1589 | test_operations_on_invalid_key(key); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1590 | |
| 1591 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1592 | /* |
| 1593 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1594 | * thus reset them as required. |
| 1595 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1596 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1597 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1598 | psa_destroy_key(key); |
| 1599 | PSA_DONE(); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1600 | } |
| 1601 | /* END_CASE */ |
| 1602 | |
| 1603 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1604 | void import_with_data(data_t *data, int type_arg, |
| 1605 | int attr_bits_arg, |
| 1606 | int expected_status_arg) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1607 | { |
| 1608 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1609 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1610 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1611 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1612 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1613 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1614 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1615 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1616 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1617 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1618 | psa_set_key_type(&attributes, type); |
| 1619 | psa_set_key_bits(&attributes, attr_bits); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1620 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1621 | status = psa_import_key(&attributes, data->x, data->len, &key); |
Manuel Pégourié-Gonnard | 0509b58 | 2023-08-08 12:47:56 +0200 | [diff] [blame] | 1622 | /* When expecting INVALID_ARGUMENT, also accept NOT_SUPPORTED. |
| 1623 | * |
| 1624 | * This can happen with a type supported only by a driver: |
| 1625 | * - the driver sees the invalid data (for example wrong size) and thinks |
| 1626 | * "well perhaps this is a key size I don't support" so it returns |
| 1627 | * NOT_SUPPORTED which is correct at this point; |
| 1628 | * - we fallback to built-ins, which don't support this type, so return |
| 1629 | * NOT_SUPPORTED which again is correct at this point. |
| 1630 | */ |
| 1631 | if (expected_status == PSA_ERROR_INVALID_ARGUMENT && |
| 1632 | status == PSA_ERROR_NOT_SUPPORTED) { |
| 1633 | ; // OK |
| 1634 | } else { |
| 1635 | TEST_EQUAL(status, expected_status); |
| 1636 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1637 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1638 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1639 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1640 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1641 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1642 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1643 | if (attr_bits != 0) { |
| 1644 | TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes)); |
| 1645 | } |
| 1646 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1647 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1648 | PSA_ASSERT(psa_destroy_key(key)); |
| 1649 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1650 | |
| 1651 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1652 | /* |
| 1653 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1654 | * thus reset them as required. |
| 1655 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1656 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1657 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1658 | psa_destroy_key(key); |
| 1659 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1660 | } |
| 1661 | /* END_CASE */ |
| 1662 | |
| 1663 | /* BEGIN_CASE */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1664 | /* Construct and attempt to import a large unstructured key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1665 | void import_large_key(int type_arg, int byte_size_arg, |
| 1666 | int expected_status_arg) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1667 | { |
| 1668 | psa_key_type_t type = type_arg; |
| 1669 | size_t byte_size = byte_size_arg; |
| 1670 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1671 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1672 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1673 | psa_status_t status; |
| 1674 | uint8_t *buffer = NULL; |
| 1675 | size_t buffer_size = byte_size + 1; |
| 1676 | size_t n; |
| 1677 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1678 | /* Skip the test case if the target running the test cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1679 | * accommodate large keys due to heap size constraints */ |
Tom Cosgrove | 412a813 | 2023-07-20 16:55:14 +0100 | [diff] [blame] | 1680 | TEST_CALLOC_OR_SKIP(buffer, buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1681 | memset(buffer, 'K', byte_size); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1682 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1683 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1684 | |
| 1685 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1686 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1687 | psa_set_key_type(&attributes, type); |
| 1688 | status = psa_import_key(&attributes, buffer, byte_size, &key); |
| 1689 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 1690 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1691 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1692 | if (status == PSA_SUCCESS) { |
| 1693 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1694 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 1695 | TEST_EQUAL(psa_get_key_bits(&attributes), |
| 1696 | PSA_BYTES_TO_BITS(byte_size)); |
| 1697 | ASSERT_NO_SLOT_NUMBER(&attributes); |
| 1698 | memset(buffer, 0, byte_size + 1); |
| 1699 | PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n)); |
| 1700 | for (n = 0; n < byte_size; n++) { |
| 1701 | TEST_EQUAL(buffer[n], 'K'); |
| 1702 | } |
| 1703 | for (n = byte_size; n < buffer_size; n++) { |
| 1704 | TEST_EQUAL(buffer[n], 0); |
| 1705 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1709 | /* |
| 1710 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1711 | * thus reset them as required. |
| 1712 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1713 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1714 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1715 | psa_destroy_key(key); |
| 1716 | PSA_DONE(); |
| 1717 | mbedtls_free(buffer); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1718 | } |
| 1719 | /* END_CASE */ |
| 1720 | |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 1721 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1722 | /* Import an RSA key with a valid structure (but not valid numbers |
| 1723 | * inside, beyond having sensible size and parity). This is expected to |
| 1724 | * fail for large keys. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1725 | 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] | 1726 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1727 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1728 | size_t bits = bits_arg; |
| 1729 | psa_status_t expected_status = expected_status_arg; |
| 1730 | psa_status_t status; |
| 1731 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1732 | 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] | 1733 | size_t buffer_size = /* Slight overapproximations */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1734 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1735 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1736 | unsigned char *p; |
| 1737 | int ret; |
| 1738 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1739 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1740 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1741 | PSA_ASSERT(psa_crypto_init()); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1742 | TEST_CALLOC(buffer, buffer_size); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1743 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1744 | TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p, |
| 1745 | bits, keypair)) >= 0); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1746 | length = ret; |
| 1747 | |
| 1748 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1749 | psa_set_key_type(&attributes, type); |
| 1750 | status = psa_import_key(&attributes, p, length, &key); |
| 1751 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1752 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1753 | if (status == PSA_SUCCESS) { |
| 1754 | PSA_ASSERT(psa_destroy_key(key)); |
| 1755 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1756 | |
| 1757 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1758 | mbedtls_free(buffer); |
| 1759 | PSA_DONE(); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1760 | } |
| 1761 | /* END_CASE */ |
| 1762 | |
| 1763 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1764 | void import_export(data_t *data, |
| 1765 | int type_arg, |
| 1766 | int usage_arg, int alg_arg, |
| 1767 | int lifetime_arg, |
| 1768 | int expected_bits, |
| 1769 | int export_size_delta, |
| 1770 | int expected_export_status_arg, |
| 1771 | /*whether reexport must give the original input exactly*/ |
| 1772 | int canonical_input) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1773 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1774 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1775 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1776 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1777 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1778 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1779 | psa_key_lifetime_t lifetime = lifetime_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1780 | unsigned char *exported = NULL; |
| 1781 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1782 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1783 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1784 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1785 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1786 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1787 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1788 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1789 | TEST_CALLOC(exported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1790 | if (!canonical_input) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1791 | TEST_CALLOC(reexported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1792 | } |
| 1793 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1794 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1795 | psa_set_key_lifetime(&attributes, lifetime); |
| 1796 | psa_set_key_usage_flags(&attributes, usage_arg); |
| 1797 | psa_set_key_algorithm(&attributes, alg); |
| 1798 | psa_set_key_type(&attributes, type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1799 | |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1800 | if (PSA_KEY_TYPE_IS_DH(type) && |
| 1801 | expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) { |
Przemek Stekiel | 654bef0 | 2022-12-15 13:28:02 +0100 | [diff] [blame] | 1802 | /* Simulate that buffer is too small, by decreasing its size by 1 byte. */ |
| 1803 | export_size -= 1; |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1804 | } |
| 1805 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1806 | /* Import the key */ |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1807 | TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key), |
Przemek Stekiel | 2e7c33d | 2023-04-27 12:29:45 +0200 | [diff] [blame] | 1808 | PSA_SUCCESS); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1809 | |
| 1810 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1811 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1812 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1813 | TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits); |
| 1814 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1815 | |
| 1816 | /* Export the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1817 | status = psa_export_key(key, exported, export_size, &exported_length); |
| 1818 | TEST_EQUAL(status, expected_export_status); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1819 | |
| 1820 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1821 | * and export_size. On errors, the exported length must be 0. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1822 | TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH); |
| 1823 | TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0); |
| 1824 | TEST_LE_U(exported_length, export_size); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1825 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1826 | TEST_ASSERT(mem_is_char(exported + exported_length, 0, |
| 1827 | export_size - exported_length)); |
| 1828 | if (status != PSA_SUCCESS) { |
| 1829 | TEST_EQUAL(exported_length, 0); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1830 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1831 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1832 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1833 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1834 | * this validates the canonical representations. For canonical inputs, |
| 1835 | * this doesn't directly validate the implementation, but it still helps |
| 1836 | * by cross-validating the test data with the sanity check code. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1837 | if (!psa_key_lifetime_is_external(lifetime)) { |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1838 | if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0, 0)) { |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1839 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1840 | } |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1841 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1842 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1843 | if (canonical_input) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1844 | TEST_MEMORY_COMPARE(data->x, data->len, exported, exported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1845 | } else { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1846 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1847 | PSA_ASSERT(psa_import_key(&attributes, exported, exported_length, |
| 1848 | &key2)); |
| 1849 | PSA_ASSERT(psa_export_key(key2, |
| 1850 | reexported, |
| 1851 | export_size, |
| 1852 | &reexported_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1853 | TEST_MEMORY_COMPARE(exported, exported_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1854 | reexported, reexported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1855 | PSA_ASSERT(psa_destroy_key(key2)); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1856 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1857 | TEST_LE_U(exported_length, |
| 1858 | PSA_EXPORT_KEY_OUTPUT_SIZE(type, |
| 1859 | psa_get_key_bits(&got_attributes))); |
Valerio Setti | 1eacae8 | 2023-07-28 16:07:03 +0200 | [diff] [blame] | 1860 | if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 1861 | TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
| 1862 | } else if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) { |
| 1863 | TEST_LE_U(exported_length, PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
| 1864 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1865 | |
| 1866 | destroy: |
| 1867 | /* Destroy the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1868 | PSA_ASSERT(psa_destroy_key(key)); |
| 1869 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1870 | |
| 1871 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1872 | /* |
| 1873 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1874 | * thus reset them as required. |
| 1875 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1876 | psa_reset_key_attributes(&got_attributes); |
| 1877 | psa_destroy_key(key); |
| 1878 | mbedtls_free(exported); |
| 1879 | mbedtls_free(reexported); |
| 1880 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1881 | } |
| 1882 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1883 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1884 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1885 | void import_export_public_key(data_t *data, |
| 1886 | int type_arg, // key pair or public key |
| 1887 | int alg_arg, |
| 1888 | int lifetime_arg, |
| 1889 | int export_size_delta, |
| 1890 | int expected_export_status_arg, |
| 1891 | data_t *expected_public_key) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1892 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1893 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1894 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1895 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1896 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1897 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1898 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1899 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1900 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1901 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1902 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1903 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1904 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1905 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1906 | psa_set_key_lifetime(&attributes, lifetime); |
| 1907 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1908 | psa_set_key_algorithm(&attributes, alg); |
| 1909 | psa_set_key_type(&attributes, type); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1910 | |
| 1911 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1912 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1913 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1914 | /* Export the public key */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1915 | TEST_CALLOC(exported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1916 | status = psa_export_public_key(key, |
| 1917 | exported, export_size, |
| 1918 | &exported_length); |
| 1919 | TEST_EQUAL(status, expected_export_status); |
| 1920 | if (status == PSA_SUCCESS) { |
| 1921 | 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] | 1922 | size_t bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1923 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1924 | bits = psa_get_key_bits(&attributes); |
| 1925 | TEST_LE_U(expected_public_key->len, |
| 1926 | PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1927 | TEST_LE_U(expected_public_key->len, |
| 1928 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1929 | TEST_LE_U(expected_public_key->len, |
| 1930 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1931 | TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1932 | exported, exported_length); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1933 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1934 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1935 | /* |
| 1936 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1937 | * thus reset them as required. |
| 1938 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1939 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1940 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1941 | mbedtls_free(exported); |
| 1942 | psa_destroy_key(key); |
| 1943 | PSA_DONE(); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1944 | } |
| 1945 | /* END_CASE */ |
| 1946 | |
Ryan Everett | 5061999 | 2024-03-12 16:55:14 +0000 | [diff] [blame] | 1947 | |
| 1948 | #if defined(MBEDTLS_THREADING_PTHREAD) |
| 1949 | /* BEGIN_CASE depends_on:MBEDTLS_THREADING_PTHREAD:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
| 1950 | void concurrently_use_same_persistent_key(data_t *data, |
| 1951 | int type_arg, |
| 1952 | int bits_arg, |
| 1953 | int alg_arg, |
| 1954 | int thread_count_arg) |
| 1955 | { |
| 1956 | size_t thread_count = (size_t) thread_count_arg; |
| 1957 | mbedtls_test_thread_t *threads = NULL; |
| 1958 | mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1); |
| 1959 | same_key_context skc; |
| 1960 | skc.data = data; |
| 1961 | skc.key = key_id; |
| 1962 | skc.type = type_arg; |
| 1963 | skc.bits = bits_arg; |
| 1964 | skc.key_loaded = 0; |
| 1965 | mbedtls_mutex_init(&skc.key_loaded_mutex); |
| 1966 | psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(skc.type, alg_arg); |
| 1967 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1968 | |
| 1969 | PSA_ASSERT(psa_crypto_init()); |
| 1970 | |
| 1971 | psa_set_key_id(&attributes, key_id); |
| 1972 | psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_PERSISTENT); |
| 1973 | psa_set_key_usage_flags(&attributes, usage); |
| 1974 | psa_set_key_algorithm(&attributes, alg_arg); |
| 1975 | psa_set_key_type(&attributes, type_arg); |
| 1976 | psa_set_key_bits(&attributes, bits_arg); |
| 1977 | skc.attributes = &attributes; |
| 1978 | |
| 1979 | TEST_CALLOC(threads, sizeof(mbedtls_test_thread_t) * thread_count); |
| 1980 | |
| 1981 | /* Test that when multiple threads import the same key, |
| 1982 | * exactly one thread succeeds and the rest fail with valid errors. |
| 1983 | * Also test that all threads can use the key as soon as it has been |
| 1984 | * imported. */ |
| 1985 | for (size_t i = 0; i < thread_count; i++) { |
| 1986 | TEST_EQUAL( |
| 1987 | mbedtls_test_thread_create(&threads[i], thread_import_key, |
| 1988 | (void *) &skc), 0); |
| 1989 | } |
| 1990 | |
| 1991 | /* Join threads. */ |
| 1992 | for (size_t i = 0; i < thread_count; i++) { |
| 1993 | TEST_EQUAL(mbedtls_test_thread_join(&threads[i]), 0); |
| 1994 | } |
| 1995 | |
| 1996 | /* Test that when multiple threads use and destroy a key no corruption |
| 1997 | * occurs, and exactly one thread succeeds when destroying the key. */ |
| 1998 | for (size_t i = 0; i < thread_count; i++) { |
| 1999 | TEST_EQUAL( |
| 2000 | mbedtls_test_thread_create(&threads[i], thread_use_and_destroy_key, |
| 2001 | (void *) &skc), 0); |
| 2002 | } |
| 2003 | |
| 2004 | /* Join threads. */ |
| 2005 | for (size_t i = 0; i < thread_count; i++) { |
| 2006 | TEST_EQUAL(mbedtls_test_thread_join(&threads[i]), 0); |
| 2007 | } |
| 2008 | /* Ensure that one thread succeeded in destroying the key. */ |
| 2009 | TEST_ASSERT(!skc.key_loaded); |
| 2010 | exit: |
| 2011 | psa_reset_key_attributes(&attributes); |
| 2012 | mbedtls_mutex_free(&skc.key_loaded_mutex); |
| 2013 | mbedtls_free(threads); |
| 2014 | PSA_DONE(); |
| 2015 | } |
| 2016 | /* END_CASE */ |
| 2017 | #endif |
| 2018 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2019 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2020 | void import_and_exercise_key(data_t *data, |
| 2021 | int type_arg, |
| 2022 | int bits_arg, |
| 2023 | int alg_arg) |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2024 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2025 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2026 | psa_key_type_t type = type_arg; |
| 2027 | size_t bits = bits_arg; |
| 2028 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2029 | 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] | 2030 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2031 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2032 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2033 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2035 | psa_set_key_usage_flags(&attributes, usage); |
| 2036 | psa_set_key_algorithm(&attributes, alg); |
| 2037 | psa_set_key_type(&attributes, type); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2038 | |
| 2039 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2040 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2041 | |
| 2042 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2043 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 2044 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 2045 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2046 | |
| 2047 | /* Do something with the key according to its type and permitted usage. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 2048 | if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 2049 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2050 | } |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2051 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2052 | PSA_ASSERT(psa_destroy_key(key)); |
| 2053 | test_operations_on_invalid_key(key); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 2054 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2055 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2056 | /* |
| 2057 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2058 | * thus reset them as required. |
| 2059 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2060 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2061 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2062 | psa_reset_key_attributes(&attributes); |
| 2063 | psa_destroy_key(key); |
| 2064 | PSA_DONE(); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2065 | } |
| 2066 | /* END_CASE */ |
| 2067 | |
| 2068 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2069 | void effective_key_attributes(int type_arg, int expected_type_arg, |
| 2070 | int bits_arg, int expected_bits_arg, |
| 2071 | int usage_arg, int expected_usage_arg, |
| 2072 | int alg_arg, int expected_alg_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2073 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2074 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 2075 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 2076 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 2077 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 2078 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2079 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 2080 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2081 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 2082 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2083 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2084 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2085 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2086 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2087 | psa_set_key_usage_flags(&attributes, usage); |
| 2088 | psa_set_key_algorithm(&attributes, alg); |
| 2089 | psa_set_key_type(&attributes, key_type); |
| 2090 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2091 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2092 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
| 2093 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2094 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2095 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 2096 | TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type); |
| 2097 | TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits); |
| 2098 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
| 2099 | TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2100 | |
| 2101 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2102 | /* |
| 2103 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2104 | * thus reset them as required. |
| 2105 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2106 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2107 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2108 | psa_destroy_key(key); |
| 2109 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2110 | } |
| 2111 | /* END_CASE */ |
| 2112 | |
| 2113 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2114 | void check_key_policy(int type_arg, int bits_arg, |
| 2115 | int usage_arg, int alg_arg) |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 2116 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2117 | test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg, |
| 2118 | usage_arg, |
| 2119 | mbedtls_test_update_key_usage_flags(usage_arg), |
| 2120 | alg_arg, alg_arg); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 2121 | goto exit; |
| 2122 | } |
| 2123 | /* END_CASE */ |
| 2124 | |
| 2125 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2126 | void key_attributes_init() |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 2127 | { |
| 2128 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2129 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2130 | * 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] | 2131 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2132 | psa_key_attributes_t func = psa_key_attributes_init(); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2133 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 2134 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 2135 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2136 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 2137 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2138 | TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE); |
| 2139 | TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE); |
| 2140 | TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2141 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2142 | TEST_EQUAL(psa_get_key_type(&func), 0); |
| 2143 | TEST_EQUAL(psa_get_key_type(&init), 0); |
| 2144 | TEST_EQUAL(psa_get_key_type(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2145 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2146 | TEST_EQUAL(psa_get_key_bits(&func), 0); |
| 2147 | TEST_EQUAL(psa_get_key_bits(&init), 0); |
| 2148 | TEST_EQUAL(psa_get_key_bits(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2150 | TEST_EQUAL(psa_get_key_usage_flags(&func), 0); |
| 2151 | TEST_EQUAL(psa_get_key_usage_flags(&init), 0); |
| 2152 | TEST_EQUAL(psa_get_key_usage_flags(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2153 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2154 | TEST_EQUAL(psa_get_key_algorithm(&func), 0); |
| 2155 | TEST_EQUAL(psa_get_key_algorithm(&init), 0); |
| 2156 | TEST_EQUAL(psa_get_key_algorithm(&zero), 0); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 2157 | } |
| 2158 | /* END_CASE */ |
| 2159 | |
| 2160 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2161 | void mac_key_policy(int policy_usage_arg, |
| 2162 | int policy_alg_arg, |
| 2163 | int key_type_arg, |
| 2164 | data_t *key_data, |
| 2165 | int exercise_alg_arg, |
| 2166 | int expected_status_sign_arg, |
| 2167 | int expected_status_verify_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2168 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2169 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2170 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2171 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2172 | psa_key_type_t key_type = key_type_arg; |
| 2173 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 2174 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 2175 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2176 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 2177 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 2178 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2179 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2181 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2183 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2184 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2185 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2186 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2187 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2188 | &key)); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2189 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2190 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 2191 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2192 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2193 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 2194 | TEST_EQUAL(status, expected_status_sign); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2195 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 2196 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2197 | uint8_t input[128] = { 0 }; |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 2198 | size_t mac_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2199 | TEST_EQUAL(psa_mac_compute(key, exercise_alg, |
| 2200 | input, 128, |
| 2201 | mac, PSA_MAC_MAX_SIZE, &mac_len), |
| 2202 | expected_status_sign); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 2203 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2204 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2205 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 2206 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 2207 | if (status == PSA_SUCCESS) { |
| 2208 | status = psa_mac_update(&operation, input, 128); |
| 2209 | if (status == PSA_SUCCESS) { |
| 2210 | TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE, |
| 2211 | &mac_len), |
| 2212 | expected_status_sign); |
| 2213 | } else { |
| 2214 | TEST_EQUAL(status, expected_status_sign); |
| 2215 | } |
| 2216 | } else { |
| 2217 | TEST_EQUAL(status, expected_status_sign); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2218 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2219 | PSA_ASSERT(psa_mac_abort(&operation)); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2220 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 2221 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2222 | status = psa_mac_verify(key, exercise_alg, input, 128, |
| 2223 | mac, mac_len); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 2224 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2225 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 2226 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2227 | } else { |
| 2228 | TEST_EQUAL(status, expected_status_verify); |
| 2229 | } |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2230 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2231 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2232 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 2233 | if (status == PSA_SUCCESS) { |
| 2234 | status = psa_mac_update(&operation, input, 128); |
| 2235 | if (status == PSA_SUCCESS) { |
| 2236 | status = psa_mac_verify_finish(&operation, mac, mac_len); |
| 2237 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 2238 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2239 | } else { |
| 2240 | TEST_EQUAL(status, expected_status_verify); |
| 2241 | } |
| 2242 | } else { |
| 2243 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2244 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2245 | } else { |
| 2246 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2247 | } |
| 2248 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2249 | psa_mac_abort(&operation); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2250 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2251 | memset(mac, 0, sizeof(mac)); |
| 2252 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 2253 | TEST_EQUAL(status, expected_status_verify); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2254 | |
| 2255 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2256 | psa_mac_abort(&operation); |
| 2257 | psa_destroy_key(key); |
| 2258 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2259 | } |
| 2260 | /* END_CASE */ |
| 2261 | |
| 2262 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2263 | void cipher_key_policy(int policy_usage_arg, |
| 2264 | int policy_alg, |
| 2265 | int key_type, |
| 2266 | data_t *key_data, |
| 2267 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2268 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2269 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2270 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2271 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2272 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2273 | size_t output_buffer_size = 0; |
| 2274 | size_t input_buffer_size = 0; |
| 2275 | size_t output_length = 0; |
| 2276 | uint8_t *output = NULL; |
| 2277 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2278 | psa_status_t status; |
| 2279 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2280 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg); |
| 2281 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg, |
| 2282 | input_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2283 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2284 | TEST_CALLOC(input, input_buffer_size); |
| 2285 | TEST_CALLOC(output, output_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2286 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2287 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2288 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2289 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2290 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2291 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2292 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2293 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2294 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2295 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2296 | /* Check if no key usage flag implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2297 | TEST_EQUAL(policy_usage, |
| 2298 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2299 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2300 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2301 | status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size, |
| 2302 | output, output_buffer_size, |
| 2303 | &output_length); |
| 2304 | if (policy_alg == exercise_alg && |
| 2305 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 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 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2312 | status = psa_cipher_encrypt_setup(&operation, key, exercise_alg); |
| 2313 | if (policy_alg == exercise_alg && |
| 2314 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2315 | PSA_ASSERT(status); |
| 2316 | } else { |
| 2317 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2318 | } |
| 2319 | psa_cipher_abort(&operation); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2320 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2321 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2322 | status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size, |
| 2323 | input, input_buffer_size, |
| 2324 | &output_length); |
| 2325 | if (policy_alg == exercise_alg && |
| 2326 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2327 | PSA_ASSERT(status); |
| 2328 | } else { |
| 2329 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2330 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2331 | |
| 2332 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2333 | status = psa_cipher_decrypt_setup(&operation, key, exercise_alg); |
| 2334 | if (policy_alg == exercise_alg && |
| 2335 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2336 | PSA_ASSERT(status); |
| 2337 | } else { |
| 2338 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2339 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2340 | |
| 2341 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2342 | psa_cipher_abort(&operation); |
| 2343 | mbedtls_free(input); |
| 2344 | mbedtls_free(output); |
| 2345 | psa_destroy_key(key); |
| 2346 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2347 | } |
| 2348 | /* END_CASE */ |
| 2349 | |
| 2350 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2351 | void aead_key_policy(int policy_usage_arg, |
| 2352 | int policy_alg, |
| 2353 | int key_type, |
| 2354 | data_t *key_data, |
| 2355 | int nonce_length_arg, |
| 2356 | int tag_length_arg, |
| 2357 | int exercise_alg, |
| 2358 | int expected_status_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2359 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2360 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2361 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2362 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2363 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2364 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2365 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2366 | unsigned char nonce[16] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2367 | size_t nonce_length = nonce_length_arg; |
| 2368 | unsigned char tag[16]; |
| 2369 | size_t tag_length = tag_length_arg; |
| 2370 | size_t output_length; |
| 2371 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2372 | TEST_LE_U(nonce_length, sizeof(nonce)); |
| 2373 | TEST_LE_U(tag_length, sizeof(tag)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2374 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2375 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2377 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2378 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2379 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2380 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2381 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2382 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2383 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2384 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2385 | TEST_EQUAL(policy_usage, |
| 2386 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2387 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2388 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2389 | status = psa_aead_encrypt(key, exercise_alg, |
| 2390 | nonce, nonce_length, |
| 2391 | NULL, 0, |
| 2392 | NULL, 0, |
| 2393 | tag, tag_length, |
| 2394 | &output_length); |
| 2395 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2396 | TEST_EQUAL(status, expected_status); |
| 2397 | } else { |
| 2398 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2399 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2400 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2401 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2402 | status = psa_aead_encrypt_setup(&operation, key, exercise_alg); |
| 2403 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2404 | TEST_EQUAL(status, expected_status); |
| 2405 | } else { |
| 2406 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2407 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2408 | |
| 2409 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2410 | memset(tag, 0, sizeof(tag)); |
| 2411 | status = psa_aead_decrypt(key, exercise_alg, |
| 2412 | nonce, nonce_length, |
| 2413 | NULL, 0, |
| 2414 | tag, tag_length, |
| 2415 | NULL, 0, |
| 2416 | &output_length); |
| 2417 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2418 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2419 | } else if (expected_status == PSA_SUCCESS) { |
| 2420 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2421 | } else { |
| 2422 | TEST_EQUAL(status, expected_status); |
| 2423 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2424 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2425 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2426 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2427 | status = psa_aead_decrypt_setup(&operation, key, exercise_alg); |
| 2428 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2429 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2430 | } else { |
| 2431 | TEST_EQUAL(status, expected_status); |
| 2432 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2433 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2434 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2435 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2436 | psa_destroy_key(key); |
| 2437 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2438 | } |
| 2439 | /* END_CASE */ |
| 2440 | |
| 2441 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2442 | void asymmetric_encryption_key_policy(int policy_usage_arg, |
| 2443 | int policy_alg, |
| 2444 | int key_type, |
| 2445 | data_t *key_data, |
Valerio Setti | f202c29 | 2024-01-15 10:42:37 +0100 | [diff] [blame] | 2446 | int exercise_alg, |
| 2447 | int use_opaque_key) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2448 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2449 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2450 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2451 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2452 | psa_status_t status; |
| 2453 | size_t key_bits; |
| 2454 | size_t buffer_length; |
| 2455 | unsigned char *buffer = NULL; |
| 2456 | size_t output_length; |
| 2457 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2458 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2459 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2460 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2461 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2462 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2463 | |
Valerio Setti | f202c29 | 2024-01-15 10:42:37 +0100 | [diff] [blame] | 2464 | if (use_opaque_key) { |
| 2465 | psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION( |
| 2466 | PSA_KEY_PERSISTENCE_VOLATILE, TEST_DRIVER_LOCATION)); |
| 2467 | } |
| 2468 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2469 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2470 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2471 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2472 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2473 | TEST_EQUAL(policy_usage, |
| 2474 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2475 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2476 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 2477 | key_bits = psa_get_key_bits(&attributes); |
| 2478 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, |
| 2479 | exercise_alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2480 | TEST_CALLOC(buffer, buffer_length); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2481 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2482 | status = psa_asymmetric_encrypt(key, exercise_alg, |
| 2483 | NULL, 0, |
| 2484 | NULL, 0, |
| 2485 | buffer, buffer_length, |
| 2486 | &output_length); |
| 2487 | if (policy_alg == exercise_alg && |
| 2488 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2489 | PSA_ASSERT(status); |
| 2490 | } else { |
| 2491 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2492 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2493 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2494 | if (buffer_length != 0) { |
| 2495 | memset(buffer, 0, buffer_length); |
| 2496 | } |
| 2497 | status = psa_asymmetric_decrypt(key, exercise_alg, |
| 2498 | buffer, buffer_length, |
| 2499 | NULL, 0, |
| 2500 | buffer, buffer_length, |
| 2501 | &output_length); |
| 2502 | if (policy_alg == exercise_alg && |
| 2503 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2504 | TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING); |
| 2505 | } else { |
| 2506 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2507 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2508 | |
| 2509 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2510 | /* |
| 2511 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2512 | * thus reset them as required. |
| 2513 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2514 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2515 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2516 | psa_destroy_key(key); |
| 2517 | PSA_DONE(); |
| 2518 | mbedtls_free(buffer); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2519 | } |
| 2520 | /* END_CASE */ |
| 2521 | |
| 2522 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2523 | void asymmetric_signature_key_policy(int policy_usage_arg, |
| 2524 | int policy_alg, |
| 2525 | int key_type, |
| 2526 | data_t *key_data, |
| 2527 | int exercise_alg, |
| 2528 | int payload_length_arg, |
| 2529 | int expected_usage_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2530 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2531 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2532 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2533 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 2534 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2535 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2536 | unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 }; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2537 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2538 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2539 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2540 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2541 | int compatible_alg = payload_length_arg > 0; |
| 2542 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2543 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2544 | size_t signature_length; |
| 2545 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2546 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2547 | in the expected usage flags. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2548 | TEST_EQUAL(expected_usage, |
| 2549 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2550 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2551 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2552 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2553 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2554 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2555 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2556 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2557 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2558 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2559 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2560 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2562 | status = psa_sign_hash(key, exercise_alg, |
| 2563 | payload, payload_length, |
| 2564 | signature, sizeof(signature), |
| 2565 | &signature_length); |
| 2566 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) { |
| 2567 | PSA_ASSERT(status); |
| 2568 | } else { |
| 2569 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2570 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2571 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2572 | memset(signature, 0, sizeof(signature)); |
| 2573 | status = psa_verify_hash(key, exercise_alg, |
| 2574 | payload, payload_length, |
| 2575 | signature, sizeof(signature)); |
| 2576 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) { |
| 2577 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2578 | } else { |
| 2579 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2580 | } |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2581 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2582 | if (PSA_ALG_IS_SIGN_HASH(exercise_alg) && |
| 2583 | PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) { |
| 2584 | status = psa_sign_message(key, exercise_alg, |
| 2585 | payload, payload_length, |
| 2586 | signature, sizeof(signature), |
| 2587 | &signature_length); |
| 2588 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) { |
| 2589 | PSA_ASSERT(status); |
| 2590 | } else { |
| 2591 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2592 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2593 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2594 | memset(signature, 0, sizeof(signature)); |
| 2595 | status = psa_verify_message(key, exercise_alg, |
| 2596 | payload, payload_length, |
| 2597 | signature, sizeof(signature)); |
| 2598 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) { |
| 2599 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2600 | } else { |
| 2601 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2602 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2603 | } |
| 2604 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2605 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2606 | psa_destroy_key(key); |
| 2607 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2608 | } |
| 2609 | /* END_CASE */ |
| 2610 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2611 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2612 | void derive_key_policy(int policy_usage, |
| 2613 | int policy_alg, |
| 2614 | int key_type, |
| 2615 | data_t *key_data, |
| 2616 | int exercise_alg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2617 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2618 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2619 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2620 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2621 | psa_status_t status; |
| 2622 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2623 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2624 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2625 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2626 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2627 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2628 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2629 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2630 | &key)); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2631 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2632 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2633 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2634 | if (PSA_ALG_IS_TLS12_PRF(exercise_alg) || |
| 2635 | PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) { |
| 2636 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 2637 | &operation, |
| 2638 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2639 | (const uint8_t *) "", 0)); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2640 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2642 | status = psa_key_derivation_input_key(&operation, |
| 2643 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 2644 | key); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2645 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2646 | if (policy_alg == exercise_alg && |
| 2647 | (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) { |
| 2648 | PSA_ASSERT(status); |
| 2649 | } else { |
| 2650 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2651 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2652 | |
| 2653 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2654 | psa_key_derivation_abort(&operation); |
| 2655 | psa_destroy_key(key); |
| 2656 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2657 | } |
| 2658 | /* END_CASE */ |
| 2659 | |
| 2660 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2661 | void agreement_key_policy(int policy_usage, |
| 2662 | int policy_alg, |
| 2663 | int key_type_arg, |
| 2664 | data_t *key_data, |
| 2665 | int exercise_alg, |
| 2666 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2667 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2668 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2669 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2670 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2671 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2672 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2673 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2674 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2675 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2676 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2677 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2678 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2679 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2680 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2681 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2682 | &key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2683 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2684 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
Ryan Everett | 73e4ea3 | 2024-03-12 16:29:55 +0000 | [diff] [blame] | 2685 | status = mbedtls_test_psa_key_agreement_with_self(&operation, key, 0); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2686 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2687 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2688 | |
| 2689 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2690 | psa_key_derivation_abort(&operation); |
| 2691 | psa_destroy_key(key); |
| 2692 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2693 | } |
| 2694 | /* END_CASE */ |
| 2695 | |
| 2696 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2697 | void key_policy_alg2(int key_type_arg, data_t *key_data, |
| 2698 | int usage_arg, int alg_arg, int alg2_arg) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2699 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2700 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2701 | psa_key_type_t key_type = key_type_arg; |
| 2702 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2703 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2704 | psa_key_usage_t usage = usage_arg; |
| 2705 | psa_algorithm_t alg = alg_arg; |
| 2706 | psa_algorithm_t alg2 = alg2_arg; |
| 2707 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2708 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2709 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2710 | psa_set_key_usage_flags(&attributes, usage); |
| 2711 | psa_set_key_algorithm(&attributes, alg); |
| 2712 | psa_set_key_enrollment_algorithm(&attributes, alg2); |
| 2713 | psa_set_key_type(&attributes, key_type); |
| 2714 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2715 | &key)); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2716 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2717 | /* Update the usage flags to obtain implicit usage flags */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2718 | usage = mbedtls_test_update_key_usage_flags(usage); |
| 2719 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 2720 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage); |
| 2721 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 2722 | TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2723 | |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 2724 | if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2725 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2726 | } |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 2727 | if (!mbedtls_test_psa_exercise_key(key, usage, alg2, 0)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2728 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2729 | } |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2730 | |
| 2731 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2732 | /* |
| 2733 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2734 | * thus reset them as required. |
| 2735 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2736 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2737 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2738 | psa_destroy_key(key); |
| 2739 | PSA_DONE(); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2740 | } |
| 2741 | /* END_CASE */ |
| 2742 | |
| 2743 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2744 | void raw_agreement_key_policy(int policy_usage, |
| 2745 | int policy_alg, |
| 2746 | int key_type_arg, |
| 2747 | data_t *key_data, |
| 2748 | int exercise_alg, |
| 2749 | int expected_status_arg) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2750 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2751 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2752 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2753 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2754 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2755 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2756 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2757 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2758 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2759 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2760 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2761 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2762 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2763 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2764 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2765 | &key)); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2766 | |
Ryan Everett | 8163028 | 2024-03-12 16:21:12 +0000 | [diff] [blame] | 2767 | 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] | 2768 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2769 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2770 | |
| 2771 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2772 | psa_key_derivation_abort(&operation); |
| 2773 | psa_destroy_key(key); |
| 2774 | PSA_DONE(); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2775 | } |
| 2776 | /* END_CASE */ |
| 2777 | |
| 2778 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2779 | void copy_success(int source_usage_arg, |
| 2780 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4ea4ad0 | 2022-12-04 15:11:00 +0100 | [diff] [blame] | 2781 | int source_lifetime_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2782 | int type_arg, data_t *material, |
| 2783 | int copy_attributes, |
| 2784 | int target_usage_arg, |
| 2785 | int target_alg_arg, int target_alg2_arg, |
Gilles Peskine | 4ea4ad0 | 2022-12-04 15:11:00 +0100 | [diff] [blame] | 2786 | int target_lifetime_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2787 | int expected_usage_arg, |
| 2788 | int expected_alg_arg, int expected_alg2_arg) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2789 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2790 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2791 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2792 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2793 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2794 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2795 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 2796 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2797 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2798 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2799 | uint8_t *export_buffer = NULL; |
| 2800 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2801 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2802 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2803 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2804 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2805 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2806 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2807 | psa_set_key_type(&source_attributes, type_arg); |
| 2808 | psa_set_key_lifetime(&source_attributes, source_lifetime); |
| 2809 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2810 | material->x, material->len, |
| 2811 | &source_key)); |
| 2812 | PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2813 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2814 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2815 | if (copy_attributes) { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2816 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2817 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2818 | psa_set_key_lifetime(&target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2819 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2820 | if (target_usage_arg != -1) { |
| 2821 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2822 | } |
| 2823 | if (target_alg_arg != -1) { |
| 2824 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2825 | } |
| 2826 | if (target_alg2_arg != -1) { |
| 2827 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
| 2828 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2829 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2830 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2831 | /* Copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2832 | PSA_ASSERT(psa_copy_key(source_key, |
| 2833 | &target_attributes, &target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2834 | |
| 2835 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2836 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2837 | |
| 2838 | /* Test that the target slot has the expected content and policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2839 | PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes)); |
| 2840 | TEST_EQUAL(psa_get_key_type(&source_attributes), |
| 2841 | psa_get_key_type(&target_attributes)); |
| 2842 | TEST_EQUAL(psa_get_key_bits(&source_attributes), |
| 2843 | psa_get_key_bits(&target_attributes)); |
| 2844 | TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes)); |
| 2845 | TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes)); |
| 2846 | TEST_EQUAL(expected_alg2, |
| 2847 | psa_get_key_enrollment_algorithm(&target_attributes)); |
| 2848 | if (expected_usage & PSA_KEY_USAGE_EXPORT) { |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2849 | size_t length; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2850 | TEST_CALLOC(export_buffer, material->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2851 | PSA_ASSERT(psa_export_key(target_key, export_buffer, |
| 2852 | material->len, &length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2853 | TEST_MEMORY_COMPARE(material->x, material->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2854 | export_buffer, length); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2855 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2856 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2857 | if (!psa_key_lifetime_is_external(target_lifetime)) { |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 2858 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg, 0)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2859 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2860 | } |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 2861 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2, 0)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2862 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2863 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2864 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2865 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2866 | PSA_ASSERT(psa_destroy_key(target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2867 | |
| 2868 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2869 | /* |
| 2870 | * Source and target key attributes may have been returned by |
| 2871 | * psa_get_key_attributes() thus reset them as required. |
| 2872 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2873 | psa_reset_key_attributes(&source_attributes); |
| 2874 | psa_reset_key_attributes(&target_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2875 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2876 | PSA_DONE(); |
| 2877 | mbedtls_free(export_buffer); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2878 | } |
| 2879 | /* END_CASE */ |
| 2880 | |
| 2881 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2882 | void copy_fail(int source_usage_arg, |
| 2883 | int source_alg_arg, int source_alg2_arg, |
| 2884 | int source_lifetime_arg, |
| 2885 | int type_arg, data_t *material, |
| 2886 | int target_type_arg, int target_bits_arg, |
| 2887 | int target_usage_arg, |
| 2888 | int target_alg_arg, int target_alg2_arg, |
| 2889 | int target_id_arg, int target_lifetime_arg, |
| 2890 | int expected_status_arg) |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2891 | { |
| 2892 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2893 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2894 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2895 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2896 | 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] | 2897 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2898 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2899 | |
| 2900 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2901 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2902 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2903 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2904 | psa_set_key_type(&source_attributes, type_arg); |
| 2905 | psa_set_key_lifetime(&source_attributes, source_lifetime_arg); |
| 2906 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2907 | material->x, material->len, |
| 2908 | &source_key)); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2909 | |
| 2910 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2911 | psa_set_key_id(&target_attributes, key_id); |
| 2912 | psa_set_key_lifetime(&target_attributes, target_lifetime_arg); |
| 2913 | psa_set_key_type(&target_attributes, target_type_arg); |
| 2914 | psa_set_key_bits(&target_attributes, target_bits_arg); |
| 2915 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2916 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2917 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2918 | |
| 2919 | /* Try to copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2920 | TEST_EQUAL(psa_copy_key(source_key, |
| 2921 | &target_attributes, &target_key), |
| 2922 | expected_status_arg); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2923 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2924 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2925 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2926 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2927 | psa_reset_key_attributes(&source_attributes); |
| 2928 | psa_reset_key_attributes(&target_attributes); |
| 2929 | PSA_DONE(); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2930 | } |
| 2931 | /* END_CASE */ |
| 2932 | |
| 2933 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2934 | void hash_operation_init() |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2935 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2936 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2937 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2938 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2939 | * 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] | 2940 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2941 | psa_hash_operation_t func = psa_hash_operation_init(); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2942 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2943 | psa_hash_operation_t zero; |
| 2944 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2945 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2946 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2947 | /* A freshly-initialized hash operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2948 | TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)), |
| 2949 | PSA_ERROR_BAD_STATE); |
| 2950 | TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)), |
| 2951 | PSA_ERROR_BAD_STATE); |
| 2952 | TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)), |
| 2953 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2954 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2955 | /* A default hash operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2956 | PSA_ASSERT(psa_hash_abort(&func)); |
| 2957 | PSA_ASSERT(psa_hash_abort(&init)); |
| 2958 | PSA_ASSERT(psa_hash_abort(&zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2959 | } |
| 2960 | /* END_CASE */ |
| 2961 | |
| 2962 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2963 | void hash_setup(int alg_arg, |
| 2964 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2965 | { |
| 2966 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2967 | uint8_t *output = NULL; |
| 2968 | size_t output_size = 0; |
| 2969 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2970 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2971 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2972 | psa_status_t status; |
| 2973 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2974 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2975 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2976 | /* Hash Setup, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2977 | output_size = PSA_HASH_LENGTH(alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2978 | TEST_CALLOC(output, output_size); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2979 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2980 | status = psa_hash_compute(alg, NULL, 0, |
| 2981 | output, output_size, &output_length); |
| 2982 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2983 | |
| 2984 | /* Hash Setup, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2985 | status = psa_hash_setup(&operation, alg); |
| 2986 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2987 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2988 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2989 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2990 | |
| 2991 | /* If setup failed, reproduce the failure, so as to |
| 2992 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2993 | if (status != PSA_SUCCESS) { |
| 2994 | TEST_EQUAL(psa_hash_setup(&operation, alg), status); |
| 2995 | } |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2996 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2997 | /* Now the operation object should be reusable. */ |
| 2998 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2999 | PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG)); |
| 3000 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3001 | #endif |
| 3002 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3003 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3004 | mbedtls_free(output); |
| 3005 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3006 | } |
| 3007 | /* END_CASE */ |
| 3008 | |
| 3009 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3010 | void hash_compute_fail(int alg_arg, data_t *input, |
| 3011 | int output_size_arg, int expected_status_arg) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3012 | { |
| 3013 | psa_algorithm_t alg = alg_arg; |
| 3014 | uint8_t *output = NULL; |
| 3015 | size_t output_size = output_size_arg; |
| 3016 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 3017 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3018 | psa_status_t expected_status = expected_status_arg; |
| 3019 | psa_status_t status; |
| 3020 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3021 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3022 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3023 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3024 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 3025 | /* Hash Compute, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3026 | status = psa_hash_compute(alg, input->x, input->len, |
| 3027 | output, output_size, &output_length); |
| 3028 | TEST_EQUAL(status, expected_status); |
| 3029 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3030 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 3031 | /* Hash Compute, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3032 | status = psa_hash_setup(&operation, alg); |
| 3033 | if (status == PSA_SUCCESS) { |
| 3034 | status = psa_hash_update(&operation, input->x, input->len); |
| 3035 | if (status == PSA_SUCCESS) { |
| 3036 | status = psa_hash_finish(&operation, output, output_size, |
| 3037 | &output_length); |
| 3038 | if (status == PSA_SUCCESS) { |
| 3039 | TEST_LE_U(output_length, output_size); |
| 3040 | } else { |
| 3041 | TEST_EQUAL(status, expected_status); |
| 3042 | } |
| 3043 | } else { |
| 3044 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 3045 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3046 | } else { |
| 3047 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 3048 | } |
| 3049 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3050 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3051 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3052 | mbedtls_free(output); |
| 3053 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3054 | } |
| 3055 | /* END_CASE */ |
| 3056 | |
| 3057 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3058 | void hash_compare_fail(int alg_arg, data_t *input, |
| 3059 | data_t *reference_hash, |
| 3060 | int expected_status_arg) |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 3061 | { |
| 3062 | psa_algorithm_t alg = alg_arg; |
| 3063 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 3064 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 3065 | psa_status_t status; |
| 3066 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3067 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 3068 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 3069 | /* Hash Compare, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3070 | status = psa_hash_compare(alg, input->x, input->len, |
| 3071 | reference_hash->x, reference_hash->len); |
| 3072 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 3073 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 3074 | /* Hash Compare, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3075 | status = psa_hash_setup(&operation, alg); |
| 3076 | if (status == PSA_SUCCESS) { |
| 3077 | status = psa_hash_update(&operation, input->x, input->len); |
| 3078 | if (status == PSA_SUCCESS) { |
| 3079 | status = psa_hash_verify(&operation, reference_hash->x, |
| 3080 | reference_hash->len); |
| 3081 | TEST_EQUAL(status, expected_status); |
| 3082 | } else { |
| 3083 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 3084 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3085 | } else { |
| 3086 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 3087 | } |
| 3088 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 3089 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3090 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3091 | PSA_DONE(); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 3092 | } |
| 3093 | /* END_CASE */ |
| 3094 | |
| 3095 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3096 | void hash_compute_compare(int alg_arg, data_t *input, |
| 3097 | data_t *expected_output) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3098 | { |
| 3099 | psa_algorithm_t alg = alg_arg; |
| 3100 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 3101 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3102 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3103 | size_t i; |
| 3104 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3105 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3106 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3107 | /* Compute with tight buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3108 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 3109 | output, PSA_HASH_LENGTH(alg), |
| 3110 | &output_length)); |
| 3111 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3112 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3113 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3114 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3115 | /* Compute with tight buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3116 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3117 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 3118 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 3119 | PSA_HASH_LENGTH(alg), |
| 3120 | &output_length)); |
| 3121 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3122 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3123 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3124 | |
| 3125 | /* Compute with larger buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3126 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 3127 | output, sizeof(output), |
| 3128 | &output_length)); |
| 3129 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3130 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3131 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3132 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3133 | /* Compute with larger buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3134 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3135 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 3136 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 3137 | sizeof(output), &output_length)); |
| 3138 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3139 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3140 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3141 | |
| 3142 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3143 | PSA_ASSERT(psa_hash_compare(alg, input->x, input->len, |
| 3144 | output, output_length)); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3145 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3146 | /* Compare with correct hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3147 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3148 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 3149 | PSA_ASSERT(psa_hash_verify(&operation, output, |
| 3150 | output_length)); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3151 | |
| 3152 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3153 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 3154 | output, output_length + 1), |
| 3155 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3156 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3157 | /* Compare with trailing garbage, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3158 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3159 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 3160 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1), |
| 3161 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3162 | |
| 3163 | /* Compare with truncated hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3164 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 3165 | output, output_length - 1), |
| 3166 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3167 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3168 | /* Compare with truncated hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3169 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3170 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 3171 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1), |
| 3172 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3173 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3174 | /* Compare with corrupted value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3175 | for (i = 0; i < output_length; i++) { |
| 3176 | mbedtls_test_set_step(i); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3177 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3178 | |
| 3179 | /* One-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3180 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 3181 | output, output_length), |
| 3182 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3183 | |
| 3184 | /* Multi-Part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3185 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3186 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 3187 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length), |
| 3188 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3189 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3190 | output[i] ^= 1; |
| 3191 | } |
| 3192 | |
| 3193 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3194 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3195 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3196 | } |
| 3197 | /* END_CASE */ |
| 3198 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3199 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3200 | void hash_bad_order() |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3201 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3202 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3203 | unsigned char input[] = ""; |
| 3204 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3205 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3206 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 3207 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3208 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 |
| 3209 | }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3210 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3211 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 3212 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3213 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3214 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3215 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3216 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3217 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3218 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3219 | TEST_EQUAL(psa_hash_setup(&operation, alg), |
| 3220 | PSA_ERROR_BAD_STATE); |
| 3221 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3222 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3223 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3224 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3225 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3226 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 3227 | PSA_ERROR_BAD_STATE); |
| 3228 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3229 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3230 | /* Check that update calls abort on error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3231 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 3232 | operation.id = UINT_MAX; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3233 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3234 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 3235 | PSA_ERROR_BAD_STATE); |
| 3236 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3237 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3238 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3239 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3240 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3241 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3242 | PSA_ASSERT(psa_hash_finish(&operation, |
| 3243 | hash, sizeof(hash), &hash_len)); |
| 3244 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 3245 | PSA_ERROR_BAD_STATE); |
| 3246 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3247 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3248 | /* Call verify without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3249 | TEST_EQUAL(psa_hash_verify(&operation, |
| 3250 | valid_hash, sizeof(valid_hash)), |
| 3251 | PSA_ERROR_BAD_STATE); |
| 3252 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3253 | |
| 3254 | /* Call verify after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3255 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3256 | PSA_ASSERT(psa_hash_finish(&operation, |
| 3257 | hash, sizeof(hash), &hash_len)); |
| 3258 | TEST_EQUAL(psa_hash_verify(&operation, |
| 3259 | valid_hash, sizeof(valid_hash)), |
| 3260 | PSA_ERROR_BAD_STATE); |
| 3261 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3262 | |
| 3263 | /* Call verify twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3264 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3265 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3266 | PSA_ASSERT(psa_hash_verify(&operation, |
| 3267 | valid_hash, sizeof(valid_hash))); |
| 3268 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3269 | TEST_EQUAL(psa_hash_verify(&operation, |
| 3270 | valid_hash, sizeof(valid_hash)), |
| 3271 | PSA_ERROR_BAD_STATE); |
| 3272 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3273 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3274 | |
| 3275 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3276 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3277 | hash, sizeof(hash), &hash_len), |
| 3278 | PSA_ERROR_BAD_STATE); |
| 3279 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3280 | |
| 3281 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3282 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3283 | PSA_ASSERT(psa_hash_finish(&operation, |
| 3284 | hash, sizeof(hash), &hash_len)); |
| 3285 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3286 | hash, sizeof(hash), &hash_len), |
| 3287 | PSA_ERROR_BAD_STATE); |
| 3288 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3289 | |
| 3290 | /* Call finish after calling verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3291 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3292 | PSA_ASSERT(psa_hash_verify(&operation, |
| 3293 | valid_hash, sizeof(valid_hash))); |
| 3294 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3295 | hash, sizeof(hash), &hash_len), |
| 3296 | PSA_ERROR_BAD_STATE); |
| 3297 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3298 | |
| 3299 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3300 | PSA_DONE(); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3301 | } |
| 3302 | /* END_CASE */ |
| 3303 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3304 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3305 | void hash_verify_bad_args() |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3306 | { |
| 3307 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3308 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 3309 | * appended to it */ |
| 3310 | unsigned char hash[] = { |
| 3311 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 3312 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3313 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb |
| 3314 | }; |
| 3315 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 3316 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3317 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3318 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3319 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3320 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3321 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3322 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3323 | TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1), |
| 3324 | PSA_ERROR_INVALID_SIGNATURE); |
| 3325 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3326 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3327 | ASSERT_OPERATION_IS_INACTIVE(operation); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3328 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3329 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3330 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3331 | TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size), |
| 3332 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3333 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3334 | /* psa_hash_verify with a hash longer 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_verify(&operation, hash, sizeof(hash)), |
| 3337 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 3338 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3339 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3340 | PSA_DONE(); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3341 | } |
| 3342 | /* END_CASE */ |
| 3343 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3344 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3345 | void hash_finish_bad_args() |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3346 | { |
| 3347 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 3348 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3349 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 3350 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3351 | size_t hash_len; |
| 3352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3353 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3354 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3355 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3356 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3357 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3358 | hash, expected_size - 1, &hash_len), |
| 3359 | PSA_ERROR_BUFFER_TOO_SMALL); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3360 | |
| 3361 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3362 | PSA_DONE(); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3363 | } |
| 3364 | /* END_CASE */ |
| 3365 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3366 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3367 | void hash_clone_source_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3368 | { |
| 3369 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3370 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3371 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 3372 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3373 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3374 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3375 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3376 | size_t hash_len; |
| 3377 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3378 | PSA_ASSERT(psa_crypto_init()); |
| 3379 | PSA_ASSERT(psa_hash_setup(&op_source, alg)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3380 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3381 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3382 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3383 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3384 | hash, sizeof(hash), &hash_len)); |
| 3385 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3386 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3387 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3388 | TEST_EQUAL(psa_hash_clone(&op_source, &op_setup), |
| 3389 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3390 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3391 | PSA_ASSERT(psa_hash_clone(&op_source, &op_init)); |
| 3392 | PSA_ASSERT(psa_hash_finish(&op_init, |
| 3393 | hash, sizeof(hash), &hash_len)); |
| 3394 | PSA_ASSERT(psa_hash_clone(&op_source, &op_finished)); |
| 3395 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3396 | hash, sizeof(hash), &hash_len)); |
| 3397 | PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted)); |
| 3398 | PSA_ASSERT(psa_hash_finish(&op_aborted, |
| 3399 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3400 | |
| 3401 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3402 | psa_hash_abort(&op_source); |
| 3403 | psa_hash_abort(&op_init); |
| 3404 | psa_hash_abort(&op_setup); |
| 3405 | psa_hash_abort(&op_finished); |
| 3406 | psa_hash_abort(&op_aborted); |
| 3407 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3408 | } |
| 3409 | /* END_CASE */ |
| 3410 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3411 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3412 | void hash_clone_target_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3413 | { |
| 3414 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3415 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3416 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3417 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3418 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3419 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3420 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 3421 | size_t hash_len; |
| 3422 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3423 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3424 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3425 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3426 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3427 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3428 | hash, sizeof(hash), &hash_len)); |
| 3429 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3430 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3431 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3432 | PSA_ASSERT(psa_hash_clone(&op_setup, &op_target)); |
| 3433 | PSA_ASSERT(psa_hash_finish(&op_target, |
| 3434 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3435 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3436 | TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE); |
| 3437 | TEST_EQUAL(psa_hash_clone(&op_finished, &op_target), |
| 3438 | PSA_ERROR_BAD_STATE); |
| 3439 | TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target), |
| 3440 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3441 | |
| 3442 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3443 | psa_hash_abort(&op_target); |
| 3444 | psa_hash_abort(&op_init); |
| 3445 | psa_hash_abort(&op_setup); |
| 3446 | psa_hash_abort(&op_finished); |
| 3447 | psa_hash_abort(&op_aborted); |
| 3448 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3449 | } |
| 3450 | /* END_CASE */ |
| 3451 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3452 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3453 | void mac_operation_init() |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3454 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3455 | const uint8_t input[1] = { 0 }; |
| 3456 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3457 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3458 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3459 | * 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] | 3460 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3461 | psa_mac_operation_t func = psa_mac_operation_init(); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3462 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 3463 | psa_mac_operation_t zero; |
| 3464 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3465 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3466 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3467 | /* A freshly-initialized MAC operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3468 | TEST_EQUAL(psa_mac_update(&func, |
| 3469 | input, sizeof(input)), |
| 3470 | PSA_ERROR_BAD_STATE); |
| 3471 | TEST_EQUAL(psa_mac_update(&init, |
| 3472 | input, sizeof(input)), |
| 3473 | PSA_ERROR_BAD_STATE); |
| 3474 | TEST_EQUAL(psa_mac_update(&zero, |
| 3475 | input, sizeof(input)), |
| 3476 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3477 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3478 | /* A default MAC operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3479 | PSA_ASSERT(psa_mac_abort(&func)); |
| 3480 | PSA_ASSERT(psa_mac_abort(&init)); |
| 3481 | PSA_ASSERT(psa_mac_abort(&zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3482 | } |
| 3483 | /* END_CASE */ |
| 3484 | |
| 3485 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3486 | void mac_setup(int key_type_arg, |
| 3487 | data_t *key, |
| 3488 | int alg_arg, |
| 3489 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3490 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3491 | psa_key_type_t key_type = key_type_arg; |
| 3492 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3493 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3494 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3495 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3496 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3497 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3498 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3499 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3500 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3501 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3502 | if (!exercise_mac_setup(key_type, key->x, key->len, alg, |
| 3503 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3504 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3505 | } |
| 3506 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3507 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3508 | /* The operation object should be reusable. */ |
| 3509 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3510 | if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 3511 | smoke_test_key_data, |
| 3512 | sizeof(smoke_test_key_data), |
| 3513 | KNOWN_SUPPORTED_MAC_ALG, |
| 3514 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3515 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3516 | } |
| 3517 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3518 | #endif |
| 3519 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3520 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3521 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3522 | } |
| 3523 | /* END_CASE */ |
| 3524 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3525 | /* 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] | 3526 | void mac_bad_order() |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3527 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3528 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3529 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 3530 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3531 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3532 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3533 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3534 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa |
| 3535 | }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3536 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3537 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3538 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 3539 | size_t sign_mac_length = 0; |
| 3540 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3541 | const uint8_t verify_mac[] = { |
| 3542 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 3543 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3544 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 |
| 3545 | }; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3546 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3547 | PSA_ASSERT(psa_crypto_init()); |
| 3548 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 3549 | psa_set_key_algorithm(&attributes, alg); |
| 3550 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3551 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3552 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3553 | &key)); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3554 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3555 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3556 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3557 | PSA_ERROR_BAD_STATE); |
| 3558 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3559 | |
| 3560 | /* Call sign finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3561 | TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac), |
| 3562 | &sign_mac_length), |
| 3563 | PSA_ERROR_BAD_STATE); |
| 3564 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3565 | |
| 3566 | /* Call verify finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3567 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3568 | verify_mac, sizeof(verify_mac)), |
| 3569 | PSA_ERROR_BAD_STATE); |
| 3570 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3571 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3572 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3573 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3574 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3575 | TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg), |
| 3576 | PSA_ERROR_BAD_STATE); |
| 3577 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3578 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3579 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3580 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3581 | /* Call update after sign finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3582 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3583 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3584 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3585 | sign_mac, sizeof(sign_mac), |
| 3586 | &sign_mac_length)); |
| 3587 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 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 update after verify finish. */ |
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_update(&operation, input, sizeof(input)), |
| 3597 | PSA_ERROR_BAD_STATE); |
| 3598 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3599 | |
| 3600 | /* Call sign finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3601 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3602 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3603 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3604 | sign_mac, sizeof(sign_mac), |
| 3605 | &sign_mac_length)); |
| 3606 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3607 | sign_mac, sizeof(sign_mac), |
| 3608 | &sign_mac_length), |
| 3609 | PSA_ERROR_BAD_STATE); |
| 3610 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3611 | |
| 3612 | /* Call verify finish twice in a row. */ |
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 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3616 | verify_mac, sizeof(verify_mac))); |
| 3617 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3618 | verify_mac, sizeof(verify_mac)), |
| 3619 | PSA_ERROR_BAD_STATE); |
| 3620 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3621 | |
| 3622 | /* Setup sign but try verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3623 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3624 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3625 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3626 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3627 | verify_mac, sizeof(verify_mac)), |
| 3628 | PSA_ERROR_BAD_STATE); |
| 3629 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3630 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3631 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3632 | |
| 3633 | /* Setup verify but try sign. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3634 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3635 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3636 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3637 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3638 | sign_mac, sizeof(sign_mac), |
| 3639 | &sign_mac_length), |
| 3640 | PSA_ERROR_BAD_STATE); |
| 3641 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3642 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3643 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3644 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3645 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3646 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3647 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3648 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3649 | } |
| 3650 | /* END_CASE */ |
| 3651 | |
| 3652 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3653 | void mac_sign_verify_multi(int key_type_arg, |
| 3654 | data_t *key_data, |
| 3655 | int alg_arg, |
| 3656 | data_t *input, |
| 3657 | int is_verify, |
| 3658 | data_t *expected_mac) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3659 | { |
| 3660 | size_t data_part_len = 0; |
| 3661 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3662 | 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] | 3663 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3664 | mbedtls_test_set_step(2000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3665 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3666 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3667 | alg_arg, |
| 3668 | input, data_part_len, |
| 3669 | expected_mac, |
| 3670 | is_verify, 0) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3671 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3672 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3673 | |
| 3674 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3675 | mbedtls_test_set_step(3000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3676 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3677 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3678 | alg_arg, |
| 3679 | input, data_part_len, |
| 3680 | expected_mac, |
| 3681 | is_verify, 1) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3682 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3683 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3684 | } |
| 3685 | |
| 3686 | /* Goto is required to silence warnings about unused labels, as we |
| 3687 | * don't actually do any test assertions in this function. */ |
| 3688 | goto exit; |
| 3689 | } |
| 3690 | /* END_CASE */ |
| 3691 | |
| 3692 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3693 | void mac_sign(int key_type_arg, |
| 3694 | data_t *key_data, |
| 3695 | int alg_arg, |
| 3696 | data_t *input, |
| 3697 | data_t *expected_mac) |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3698 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3699 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3700 | psa_key_type_t key_type = key_type_arg; |
| 3701 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3702 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3703 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3704 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3705 | size_t mac_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3706 | 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] | 3707 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3708 | const size_t output_sizes_to_test[] = { |
| 3709 | 0, |
| 3710 | 1, |
| 3711 | expected_mac->len - 1, |
| 3712 | expected_mac->len, |
| 3713 | expected_mac->len + 1, |
| 3714 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3715 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3716 | TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3717 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3718 | TEST_ASSERT(expected_mac->len == mac_buffer_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3719 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3720 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3721 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3722 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 3723 | psa_set_key_algorithm(&attributes, alg); |
| 3724 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3725 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3726 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3727 | &key)); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3728 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3729 | 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] | 3730 | const size_t output_size = output_sizes_to_test[i]; |
| 3731 | psa_status_t expected_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3732 | (output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3733 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3734 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3735 | mbedtls_test_set_step(output_size); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3736 | TEST_CALLOC(actual_mac, output_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3737 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3738 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3739 | TEST_EQUAL(psa_mac_compute(key, alg, |
| 3740 | input->x, input->len, |
| 3741 | actual_mac, output_size, &mac_length), |
| 3742 | expected_status); |
| 3743 | if (expected_status == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3744 | TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3745 | actual_mac, mac_length); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3746 | } |
| 3747 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3748 | if (output_size > 0) { |
| 3749 | memset(actual_mac, 0, output_size); |
| 3750 | } |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3751 | |
| 3752 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3753 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3754 | PSA_ASSERT(psa_mac_update(&operation, |
| 3755 | input->x, input->len)); |
| 3756 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3757 | actual_mac, output_size, |
| 3758 | &mac_length), |
| 3759 | expected_status); |
| 3760 | PSA_ASSERT(psa_mac_abort(&operation)); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3761 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3762 | if (expected_status == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3763 | TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3764 | actual_mac, mac_length); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3765 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3766 | mbedtls_free(actual_mac); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3767 | actual_mac = NULL; |
| 3768 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3769 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3770 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3771 | psa_mac_abort(&operation); |
| 3772 | psa_destroy_key(key); |
| 3773 | PSA_DONE(); |
| 3774 | mbedtls_free(actual_mac); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3775 | } |
| 3776 | /* END_CASE */ |
| 3777 | |
| 3778 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3779 | void mac_verify(int key_type_arg, |
| 3780 | data_t *key_data, |
| 3781 | int alg_arg, |
| 3782 | data_t *input, |
| 3783 | data_t *expected_mac) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3784 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3785 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3786 | psa_key_type_t key_type = key_type_arg; |
| 3787 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3788 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3789 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3790 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3791 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3792 | TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3793 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3794 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3795 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3796 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 3797 | psa_set_key_algorithm(&attributes, alg); |
| 3798 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3799 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3800 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3801 | &key)); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3802 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3803 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3804 | PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len, |
| 3805 | expected_mac->x, expected_mac->len)); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3806 | |
| 3807 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3808 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3809 | PSA_ASSERT(psa_mac_update(&operation, |
| 3810 | input->x, input->len)); |
| 3811 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3812 | expected_mac->x, |
| 3813 | expected_mac->len)); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3814 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3815 | /* Test a MAC that's too short, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3816 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3817 | input->x, input->len, |
| 3818 | expected_mac->x, |
| 3819 | expected_mac->len - 1), |
| 3820 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3821 | |
| 3822 | /* Test a MAC that's too short, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3823 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3824 | PSA_ASSERT(psa_mac_update(&operation, |
| 3825 | input->x, input->len)); |
| 3826 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3827 | expected_mac->x, |
| 3828 | expected_mac->len - 1), |
| 3829 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3830 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3831 | /* Test a MAC that's too long, one-shot case. */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3832 | TEST_CALLOC(perturbed_mac, expected_mac->len + 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3833 | memcpy(perturbed_mac, expected_mac->x, expected_mac->len); |
| 3834 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3835 | input->x, input->len, |
| 3836 | perturbed_mac, expected_mac->len + 1), |
| 3837 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3838 | |
| 3839 | /* Test a MAC that's too long, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3840 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3841 | PSA_ASSERT(psa_mac_update(&operation, |
| 3842 | input->x, input->len)); |
| 3843 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3844 | perturbed_mac, |
| 3845 | expected_mac->len + 1), |
| 3846 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3847 | |
| 3848 | /* Test changing one byte. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3849 | for (size_t i = 0; i < expected_mac->len; i++) { |
| 3850 | mbedtls_test_set_step(i); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3851 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3852 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3853 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3854 | input->x, input->len, |
| 3855 | perturbed_mac, expected_mac->len), |
| 3856 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3857 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3858 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3859 | PSA_ASSERT(psa_mac_update(&operation, |
| 3860 | input->x, input->len)); |
| 3861 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3862 | perturbed_mac, |
| 3863 | expected_mac->len), |
| 3864 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3865 | perturbed_mac[i] ^= 1; |
| 3866 | } |
| 3867 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3868 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3869 | psa_mac_abort(&operation); |
| 3870 | psa_destroy_key(key); |
| 3871 | PSA_DONE(); |
| 3872 | mbedtls_free(perturbed_mac); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3873 | } |
| 3874 | /* END_CASE */ |
| 3875 | |
| 3876 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3877 | void cipher_operation_init() |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3878 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3879 | const uint8_t input[1] = { 0 }; |
| 3880 | unsigned char output[1] = { 0 }; |
| 3881 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3882 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3883 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3884 | * 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] | 3885 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3886 | psa_cipher_operation_t func = psa_cipher_operation_init(); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3887 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3888 | psa_cipher_operation_t zero; |
| 3889 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3890 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3891 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3892 | /* A freshly-initialized cipher operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3893 | TEST_EQUAL(psa_cipher_update(&func, |
| 3894 | input, sizeof(input), |
| 3895 | output, sizeof(output), |
| 3896 | &output_length), |
| 3897 | PSA_ERROR_BAD_STATE); |
| 3898 | TEST_EQUAL(psa_cipher_update(&init, |
| 3899 | input, sizeof(input), |
| 3900 | output, sizeof(output), |
| 3901 | &output_length), |
| 3902 | PSA_ERROR_BAD_STATE); |
| 3903 | TEST_EQUAL(psa_cipher_update(&zero, |
| 3904 | input, sizeof(input), |
| 3905 | output, sizeof(output), |
| 3906 | &output_length), |
| 3907 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3908 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3909 | /* A default cipher operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3910 | PSA_ASSERT(psa_cipher_abort(&func)); |
| 3911 | PSA_ASSERT(psa_cipher_abort(&init)); |
| 3912 | PSA_ASSERT(psa_cipher_abort(&zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3913 | } |
| 3914 | /* END_CASE */ |
| 3915 | |
| 3916 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3917 | void cipher_setup(int key_type_arg, |
| 3918 | data_t *key, |
| 3919 | int alg_arg, |
| 3920 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3921 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3922 | psa_key_type_t key_type = key_type_arg; |
| 3923 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3924 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3925 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3926 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3927 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3928 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3929 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3930 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3931 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3932 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3933 | if (!exercise_cipher_setup(key_type, key->x, key->len, alg, |
| 3934 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3935 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3936 | } |
| 3937 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3938 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3939 | /* The operation object should be reusable. */ |
| 3940 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3941 | if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3942 | smoke_test_key_data, |
| 3943 | sizeof(smoke_test_key_data), |
| 3944 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3945 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3946 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3947 | } |
| 3948 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3949 | #endif |
| 3950 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3951 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3952 | psa_cipher_abort(&operation); |
| 3953 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3954 | } |
| 3955 | /* END_CASE */ |
| 3956 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3957 | /* 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] | 3958 | void cipher_bad_order() |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3959 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3960 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3961 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3962 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3963 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3964 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3965 | 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] | 3966 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3967 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3968 | 0xaa, 0xaa, 0xaa, 0xaa |
| 3969 | }; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3970 | const uint8_t text[] = { |
| 3971 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3972 | 0xbb, 0xbb, 0xbb, 0xbb |
| 3973 | }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3974 | 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] | 3975 | size_t length = 0; |
| 3976 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3977 | PSA_ASSERT(psa_crypto_init()); |
| 3978 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3979 | psa_set_key_algorithm(&attributes, alg); |
| 3980 | psa_set_key_type(&attributes, key_type); |
| 3981 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3982 | &key)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3983 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3984 | /* Call encrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3985 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3986 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3987 | TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg), |
| 3988 | PSA_ERROR_BAD_STATE); |
| 3989 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3990 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3991 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3992 | |
| 3993 | /* Call decrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3994 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3995 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3996 | TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg), |
| 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 | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 4001 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4002 | /* Generate an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4003 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 4004 | buffer, sizeof(buffer), |
| 4005 | &length), |
| 4006 | PSA_ERROR_BAD_STATE); |
| 4007 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4008 | |
| 4009 | /* Generate an IV twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4010 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4011 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 4012 | buffer, sizeof(buffer), |
| 4013 | &length)); |
| 4014 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 4015 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 4016 | buffer, sizeof(buffer), |
| 4017 | &length), |
| 4018 | PSA_ERROR_BAD_STATE); |
| 4019 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 4020 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4021 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4022 | |
| 4023 | /* Generate an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4024 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4025 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 4026 | iv, sizeof(iv))); |
| 4027 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 4028 | buffer, sizeof(buffer), |
| 4029 | &length), |
| 4030 | PSA_ERROR_BAD_STATE); |
| 4031 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4032 | |
| 4033 | /* Set an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4034 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 4035 | iv, sizeof(iv)), |
| 4036 | PSA_ERROR_BAD_STATE); |
| 4037 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4038 | |
| 4039 | /* Set an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4040 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4041 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 4042 | iv, sizeof(iv))); |
| 4043 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 4044 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 4045 | iv, sizeof(iv)), |
| 4046 | PSA_ERROR_BAD_STATE); |
| 4047 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 4048 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4049 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4050 | |
| 4051 | /* Set an IV after it's already generated. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4052 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4053 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 4054 | buffer, sizeof(buffer), |
| 4055 | &length)); |
| 4056 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 4057 | iv, sizeof(iv)), |
| 4058 | PSA_ERROR_BAD_STATE); |
| 4059 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4060 | |
| 4061 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4062 | TEST_EQUAL(psa_cipher_update(&operation, |
| 4063 | text, sizeof(text), |
| 4064 | buffer, sizeof(buffer), |
| 4065 | &length), |
| 4066 | PSA_ERROR_BAD_STATE); |
| 4067 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4068 | |
| 4069 | /* Call update without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4070 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4071 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 4072 | TEST_EQUAL(psa_cipher_update(&operation, |
| 4073 | text, sizeof(text), |
| 4074 | buffer, sizeof(buffer), |
| 4075 | &length), |
| 4076 | PSA_ERROR_BAD_STATE); |
| 4077 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 4078 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4079 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4080 | |
| 4081 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4082 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4083 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 4084 | iv, sizeof(iv))); |
| 4085 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4086 | buffer, sizeof(buffer), &length)); |
| 4087 | TEST_EQUAL(psa_cipher_update(&operation, |
| 4088 | text, sizeof(text), |
| 4089 | buffer, sizeof(buffer), |
| 4090 | &length), |
| 4091 | PSA_ERROR_BAD_STATE); |
| 4092 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4093 | |
| 4094 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4095 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 4096 | buffer, sizeof(buffer), &length), |
| 4097 | PSA_ERROR_BAD_STATE); |
| 4098 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4099 | |
| 4100 | /* Call finish without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4101 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4102 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 4103 | * for cipher modes with padding. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4104 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 4105 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 4106 | buffer, sizeof(buffer), &length), |
| 4107 | PSA_ERROR_BAD_STATE); |
| 4108 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 4109 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4110 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4111 | |
| 4112 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4113 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4114 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 4115 | iv, sizeof(iv))); |
| 4116 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4117 | buffer, sizeof(buffer), &length)); |
| 4118 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 4119 | buffer, sizeof(buffer), &length), |
| 4120 | PSA_ERROR_BAD_STATE); |
| 4121 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4122 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4123 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 4124 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4125 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4126 | psa_cipher_abort(&operation); |
| 4127 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4128 | } |
| 4129 | /* END_CASE */ |
| 4130 | |
| 4131 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4132 | void cipher_encrypt_fail(int alg_arg, |
| 4133 | int key_type_arg, |
| 4134 | data_t *key_data, |
| 4135 | data_t *input, |
| 4136 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4137 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4138 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4139 | psa_status_t status; |
| 4140 | psa_key_type_t key_type = key_type_arg; |
| 4141 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 4142 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4143 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 }; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4144 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; |
| 4145 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4146 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4147 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4148 | size_t output_length = 0; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4149 | size_t function_output_length; |
| 4150 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4151 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4153 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 4154 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4155 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4156 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4157 | psa_set_key_algorithm(&attributes, alg); |
| 4158 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4159 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4160 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 4161 | input->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4162 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4164 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4165 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4166 | } |
| 4167 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4168 | /* Encrypt, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4169 | status = psa_cipher_encrypt(key, alg, input->x, input->len, output, |
| 4170 | output_buffer_size, &output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4172 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4173 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4174 | /* Encrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4175 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
| 4176 | if (status == PSA_SUCCESS) { |
| 4177 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 4178 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 4179 | iv, iv_size, |
| 4180 | &iv_length)); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4181 | } |
| 4182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4183 | status = psa_cipher_update(&operation, input->x, input->len, |
| 4184 | output, output_buffer_size, |
| 4185 | &function_output_length); |
| 4186 | if (status == PSA_SUCCESS) { |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4187 | output_length += function_output_length; |
| 4188 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4189 | status = psa_cipher_finish(&operation, output + output_length, |
| 4190 | output_buffer_size - output_length, |
| 4191 | &function_output_length); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4192 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4193 | TEST_EQUAL(status, expected_status); |
| 4194 | } else { |
| 4195 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4196 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4197 | } else { |
| 4198 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4199 | } |
| 4200 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4201 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4202 | psa_cipher_abort(&operation); |
| 4203 | mbedtls_free(output); |
| 4204 | psa_destroy_key(key); |
| 4205 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4206 | } |
| 4207 | /* END_CASE */ |
| 4208 | |
| 4209 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4210 | void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data, |
| 4211 | data_t *input, int iv_length, |
| 4212 | int expected_result) |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4213 | { |
| 4214 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4215 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4216 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4217 | size_t output_buffer_size = 0; |
| 4218 | unsigned char *output = NULL; |
| 4219 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4220 | 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] | 4221 | TEST_CALLOC(output, output_buffer_size); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4222 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4223 | PSA_ASSERT(psa_crypto_init()); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4224 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4225 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4226 | psa_set_key_algorithm(&attributes, alg); |
| 4227 | psa_set_key_type(&attributes, key_type); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4228 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4229 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4230 | &key)); |
| 4231 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4232 | TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output, |
| 4233 | iv_length)); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4234 | |
| 4235 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4236 | psa_cipher_abort(&operation); |
| 4237 | mbedtls_free(output); |
| 4238 | psa_destroy_key(key); |
| 4239 | PSA_DONE(); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4240 | } |
| 4241 | /* END_CASE */ |
| 4242 | |
| 4243 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4244 | void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data, |
| 4245 | data_t *plaintext, data_t *ciphertext) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4246 | { |
| 4247 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4248 | psa_key_type_t key_type = key_type_arg; |
| 4249 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 4250 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4251 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4252 | unsigned char *output = NULL; |
| 4253 | size_t output_buffer_size = 0; |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4254 | size_t output_length, length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4255 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4256 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4257 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4258 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4259 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4260 | TEST_LE_U(ciphertext->len, |
| 4261 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len)); |
| 4262 | TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len), |
| 4263 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len)); |
| 4264 | TEST_LE_U(plaintext->len, |
| 4265 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len)); |
| 4266 | TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len), |
| 4267 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len)); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4268 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4269 | |
| 4270 | /* Set up key and output buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4271 | psa_set_key_usage_flags(&attributes, |
| 4272 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4273 | psa_set_key_algorithm(&attributes, alg); |
| 4274 | psa_set_key_type(&attributes, key_type); |
| 4275 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4276 | &key)); |
| 4277 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 4278 | plaintext->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4279 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4280 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4281 | /* set_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4282 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4283 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 4284 | PSA_ERROR_BAD_STATE); |
| 4285 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 4286 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 4287 | PSA_ERROR_BAD_STATE); |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 4288 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4289 | /* generate_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4290 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4291 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 4292 | &length), |
| 4293 | PSA_ERROR_BAD_STATE); |
| 4294 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 4295 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 4296 | &length), |
| 4297 | PSA_ERROR_BAD_STATE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4298 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4299 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4300 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4301 | output_length = 0; |
| 4302 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4303 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4304 | plaintext->x, plaintext->len, |
| 4305 | output, output_buffer_size, |
| 4306 | &length)); |
| 4307 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4308 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4309 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4310 | mbedtls_buffer_offset(output, output_length), |
| 4311 | output_buffer_size - output_length, |
| 4312 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4313 | output_length += length; |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4314 | TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4315 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4316 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4317 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4318 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4319 | output_length = 0; |
| 4320 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4321 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4322 | ciphertext->x, ciphertext->len, |
| 4323 | output, output_buffer_size, |
| 4324 | &length)); |
| 4325 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4326 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4327 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4328 | mbedtls_buffer_offset(output, output_length), |
| 4329 | output_buffer_size - output_length, |
| 4330 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4331 | output_length += length; |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4332 | TEST_MEMORY_COMPARE(plaintext->x, plaintext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4333 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4334 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4335 | /* One-shot encryption */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4336 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4337 | PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len, |
| 4338 | output, output_buffer_size, |
| 4339 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4340 | TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4341 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4342 | |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4343 | /* One-shot decryption */ |
| 4344 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4345 | PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len, |
| 4346 | output, output_buffer_size, |
| 4347 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4348 | TEST_MEMORY_COMPARE(plaintext->x, plaintext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4349 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4350 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4351 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4352 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4353 | mbedtls_free(output); |
| 4354 | psa_cipher_abort(&operation); |
| 4355 | psa_destroy_key(key); |
| 4356 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4357 | } |
| 4358 | /* END_CASE */ |
| 4359 | |
| 4360 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4361 | 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] | 4362 | { |
| 4363 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4364 | psa_algorithm_t alg = alg_arg; |
| 4365 | psa_key_type_t key_type = key_type_arg; |
| 4366 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4367 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4368 | psa_status_t status; |
| 4369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4370 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4371 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4372 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4373 | psa_set_key_algorithm(&attributes, alg); |
| 4374 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4375 | |
| 4376 | /* Usage of either of these two size macros would cause divide by zero |
| 4377 | * with incorrect key types previously. Input length should be irrelevant |
| 4378 | * here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4379 | TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16), |
| 4380 | 0); |
| 4381 | TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4382 | |
| 4383 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4384 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4385 | &key)); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4386 | |
| 4387 | /* Should fail due to invalid alg type (to support invalid key type). |
| 4388 | * Encrypt or decrypt will end up in the same place. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4389 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4390 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4391 | TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4392 | |
| 4393 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4394 | psa_cipher_abort(&operation); |
| 4395 | psa_destroy_key(key); |
| 4396 | PSA_DONE(); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4397 | } |
| 4398 | /* END_CASE */ |
| 4399 | |
| 4400 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4401 | void cipher_encrypt_validation(int alg_arg, |
| 4402 | int key_type_arg, |
| 4403 | data_t *key_data, |
| 4404 | data_t *input) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4405 | { |
| 4406 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4407 | psa_key_type_t key_type = key_type_arg; |
| 4408 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4409 | size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4410 | unsigned char *output1 = NULL; |
| 4411 | size_t output1_buffer_size = 0; |
| 4412 | size_t output1_length = 0; |
| 4413 | unsigned char *output2 = NULL; |
| 4414 | size_t output2_buffer_size = 0; |
| 4415 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4416 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4417 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4418 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4419 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4420 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4421 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4422 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4423 | psa_set_key_algorithm(&attributes, alg); |
| 4424 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4425 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4426 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4427 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4428 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4429 | TEST_CALLOC(output1, output1_buffer_size); |
| 4430 | TEST_CALLOC(output2, output2_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4431 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4432 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4433 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4434 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 4435 | /* The one-shot cipher encryption uses generated iv so validating |
| 4436 | the output is not possible. Validating with multipart encryption. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4437 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1, |
| 4438 | output1_buffer_size, &output1_length)); |
| 4439 | TEST_LE_U(output1_length, |
| 4440 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4441 | TEST_LE_U(output1_length, |
| 4442 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4444 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4445 | PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4446 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4447 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4448 | input->x, input->len, |
| 4449 | output2, output2_buffer_size, |
| 4450 | &function_output_length)); |
| 4451 | TEST_LE_U(function_output_length, |
| 4452 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4453 | TEST_LE_U(function_output_length, |
| 4454 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4455 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4456 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4457 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4458 | output2 + output2_length, |
| 4459 | output2_buffer_size - output2_length, |
| 4460 | &function_output_length)); |
| 4461 | TEST_LE_U(function_output_length, |
| 4462 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4463 | TEST_LE_U(function_output_length, |
| 4464 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4465 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4466 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4467 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4468 | TEST_MEMORY_COMPARE(output1 + iv_size, output1_length - iv_size, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4469 | output2, output2_length); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4470 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4471 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4472 | psa_cipher_abort(&operation); |
| 4473 | mbedtls_free(output1); |
| 4474 | mbedtls_free(output2); |
| 4475 | psa_destroy_key(key); |
| 4476 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4477 | } |
| 4478 | /* END_CASE */ |
| 4479 | |
| 4480 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4481 | void cipher_encrypt_multipart(int alg_arg, int key_type_arg, |
| 4482 | data_t *key_data, data_t *iv, |
| 4483 | data_t *input, |
| 4484 | int first_part_size_arg, |
| 4485 | int output1_length_arg, int output2_length_arg, |
| 4486 | data_t *expected_output, |
| 4487 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4488 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4489 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4490 | psa_key_type_t key_type = key_type_arg; |
| 4491 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4492 | psa_status_t status; |
| 4493 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4494 | size_t first_part_size = first_part_size_arg; |
| 4495 | size_t output1_length = output1_length_arg; |
| 4496 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4497 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4498 | size_t output_buffer_size = 0; |
| 4499 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4500 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4501 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4502 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4503 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4504 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4505 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4506 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4507 | psa_set_key_algorithm(&attributes, alg); |
| 4508 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4509 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4510 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4511 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4512 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4513 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4514 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4515 | if (iv->len > 0) { |
| 4516 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4517 | } |
| 4518 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4519 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4520 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4521 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4522 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4523 | TEST_LE_U(first_part_size, input->len); |
| 4524 | PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size, |
| 4525 | output, output_buffer_size, |
| 4526 | &function_output_length)); |
| 4527 | TEST_ASSERT(function_output_length == output1_length); |
| 4528 | TEST_LE_U(function_output_length, |
| 4529 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4530 | TEST_LE_U(function_output_length, |
| 4531 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4532 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4533 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4534 | if (first_part_size < input->len) { |
| 4535 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4536 | input->x + first_part_size, |
| 4537 | input->len - first_part_size, |
| 4538 | (output_buffer_size == 0 ? NULL : |
| 4539 | output + total_output_length), |
| 4540 | output_buffer_size - total_output_length, |
| 4541 | &function_output_length)); |
| 4542 | TEST_ASSERT(function_output_length == output2_length); |
| 4543 | TEST_LE_U(function_output_length, |
| 4544 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4545 | alg, |
| 4546 | input->len - first_part_size)); |
| 4547 | TEST_LE_U(function_output_length, |
| 4548 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4549 | total_output_length += function_output_length; |
| 4550 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4551 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4552 | status = psa_cipher_finish(&operation, |
| 4553 | (output_buffer_size == 0 ? NULL : |
| 4554 | output + total_output_length), |
| 4555 | output_buffer_size - total_output_length, |
| 4556 | &function_output_length); |
| 4557 | TEST_LE_U(function_output_length, |
| 4558 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4559 | TEST_LE_U(function_output_length, |
| 4560 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4561 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4562 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4563 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4564 | if (expected_status == PSA_SUCCESS) { |
| 4565 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4566 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4567 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4568 | output, total_output_length); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4569 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4570 | |
| 4571 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4572 | psa_cipher_abort(&operation); |
| 4573 | mbedtls_free(output); |
| 4574 | psa_destroy_key(key); |
| 4575 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4576 | } |
| 4577 | /* END_CASE */ |
| 4578 | |
| 4579 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4580 | void cipher_decrypt_multipart(int alg_arg, int key_type_arg, |
| 4581 | data_t *key_data, data_t *iv, |
| 4582 | data_t *input, |
| 4583 | int first_part_size_arg, |
| 4584 | int output1_length_arg, int output2_length_arg, |
| 4585 | data_t *expected_output, |
| 4586 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4587 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4588 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4589 | psa_key_type_t key_type = key_type_arg; |
| 4590 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4591 | psa_status_t status; |
| 4592 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4593 | size_t first_part_size = first_part_size_arg; |
| 4594 | size_t output1_length = output1_length_arg; |
| 4595 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4596 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4597 | size_t output_buffer_size = 0; |
| 4598 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4599 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4600 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4601 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4602 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4603 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4604 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4605 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4606 | psa_set_key_algorithm(&attributes, alg); |
| 4607 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4608 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4609 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4610 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4611 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4612 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4613 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4614 | if (iv->len > 0) { |
| 4615 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4616 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4617 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4618 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4619 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4620 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4621 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4622 | TEST_LE_U(first_part_size, input->len); |
| 4623 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4624 | input->x, first_part_size, |
| 4625 | output, output_buffer_size, |
| 4626 | &function_output_length)); |
| 4627 | TEST_ASSERT(function_output_length == output1_length); |
| 4628 | TEST_LE_U(function_output_length, |
| 4629 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4630 | TEST_LE_U(function_output_length, |
| 4631 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4632 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4633 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4634 | if (first_part_size < input->len) { |
| 4635 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4636 | input->x + first_part_size, |
| 4637 | input->len - first_part_size, |
| 4638 | (output_buffer_size == 0 ? NULL : |
| 4639 | output + total_output_length), |
| 4640 | output_buffer_size - total_output_length, |
| 4641 | &function_output_length)); |
| 4642 | TEST_ASSERT(function_output_length == output2_length); |
| 4643 | TEST_LE_U(function_output_length, |
| 4644 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4645 | alg, |
| 4646 | input->len - first_part_size)); |
| 4647 | TEST_LE_U(function_output_length, |
| 4648 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4649 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4650 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4651 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4652 | status = psa_cipher_finish(&operation, |
| 4653 | (output_buffer_size == 0 ? NULL : |
| 4654 | output + total_output_length), |
| 4655 | output_buffer_size - total_output_length, |
| 4656 | &function_output_length); |
| 4657 | TEST_LE_U(function_output_length, |
| 4658 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4659 | TEST_LE_U(function_output_length, |
| 4660 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4661 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4662 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4663 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4664 | if (expected_status == PSA_SUCCESS) { |
| 4665 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4666 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4667 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4668 | output, total_output_length); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4669 | } |
| 4670 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4671 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4672 | psa_cipher_abort(&operation); |
| 4673 | mbedtls_free(output); |
| 4674 | psa_destroy_key(key); |
| 4675 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4676 | } |
| 4677 | /* END_CASE */ |
| 4678 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4679 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4680 | void cipher_decrypt_fail(int alg_arg, |
| 4681 | int key_type_arg, |
| 4682 | data_t *key_data, |
| 4683 | data_t *iv, |
| 4684 | data_t *input_arg, |
| 4685 | int expected_status_arg) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4686 | { |
| 4687 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4688 | psa_status_t status; |
| 4689 | psa_key_type_t key_type = key_type_arg; |
| 4690 | psa_algorithm_t alg = alg_arg; |
| 4691 | psa_status_t expected_status = expected_status_arg; |
| 4692 | unsigned char *input = NULL; |
| 4693 | size_t input_buffer_size = 0; |
| 4694 | unsigned char *output = NULL; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4695 | unsigned char *output_multi = NULL; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4696 | size_t output_buffer_size = 0; |
| 4697 | size_t output_length = 0; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4698 | size_t function_output_length; |
| 4699 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4700 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4701 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4702 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 4703 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4704 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4705 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4706 | psa_set_key_algorithm(&attributes, alg); |
| 4707 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4708 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4709 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4710 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4711 | } |
| 4712 | |
| 4713 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4714 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4715 | if (input_buffer_size > 0) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4716 | TEST_CALLOC(input, input_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4717 | memcpy(input, iv->x, iv->len); |
| 4718 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4719 | } |
| 4720 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4721 | 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] | 4722 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4723 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4724 | /* Decrypt, one-short */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4725 | status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4726 | output_buffer_size, &output_length); |
| 4727 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4728 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4729 | /* Decrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4730 | status = psa_cipher_decrypt_setup(&operation, key, alg); |
| 4731 | if (status == PSA_SUCCESS) { |
| 4732 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 4733 | input_arg->len) + |
| 4734 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4735 | TEST_CALLOC(output_multi, output_buffer_size); |
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 | if (iv->len > 0) { |
| 4738 | status = psa_cipher_set_iv(&operation, iv->x, iv->len); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4739 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4740 | if (status != PSA_SUCCESS) { |
| 4741 | TEST_EQUAL(status, expected_status); |
| 4742 | } |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4743 | } |
| 4744 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4745 | if (status == PSA_SUCCESS) { |
| 4746 | status = psa_cipher_update(&operation, |
| 4747 | input_arg->x, input_arg->len, |
| 4748 | output_multi, output_buffer_size, |
| 4749 | &function_output_length); |
| 4750 | if (status == PSA_SUCCESS) { |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4751 | output_length = function_output_length; |
| 4752 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4753 | status = psa_cipher_finish(&operation, |
| 4754 | output_multi + output_length, |
| 4755 | output_buffer_size - output_length, |
| 4756 | &function_output_length); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4757 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4758 | TEST_EQUAL(status, expected_status); |
| 4759 | } else { |
| 4760 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4761 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4762 | } else { |
| 4763 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4764 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4765 | } else { |
| 4766 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4767 | } |
| 4768 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4769 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4770 | psa_cipher_abort(&operation); |
| 4771 | mbedtls_free(input); |
| 4772 | mbedtls_free(output); |
| 4773 | mbedtls_free(output_multi); |
| 4774 | psa_destroy_key(key); |
| 4775 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4776 | } |
| 4777 | /* END_CASE */ |
| 4778 | |
| 4779 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4780 | void cipher_decrypt(int alg_arg, |
| 4781 | int key_type_arg, |
| 4782 | data_t *key_data, |
| 4783 | data_t *iv, |
| 4784 | data_t *input_arg, |
| 4785 | data_t *expected_output) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4786 | { |
| 4787 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4788 | psa_key_type_t key_type = key_type_arg; |
| 4789 | psa_algorithm_t alg = alg_arg; |
| 4790 | unsigned char *input = NULL; |
| 4791 | size_t input_buffer_size = 0; |
| 4792 | unsigned char *output = NULL; |
| 4793 | size_t output_buffer_size = 0; |
| 4794 | size_t output_length = 0; |
| 4795 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4796 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4797 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4798 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4799 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4800 | psa_set_key_algorithm(&attributes, alg); |
| 4801 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4802 | |
| 4803 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4804 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4805 | if (input_buffer_size > 0) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4806 | TEST_CALLOC(input, input_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4807 | memcpy(input, iv->x, iv->len); |
| 4808 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4809 | } |
| 4810 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4811 | 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] | 4812 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4813 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4814 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4815 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4816 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4817 | PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4818 | output_buffer_size, &output_length)); |
| 4819 | TEST_LE_U(output_length, |
| 4820 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size)); |
| 4821 | TEST_LE_U(output_length, |
| 4822 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4823 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4824 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4825 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4826 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4827 | mbedtls_free(input); |
| 4828 | mbedtls_free(output); |
| 4829 | psa_destroy_key(key); |
| 4830 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4831 | } |
| 4832 | /* END_CASE */ |
| 4833 | |
| 4834 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4835 | void cipher_verify_output(int alg_arg, |
| 4836 | int key_type_arg, |
| 4837 | data_t *key_data, |
| 4838 | data_t *input) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4839 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4840 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4841 | psa_key_type_t key_type = key_type_arg; |
| 4842 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4843 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4844 | size_t output1_size = 0; |
| 4845 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4846 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4847 | size_t output2_size = 0; |
| 4848 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4849 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4850 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4851 | PSA_ASSERT(psa_crypto_init()); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4852 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4853 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4854 | psa_set_key_algorithm(&attributes, alg); |
| 4855 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4856 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4857 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4858 | &key)); |
| 4859 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4860 | TEST_CALLOC(output1, output1_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4861 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4862 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, |
| 4863 | output1, output1_size, |
| 4864 | &output1_length)); |
| 4865 | TEST_LE_U(output1_length, |
| 4866 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4867 | TEST_LE_U(output1_length, |
| 4868 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4869 | |
| 4870 | output2_size = output1_length; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4871 | TEST_CALLOC(output2, output2_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4872 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4873 | PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length, |
| 4874 | output2, output2_size, |
| 4875 | &output2_length)); |
| 4876 | TEST_LE_U(output2_length, |
| 4877 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4878 | TEST_LE_U(output2_length, |
| 4879 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4880 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4881 | TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4882 | |
| 4883 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4884 | mbedtls_free(output1); |
| 4885 | mbedtls_free(output2); |
| 4886 | psa_destroy_key(key); |
| 4887 | PSA_DONE(); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4888 | } |
| 4889 | /* END_CASE */ |
| 4890 | |
| 4891 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4892 | void cipher_verify_output_multipart(int alg_arg, |
| 4893 | int key_type_arg, |
| 4894 | data_t *key_data, |
| 4895 | data_t *input, |
| 4896 | int first_part_size_arg) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4897 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4898 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4899 | psa_key_type_t key_type = key_type_arg; |
| 4900 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4901 | size_t first_part_size = first_part_size_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4902 | unsigned char iv[16] = { 0 }; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4903 | size_t iv_size = 16; |
| 4904 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4905 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4906 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4907 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4908 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4909 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4910 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4911 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4912 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 4913 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4914 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4915 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4916 | PSA_ASSERT(psa_crypto_init()); |
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 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4919 | psa_set_key_algorithm(&attributes, alg); |
| 4920 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4921 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4922 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4923 | &key)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4924 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4925 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg)); |
| 4926 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4927 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4928 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 4929 | PSA_ASSERT(psa_cipher_generate_iv(&operation1, |
| 4930 | iv, iv_size, |
| 4931 | &iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4932 | } |
| 4933 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4934 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4935 | TEST_LE_U(output1_buffer_size, |
| 4936 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4937 | TEST_CALLOC(output1, output1_buffer_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4938 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4939 | TEST_LE_U(first_part_size, input->len); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4940 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4941 | PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size, |
| 4942 | output1, output1_buffer_size, |
| 4943 | &function_output_length)); |
| 4944 | TEST_LE_U(function_output_length, |
| 4945 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4946 | TEST_LE_U(function_output_length, |
| 4947 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4948 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4949 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4950 | PSA_ASSERT(psa_cipher_update(&operation1, |
| 4951 | input->x + first_part_size, |
| 4952 | input->len - first_part_size, |
| 4953 | output1, output1_buffer_size, |
| 4954 | &function_output_length)); |
| 4955 | TEST_LE_U(function_output_length, |
| 4956 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4957 | alg, |
| 4958 | input->len - first_part_size)); |
| 4959 | TEST_LE_U(function_output_length, |
| 4960 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4961 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4962 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4963 | PSA_ASSERT(psa_cipher_finish(&operation1, |
| 4964 | output1 + output1_length, |
| 4965 | output1_buffer_size - output1_length, |
| 4966 | &function_output_length)); |
| 4967 | TEST_LE_U(function_output_length, |
| 4968 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4969 | TEST_LE_U(function_output_length, |
| 4970 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4971 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4972 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4973 | PSA_ASSERT(psa_cipher_abort(&operation1)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4974 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4975 | output2_buffer_size = output1_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4976 | TEST_LE_U(output2_buffer_size, |
| 4977 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4978 | TEST_LE_U(output2_buffer_size, |
| 4979 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4980 | TEST_CALLOC(output2, output2_buffer_size); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4981 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4982 | if (iv_length > 0) { |
| 4983 | PSA_ASSERT(psa_cipher_set_iv(&operation2, |
| 4984 | iv, iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4985 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4986 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4987 | PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size, |
| 4988 | output2, output2_buffer_size, |
| 4989 | &function_output_length)); |
| 4990 | TEST_LE_U(function_output_length, |
| 4991 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4992 | TEST_LE_U(function_output_length, |
| 4993 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4994 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4995 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4996 | PSA_ASSERT(psa_cipher_update(&operation2, |
| 4997 | output1 + first_part_size, |
| 4998 | output1_length - first_part_size, |
| 4999 | output2, output2_buffer_size, |
| 5000 | &function_output_length)); |
| 5001 | TEST_LE_U(function_output_length, |
| 5002 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 5003 | alg, |
| 5004 | output1_length - first_part_size)); |
| 5005 | TEST_LE_U(function_output_length, |
| 5006 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 5007 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 5008 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5009 | PSA_ASSERT(psa_cipher_finish(&operation2, |
| 5010 | output2 + output2_length, |
| 5011 | output2_buffer_size - output2_length, |
| 5012 | &function_output_length)); |
| 5013 | TEST_LE_U(function_output_length, |
| 5014 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 5015 | TEST_LE_U(function_output_length, |
| 5016 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 5017 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 5018 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5019 | PSA_ASSERT(psa_cipher_abort(&operation2)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 5020 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 5021 | TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 5022 | |
| 5023 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5024 | psa_cipher_abort(&operation1); |
| 5025 | psa_cipher_abort(&operation2); |
| 5026 | mbedtls_free(output1); |
| 5027 | mbedtls_free(output2); |
| 5028 | psa_destroy_key(key); |
| 5029 | PSA_DONE(); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 5030 | } |
| 5031 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 5032 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5033 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5034 | void aead_encrypt_decrypt(int key_type_arg, data_t *key_data, |
| 5035 | int alg_arg, |
| 5036 | data_t *nonce, |
| 5037 | data_t *additional_data, |
| 5038 | data_t *input_data, |
| 5039 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5040 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5041 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5042 | psa_key_type_t key_type = key_type_arg; |
| 5043 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5044 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5045 | unsigned char *output_data = NULL; |
| 5046 | size_t output_size = 0; |
| 5047 | size_t output_length = 0; |
| 5048 | unsigned char *output_data2 = NULL; |
| 5049 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 5050 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 5051 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5052 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5053 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5054 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5055 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5056 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5057 | psa_set_key_algorithm(&attributes, alg); |
| 5058 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5059 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5060 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5061 | &key)); |
| 5062 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5063 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5064 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5065 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 5066 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5067 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 5068 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5069 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 5070 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
| 5071 | TEST_EQUAL(output_size, |
| 5072 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 5073 | TEST_LE_U(output_size, |
| 5074 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5075 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5076 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5077 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5078 | status = psa_aead_encrypt(key, alg, |
| 5079 | nonce->x, nonce->len, |
| 5080 | additional_data->x, |
| 5081 | additional_data->len, |
| 5082 | input_data->x, input_data->len, |
| 5083 | output_data, output_size, |
| 5084 | &output_length); |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 5085 | |
| 5086 | /* If the operation is not supported, just skip and not fail in case the |
| 5087 | * encryption involves a common limitation of cryptography hardwares and |
| 5088 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5089 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5090 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5091 | 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] | 5092 | } |
| 5093 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5094 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5095 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5096 | if (PSA_SUCCESS == expected_result) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5097 | TEST_CALLOC(output_data2, output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5098 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 5099 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 5100 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5101 | TEST_EQUAL(input_data->len, |
| 5102 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length)); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 5103 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5104 | TEST_LE_U(input_data->len, |
| 5105 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length)); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5106 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5107 | TEST_EQUAL(psa_aead_decrypt(key, alg, |
| 5108 | nonce->x, nonce->len, |
| 5109 | additional_data->x, |
| 5110 | additional_data->len, |
| 5111 | output_data, output_length, |
| 5112 | output_data2, output_length, |
| 5113 | &output_length2), |
| 5114 | expected_result); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5115 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 5116 | TEST_MEMORY_COMPARE(input_data->x, input_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 5117 | output_data2, output_length2); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5118 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5119 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5120 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5121 | psa_destroy_key(key); |
| 5122 | mbedtls_free(output_data); |
| 5123 | mbedtls_free(output_data2); |
| 5124 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5125 | } |
| 5126 | /* END_CASE */ |
| 5127 | |
| 5128 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5129 | void aead_encrypt(int key_type_arg, data_t *key_data, |
| 5130 | int alg_arg, |
| 5131 | data_t *nonce, |
| 5132 | data_t *additional_data, |
| 5133 | data_t *input_data, |
| 5134 | data_t *expected_result) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5135 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5136 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5137 | psa_key_type_t key_type = key_type_arg; |
| 5138 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5139 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5140 | unsigned char *output_data = NULL; |
| 5141 | size_t output_size = 0; |
| 5142 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5143 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 5144 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5145 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5146 | PSA_ASSERT(psa_crypto_init()); |
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 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5149 | psa_set_key_algorithm(&attributes, alg); |
| 5150 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5151 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5152 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5153 | &key)); |
| 5154 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5155 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5156 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5157 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 5158 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5159 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 5160 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5161 | TEST_EQUAL(output_size, |
| 5162 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 5163 | TEST_LE_U(output_size, |
| 5164 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5165 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5166 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5167 | status = psa_aead_encrypt(key, alg, |
| 5168 | nonce->x, nonce->len, |
| 5169 | additional_data->x, additional_data->len, |
| 5170 | input_data->x, input_data->len, |
| 5171 | output_data, output_size, |
| 5172 | &output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5173 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 5174 | /* If the operation is not supported, just skip and not fail in case the |
| 5175 | * encryption involves a common limitation of cryptography hardwares and |
| 5176 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5177 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5178 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5179 | 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] | 5180 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 5181 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5182 | PSA_ASSERT(status); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 5183 | TEST_MEMORY_COMPARE(expected_result->x, expected_result->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 5184 | output_data, output_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5185 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5186 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5187 | psa_destroy_key(key); |
| 5188 | mbedtls_free(output_data); |
| 5189 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5190 | } |
| 5191 | /* END_CASE */ |
| 5192 | |
| 5193 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5194 | void aead_decrypt(int key_type_arg, data_t *key_data, |
| 5195 | int alg_arg, |
| 5196 | data_t *nonce, |
| 5197 | data_t *additional_data, |
| 5198 | data_t *input_data, |
| 5199 | data_t *expected_data, |
| 5200 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5201 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5202 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5203 | psa_key_type_t key_type = key_type_arg; |
| 5204 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5205 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5206 | unsigned char *output_data = NULL; |
| 5207 | size_t output_size = 0; |
| 5208 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5209 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 5210 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 5211 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5212 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5213 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5215 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 5216 | psa_set_key_algorithm(&attributes, alg); |
| 5217 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5218 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5219 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5220 | &key)); |
| 5221 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5222 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5223 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5224 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 5225 | alg); |
| 5226 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 5227 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5228 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 5229 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5230 | TEST_EQUAL(output_size, |
| 5231 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 5232 | TEST_LE_U(output_size, |
| 5233 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5234 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5235 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5236 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5237 | status = psa_aead_decrypt(key, alg, |
| 5238 | nonce->x, nonce->len, |
| 5239 | additional_data->x, |
| 5240 | additional_data->len, |
| 5241 | input_data->x, input_data->len, |
| 5242 | output_data, output_size, |
| 5243 | &output_length); |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 5244 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 5245 | /* If the operation is not supported, just skip and not fail in case the |
| 5246 | * decryption involves a common limitation of cryptography hardwares and |
| 5247 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5248 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5249 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5250 | 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] | 5251 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 5252 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5253 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5254 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5255 | if (expected_result == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 5256 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 5257 | output_data, output_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5258 | } |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5259 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5260 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5261 | psa_destroy_key(key); |
| 5262 | mbedtls_free(output_data); |
| 5263 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5264 | } |
| 5265 | /* END_CASE */ |
| 5266 | |
| 5267 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5268 | void aead_multipart_encrypt(int key_type_arg, data_t *key_data, |
| 5269 | int alg_arg, |
| 5270 | data_t *nonce, |
| 5271 | data_t *additional_data, |
| 5272 | data_t *input_data, |
| 5273 | int do_set_lengths, |
| 5274 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5275 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5276 | size_t ad_part_len = 0; |
| 5277 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5278 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5279 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5280 | for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) { |
| 5281 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5282 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5283 | if (do_set_lengths) { |
| 5284 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5285 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5286 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5287 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5288 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5289 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5290 | |
| 5291 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5292 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5293 | alg_arg, nonce, |
| 5294 | additional_data, |
| 5295 | ad_part_len, |
| 5296 | input_data, -1, |
| 5297 | set_lengths_method, |
| 5298 | expected_output, |
| 5299 | 1, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5300 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5301 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5302 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5303 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 5304 | mbedtls_test_set_step(1000 + ad_part_len); |
| 5305 | |
| 5306 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5307 | alg_arg, nonce, |
| 5308 | additional_data, |
| 5309 | ad_part_len, |
| 5310 | input_data, -1, |
| 5311 | set_lengths_method, |
| 5312 | expected_output, |
| 5313 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5314 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5315 | } |
| 5316 | } |
| 5317 | |
| 5318 | for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) { |
| 5319 | /* Split data into length(data_part_len) parts. */ |
| 5320 | mbedtls_test_set_step(2000 + data_part_len); |
| 5321 | |
| 5322 | if (do_set_lengths) { |
| 5323 | if (data_part_len & 0x01) { |
| 5324 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 5325 | } else { |
| 5326 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
| 5327 | } |
| 5328 | } |
| 5329 | |
| 5330 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5331 | alg_arg, nonce, |
| 5332 | additional_data, -1, |
| 5333 | input_data, data_part_len, |
| 5334 | set_lengths_method, |
| 5335 | expected_output, |
| 5336 | 1, 0)) { |
| 5337 | break; |
| 5338 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5339 | |
| 5340 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5341 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5342 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5343 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5344 | alg_arg, nonce, |
| 5345 | additional_data, -1, |
| 5346 | input_data, data_part_len, |
| 5347 | set_lengths_method, |
| 5348 | expected_output, |
| 5349 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5350 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5351 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5352 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5353 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5354 | /* Goto is required to silence warnings about unused labels, as we |
| 5355 | * don't actually do any test assertions in this function. */ |
| 5356 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5357 | } |
| 5358 | /* END_CASE */ |
| 5359 | |
| 5360 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5361 | void aead_multipart_decrypt(int key_type_arg, data_t *key_data, |
| 5362 | int alg_arg, |
| 5363 | data_t *nonce, |
| 5364 | data_t *additional_data, |
| 5365 | data_t *input_data, |
| 5366 | int do_set_lengths, |
| 5367 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5368 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5369 | size_t ad_part_len = 0; |
| 5370 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5371 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5372 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5373 | 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] | 5374 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5375 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5377 | if (do_set_lengths) { |
| 5378 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5379 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5380 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5381 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5382 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5383 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5385 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5386 | alg_arg, nonce, |
| 5387 | additional_data, |
| 5388 | ad_part_len, |
| 5389 | input_data, -1, |
| 5390 | set_lengths_method, |
| 5391 | expected_output, |
| 5392 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5393 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5394 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5395 | |
| 5396 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5397 | mbedtls_test_set_step(1000 + ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5398 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5399 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5400 | alg_arg, nonce, |
| 5401 | additional_data, |
| 5402 | ad_part_len, |
| 5403 | input_data, -1, |
| 5404 | set_lengths_method, |
| 5405 | expected_output, |
| 5406 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5407 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5408 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5409 | } |
| 5410 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5411 | 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] | 5412 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5413 | mbedtls_test_set_step(2000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5414 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5415 | if (do_set_lengths) { |
| 5416 | if (data_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5417 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5418 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5419 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5420 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5421 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5422 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5423 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5424 | alg_arg, nonce, |
| 5425 | additional_data, -1, |
| 5426 | input_data, data_part_len, |
| 5427 | set_lengths_method, |
| 5428 | expected_output, |
| 5429 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5430 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5431 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5432 | |
| 5433 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5434 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5435 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5436 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5437 | alg_arg, nonce, |
| 5438 | additional_data, -1, |
| 5439 | input_data, data_part_len, |
| 5440 | set_lengths_method, |
| 5441 | expected_output, |
| 5442 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5443 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5444 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5445 | } |
| 5446 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5447 | /* Goto is required to silence warnings about unused labels, as we |
| 5448 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5449 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5450 | } |
| 5451 | /* END_CASE */ |
| 5452 | |
| 5453 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5454 | void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data, |
| 5455 | int alg_arg, |
| 5456 | int nonce_length, |
| 5457 | int expected_nonce_length_arg, |
| 5458 | data_t *additional_data, |
| 5459 | data_t *input_data, |
| 5460 | int expected_status_arg) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5461 | { |
| 5462 | |
| 5463 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5464 | psa_key_type_t key_type = key_type_arg; |
| 5465 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5466 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5467 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5468 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5469 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5470 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5471 | size_t actual_nonce_length = 0; |
| 5472 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 5473 | unsigned char *output = NULL; |
| 5474 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5475 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5476 | size_t ciphertext_size = 0; |
| 5477 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5478 | size_t tag_length = 0; |
| 5479 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5480 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5481 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5482 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5483 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5484 | psa_set_key_algorithm(&attributes, alg); |
| 5485 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5486 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5487 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5488 | &key)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5489 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5490 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5491 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5492 | 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] | 5493 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5494 | TEST_CALLOC(output, output_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5495 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5496 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5497 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5498 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5499 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5500 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5501 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5502 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5503 | |
| 5504 | /* If the operation is not supported, just skip and not fail in case the |
| 5505 | * encryption involves a common limitation of cryptography hardwares and |
| 5506 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5507 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5508 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5509 | 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] | 5510 | } |
| 5511 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5512 | PSA_ASSERT(status); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5513 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5514 | status = psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5515 | nonce_length, |
| 5516 | &actual_nonce_length); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5517 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5518 | TEST_EQUAL(status, expected_status); |
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 | TEST_EQUAL(actual_nonce_length, expected_nonce_length); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 5521 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5522 | if (expected_status == PSA_SUCCESS) { |
| 5523 | TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type, |
| 5524 | alg)); |
| 5525 | } |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 5526 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5527 | TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 5528 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5529 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5530 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5531 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5532 | input_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5533 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5534 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5535 | additional_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5536 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5537 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5538 | output, output_size, |
| 5539 | &ciphertext_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5540 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5541 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5542 | &ciphertext_length, tag_buffer, |
| 5543 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5544 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5545 | |
| 5546 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5547 | psa_destroy_key(key); |
| 5548 | mbedtls_free(output); |
| 5549 | mbedtls_free(ciphertext); |
| 5550 | psa_aead_abort(&operation); |
| 5551 | PSA_DONE(); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5552 | } |
| 5553 | /* END_CASE */ |
| 5554 | |
| 5555 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5556 | void aead_multipart_set_nonce(int key_type_arg, data_t *key_data, |
| 5557 | int alg_arg, |
| 5558 | int nonce_length_arg, |
| 5559 | int set_lengths_method_arg, |
| 5560 | data_t *additional_data, |
| 5561 | data_t *input_data, |
| 5562 | int expected_status_arg) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5563 | { |
| 5564 | |
| 5565 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5566 | psa_key_type_t key_type = key_type_arg; |
| 5567 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5568 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5569 | uint8_t *nonce_buffer = NULL; |
| 5570 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5571 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5572 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5573 | unsigned char *output = NULL; |
| 5574 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5575 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5576 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5577 | size_t ciphertext_size = 0; |
| 5578 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5579 | size_t tag_length = 0; |
| 5580 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5581 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5582 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
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 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5585 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5586 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5587 | psa_set_key_algorithm(&attributes, alg); |
| 5588 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5589 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5590 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5591 | &key)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5592 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5593 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5594 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5595 | 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] | 5596 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5597 | TEST_CALLOC(output, output_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5598 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5599 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5600 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5601 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5602 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5603 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5604 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5605 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5606 | |
| 5607 | /* If the operation is not supported, just skip and not fail in case the |
| 5608 | * encryption involves a common limitation of cryptography hardwares and |
| 5609 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5610 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5611 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5612 | 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] | 5613 | } |
| 5614 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5615 | PSA_ASSERT(status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5616 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5617 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5618 | if (nonce_length_arg == -1) { |
| 5619 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5620 | TEST_CALLOC(nonce_buffer, 4); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5621 | nonce_length = 0; |
| 5622 | } else { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5623 | /* If length is zero, then this will return NULL. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5624 | nonce_length = (size_t) nonce_length_arg; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5625 | TEST_CALLOC(nonce_buffer, nonce_length); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5626 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5627 | if (nonce_buffer) { |
| 5628 | for (index = 0; index < nonce_length - 1; ++index) { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5629 | nonce_buffer[index] = 'a' + index; |
| 5630 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5631 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5632 | } |
| 5633 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5634 | if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { |
| 5635 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5636 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5637 | } |
| 5638 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5639 | status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5640 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5641 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5642 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5643 | if (expected_status == PSA_SUCCESS) { |
| 5644 | if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) { |
| 5645 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5646 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5647 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5648 | 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] | 5649 | expected_status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5650 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5651 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5652 | /* 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] | 5653 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5654 | additional_data->len), |
| 5655 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5656 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5657 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5658 | output, output_size, |
| 5659 | &ciphertext_length), |
| 5660 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5661 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5662 | TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5663 | &ciphertext_length, tag_buffer, |
| 5664 | PSA_AEAD_TAG_MAX_SIZE, &tag_length), |
| 5665 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5666 | } |
| 5667 | |
| 5668 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5669 | psa_destroy_key(key); |
| 5670 | mbedtls_free(output); |
| 5671 | mbedtls_free(ciphertext); |
| 5672 | mbedtls_free(nonce_buffer); |
| 5673 | psa_aead_abort(&operation); |
| 5674 | PSA_DONE(); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5675 | } |
| 5676 | /* END_CASE */ |
| 5677 | |
| 5678 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5679 | 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] | 5680 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5681 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5682 | data_t *nonce, |
| 5683 | data_t *additional_data, |
| 5684 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5685 | int expected_status_arg) |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5686 | { |
| 5687 | |
| 5688 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5689 | psa_key_type_t key_type = key_type_arg; |
| 5690 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5691 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5692 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5693 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5694 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5695 | unsigned char *output = NULL; |
| 5696 | unsigned char *ciphertext = NULL; |
| 5697 | size_t output_size = output_size_arg; |
| 5698 | size_t ciphertext_size = 0; |
| 5699 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5700 | size_t tag_length = 0; |
| 5701 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5702 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5703 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5704 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5705 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5706 | psa_set_key_algorithm(&attributes, alg); |
| 5707 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5708 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5709 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5710 | &key)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5711 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5712 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5713 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5714 | TEST_CALLOC(output, output_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5715 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5716 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5717 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5718 | TEST_CALLOC(ciphertext, ciphertext_size); |
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 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5721 | |
| 5722 | /* If the operation is not supported, just skip and not fail in case the |
| 5723 | * encryption involves a common limitation of cryptography hardwares and |
| 5724 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5725 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5726 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5727 | 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] | 5728 | } |
| 5729 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5730 | PSA_ASSERT(status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5731 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5732 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5733 | input_data->len)); |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5734 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5735 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5736 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5737 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5738 | additional_data->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5739 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5740 | status = psa_aead_update(&operation, input_data->x, input_data->len, |
| 5741 | output, output_size, &ciphertext_length); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5742 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5743 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5744 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5745 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5746 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5747 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5748 | &ciphertext_length, tag_buffer, |
| 5749 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5750 | } |
| 5751 | |
| 5752 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5753 | psa_destroy_key(key); |
| 5754 | mbedtls_free(output); |
| 5755 | mbedtls_free(ciphertext); |
| 5756 | psa_aead_abort(&operation); |
| 5757 | PSA_DONE(); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5758 | } |
| 5759 | /* END_CASE */ |
| 5760 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5761 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5762 | void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data, |
| 5763 | int alg_arg, |
| 5764 | int finish_ciphertext_size_arg, |
| 5765 | int tag_size_arg, |
| 5766 | data_t *nonce, |
| 5767 | data_t *additional_data, |
| 5768 | data_t *input_data, |
| 5769 | int expected_status_arg) |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5770 | { |
| 5771 | |
| 5772 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5773 | psa_key_type_t key_type = key_type_arg; |
| 5774 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5775 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5776 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5777 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5778 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5779 | unsigned char *ciphertext = NULL; |
| 5780 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5781 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5782 | size_t ciphertext_size = 0; |
| 5783 | size_t ciphertext_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5784 | size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg; |
| 5785 | size_t tag_size = (size_t) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5786 | size_t tag_length = 0; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5787 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5788 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5789 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5790 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5791 | psa_set_key_algorithm(&attributes, alg); |
| 5792 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5793 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5794 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5795 | &key)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5796 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5797 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5798 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5799 | 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] | 5800 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5801 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5802 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5803 | TEST_CALLOC(finish_ciphertext, finish_ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5804 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5805 | TEST_CALLOC(tag_buffer, tag_size); |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5806 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5807 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5808 | |
| 5809 | /* If the operation is not supported, just skip and not fail in case the |
| 5810 | * encryption involves a common limitation of cryptography hardwares and |
| 5811 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5812 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5813 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5814 | 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] | 5815 | } |
| 5816 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5817 | PSA_ASSERT(status); |
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 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5820 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5821 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5822 | input_data->len)); |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5823 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5824 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5825 | additional_data->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5826 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5827 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5828 | ciphertext, ciphertext_size, &ciphertext_length)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5829 | |
| 5830 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5831 | status = psa_aead_finish(&operation, finish_ciphertext, |
| 5832 | finish_ciphertext_size, |
| 5833 | &ciphertext_length, tag_buffer, |
| 5834 | tag_size, &tag_length); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5835 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5836 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5837 | |
| 5838 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5839 | psa_destroy_key(key); |
| 5840 | mbedtls_free(ciphertext); |
| 5841 | mbedtls_free(finish_ciphertext); |
| 5842 | mbedtls_free(tag_buffer); |
| 5843 | psa_aead_abort(&operation); |
| 5844 | PSA_DONE(); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5845 | } |
| 5846 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5847 | |
| 5848 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5849 | void aead_multipart_verify(int key_type_arg, data_t *key_data, |
| 5850 | int alg_arg, |
| 5851 | data_t *nonce, |
| 5852 | data_t *additional_data, |
| 5853 | data_t *input_data, |
| 5854 | data_t *tag, |
| 5855 | int tag_usage_arg, |
| 5856 | int expected_setup_status_arg, |
| 5857 | int expected_status_arg) |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5858 | { |
| 5859 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5860 | psa_key_type_t key_type = key_type_arg; |
| 5861 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5862 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5863 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5864 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5865 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5866 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5867 | unsigned char *plaintext = NULL; |
| 5868 | unsigned char *finish_plaintext = NULL; |
| 5869 | size_t plaintext_size = 0; |
| 5870 | size_t plaintext_length = 0; |
| 5871 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5872 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5873 | unsigned char *tag_buffer = NULL; |
| 5874 | size_t tag_size = 0; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5875 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5876 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5877 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5878 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 5879 | psa_set_key_algorithm(&attributes, alg); |
| 5880 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5881 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5882 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5883 | &key)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5884 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5885 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5886 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5887 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 5888 | input_data->len); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5889 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5890 | TEST_CALLOC(plaintext, plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5891 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5892 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5893 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5894 | TEST_CALLOC(finish_plaintext, verify_plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5895 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5896 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5897 | |
| 5898 | /* If the operation is not supported, just skip and not fail in case the |
| 5899 | * encryption involves a common limitation of cryptography hardwares and |
| 5900 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5901 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5902 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5903 | 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] | 5904 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5905 | TEST_EQUAL(status, expected_setup_status); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5906 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5907 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5908 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5909 | } |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5910 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5911 | PSA_ASSERT(status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5912 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5913 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5914 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5915 | status = psa_aead_set_lengths(&operation, additional_data->len, |
| 5916 | input_data->len); |
| 5917 | PSA_ASSERT(status); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5918 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5919 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5920 | additional_data->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5921 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5922 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 5923 | input_data->len, |
| 5924 | plaintext, plaintext_size, |
| 5925 | &plaintext_length)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5926 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5927 | if (tag_usage == USE_GIVEN_TAG) { |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5928 | tag_buffer = tag->x; |
| 5929 | tag_size = tag->len; |
| 5930 | } |
| 5931 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5932 | status = psa_aead_verify(&operation, finish_plaintext, |
| 5933 | verify_plaintext_size, |
| 5934 | &plaintext_length, |
| 5935 | tag_buffer, tag_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5936 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5937 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5938 | |
| 5939 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5940 | psa_destroy_key(key); |
| 5941 | mbedtls_free(plaintext); |
| 5942 | mbedtls_free(finish_plaintext); |
| 5943 | psa_aead_abort(&operation); |
| 5944 | PSA_DONE(); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5945 | } |
| 5946 | /* END_CASE */ |
| 5947 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5948 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5949 | void aead_multipart_setup(int key_type_arg, data_t *key_data, |
| 5950 | int alg_arg, int expected_status_arg) |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5951 | { |
| 5952 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5953 | psa_key_type_t key_type = key_type_arg; |
| 5954 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5955 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5956 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5957 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5958 | psa_status_t expected_status = expected_status_arg; |
| 5959 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5960 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5961 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5962 | psa_set_key_usage_flags(&attributes, |
| 5963 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5964 | psa_set_key_algorithm(&attributes, alg); |
| 5965 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5966 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5967 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5968 | &key)); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5969 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5970 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5971 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5972 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5973 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5974 | psa_aead_abort(&operation); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5975 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5976 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5977 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5978 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5979 | |
| 5980 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5981 | psa_destroy_key(key); |
| 5982 | psa_aead_abort(&operation); |
| 5983 | PSA_DONE(); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5984 | } |
| 5985 | /* END_CASE */ |
| 5986 | |
| 5987 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5988 | void aead_multipart_state_test(int key_type_arg, data_t *key_data, |
| 5989 | int alg_arg, |
| 5990 | data_t *nonce, |
| 5991 | data_t *additional_data, |
| 5992 | data_t *input_data) |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5993 | { |
| 5994 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5995 | psa_key_type_t key_type = key_type_arg; |
| 5996 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5997 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5998 | unsigned char *output_data = NULL; |
| 5999 | unsigned char *final_data = NULL; |
| 6000 | size_t output_size = 0; |
| 6001 | size_t finish_output_size = 0; |
| 6002 | size_t output_length = 0; |
| 6003 | size_t key_bits = 0; |
| 6004 | size_t tag_length = 0; |
| 6005 | size_t tag_size = 0; |
| 6006 | size_t nonce_length = 0; |
| 6007 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 6008 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 6009 | size_t output_part_length = 0; |
| 6010 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6011 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6012 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6013 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6014 | psa_set_key_usage_flags(&attributes, |
| 6015 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 6016 | psa_set_key_algorithm(&attributes, alg); |
| 6017 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6018 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6019 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6020 | &key)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6021 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6022 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6023 | key_bits = psa_get_key_bits(&attributes); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6024 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6025 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6026 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6027 | TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE); |
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 | 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] | 6030 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6031 | TEST_CALLOC(output_data, output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6032 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6033 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6035 | TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6036 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6037 | TEST_CALLOC(final_data, finish_output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6038 | |
| 6039 | /* Test all operations error without calling setup first. */ |
| 6040 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6041 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6042 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6043 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6044 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6045 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6046 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6047 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6048 | &nonce_length), |
| 6049 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6050 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6051 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6052 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 6053 | /* ------------------------------------------------------- */ |
| 6054 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6055 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6056 | input_data->len), |
| 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_update_ad(&operation, additional_data->x, |
| 6064 | additional_data->len), |
| 6065 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6066 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6067 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6068 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 6069 | /* ------------------------------------------------------- */ |
| 6070 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6071 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6072 | input_data->len, output_data, |
| 6073 | output_size, &output_length), |
| 6074 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6075 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6076 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6077 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 6078 | /* ------------------------------------------------------- */ |
| 6079 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6080 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6081 | finish_output_size, |
| 6082 | &output_part_length, |
| 6083 | tag_buffer, tag_length, |
| 6084 | &tag_size), |
| 6085 | PSA_ERROR_BAD_STATE); |
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 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6088 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 6089 | /* ------------------------------------------------------- */ |
| 6090 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6091 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6092 | finish_output_size, |
| 6093 | &output_part_length, |
| 6094 | tag_buffer, |
| 6095 | tag_length), |
| 6096 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6097 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6098 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6099 | |
| 6100 | /* Test for double setups. */ |
| 6101 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6102 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6103 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6104 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 6105 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6106 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6107 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6108 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 6109 | /* ------------------------------------------------------- */ |
| 6110 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6111 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6112 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6113 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 6114 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6115 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6116 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6117 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6118 | /* ------------------------------------------------------- */ |
| 6119 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6120 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6121 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6122 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 6123 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6124 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6125 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6126 | |
| 6127 | /* ------------------------------------------------------- */ |
| 6128 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6129 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6130 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6131 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 6132 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6133 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6134 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6135 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6136 | /* Test for not setting a nonce. */ |
| 6137 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6138 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6139 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6140 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6141 | additional_data->len), |
| 6142 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6143 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6144 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6145 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 6146 | /* ------------------------------------------------------- */ |
| 6147 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6148 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 6149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6150 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6151 | input_data->len, output_data, |
| 6152 | output_size, &output_length), |
| 6153 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 6154 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6155 | psa_aead_abort(&operation); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 6156 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6157 | /* ------------------------------------------------------- */ |
| 6158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6159 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6160 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6161 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6162 | finish_output_size, |
| 6163 | &output_part_length, |
| 6164 | tag_buffer, tag_length, |
| 6165 | &tag_size), |
| 6166 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6167 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6168 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6169 | |
| 6170 | /* ------------------------------------------------------- */ |
| 6171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6172 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6173 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6174 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6175 | finish_output_size, |
| 6176 | &output_part_length, |
| 6177 | tag_buffer, |
| 6178 | tag_length), |
| 6179 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6181 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6182 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6183 | /* Test for double setting nonce. */ |
| 6184 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6185 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6186 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6187 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6188 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6189 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6190 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6191 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6192 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6193 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6194 | /* Test for double generating nonce. */ |
| 6195 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6196 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6197 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6198 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6199 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6200 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6201 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6202 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6203 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6204 | &nonce_length), |
| 6205 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6206 | |
| 6207 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6208 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6209 | |
| 6210 | /* Test for generate nonce then set and vice versa */ |
| 6211 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6212 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6213 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6214 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6215 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6216 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6217 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6218 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6219 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6221 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6222 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6223 | /* Test for generating nonce after calling set lengths */ |
| 6224 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6225 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6226 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6227 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6228 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6230 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6231 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6232 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6233 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6234 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6235 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6236 | /* 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] | 6237 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6238 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6239 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6240 | if (operation.alg == PSA_ALG_CCM) { |
| 6241 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6242 | input_data->len), |
| 6243 | PSA_ERROR_INVALID_ARGUMENT); |
| 6244 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6245 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6246 | &nonce_length), |
| 6247 | PSA_ERROR_BAD_STATE); |
| 6248 | } else { |
| 6249 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6250 | input_data->len)); |
| 6251 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6252 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6253 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6254 | } |
| 6255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6256 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6257 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6258 | /* 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] | 6259 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6260 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6261 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6262 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 6263 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 6264 | input_data->len), |
| 6265 | PSA_ERROR_INVALID_ARGUMENT); |
| 6266 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6267 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6268 | &nonce_length), |
| 6269 | PSA_ERROR_BAD_STATE); |
| 6270 | } else { |
| 6271 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 6272 | input_data->len)); |
| 6273 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6274 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6275 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6276 | } |
| 6277 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6278 | psa_aead_abort(&operation); |
Dave Rodgman | d26d744 | 2023-02-11 17:14:54 +0000 | [diff] [blame] | 6279 | #endif |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6280 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6281 | /* 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] | 6282 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6283 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6284 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6285 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6286 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6287 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6288 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6289 | if (operation.alg == PSA_ALG_CCM) { |
| 6290 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6291 | input_data->len), |
| 6292 | PSA_ERROR_INVALID_ARGUMENT); |
| 6293 | } else { |
| 6294 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6295 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6296 | } |
| 6297 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6298 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6299 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6300 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6301 | /* Test for setting nonce after calling set lengths */ |
| 6302 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6303 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6304 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6305 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6306 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6307 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 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 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6310 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6311 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6312 | /* 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] | 6313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6314 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6315 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6316 | if (operation.alg == PSA_ALG_CCM) { |
| 6317 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6318 | input_data->len), |
| 6319 | PSA_ERROR_INVALID_ARGUMENT); |
| 6320 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6321 | PSA_ERROR_BAD_STATE); |
| 6322 | } else { |
| 6323 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6324 | input_data->len)); |
| 6325 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6326 | } |
| 6327 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6328 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6329 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6330 | /* 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] | 6331 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6332 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6333 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6334 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 6335 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 6336 | input_data->len), |
| 6337 | PSA_ERROR_INVALID_ARGUMENT); |
| 6338 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6339 | PSA_ERROR_BAD_STATE); |
| 6340 | } else { |
| 6341 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 6342 | input_data->len)); |
| 6343 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6344 | } |
| 6345 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6346 | psa_aead_abort(&operation); |
Dave Rodgman | a476363 | 2023-02-11 18:36:23 +0000 | [diff] [blame] | 6347 | #endif |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6348 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6349 | /* 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] | 6350 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6351 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6353 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6354 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6355 | if (operation.alg == PSA_ALG_CCM) { |
| 6356 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6357 | input_data->len), |
| 6358 | PSA_ERROR_INVALID_ARGUMENT); |
| 6359 | } else { |
| 6360 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6361 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6362 | } |
| 6363 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6364 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6365 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6366 | /* 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] | 6367 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6368 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6370 | if (operation.alg == PSA_ALG_GCM) { |
| 6371 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6372 | SIZE_MAX), |
| 6373 | PSA_ERROR_INVALID_ARGUMENT); |
| 6374 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6375 | PSA_ERROR_BAD_STATE); |
| 6376 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6377 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6378 | SIZE_MAX)); |
| 6379 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6380 | } |
| 6381 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6382 | psa_aead_abort(&operation); |
Dave Rodgman | 91e8321 | 2023-02-11 20:07:43 +0000 | [diff] [blame] | 6383 | #endif |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6384 | |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 6385 | /* 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] | 6386 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6387 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6389 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6390 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6391 | if (operation.alg == PSA_ALG_GCM) { |
| 6392 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6393 | SIZE_MAX), |
| 6394 | PSA_ERROR_INVALID_ARGUMENT); |
| 6395 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6396 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6397 | SIZE_MAX)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6398 | } |
| 6399 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6400 | psa_aead_abort(&operation); |
Dave Rodgman | 641288b | 2023-02-11 22:02:04 +0000 | [diff] [blame] | 6401 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6402 | |
| 6403 | /* ------------------------------------------------------- */ |
| 6404 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6405 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6406 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6407 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6408 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6409 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6410 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6411 | &nonce_length), |
| 6412 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6413 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6414 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6415 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6416 | /* Test for generating nonce in decrypt setup. */ |
| 6417 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6418 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6419 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6420 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6421 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6422 | &nonce_length), |
| 6423 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6424 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6425 | psa_aead_abort(&operation); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6426 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6427 | /* Test for setting lengths twice. */ |
| 6428 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6429 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6433 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6434 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6435 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6436 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6437 | input_data->len), |
| 6438 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6439 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6440 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6441 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6442 | /* Test for setting lengths after setting nonce + already starting data. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6444 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6445 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6446 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6447 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6448 | if (operation.alg == PSA_ALG_CCM) { |
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 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6451 | additional_data->len), |
| 6452 | PSA_ERROR_BAD_STATE); |
| 6453 | } else { |
| 6454 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6455 | additional_data->len)); |
| 6456 | |
| 6457 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6458 | input_data->len), |
| 6459 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6460 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6461 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6462 | |
| 6463 | /* ------------------------------------------------------- */ |
| 6464 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6465 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6466 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6467 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6468 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6469 | if (operation.alg == PSA_ALG_CCM) { |
| 6470 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6471 | input_data->len, output_data, |
| 6472 | output_size, &output_length), |
| 6473 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6474 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6475 | } else { |
| 6476 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6477 | input_data->len, output_data, |
| 6478 | output_size, &output_length)); |
| 6479 | |
| 6480 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6481 | input_data->len), |
| 6482 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6483 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6484 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6485 | |
| 6486 | /* ------------------------------------------------------- */ |
| 6487 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6488 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6489 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6490 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
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 | if (operation.alg == PSA_ALG_CCM) { |
| 6493 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6494 | finish_output_size, |
| 6495 | &output_part_length, |
| 6496 | tag_buffer, tag_length, |
| 6497 | &tag_size)); |
| 6498 | } else { |
| 6499 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6500 | finish_output_size, |
| 6501 | &output_part_length, |
| 6502 | tag_buffer, tag_length, |
| 6503 | &tag_size)); |
| 6504 | |
| 6505 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6506 | input_data->len), |
| 6507 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6508 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6509 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6510 | |
| 6511 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 6512 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6513 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6514 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6515 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6516 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6517 | &nonce_length)); |
| 6518 | if (operation.alg == PSA_ALG_CCM) { |
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 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6521 | additional_data->len), |
| 6522 | PSA_ERROR_BAD_STATE); |
| 6523 | } else { |
| 6524 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6525 | additional_data->len)); |
| 6526 | |
| 6527 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6528 | input_data->len), |
| 6529 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6530 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6531 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6532 | |
| 6533 | /* ------------------------------------------------------- */ |
| 6534 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6535 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6536 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6537 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6538 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6539 | &nonce_length)); |
| 6540 | if (operation.alg == PSA_ALG_CCM) { |
| 6541 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6542 | input_data->len, output_data, |
| 6543 | output_size, &output_length), |
| 6544 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6545 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6546 | } else { |
| 6547 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6548 | input_data->len, output_data, |
| 6549 | output_size, &output_length)); |
| 6550 | |
| 6551 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6552 | input_data->len), |
| 6553 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6554 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6555 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6556 | |
| 6557 | /* ------------------------------------------------------- */ |
| 6558 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6559 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6560 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6561 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6562 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6563 | &nonce_length)); |
| 6564 | if (operation.alg == PSA_ALG_CCM) { |
| 6565 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6566 | finish_output_size, |
| 6567 | &output_part_length, |
| 6568 | tag_buffer, tag_length, |
| 6569 | &tag_size)); |
| 6570 | } else { |
| 6571 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6572 | finish_output_size, |
| 6573 | &output_part_length, |
| 6574 | tag_buffer, tag_length, |
| 6575 | &tag_size)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6576 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6577 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6578 | input_data->len), |
| 6579 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6580 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6581 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6582 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6583 | /* Test for not sending any additional data or data after setting non zero |
| 6584 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6585 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6586 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6587 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6588 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6589 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6590 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6591 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6592 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6593 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6594 | finish_output_size, |
| 6595 | &output_part_length, |
| 6596 | tag_buffer, tag_length, |
| 6597 | &tag_size), |
| 6598 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6599 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6600 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6601 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6602 | /* Test for not sending any additional data or data after setting non-zero |
| 6603 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6604 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6605 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6606 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6607 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6608 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6609 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6610 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6611 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6612 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6613 | finish_output_size, |
| 6614 | &output_part_length, |
| 6615 | tag_buffer, |
| 6616 | tag_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 | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6621 | /* Test for not sending any additional data after setting a non-zero length |
| 6622 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6623 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6624 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6625 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6626 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6627 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6628 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6629 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6630 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6631 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6632 | input_data->len, output_data, |
| 6633 | output_size, &output_length), |
| 6634 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6635 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6636 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6637 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6638 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 6639 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6640 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6642 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6643 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6644 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6645 | input_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6646 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6647 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6648 | additional_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6649 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6650 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6651 | finish_output_size, |
| 6652 | &output_part_length, |
| 6653 | tag_buffer, tag_length, |
| 6654 | &tag_size), |
| 6655 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6656 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6657 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6658 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6659 | /* Test for sending too much additional data after setting lengths. */ |
| 6660 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6661 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6662 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6663 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6664 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6665 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6666 | |
| 6667 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6668 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6669 | additional_data->len), |
| 6670 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6671 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6672 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6673 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6674 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6676 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6677 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6678 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6679 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6680 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6681 | input_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6682 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6683 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6684 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6685 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6686 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6687 | 1), |
| 6688 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6689 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6690 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6691 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6692 | /* Test for sending too much data after setting lengths. */ |
| 6693 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6694 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6695 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6696 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6697 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6698 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6699 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6700 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6701 | input_data->len, output_data, |
| 6702 | output_size, &output_length), |
| 6703 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6704 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6705 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6706 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6707 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6708 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6709 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6712 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6713 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6714 | input_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6715 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6716 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6717 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6718 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6719 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6720 | input_data->len, output_data, |
| 6721 | output_size, &output_length)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6722 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6723 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6724 | 1, output_data, |
| 6725 | output_size, &output_length), |
| 6726 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6727 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6728 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6729 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6730 | /* Test sending additional data after data. */ |
| 6731 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6732 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +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 | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6735 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6736 | if (operation.alg != PSA_ALG_CCM) { |
| 6737 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6738 | input_data->len, output_data, |
| 6739 | output_size, &output_length)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6740 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6741 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6742 | additional_data->len), |
| 6743 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6744 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6745 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6746 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6747 | /* Test calling finish on decryption. */ |
| 6748 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6749 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
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 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6752 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6753 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6754 | finish_output_size, |
| 6755 | &output_part_length, |
| 6756 | tag_buffer, tag_length, |
| 6757 | &tag_size), |
| 6758 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6759 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6760 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6761 | |
| 6762 | /* Test calling verify on encryption. */ |
| 6763 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6764 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6765 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6766 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6767 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6768 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6769 | finish_output_size, |
| 6770 | &output_part_length, |
| 6771 | tag_buffer, |
| 6772 | tag_length), |
| 6773 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6774 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6775 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6776 | |
| 6777 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6778 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6779 | psa_destroy_key(key); |
| 6780 | psa_aead_abort(&operation); |
| 6781 | mbedtls_free(output_data); |
| 6782 | mbedtls_free(final_data); |
| 6783 | PSA_DONE(); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6784 | } |
| 6785 | /* END_CASE */ |
| 6786 | |
| 6787 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6788 | void signature_size(int type_arg, |
| 6789 | int bits, |
| 6790 | int alg_arg, |
| 6791 | int expected_size_arg) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6792 | { |
| 6793 | psa_key_type_t type = type_arg; |
| 6794 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6795 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6796 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6797 | TEST_EQUAL(actual_size, (size_t) expected_size_arg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6798 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6799 | exit: |
| 6800 | ; |
| 6801 | } |
| 6802 | /* END_CASE */ |
| 6803 | |
| 6804 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6805 | void sign_hash_deterministic(int key_type_arg, data_t *key_data, |
| 6806 | int alg_arg, data_t *input_data, |
| 6807 | data_t *output_data) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6808 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6809 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6810 | psa_key_type_t key_type = key_type_arg; |
| 6811 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6812 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6813 | unsigned char *signature = NULL; |
| 6814 | size_t signature_size; |
| 6815 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6816 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6817 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6818 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6819 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6820 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6821 | psa_set_key_algorithm(&attributes, alg); |
| 6822 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6823 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6824 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6825 | &key)); |
| 6826 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6827 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6828 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6829 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6830 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6831 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6832 | key_bits, alg); |
| 6833 | TEST_ASSERT(signature_size != 0); |
| 6834 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6835 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6836 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6837 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6838 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6839 | input_data->x, input_data->len, |
| 6840 | signature, signature_size, |
| 6841 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6842 | /* Verify that the signature is what is expected. */ |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 6843 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 6844 | signature, signature_length); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6845 | |
| 6846 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6847 | /* |
| 6848 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6849 | * thus reset them as required. |
| 6850 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6851 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6852 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6853 | psa_destroy_key(key); |
| 6854 | mbedtls_free(signature); |
| 6855 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6856 | } |
| 6857 | /* END_CASE */ |
| 6858 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6859 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6860 | /** |
| 6861 | * sign_hash_interruptible() test intentions: |
| 6862 | * |
| 6863 | * Note: This test can currently only handle ECDSA. |
| 6864 | * |
| 6865 | * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 6866 | * and private keys / keypairs only). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6867 | * |
| 6868 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 6869 | * expected for different max_ops values. |
| 6870 | * |
| 6871 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 6872 | * and that each successful stage completes some ops (this is not mandated by |
| 6873 | * the PSA specification, but is currently the case). |
| 6874 | * |
| 6875 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 6876 | * complete() calls does not alter the number of ops returned. |
| 6877 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6878 | void sign_hash_interruptible(int key_type_arg, data_t *key_data, |
| 6879 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6880 | data_t *output_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6881 | { |
| 6882 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6883 | psa_key_type_t key_type = key_type_arg; |
| 6884 | psa_algorithm_t alg = alg_arg; |
| 6885 | size_t key_bits; |
| 6886 | unsigned char *signature = NULL; |
| 6887 | size_t signature_size; |
| 6888 | size_t signature_length = 0xdeadbeef; |
| 6889 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6890 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6891 | uint32_t num_ops = 0; |
| 6892 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6893 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6894 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6895 | size_t min_completes = 0; |
| 6896 | size_t max_completes = 0; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6897 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6898 | psa_sign_hash_interruptible_operation_t operation = |
| 6899 | psa_sign_hash_interruptible_operation_init(); |
| 6900 | |
| 6901 | PSA_ASSERT(psa_crypto_init()); |
| 6902 | |
| 6903 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6904 | psa_set_key_algorithm(&attributes, alg); |
| 6905 | psa_set_key_type(&attributes, key_type); |
| 6906 | |
| 6907 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6908 | &key)); |
| 6909 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6910 | key_bits = psa_get_key_bits(&attributes); |
| 6911 | |
| 6912 | /* Allocate a buffer which has the size advertised by the |
| 6913 | * library. */ |
| 6914 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6915 | key_bits, alg); |
| 6916 | TEST_ASSERT(signature_size != 0); |
| 6917 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6918 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6919 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6920 | psa_interruptible_set_max_ops(max_ops); |
| 6921 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6922 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6923 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6924 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6925 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6926 | TEST_ASSERT(num_ops_prior == 0); |
| 6927 | |
| 6928 | /* Start performing the signature. */ |
| 6929 | PSA_ASSERT(psa_sign_hash_start(&operation, key, alg, |
| 6930 | input_data->x, input_data->len)); |
| 6931 | |
| 6932 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6933 | TEST_ASSERT(num_ops_prior == 0); |
| 6934 | |
| 6935 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6936 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6937 | status = psa_sign_hash_complete(&operation, signature, signature_size, |
| 6938 | &signature_length); |
| 6939 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6940 | num_completes++; |
| 6941 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6942 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 6943 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6944 | /* We are asserting here that every complete makes progress |
| 6945 | * (completes some ops), which is true of the internal |
| 6946 | * implementation and probably any implementation, however this is |
| 6947 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6948 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6949 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6950 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 6951 | |
| 6952 | /* Ensure calling get_num_ops() twice still returns the same |
| 6953 | * number of ops as previously reported. */ |
| 6954 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6955 | |
| 6956 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6957 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6958 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6959 | |
| 6960 | TEST_ASSERT(status == PSA_SUCCESS); |
| 6961 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6962 | TEST_LE_U(min_completes, num_completes); |
| 6963 | TEST_LE_U(num_completes, max_completes); |
| 6964 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6965 | /* Verify that the signature is what is expected. */ |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 6966 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 6967 | signature, signature_length); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6968 | |
| 6969 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6970 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6971 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6972 | TEST_ASSERT(num_ops == 0); |
| 6973 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6974 | exit: |
| 6975 | |
| 6976 | /* |
| 6977 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6978 | * thus reset them as required. |
| 6979 | */ |
| 6980 | psa_reset_key_attributes(&attributes); |
| 6981 | |
| 6982 | psa_destroy_key(key); |
| 6983 | mbedtls_free(signature); |
| 6984 | PSA_DONE(); |
| 6985 | } |
| 6986 | /* END_CASE */ |
| 6987 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6988 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6989 | void sign_hash_fail(int key_type_arg, data_t *key_data, |
| 6990 | int alg_arg, data_t *input_data, |
| 6991 | int signature_size_arg, int expected_status_arg) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6992 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6993 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6994 | psa_key_type_t key_type = key_type_arg; |
| 6995 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6996 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6997 | psa_status_t actual_status; |
| 6998 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 6999 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 7000 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7001 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 7002 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7003 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 7004 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7005 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 7006 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7007 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 7008 | psa_set_key_algorithm(&attributes, alg); |
| 7009 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 7010 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7011 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7012 | &key)); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 7013 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7014 | actual_status = psa_sign_hash(key, alg, |
| 7015 | input_data->x, input_data->len, |
| 7016 | signature, signature_size, |
| 7017 | &signature_length); |
| 7018 | TEST_EQUAL(actual_status, expected_status); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 7019 | /* The value of *signature_length is unspecified on error, but |
| 7020 | * whatever it is, it should be less than signature_size, so that |
| 7021 | * if the caller tries to read *signature_length bytes without |
| 7022 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7023 | TEST_LE_U(signature_length, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 7024 | |
| 7025 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7026 | psa_reset_key_attributes(&attributes); |
| 7027 | psa_destroy_key(key); |
| 7028 | mbedtls_free(signature); |
| 7029 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 7030 | } |
| 7031 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 7032 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7033 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7034 | /** |
| 7035 | * sign_hash_fail_interruptible() test intentions: |
| 7036 | * |
| 7037 | * Note: This test can currently only handle ECDSA. |
| 7038 | * |
| 7039 | * 1. Test that various failure cases for interruptible sign hash fail with the |
| 7040 | * correct error codes, and at the correct point (at start or during |
| 7041 | * complete). |
| 7042 | * |
| 7043 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 7044 | * expected for different max_ops values. |
| 7045 | * |
| 7046 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7047 | * and that each successful stage completes some ops (this is not mandated by |
| 7048 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7049 | * |
| 7050 | * 4. Check that calling complete() when start() fails and complete() |
| 7051 | * after completion results in a BAD_STATE error. |
| 7052 | * |
| 7053 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 7054 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7055 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7056 | void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 7057 | int alg_arg, data_t *input_data, |
| 7058 | int signature_size_arg, |
| 7059 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7060 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7061 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7062 | { |
| 7063 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7064 | psa_key_type_t key_type = key_type_arg; |
| 7065 | psa_algorithm_t alg = alg_arg; |
| 7066 | size_t signature_size = signature_size_arg; |
| 7067 | psa_status_t actual_status; |
| 7068 | psa_status_t expected_start_status = expected_start_status_arg; |
| 7069 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 7070 | unsigned char *signature = NULL; |
| 7071 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7072 | uint32_t num_ops = 0; |
| 7073 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7074 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7075 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7076 | size_t min_completes = 0; |
| 7077 | size_t max_completes = 0; |
| 7078 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7079 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7080 | psa_sign_hash_interruptible_operation_t operation = |
| 7081 | psa_sign_hash_interruptible_operation_init(); |
| 7082 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7083 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7084 | |
| 7085 | PSA_ASSERT(psa_crypto_init()); |
| 7086 | |
| 7087 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 7088 | psa_set_key_algorithm(&attributes, alg); |
| 7089 | psa_set_key_type(&attributes, key_type); |
| 7090 | |
| 7091 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7092 | &key)); |
| 7093 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7094 | psa_interruptible_set_max_ops(max_ops); |
| 7095 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7096 | interruptible_signverify_get_minmax_completes(max_ops, |
| 7097 | expected_complete_status, |
| 7098 | &min_completes, |
| 7099 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7100 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7101 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 7102 | TEST_ASSERT(num_ops_prior == 0); |
| 7103 | |
| 7104 | /* Start performing the signature. */ |
| 7105 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 7106 | input_data->x, input_data->len); |
| 7107 | |
| 7108 | TEST_EQUAL(actual_status, expected_start_status); |
| 7109 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7110 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 7111 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7112 | * start failed. */ |
| 7113 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 7114 | signature_size, |
| 7115 | &signature_length); |
| 7116 | |
| 7117 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7118 | |
| 7119 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7120 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 7121 | input_data->x, input_data->len); |
| 7122 | |
| 7123 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7124 | } |
| 7125 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7126 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 7127 | TEST_ASSERT(num_ops_prior == 0); |
| 7128 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7129 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7130 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7131 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 7132 | signature_size, |
| 7133 | &signature_length); |
| 7134 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7135 | num_completes++; |
| 7136 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7137 | if (actual_status == PSA_SUCCESS || |
| 7138 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7139 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7140 | /* We are asserting here that every complete makes progress |
| 7141 | * (completes some ops), which is true of the internal |
| 7142 | * implementation and probably any implementation, however this is |
| 7143 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7144 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7145 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7146 | num_ops_prior = num_ops; |
| 7147 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7148 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7149 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7150 | TEST_EQUAL(actual_status, expected_complete_status); |
| 7151 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 7152 | /* Check that another complete returns BAD_STATE. */ |
| 7153 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 7154 | signature_size, |
| 7155 | &signature_length); |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7156 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 7157 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7158 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7159 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 7160 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7161 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 7162 | TEST_ASSERT(num_ops == 0); |
| 7163 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7164 | /* The value of *signature_length is unspecified on error, but |
| 7165 | * whatever it is, it should be less than signature_size, so that |
| 7166 | * if the caller tries to read *signature_length bytes without |
| 7167 | * checking the error code then they don't overflow a buffer. */ |
| 7168 | TEST_LE_U(signature_length, signature_size); |
| 7169 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7170 | TEST_LE_U(min_completes, num_completes); |
| 7171 | TEST_LE_U(num_completes, max_completes); |
| 7172 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7173 | exit: |
| 7174 | psa_reset_key_attributes(&attributes); |
| 7175 | psa_destroy_key(key); |
| 7176 | mbedtls_free(signature); |
| 7177 | PSA_DONE(); |
| 7178 | } |
| 7179 | /* END_CASE */ |
| 7180 | |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 7181 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7182 | void sign_verify_hash(int key_type_arg, data_t *key_data, |
| 7183 | int alg_arg, data_t *input_data) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7184 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7185 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7186 | psa_key_type_t key_type = key_type_arg; |
| 7187 | psa_algorithm_t alg = alg_arg; |
| 7188 | size_t key_bits; |
| 7189 | unsigned char *signature = NULL; |
| 7190 | size_t signature_size; |
| 7191 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7192 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7193 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7194 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7195 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7196 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 7197 | psa_set_key_algorithm(&attributes, alg); |
| 7198 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7199 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7200 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7201 | &key)); |
| 7202 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7203 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7204 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 7205 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7206 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7207 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7208 | key_bits, alg); |
| 7209 | TEST_ASSERT(signature_size != 0); |
| 7210 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7211 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7212 | |
| 7213 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7214 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 7215 | input_data->x, input_data->len, |
| 7216 | signature, signature_size, |
| 7217 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7218 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7219 | TEST_LE_U(signature_length, signature_size); |
| 7220 | TEST_ASSERT(signature_length > 0); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7221 | |
| 7222 | /* Use the library to verify that the signature is correct. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7223 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 7224 | input_data->x, input_data->len, |
| 7225 | signature, signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7226 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7227 | if (input_data->len != 0) { |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7228 | /* Flip a bit in the input and verify that the signature is now |
| 7229 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 7230 | * because ECDSA may ignore the last few bits of the input. */ |
| 7231 | input_data->x[0] ^= 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7232 | TEST_EQUAL(psa_verify_hash(key, alg, |
| 7233 | input_data->x, input_data->len, |
| 7234 | signature, signature_length), |
| 7235 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7236 | } |
| 7237 | |
| 7238 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7239 | /* |
| 7240 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7241 | * thus reset them as required. |
| 7242 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7243 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7244 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7245 | psa_destroy_key(key); |
| 7246 | mbedtls_free(signature); |
| 7247 | PSA_DONE(); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7248 | } |
| 7249 | /* END_CASE */ |
| 7250 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7251 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7252 | /** |
| 7253 | * sign_verify_hash_interruptible() test intentions: |
| 7254 | * |
| 7255 | * Note: This test can currently only handle ECDSA. |
| 7256 | * |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 7257 | * 1. Test that we can sign an input hash with the given keypair and then |
| 7258 | * afterwards verify that signature. This is currently the only way to test |
| 7259 | * non deterministic ECDSA, but this test can also handle deterministic. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7260 | * |
| 7261 | * 2. Test that after corrupting the hash, the verification detects an invalid |
| 7262 | * signature. |
| 7263 | * |
| 7264 | * 3. Test the number of calls to psa_sign_hash_complete() required are as |
| 7265 | * expected for different max_ops values. |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7266 | * |
| 7267 | * 4. Test that the number of ops done prior to starting signing and after abort |
| 7268 | * is zero and that each successful signing stage completes some ops (this is |
| 7269 | * not mandated by the PSA specification, but is currently the case). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7270 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7271 | 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] | 7272 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7273 | int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7274 | { |
| 7275 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7276 | psa_key_type_t key_type = key_type_arg; |
| 7277 | psa_algorithm_t alg = alg_arg; |
| 7278 | size_t key_bits; |
| 7279 | unsigned char *signature = NULL; |
| 7280 | size_t signature_size; |
| 7281 | size_t signature_length = 0xdeadbeef; |
| 7282 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7283 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7284 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7285 | uint32_t num_ops = 0; |
| 7286 | uint32_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7287 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7288 | size_t min_completes = 0; |
| 7289 | size_t max_completes = 0; |
| 7290 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7291 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7292 | psa_sign_hash_interruptible_operation_init(); |
| 7293 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7294 | psa_verify_hash_interruptible_operation_init(); |
| 7295 | |
| 7296 | PSA_ASSERT(psa_crypto_init()); |
| 7297 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7298 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7299 | PSA_KEY_USAGE_VERIFY_HASH); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7300 | psa_set_key_algorithm(&attributes, alg); |
| 7301 | psa_set_key_type(&attributes, key_type); |
| 7302 | |
| 7303 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7304 | &key)); |
| 7305 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7306 | key_bits = psa_get_key_bits(&attributes); |
| 7307 | |
| 7308 | /* Allocate a buffer which has the size advertised by the |
| 7309 | * library. */ |
| 7310 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7311 | key_bits, alg); |
| 7312 | TEST_ASSERT(signature_size != 0); |
| 7313 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7314 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7315 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7316 | psa_interruptible_set_max_ops(max_ops); |
| 7317 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7318 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 7319 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7320 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7321 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); |
| 7322 | TEST_ASSERT(num_ops_prior == 0); |
| 7323 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7324 | /* Start performing the signature. */ |
| 7325 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7326 | input_data->x, input_data->len)); |
| 7327 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7328 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); |
| 7329 | TEST_ASSERT(num_ops_prior == 0); |
| 7330 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7331 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7332 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7333 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7334 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7335 | signature_size, |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7336 | &signature_length); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7337 | |
| 7338 | num_completes++; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7339 | |
| 7340 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 7341 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7342 | /* We are asserting here that every complete makes progress |
| 7343 | * (completes some ops), which is true of the internal |
| 7344 | * implementation and probably any implementation, however this is |
| 7345 | * not mandated by the PSA specification. */ |
| 7346 | TEST_ASSERT(num_ops > num_ops_prior); |
| 7347 | |
| 7348 | num_ops_prior = num_ops; |
| 7349 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7350 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7351 | |
| 7352 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7353 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7354 | TEST_LE_U(min_completes, num_completes); |
| 7355 | TEST_LE_U(num_completes, max_completes); |
| 7356 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7357 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7358 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7359 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7360 | TEST_ASSERT(num_ops == 0); |
| 7361 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7362 | /* Check that the signature length looks sensible. */ |
| 7363 | TEST_LE_U(signature_length, signature_size); |
| 7364 | TEST_ASSERT(signature_length > 0); |
| 7365 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7366 | num_completes = 0; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7367 | |
| 7368 | /* Start verification. */ |
| 7369 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7370 | input_data->x, input_data->len, |
| 7371 | signature, signature_length)); |
| 7372 | |
| 7373 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7374 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7375 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7376 | |
| 7377 | num_completes++; |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7378 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7379 | |
| 7380 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7381 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7382 | TEST_LE_U(min_completes, num_completes); |
| 7383 | TEST_LE_U(num_completes, max_completes); |
| 7384 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7385 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7386 | |
| 7387 | verify_operation = psa_verify_hash_interruptible_operation_init(); |
| 7388 | |
| 7389 | if (input_data->len != 0) { |
| 7390 | /* Flip a bit in the input and verify that the signature is now |
| 7391 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 7392 | * because ECDSA may ignore the last few bits of the input. */ |
| 7393 | input_data->x[0] ^= 1; |
| 7394 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7395 | /* Start verification. */ |
| 7396 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7397 | input_data->x, input_data->len, |
| 7398 | signature, signature_length)); |
| 7399 | |
| 7400 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7401 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7402 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7403 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7404 | |
| 7405 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7406 | } |
| 7407 | |
| 7408 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7409 | |
| 7410 | exit: |
| 7411 | /* |
| 7412 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7413 | * thus reset them as required. |
| 7414 | */ |
| 7415 | psa_reset_key_attributes(&attributes); |
| 7416 | |
| 7417 | psa_destroy_key(key); |
| 7418 | mbedtls_free(signature); |
| 7419 | PSA_DONE(); |
| 7420 | } |
| 7421 | /* END_CASE */ |
| 7422 | |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7423 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7424 | void verify_hash(int key_type_arg, data_t *key_data, |
| 7425 | int alg_arg, data_t *hash_data, |
| 7426 | data_t *signature_data) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7427 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7428 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7429 | psa_key_type_t key_type = key_type_arg; |
| 7430 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7431 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7433 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 7434 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7435 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7436 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7437 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7438 | psa_set_key_algorithm(&attributes, alg); |
| 7439 | psa_set_key_type(&attributes, key_type); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7440 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7441 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7442 | &key)); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7444 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 7445 | hash_data->x, hash_data->len, |
| 7446 | signature_data->x, signature_data->len)); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 7447 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7448 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7449 | psa_reset_key_attributes(&attributes); |
| 7450 | psa_destroy_key(key); |
| 7451 | PSA_DONE(); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7452 | } |
| 7453 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7454 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7455 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7456 | /** |
| 7457 | * verify_hash_interruptible() test intentions: |
| 7458 | * |
| 7459 | * Note: This test can currently only handle ECDSA. |
| 7460 | * |
| 7461 | * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 7462 | * only). Given this test only does verification it can accept public keys as |
| 7463 | * well as private keys / keypairs. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7464 | * |
| 7465 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7466 | * expected for different max_ops values. |
| 7467 | * |
| 7468 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7469 | * and that each successful stage completes some ops (this is not mandated by |
| 7470 | * the PSA specification, but is currently the case). |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7471 | * |
| 7472 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 7473 | * complete() calls does not alter the number of ops returned. |
| 7474 | * |
| 7475 | * 5. Test that after corrupting the hash, the verification detects an invalid |
| 7476 | * signature. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7477 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7478 | void verify_hash_interruptible(int key_type_arg, data_t *key_data, |
| 7479 | int alg_arg, data_t *hash_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7480 | data_t *signature_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7481 | { |
| 7482 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7483 | psa_key_type_t key_type = key_type_arg; |
| 7484 | psa_algorithm_t alg = alg_arg; |
| 7485 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7486 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7487 | uint32_t num_ops = 0; |
| 7488 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7489 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7490 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7491 | size_t min_completes = 0; |
| 7492 | size_t max_completes = 0; |
| 7493 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7494 | psa_verify_hash_interruptible_operation_t operation = |
| 7495 | psa_verify_hash_interruptible_operation_init(); |
| 7496 | |
| 7497 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 7498 | |
| 7499 | PSA_ASSERT(psa_crypto_init()); |
| 7500 | |
| 7501 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7502 | psa_set_key_algorithm(&attributes, alg); |
| 7503 | psa_set_key_type(&attributes, key_type); |
| 7504 | |
| 7505 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7506 | &key)); |
| 7507 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7508 | psa_interruptible_set_max_ops(max_ops); |
| 7509 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7510 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 7511 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7512 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7513 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7514 | |
| 7515 | TEST_ASSERT(num_ops_prior == 0); |
| 7516 | |
| 7517 | /* Start verification. */ |
| 7518 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7519 | hash_data->x, hash_data->len, |
| 7520 | signature_data->x, signature_data->len) |
| 7521 | ); |
| 7522 | |
| 7523 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7524 | |
| 7525 | TEST_ASSERT(num_ops_prior == 0); |
| 7526 | |
| 7527 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7528 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7529 | status = psa_verify_hash_complete(&operation); |
| 7530 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7531 | num_completes++; |
| 7532 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7533 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 7534 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7535 | /* We are asserting here that every complete makes progress |
| 7536 | * (completes some ops), which is true of the internal |
| 7537 | * implementation and probably any implementation, however this is |
| 7538 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7539 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7540 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7541 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 7542 | |
| 7543 | /* Ensure calling get_num_ops() twice still returns the same |
| 7544 | * number of ops as previously reported. */ |
| 7545 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7546 | |
| 7547 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7548 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7549 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7550 | |
| 7551 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7552 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7553 | TEST_LE_U(min_completes, num_completes); |
| 7554 | TEST_LE_U(num_completes, max_completes); |
| 7555 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7556 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7557 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7558 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7559 | TEST_ASSERT(num_ops == 0); |
| 7560 | |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7561 | if (hash_data->len != 0) { |
| 7562 | /* Flip a bit in the hash and verify that the signature is now detected |
| 7563 | * as invalid. Flip a bit at the beginning, not at the end, because |
| 7564 | * ECDSA may ignore the last few bits of the input. */ |
| 7565 | hash_data->x[0] ^= 1; |
| 7566 | |
| 7567 | /* Start verification. */ |
| 7568 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7569 | hash_data->x, hash_data->len, |
| 7570 | signature_data->x, signature_data->len)); |
| 7571 | |
| 7572 | /* Continue performing the signature until complete. */ |
| 7573 | do { |
| 7574 | status = psa_verify_hash_complete(&operation); |
| 7575 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7576 | |
| 7577 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7578 | } |
| 7579 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7580 | exit: |
| 7581 | psa_reset_key_attributes(&attributes); |
| 7582 | psa_destroy_key(key); |
| 7583 | PSA_DONE(); |
| 7584 | } |
| 7585 | /* END_CASE */ |
| 7586 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7587 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7588 | void verify_hash_fail(int key_type_arg, data_t *key_data, |
| 7589 | int alg_arg, data_t *hash_data, |
| 7590 | data_t *signature_data, |
| 7591 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7592 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7593 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7594 | psa_key_type_t key_type = key_type_arg; |
| 7595 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7596 | psa_status_t actual_status; |
| 7597 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7598 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7599 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7600 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7601 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7602 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7603 | psa_set_key_algorithm(&attributes, alg); |
| 7604 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7605 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7606 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7607 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7608 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7609 | actual_status = psa_verify_hash(key, alg, |
| 7610 | hash_data->x, hash_data->len, |
| 7611 | signature_data->x, signature_data->len); |
| 7612 | TEST_EQUAL(actual_status, expected_status); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7613 | |
| 7614 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7615 | psa_reset_key_attributes(&attributes); |
| 7616 | psa_destroy_key(key); |
| 7617 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7618 | } |
| 7619 | /* END_CASE */ |
| 7620 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7621 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7622 | /** |
| 7623 | * verify_hash_fail_interruptible() test intentions: |
| 7624 | * |
| 7625 | * Note: This test can currently only handle ECDSA. |
| 7626 | * |
| 7627 | * 1. Test that various failure cases for interruptible verify hash fail with |
| 7628 | * the correct error codes, and at the correct point (at start or during |
| 7629 | * complete). |
| 7630 | * |
| 7631 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7632 | * expected for different max_ops values. |
| 7633 | * |
| 7634 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7635 | * and that each successful stage completes some ops (this is not mandated by |
| 7636 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7637 | * |
| 7638 | * 4. Check that calling complete() when start() fails and complete() |
| 7639 | * after completion results in a BAD_STATE error. |
| 7640 | * |
| 7641 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 7642 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7643 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7644 | void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 7645 | int alg_arg, data_t *hash_data, |
| 7646 | data_t *signature_data, |
| 7647 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7648 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7649 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7650 | { |
| 7651 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7652 | psa_key_type_t key_type = key_type_arg; |
| 7653 | psa_algorithm_t alg = alg_arg; |
| 7654 | psa_status_t actual_status; |
| 7655 | psa_status_t expected_start_status = expected_start_status_arg; |
| 7656 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 7657 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7658 | uint32_t num_ops = 0; |
| 7659 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7660 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7661 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7662 | size_t min_completes = 0; |
| 7663 | size_t max_completes = 0; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7664 | psa_verify_hash_interruptible_operation_t operation = |
| 7665 | psa_verify_hash_interruptible_operation_init(); |
| 7666 | |
| 7667 | PSA_ASSERT(psa_crypto_init()); |
| 7668 | |
| 7669 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7670 | psa_set_key_algorithm(&attributes, alg); |
| 7671 | psa_set_key_type(&attributes, key_type); |
| 7672 | |
| 7673 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7674 | &key)); |
| 7675 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7676 | psa_interruptible_set_max_ops(max_ops); |
| 7677 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7678 | interruptible_signverify_get_minmax_completes(max_ops, |
| 7679 | expected_complete_status, |
| 7680 | &min_completes, |
| 7681 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7682 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7683 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7684 | TEST_ASSERT(num_ops_prior == 0); |
| 7685 | |
| 7686 | /* Start verification. */ |
| 7687 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7688 | hash_data->x, hash_data->len, |
| 7689 | signature_data->x, |
| 7690 | signature_data->len); |
| 7691 | |
| 7692 | TEST_EQUAL(actual_status, expected_start_status); |
| 7693 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7694 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 7695 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7696 | * start failed. */ |
| 7697 | actual_status = psa_verify_hash_complete(&operation); |
| 7698 | |
| 7699 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7700 | |
| 7701 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7702 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7703 | hash_data->x, hash_data->len, |
| 7704 | signature_data->x, |
| 7705 | signature_data->len); |
| 7706 | |
| 7707 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7708 | } |
| 7709 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7710 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7711 | TEST_ASSERT(num_ops_prior == 0); |
| 7712 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7713 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7714 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7715 | actual_status = psa_verify_hash_complete(&operation); |
| 7716 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7717 | num_completes++; |
| 7718 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7719 | if (actual_status == PSA_SUCCESS || |
| 7720 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7721 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7722 | /* We are asserting here that every complete makes progress |
| 7723 | * (completes some ops), which is true of the internal |
| 7724 | * implementation and probably any implementation, however this is |
| 7725 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7726 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7727 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7728 | num_ops_prior = num_ops; |
| 7729 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7730 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7731 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7732 | TEST_EQUAL(actual_status, expected_complete_status); |
| 7733 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 7734 | /* Check that another complete returns BAD_STATE. */ |
| 7735 | actual_status = psa_verify_hash_complete(&operation); |
| 7736 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7737 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7738 | TEST_LE_U(min_completes, num_completes); |
| 7739 | TEST_LE_U(num_completes, max_completes); |
| 7740 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7741 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7742 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7743 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7744 | TEST_ASSERT(num_ops == 0); |
| 7745 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7746 | exit: |
| 7747 | psa_reset_key_attributes(&attributes); |
| 7748 | psa_destroy_key(key); |
| 7749 | PSA_DONE(); |
| 7750 | } |
| 7751 | /* END_CASE */ |
| 7752 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7753 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7754 | /** |
| 7755 | * interruptible_signverify_hash_state_test() test intentions: |
| 7756 | * |
| 7757 | * Note: This test can currently only handle ECDSA. |
| 7758 | * |
| 7759 | * 1. Test that calling the various interruptible sign and verify hash functions |
| 7760 | * in incorrect orders returns BAD_STATE errors. |
| 7761 | */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7762 | void interruptible_signverify_hash_state_test(int key_type_arg, |
| 7763 | data_t *key_data, int alg_arg, data_t *input_data) |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7764 | { |
| 7765 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7766 | psa_key_type_t key_type = key_type_arg; |
| 7767 | psa_algorithm_t alg = alg_arg; |
| 7768 | size_t key_bits; |
| 7769 | unsigned char *signature = NULL; |
| 7770 | size_t signature_size; |
| 7771 | size_t signature_length = 0xdeadbeef; |
| 7772 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7773 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7774 | psa_sign_hash_interruptible_operation_init(); |
| 7775 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7776 | psa_verify_hash_interruptible_operation_init(); |
| 7777 | |
| 7778 | PSA_ASSERT(psa_crypto_init()); |
| 7779 | |
| 7780 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7781 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7782 | psa_set_key_algorithm(&attributes, alg); |
| 7783 | psa_set_key_type(&attributes, key_type); |
| 7784 | |
| 7785 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7786 | &key)); |
| 7787 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7788 | key_bits = psa_get_key_bits(&attributes); |
| 7789 | |
| 7790 | /* Allocate a buffer which has the size advertised by the |
| 7791 | * library. */ |
| 7792 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7793 | key_bits, alg); |
| 7794 | TEST_ASSERT(signature_size != 0); |
| 7795 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7796 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7797 | |
| 7798 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7799 | |
| 7800 | /* --- Attempt completes prior to starts --- */ |
| 7801 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7802 | signature_size, |
| 7803 | &signature_length), |
| 7804 | PSA_ERROR_BAD_STATE); |
| 7805 | |
| 7806 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7807 | |
| 7808 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7809 | PSA_ERROR_BAD_STATE); |
| 7810 | |
| 7811 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7812 | |
| 7813 | /* --- Aborts in all other places. --- */ |
| 7814 | psa_sign_hash_abort(&sign_operation); |
| 7815 | |
| 7816 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7817 | input_data->x, input_data->len)); |
| 7818 | |
| 7819 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7820 | |
| 7821 | psa_interruptible_set_max_ops(1); |
| 7822 | |
| 7823 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7824 | input_data->x, input_data->len)); |
| 7825 | |
| 7826 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7827 | signature_size, |
| 7828 | &signature_length), |
| 7829 | PSA_OPERATION_INCOMPLETE); |
| 7830 | |
| 7831 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7832 | |
| 7833 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7834 | |
| 7835 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7836 | input_data->x, input_data->len)); |
| 7837 | |
| 7838 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7839 | signature_size, |
| 7840 | &signature_length)); |
| 7841 | |
| 7842 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7843 | |
| 7844 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7845 | |
| 7846 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7847 | input_data->x, input_data->len, |
| 7848 | signature, signature_length)); |
| 7849 | |
| 7850 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7851 | |
| 7852 | psa_interruptible_set_max_ops(1); |
| 7853 | |
| 7854 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7855 | input_data->x, input_data->len, |
| 7856 | signature, signature_length)); |
| 7857 | |
| 7858 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7859 | PSA_OPERATION_INCOMPLETE); |
| 7860 | |
| 7861 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7862 | |
| 7863 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7864 | |
| 7865 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7866 | input_data->x, input_data->len, |
| 7867 | signature, signature_length)); |
| 7868 | |
| 7869 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7870 | |
| 7871 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7872 | |
| 7873 | /* --- Attempt double starts. --- */ |
| 7874 | |
| 7875 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7876 | input_data->x, input_data->len)); |
| 7877 | |
| 7878 | TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg, |
| 7879 | input_data->x, input_data->len), |
| 7880 | PSA_ERROR_BAD_STATE); |
| 7881 | |
| 7882 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7883 | |
| 7884 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7885 | input_data->x, input_data->len, |
| 7886 | signature, signature_length)); |
| 7887 | |
| 7888 | TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg, |
| 7889 | input_data->x, input_data->len, |
| 7890 | signature, signature_length), |
| 7891 | PSA_ERROR_BAD_STATE); |
| 7892 | |
| 7893 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7894 | |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7895 | exit: |
| 7896 | /* |
| 7897 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7898 | * thus reset them as required. |
| 7899 | */ |
| 7900 | psa_reset_key_attributes(&attributes); |
| 7901 | |
| 7902 | psa_destroy_key(key); |
| 7903 | mbedtls_free(signature); |
| 7904 | PSA_DONE(); |
| 7905 | } |
| 7906 | /* END_CASE */ |
| 7907 | |
| 7908 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7909 | /** |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7910 | * interruptible_signverify_hash_edgecase_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7911 | * |
| 7912 | * Note: This test can currently only handle ECDSA. |
| 7913 | * |
| 7914 | * 1. Test various edge cases in the interruptible sign and verify hash |
| 7915 | * interfaces. |
| 7916 | */ |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7917 | void interruptible_signverify_hash_edgecase_tests(int key_type_arg, |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7918 | data_t *key_data, int alg_arg, data_t *input_data) |
| 7919 | { |
| 7920 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7921 | psa_key_type_t key_type = key_type_arg; |
| 7922 | psa_algorithm_t alg = alg_arg; |
| 7923 | size_t key_bits; |
| 7924 | unsigned char *signature = NULL; |
| 7925 | size_t signature_size; |
| 7926 | size_t signature_length = 0xdeadbeef; |
| 7927 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7928 | uint8_t *input_buffer = NULL; |
| 7929 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7930 | psa_sign_hash_interruptible_operation_init(); |
| 7931 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7932 | psa_verify_hash_interruptible_operation_init(); |
| 7933 | |
| 7934 | PSA_ASSERT(psa_crypto_init()); |
| 7935 | |
| 7936 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7937 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7938 | psa_set_key_algorithm(&attributes, alg); |
| 7939 | psa_set_key_type(&attributes, key_type); |
| 7940 | |
| 7941 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7942 | &key)); |
| 7943 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7944 | key_bits = psa_get_key_bits(&attributes); |
| 7945 | |
| 7946 | /* Allocate a buffer which has the size advertised by the |
| 7947 | * library. */ |
| 7948 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7949 | key_bits, alg); |
| 7950 | TEST_ASSERT(signature_size != 0); |
| 7951 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7952 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7953 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7954 | /* --- Change function inputs mid run, to cause an error (sign only, |
| 7955 | * verify passes all inputs to start. --- */ |
| 7956 | |
| 7957 | psa_interruptible_set_max_ops(1); |
| 7958 | |
| 7959 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7960 | input_data->x, input_data->len)); |
| 7961 | |
| 7962 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7963 | signature_size, |
| 7964 | &signature_length), |
| 7965 | PSA_OPERATION_INCOMPLETE); |
| 7966 | |
| 7967 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7968 | 0, |
| 7969 | &signature_length), |
| 7970 | PSA_ERROR_BUFFER_TOO_SMALL); |
| 7971 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7972 | /* And test that this invalidates the operation. */ |
| 7973 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7974 | 0, |
| 7975 | &signature_length), |
| 7976 | PSA_ERROR_BAD_STATE); |
| 7977 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7978 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7979 | |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7980 | /* Trash the hash buffer in between start and complete, to ensure |
| 7981 | * no reliance on external buffers. */ |
| 7982 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 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_sign_hash_start(&sign_operation, key, alg, |
| 7989 | input_buffer, input_data->len)); |
| 7990 | |
| 7991 | memset(input_buffer, '!', input_data->len); |
| 7992 | mbedtls_free(input_buffer); |
| 7993 | input_buffer = NULL; |
| 7994 | |
| 7995 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7996 | signature_size, |
| 7997 | &signature_length)); |
| 7998 | |
| 7999 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 8000 | |
Paul Elliott | 6c68df4 | 2023-10-23 15:33:37 +0100 | [diff] [blame] | 8001 | TEST_CALLOC(input_buffer, input_data->len); |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 8002 | |
| 8003 | memcpy(input_buffer, input_data->x, input_data->len); |
| 8004 | |
| 8005 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 8006 | input_buffer, input_data->len, |
| 8007 | signature, signature_length)); |
| 8008 | |
| 8009 | memset(input_buffer, '!', input_data->len); |
| 8010 | mbedtls_free(input_buffer); |
| 8011 | input_buffer = NULL; |
| 8012 | |
| 8013 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 8014 | |
| 8015 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 8016 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 8017 | exit: |
| 8018 | /* |
| 8019 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8020 | * thus reset them as required. |
| 8021 | */ |
| 8022 | psa_reset_key_attributes(&attributes); |
| 8023 | |
| 8024 | psa_destroy_key(key); |
| 8025 | mbedtls_free(signature); |
Paul Elliott | 6c68df4 | 2023-10-23 15:33:37 +0100 | [diff] [blame] | 8026 | mbedtls_free(input_buffer); |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 8027 | PSA_DONE(); |
| 8028 | } |
| 8029 | /* END_CASE */ |
| 8030 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8031 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 8032 | /** |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 8033 | * interruptible_signverify_hash_ops_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 8034 | * |
| 8035 | * Note: This test can currently only handle ECDSA. |
| 8036 | * |
| 8037 | * 1. Test that setting max ops is reflected in both interruptible sign and |
| 8038 | * verify hash |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 8039 | * 2. Test that changing the value of max_ops to unlimited during an operation |
| 8040 | * causes that operation to complete in the next call. |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 8041 | * |
| 8042 | * 3. Test that calling get_num_ops() between complete calls gives the same |
| 8043 | * 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] | 8044 | */ |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 8045 | void interruptible_signverify_hash_ops_tests(int key_type_arg, |
| 8046 | data_t *key_data, int alg_arg, |
| 8047 | data_t *input_data) |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8048 | { |
| 8049 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8050 | psa_key_type_t key_type = key_type_arg; |
| 8051 | psa_algorithm_t alg = alg_arg; |
| 8052 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 8053 | size_t key_bits; |
| 8054 | unsigned char *signature = NULL; |
| 8055 | size_t signature_size; |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 8056 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 8057 | uint32_t num_ops = 0; |
| 8058 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 8059 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8060 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 8061 | psa_sign_hash_interruptible_operation_init(); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 8062 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 8063 | psa_verify_hash_interruptible_operation_init(); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8064 | |
| 8065 | PSA_ASSERT(psa_crypto_init()); |
| 8066 | |
| 8067 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 8068 | PSA_KEY_USAGE_VERIFY_HASH); |
| 8069 | psa_set_key_algorithm(&attributes, alg); |
| 8070 | psa_set_key_type(&attributes, key_type); |
| 8071 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 8072 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key)); |
| 8073 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8074 | key_bits = psa_get_key_bits(&attributes); |
| 8075 | |
| 8076 | /* Allocate a buffer which has the size advertised by the |
| 8077 | * library. */ |
| 8078 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8079 | |
| 8080 | TEST_ASSERT(signature_size != 0); |
| 8081 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8082 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8083 | |
| 8084 | /* Check that default max ops gets set if we don't set it. */ |
| 8085 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 8086 | input_data->x, input_data->len)); |
| 8087 | |
| 8088 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 8089 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 8090 | |
| 8091 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 8092 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 8093 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 8094 | input_data->x, input_data->len, |
| 8095 | signature, signature_size)); |
| 8096 | |
| 8097 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 8098 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 8099 | |
| 8100 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 8101 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8102 | /* Check that max ops gets set properly. */ |
| 8103 | |
| 8104 | psa_interruptible_set_max_ops(0xbeef); |
| 8105 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 8106 | TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8107 | |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 8108 | /* --- Ensure changing the max ops mid operation works (operation should |
| 8109 | * complete successfully after setting max ops to unlimited --- */ |
| 8110 | psa_interruptible_set_max_ops(1); |
| 8111 | |
| 8112 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 8113 | input_data->x, input_data->len)); |
| 8114 | |
| 8115 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 8116 | signature_size, |
| 8117 | &signature_length), |
| 8118 | PSA_OPERATION_INCOMPLETE); |
| 8119 | |
| 8120 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 8121 | |
| 8122 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 8123 | signature_size, |
| 8124 | &signature_length)); |
| 8125 | |
| 8126 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 8127 | |
| 8128 | psa_interruptible_set_max_ops(1); |
| 8129 | |
| 8130 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 8131 | input_data->x, input_data->len, |
| 8132 | signature, signature_length)); |
| 8133 | |
| 8134 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 8135 | PSA_OPERATION_INCOMPLETE); |
| 8136 | |
| 8137 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 8138 | |
| 8139 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 8140 | |
| 8141 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 8142 | |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 8143 | /* --- Test that not calling get_num_ops inbetween complete calls does not |
| 8144 | * result in lost ops. ---*/ |
| 8145 | |
| 8146 | psa_interruptible_set_max_ops(1); |
| 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 | |
| 8157 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 8158 | |
| 8159 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 8160 | |
| 8161 | PSA_ASSERT(status); |
| 8162 | |
| 8163 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 8164 | |
| 8165 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 8166 | input_data->x, input_data->len)); |
| 8167 | |
| 8168 | /* Continue performing the signature until complete. */ |
| 8169 | do { |
| 8170 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 8171 | signature_size, |
| 8172 | &signature_length); |
| 8173 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 8174 | |
| 8175 | PSA_ASSERT(status); |
| 8176 | |
| 8177 | TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation)); |
| 8178 | |
| 8179 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 8180 | |
| 8181 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 8182 | input_data->x, input_data->len, |
| 8183 | signature, signature_length)); |
| 8184 | |
| 8185 | /* Continue performing the verification until complete. */ |
| 8186 | do { |
| 8187 | status = psa_verify_hash_complete(&verify_operation); |
| 8188 | |
| 8189 | num_ops = psa_verify_hash_get_num_ops(&verify_operation); |
| 8190 | |
| 8191 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 8192 | |
| 8193 | PSA_ASSERT(status); |
| 8194 | |
| 8195 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 8196 | |
| 8197 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 8198 | input_data->x, input_data->len, |
| 8199 | signature, signature_length)); |
| 8200 | |
| 8201 | /* Continue performing the verification until complete. */ |
| 8202 | do { |
| 8203 | status = psa_verify_hash_complete(&verify_operation); |
| 8204 | |
| 8205 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 8206 | |
| 8207 | PSA_ASSERT(status); |
| 8208 | |
| 8209 | TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation)); |
| 8210 | |
| 8211 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 8212 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8213 | exit: |
| 8214 | /* |
| 8215 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8216 | * thus reset them as required. |
| 8217 | */ |
| 8218 | psa_reset_key_attributes(&attributes); |
| 8219 | |
| 8220 | psa_destroy_key(key); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 8221 | mbedtls_free(signature); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8222 | PSA_DONE(); |
| 8223 | } |
| 8224 | /* END_CASE */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 8225 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8226 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8227 | void sign_message_deterministic(int key_type_arg, |
| 8228 | data_t *key_data, |
| 8229 | int alg_arg, |
| 8230 | data_t *input_data, |
| 8231 | data_t *output_data) |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8232 | { |
| 8233 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8234 | psa_key_type_t key_type = key_type_arg; |
| 8235 | psa_algorithm_t alg = alg_arg; |
| 8236 | size_t key_bits; |
| 8237 | unsigned char *signature = NULL; |
| 8238 | size_t signature_size; |
| 8239 | size_t signature_length = 0xdeadbeef; |
| 8240 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8241 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8242 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8243 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8244 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 8245 | psa_set_key_algorithm(&attributes, alg); |
| 8246 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8247 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8248 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8249 | &key)); |
| 8250 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8251 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8252 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8253 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8254 | TEST_ASSERT(signature_size != 0); |
| 8255 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8256 | TEST_CALLOC(signature, signature_size); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8257 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8258 | PSA_ASSERT(psa_sign_message(key, alg, |
| 8259 | input_data->x, input_data->len, |
| 8260 | signature, signature_size, |
| 8261 | &signature_length)); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8262 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8263 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8264 | signature, signature_length); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8265 | |
| 8266 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8267 | psa_reset_key_attributes(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8268 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8269 | psa_destroy_key(key); |
| 8270 | mbedtls_free(signature); |
| 8271 | PSA_DONE(); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8272 | |
| 8273 | } |
| 8274 | /* END_CASE */ |
| 8275 | |
| 8276 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8277 | void sign_message_fail(int key_type_arg, |
| 8278 | data_t *key_data, |
| 8279 | int alg_arg, |
| 8280 | data_t *input_data, |
| 8281 | int signature_size_arg, |
| 8282 | int expected_status_arg) |
| 8283 | { |
| 8284 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8285 | psa_key_type_t key_type = key_type_arg; |
| 8286 | psa_algorithm_t alg = alg_arg; |
| 8287 | size_t signature_size = signature_size_arg; |
| 8288 | psa_status_t actual_status; |
| 8289 | psa_status_t expected_status = expected_status_arg; |
| 8290 | unsigned char *signature = NULL; |
| 8291 | size_t signature_length = 0xdeadbeef; |
| 8292 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8293 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8294 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8295 | |
| 8296 | PSA_ASSERT(psa_crypto_init()); |
| 8297 | |
| 8298 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 8299 | psa_set_key_algorithm(&attributes, alg); |
| 8300 | psa_set_key_type(&attributes, key_type); |
| 8301 | |
| 8302 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8303 | &key)); |
| 8304 | |
| 8305 | actual_status = psa_sign_message(key, alg, |
| 8306 | input_data->x, input_data->len, |
| 8307 | signature, signature_size, |
| 8308 | &signature_length); |
| 8309 | TEST_EQUAL(actual_status, expected_status); |
| 8310 | /* The value of *signature_length is unspecified on error, but |
| 8311 | * whatever it is, it should be less than signature_size, so that |
| 8312 | * if the caller tries to read *signature_length bytes without |
| 8313 | * checking the error code then they don't overflow a buffer. */ |
| 8314 | TEST_LE_U(signature_length, signature_size); |
| 8315 | |
| 8316 | exit: |
| 8317 | psa_reset_key_attributes(&attributes); |
| 8318 | psa_destroy_key(key); |
| 8319 | mbedtls_free(signature); |
| 8320 | PSA_DONE(); |
| 8321 | } |
| 8322 | /* END_CASE */ |
| 8323 | |
| 8324 | /* BEGIN_CASE */ |
| 8325 | void sign_verify_message(int key_type_arg, |
| 8326 | data_t *key_data, |
| 8327 | int alg_arg, |
| 8328 | data_t *input_data) |
| 8329 | { |
| 8330 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8331 | psa_key_type_t key_type = key_type_arg; |
| 8332 | psa_algorithm_t alg = alg_arg; |
| 8333 | size_t key_bits; |
| 8334 | unsigned char *signature = NULL; |
| 8335 | size_t signature_size; |
| 8336 | size_t signature_length = 0xdeadbeef; |
| 8337 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8338 | |
| 8339 | PSA_ASSERT(psa_crypto_init()); |
| 8340 | |
| 8341 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 8342 | PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8343 | psa_set_key_algorithm(&attributes, alg); |
| 8344 | psa_set_key_type(&attributes, key_type); |
| 8345 | |
| 8346 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8347 | &key)); |
| 8348 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8349 | key_bits = psa_get_key_bits(&attributes); |
| 8350 | |
| 8351 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8352 | TEST_ASSERT(signature_size != 0); |
| 8353 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8354 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8355 | |
| 8356 | PSA_ASSERT(psa_sign_message(key, alg, |
| 8357 | input_data->x, input_data->len, |
| 8358 | signature, signature_size, |
| 8359 | &signature_length)); |
| 8360 | TEST_LE_U(signature_length, signature_size); |
| 8361 | TEST_ASSERT(signature_length > 0); |
| 8362 | |
| 8363 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8364 | input_data->x, input_data->len, |
| 8365 | signature, signature_length)); |
| 8366 | |
| 8367 | if (input_data->len != 0) { |
| 8368 | /* Flip a bit in the input and verify that the signature is now |
| 8369 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 8370 | * because ECDSA may ignore the last few bits of the input. */ |
| 8371 | input_data->x[0] ^= 1; |
| 8372 | TEST_EQUAL(psa_verify_message(key, alg, |
| 8373 | input_data->x, input_data->len, |
| 8374 | signature, signature_length), |
| 8375 | PSA_ERROR_INVALID_SIGNATURE); |
| 8376 | } |
| 8377 | |
| 8378 | exit: |
| 8379 | psa_reset_key_attributes(&attributes); |
| 8380 | |
| 8381 | psa_destroy_key(key); |
| 8382 | mbedtls_free(signature); |
| 8383 | PSA_DONE(); |
| 8384 | } |
| 8385 | /* END_CASE */ |
| 8386 | |
| 8387 | /* BEGIN_CASE */ |
| 8388 | void verify_message(int key_type_arg, |
| 8389 | data_t *key_data, |
| 8390 | int alg_arg, |
| 8391 | data_t *input_data, |
| 8392 | data_t *signature_data) |
| 8393 | { |
| 8394 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8395 | psa_key_type_t key_type = key_type_arg; |
| 8396 | psa_algorithm_t alg = alg_arg; |
| 8397 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8398 | |
| 8399 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 8400 | |
| 8401 | PSA_ASSERT(psa_crypto_init()); |
| 8402 | |
| 8403 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8404 | psa_set_key_algorithm(&attributes, alg); |
| 8405 | psa_set_key_type(&attributes, key_type); |
| 8406 | |
| 8407 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8408 | &key)); |
| 8409 | |
| 8410 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8411 | input_data->x, input_data->len, |
| 8412 | signature_data->x, signature_data->len)); |
| 8413 | |
| 8414 | exit: |
| 8415 | psa_reset_key_attributes(&attributes); |
| 8416 | psa_destroy_key(key); |
| 8417 | PSA_DONE(); |
| 8418 | } |
| 8419 | /* END_CASE */ |
| 8420 | |
| 8421 | /* BEGIN_CASE */ |
| 8422 | void verify_message_fail(int key_type_arg, |
| 8423 | data_t *key_data, |
| 8424 | int alg_arg, |
| 8425 | data_t *hash_data, |
| 8426 | data_t *signature_data, |
| 8427 | int expected_status_arg) |
| 8428 | { |
| 8429 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8430 | psa_key_type_t key_type = key_type_arg; |
| 8431 | psa_algorithm_t alg = alg_arg; |
| 8432 | psa_status_t actual_status; |
| 8433 | psa_status_t expected_status = expected_status_arg; |
| 8434 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8435 | |
| 8436 | PSA_ASSERT(psa_crypto_init()); |
| 8437 | |
| 8438 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8439 | psa_set_key_algorithm(&attributes, alg); |
| 8440 | psa_set_key_type(&attributes, key_type); |
| 8441 | |
| 8442 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8443 | &key)); |
| 8444 | |
| 8445 | actual_status = psa_verify_message(key, alg, |
| 8446 | hash_data->x, hash_data->len, |
| 8447 | signature_data->x, |
| 8448 | signature_data->len); |
| 8449 | TEST_EQUAL(actual_status, expected_status); |
| 8450 | |
| 8451 | exit: |
| 8452 | psa_reset_key_attributes(&attributes); |
| 8453 | psa_destroy_key(key); |
| 8454 | PSA_DONE(); |
| 8455 | } |
| 8456 | /* END_CASE */ |
| 8457 | |
| 8458 | /* BEGIN_CASE */ |
| 8459 | void asymmetric_encrypt(int key_type_arg, |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8460 | data_t *key_data, |
| 8461 | int alg_arg, |
| 8462 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8463 | data_t *label, |
| 8464 | int expected_output_length_arg, |
| 8465 | int expected_status_arg) |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8466 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8467 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8468 | psa_key_type_t key_type = key_type_arg; |
| 8469 | psa_algorithm_t alg = alg_arg; |
| 8470 | size_t expected_output_length = expected_output_length_arg; |
| 8471 | size_t key_bits; |
| 8472 | unsigned char *output = NULL; |
| 8473 | size_t output_size; |
| 8474 | size_t output_length = ~0; |
| 8475 | psa_status_t actual_status; |
| 8476 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8477 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8478 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8479 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 8480 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8481 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8482 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 8483 | psa_set_key_algorithm(&attributes, alg); |
| 8484 | psa_set_key_type(&attributes, key_type); |
| 8485 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8486 | &key)); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8487 | |
| 8488 | /* Determine the maximum output length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8489 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8490 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8491 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8492 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8493 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8494 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8495 | |
| 8496 | /* Encrypt the input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8497 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8498 | input_data->x, input_data->len, |
| 8499 | label->x, label->len, |
| 8500 | output, output_size, |
| 8501 | &output_length); |
| 8502 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8503 | if (actual_status == PSA_SUCCESS) { |
| 8504 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8505 | } else { |
| 8506 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8507 | } |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8508 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8509 | /* If the label is empty, the test framework puts a non-null pointer |
| 8510 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8511 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8512 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8513 | if (output_size != 0) { |
| 8514 | memset(output, 0, output_size); |
| 8515 | } |
| 8516 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8517 | input_data->x, input_data->len, |
| 8518 | NULL, label->len, |
| 8519 | output, output_size, |
| 8520 | &output_length); |
| 8521 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8522 | if (actual_status == PSA_SUCCESS) { |
| 8523 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8524 | } else { |
| 8525 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8526 | } |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8527 | } |
| 8528 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8529 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8530 | /* |
| 8531 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8532 | * thus reset them as required. |
| 8533 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8534 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8535 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8536 | psa_destroy_key(key); |
| 8537 | mbedtls_free(output); |
| 8538 | PSA_DONE(); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8539 | } |
| 8540 | /* END_CASE */ |
| 8541 | |
| 8542 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8543 | void asymmetric_encrypt_decrypt(int key_type_arg, |
| 8544 | data_t *key_data, |
| 8545 | int alg_arg, |
| 8546 | data_t *input_data, |
| 8547 | data_t *label) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8548 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8549 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8550 | psa_key_type_t key_type = key_type_arg; |
| 8551 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8552 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8553 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8554 | size_t output_size; |
| 8555 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8556 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8557 | size_t output2_size; |
| 8558 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8559 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8560 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8561 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8562 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8563 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 8564 | psa_set_key_algorithm(&attributes, alg); |
| 8565 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8566 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8567 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8568 | &key)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8569 | |
| 8570 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8571 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8572 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8573 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8574 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8575 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8576 | TEST_CALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8577 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8578 | output2_size = input_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8579 | TEST_LE_U(output2_size, |
| 8580 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)); |
| 8581 | TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8582 | TEST_CALLOC(output2, output2_size); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8583 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 8584 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 8585 | * the original plaintext because of the non-optional random |
| 8586 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8587 | PSA_ASSERT(psa_asymmetric_encrypt(key, alg, |
| 8588 | input_data->x, input_data->len, |
| 8589 | label->x, label->len, |
| 8590 | output, output_size, |
| 8591 | &output_length)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8592 | /* We don't know what ciphertext length to expect, but check that |
| 8593 | * it looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8594 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8595 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8596 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8597 | output, output_length, |
| 8598 | label->x, label->len, |
| 8599 | output2, output2_size, |
| 8600 | &output2_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8601 | TEST_MEMORY_COMPARE(input_data->x, input_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8602 | output2, output2_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8603 | |
| 8604 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8605 | /* |
| 8606 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8607 | * thus reset them as required. |
| 8608 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8609 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8610 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8611 | psa_destroy_key(key); |
| 8612 | mbedtls_free(output); |
| 8613 | mbedtls_free(output2); |
| 8614 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8615 | } |
| 8616 | /* END_CASE */ |
| 8617 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8618 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8619 | void asymmetric_decrypt(int key_type_arg, |
| 8620 | data_t *key_data, |
| 8621 | int alg_arg, |
| 8622 | data_t *input_data, |
| 8623 | data_t *label, |
| 8624 | data_t *expected_data) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8625 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8626 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8627 | psa_key_type_t key_type = key_type_arg; |
| 8628 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8629 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8630 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 8631 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8632 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8633 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8634 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8635 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8636 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8637 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8638 | psa_set_key_algorithm(&attributes, alg); |
| 8639 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8640 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8641 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8642 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8643 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8644 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8645 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8646 | |
| 8647 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8648 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8649 | TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8650 | TEST_CALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8651 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8652 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8653 | input_data->x, input_data->len, |
| 8654 | label->x, label->len, |
| 8655 | output, |
| 8656 | output_size, |
| 8657 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8658 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8659 | output, output_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8660 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8661 | /* If the label is empty, the test framework puts a non-null pointer |
| 8662 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8663 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8664 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8665 | if (output_size != 0) { |
| 8666 | memset(output, 0, output_size); |
| 8667 | } |
| 8668 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8669 | input_data->x, input_data->len, |
| 8670 | NULL, label->len, |
| 8671 | output, |
| 8672 | output_size, |
| 8673 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8674 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8675 | output, output_length); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8676 | } |
| 8677 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8678 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8679 | psa_reset_key_attributes(&attributes); |
| 8680 | psa_destroy_key(key); |
| 8681 | mbedtls_free(output); |
| 8682 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8683 | } |
| 8684 | /* END_CASE */ |
| 8685 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8686 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8687 | void asymmetric_decrypt_fail(int key_type_arg, |
| 8688 | data_t *key_data, |
| 8689 | int alg_arg, |
| 8690 | data_t *input_data, |
| 8691 | data_t *label, |
| 8692 | int output_size_arg, |
| 8693 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8694 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8695 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8696 | psa_key_type_t key_type = key_type_arg; |
| 8697 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8698 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 8699 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8700 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8701 | psa_status_t actual_status; |
| 8702 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8703 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8704 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8705 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8706 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8707 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8708 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8709 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8710 | psa_set_key_algorithm(&attributes, alg); |
| 8711 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8712 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8713 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8714 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8715 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8716 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8717 | input_data->x, input_data->len, |
| 8718 | label->x, label->len, |
| 8719 | output, output_size, |
| 8720 | &output_length); |
| 8721 | TEST_EQUAL(actual_status, expected_status); |
| 8722 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8723 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8724 | /* If the label is empty, the test framework puts a non-null pointer |
| 8725 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8726 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8727 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8728 | if (output_size != 0) { |
| 8729 | memset(output, 0, output_size); |
| 8730 | } |
| 8731 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8732 | input_data->x, input_data->len, |
| 8733 | NULL, label->len, |
| 8734 | output, output_size, |
| 8735 | &output_length); |
| 8736 | TEST_EQUAL(actual_status, expected_status); |
| 8737 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8738 | } |
| 8739 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8740 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8741 | psa_reset_key_attributes(&attributes); |
| 8742 | psa_destroy_key(key); |
| 8743 | mbedtls_free(output); |
| 8744 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8745 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8746 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8747 | |
| 8748 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8749 | void key_derivation_init() |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8750 | { |
| 8751 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 8752 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 8753 | * 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] | 8754 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8755 | size_t capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8756 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init(); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 8757 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8758 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8759 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8760 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8761 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8762 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8763 | TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity), |
| 8764 | PSA_ERROR_BAD_STATE); |
| 8765 | TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity), |
| 8766 | PSA_ERROR_BAD_STATE); |
| 8767 | TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity), |
| 8768 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8769 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8770 | /* A default operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8771 | PSA_ASSERT(psa_key_derivation_abort(&func)); |
| 8772 | PSA_ASSERT(psa_key_derivation_abort(&init)); |
| 8773 | PSA_ASSERT(psa_key_derivation_abort(&zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8774 | } |
| 8775 | /* END_CASE */ |
| 8776 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 8777 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8778 | void derive_setup(int alg_arg, int expected_status_arg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8779 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8780 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8781 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8782 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8783 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8784 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8785 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8786 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8787 | expected_status); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8788 | |
| 8789 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8790 | psa_key_derivation_abort(&operation); |
| 8791 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8792 | } |
| 8793 | /* END_CASE */ |
| 8794 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8795 | /* BEGIN_CASE */ |
Kusumit Ghoderao | 9ffd397 | 2023-12-01 16:40:13 +0530 | [diff] [blame] | 8796 | void derive_set_capacity(int alg_arg, int64_t capacity_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8797 | int expected_status_arg) |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8798 | { |
| 8799 | psa_algorithm_t alg = alg_arg; |
| 8800 | size_t capacity = capacity_arg; |
| 8801 | psa_status_t expected_status = expected_status_arg; |
| 8802 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8803 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8804 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8805 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8806 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8807 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8808 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 8809 | expected_status); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8810 | |
| 8811 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8812 | psa_key_derivation_abort(&operation); |
| 8813 | PSA_DONE(); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8814 | } |
| 8815 | /* END_CASE */ |
| 8816 | |
| 8817 | /* BEGIN_CASE */ |
Kusumit Ghoderao | d60dfc0 | 2023-05-01 17:39:27 +0530 | [diff] [blame] | 8818 | void parse_binary_string_test(data_t *input, int output) |
| 8819 | { |
| 8820 | uint64_t value; |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 8821 | value = mbedtls_test_parse_binary_string(input); |
Kusumit Ghoderao | d60dfc0 | 2023-05-01 17:39:27 +0530 | [diff] [blame] | 8822 | TEST_EQUAL(value, output); |
| 8823 | } |
| 8824 | /* END_CASE */ |
| 8825 | |
| 8826 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8827 | void derive_input(int alg_arg, |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8828 | int step_arg1, int key_type_arg1, data_t *input1, |
| 8829 | int expected_status_arg1, |
| 8830 | int step_arg2, int key_type_arg2, data_t *input2, |
| 8831 | int expected_status_arg2, |
| 8832 | int step_arg3, int key_type_arg3, data_t *input3, |
| 8833 | int expected_status_arg3, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8834 | int output_key_type_arg, int expected_output_status_arg) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8835 | { |
| 8836 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8837 | psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 }; |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8838 | 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] | 8839 | psa_status_t expected_statuses[] = { expected_status_arg1, |
| 8840 | expected_status_arg2, |
| 8841 | expected_status_arg3 }; |
| 8842 | data_t *inputs[] = { input1, input2, input3 }; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8843 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8844 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8845 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8846 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8847 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8848 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8849 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8850 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8851 | psa_status_t expected_output_status = expected_output_status_arg; |
| 8852 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8853 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8854 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8855 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8856 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8857 | psa_set_key_algorithm(&attributes, alg); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8858 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8859 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8860 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8861 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8862 | mbedtls_test_set_step(i); |
| 8863 | if (steps[i] == 0) { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 8864 | /* Skip this step */ |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8865 | } else if (((psa_key_type_t) key_types[i]) != PSA_KEY_TYPE_NONE && |
| 8866 | key_types[i] != INPUT_INTEGER) { |
| 8867 | psa_set_key_type(&attributes, ((psa_key_type_t) key_types[i])); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8868 | PSA_ASSERT(psa_import_key(&attributes, |
| 8869 | inputs[i]->x, inputs[i]->len, |
| 8870 | &keys[i])); |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8871 | 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] | 8872 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8873 | // When taking a private key as secret input, use key agreement |
| 8874 | // to add the shared secret to the derivation |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8875 | TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self( |
Ryan Everett | 73e4ea3 | 2024-03-12 16:29:55 +0000 | [diff] [blame] | 8876 | &operation, keys[i], 0), |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8877 | expected_statuses[i]); |
| 8878 | } else { |
| 8879 | TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i], |
| 8880 | keys[i]), |
| 8881 | expected_statuses[i]); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8882 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8883 | } else { |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8884 | if (key_types[i] == INPUT_INTEGER) { |
| 8885 | TEST_EQUAL(psa_key_derivation_input_integer( |
| 8886 | &operation, steps[i], |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 8887 | mbedtls_test_parse_binary_string(inputs[i])), |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8888 | expected_statuses[i]); |
| 8889 | } else { |
Kusumit Ghoderao | 02326d5 | 2023-04-06 17:47:59 +0530 | [diff] [blame] | 8890 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8891 | &operation, steps[i], |
| 8892 | inputs[i]->x, inputs[i]->len), |
| 8893 | expected_statuses[i]); |
Kusumit Ghoderao | 02326d5 | 2023-04-06 17:47:59 +0530 | [diff] [blame] | 8894 | } |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8895 | } |
| 8896 | } |
| 8897 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8898 | if (output_key_type != PSA_KEY_TYPE_NONE) { |
| 8899 | psa_reset_key_attributes(&attributes); |
| 8900 | psa_set_key_type(&attributes, output_key_type); |
| 8901 | psa_set_key_bits(&attributes, 8); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8902 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8903 | psa_key_derivation_output_key(&attributes, &operation, |
| 8904 | &output_key); |
| 8905 | } else { |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8906 | uint8_t buffer[1]; |
| 8907 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8908 | psa_key_derivation_output_bytes(&operation, |
| 8909 | buffer, sizeof(buffer)); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8910 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8911 | TEST_EQUAL(actual_output_status, expected_output_status); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8912 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8913 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8914 | psa_key_derivation_abort(&operation); |
| 8915 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8916 | psa_destroy_key(keys[i]); |
| 8917 | } |
| 8918 | psa_destroy_key(output_key); |
| 8919 | PSA_DONE(); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8920 | } |
| 8921 | /* END_CASE */ |
| 8922 | |
Kusumit Ghoderao | 42b02b9 | 2023-06-06 16:48:46 +0530 | [diff] [blame] | 8923 | /* BEGIN_CASE*/ |
| 8924 | void derive_input_invalid_cost(int alg_arg, int64_t cost) |
| 8925 | { |
| 8926 | psa_algorithm_t alg = alg_arg; |
| 8927 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8928 | |
| 8929 | PSA_ASSERT(psa_crypto_init()); |
| 8930 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8931 | |
| 8932 | TEST_EQUAL(psa_key_derivation_input_integer(&operation, |
| 8933 | PSA_KEY_DERIVATION_INPUT_COST, |
| 8934 | cost), |
| 8935 | PSA_ERROR_NOT_SUPPORTED); |
| 8936 | |
| 8937 | exit: |
| 8938 | psa_key_derivation_abort(&operation); |
| 8939 | PSA_DONE(); |
| 8940 | } |
| 8941 | /* END_CASE*/ |
| 8942 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8943 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8944 | void derive_over_capacity(int alg_arg) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8945 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8946 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8947 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 8948 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8949 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8950 | unsigned char input1[] = "Input 1"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8951 | size_t input1_length = sizeof(input1); |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8952 | unsigned char input2[] = "Input 2"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8953 | size_t input2_length = sizeof(input2); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8954 | uint8_t buffer[42]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8955 | size_t capacity = sizeof(buffer); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 8956 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 8957 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8958 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8959 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8960 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8961 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8962 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8963 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8964 | psa_set_key_algorithm(&attributes, alg); |
| 8965 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8966 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8967 | PSA_ASSERT(psa_import_key(&attributes, |
| 8968 | key_data, sizeof(key_data), |
| 8969 | &key)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8970 | |
| 8971 | /* valid key derivation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8972 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8973 | input1, input1_length, |
| 8974 | input2, input2_length, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 8975 | capacity, 0)) { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8976 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8977 | } |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8978 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8979 | /* state of operation shouldn't allow additional generation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8980 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8981 | PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8982 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8983 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8984 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8985 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity), |
| 8986 | PSA_ERROR_INSUFFICIENT_DATA); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8987 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8988 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8989 | psa_key_derivation_abort(&operation); |
| 8990 | psa_destroy_key(key); |
| 8991 | PSA_DONE(); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8992 | } |
| 8993 | /* END_CASE */ |
| 8994 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8995 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8996 | void derive_actions_without_setup() |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8997 | { |
| 8998 | uint8_t output_buffer[16]; |
| 8999 | size_t buffer_size = 16; |
| 9000 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9001 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 9002 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9003 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9004 | output_buffer, buffer_size) |
| 9005 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 9006 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9007 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 9008 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 9009 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9010 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 9011 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9012 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9013 | output_buffer, buffer_size) |
| 9014 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 9015 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9016 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 9017 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 9018 | |
| 9019 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9020 | psa_key_derivation_abort(&operation); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 9021 | } |
| 9022 | /* END_CASE */ |
| 9023 | |
| 9024 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9025 | void derive_output(int alg_arg, |
| 9026 | int step1_arg, data_t *input1, int expected_status_arg1, |
| 9027 | int step2_arg, data_t *input2, int expected_status_arg2, |
| 9028 | int step3_arg, data_t *input3, int expected_status_arg3, |
| 9029 | int step4_arg, data_t *input4, int expected_status_arg4, |
| 9030 | data_t *key_agreement_peer_key, |
| 9031 | int requested_capacity_arg, |
| 9032 | data_t *expected_output1, |
| 9033 | data_t *expected_output2, |
| 9034 | int other_key_input_type, |
| 9035 | int key_input_type, |
| 9036 | int derive_type) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9037 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9038 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9039 | psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg }; |
| 9040 | data_t *inputs[] = { input1, input2, input3, input4 }; |
| 9041 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 9042 | MBEDTLS_SVC_KEY_ID_INIT, |
| 9043 | MBEDTLS_SVC_KEY_ID_INIT, |
| 9044 | MBEDTLS_SVC_KEY_ID_INIT }; |
| 9045 | psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2, |
| 9046 | expected_status_arg3, expected_status_arg4 }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9047 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9048 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9049 | uint8_t *expected_outputs[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9050 | { expected_output1->x, expected_output2->x }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9051 | size_t output_sizes[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9052 | { expected_output1->len, expected_output2->len }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9053 | size_t output_buffer_size = 0; |
| 9054 | uint8_t *output_buffer = NULL; |
| 9055 | size_t expected_capacity; |
| 9056 | size_t current_capacity; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9057 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; |
| 9058 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; |
| 9059 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; |
| 9060 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 9061 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9062 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 9063 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9064 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9065 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
| 9066 | if (output_sizes[i] > output_buffer_size) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9067 | output_buffer_size = output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9068 | } |
| 9069 | if (output_sizes[i] == 0) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9070 | expected_outputs[i] = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9071 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9072 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9073 | TEST_CALLOC(output_buffer, output_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9074 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9075 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9076 | /* Extraction phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9077 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9078 | PSA_ASSERT(psa_key_derivation_set_capacity(&operation, |
| 9079 | requested_capacity)); |
| 9080 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 9081 | switch (steps[i]) { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 9082 | case 0: |
| 9083 | break; |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 9084 | case PSA_KEY_DERIVATION_INPUT_COST: |
| 9085 | TEST_EQUAL(psa_key_derivation_input_integer( |
Kusumit Ghoderao | f28e0f5 | 2023-06-06 15:03:22 +0530 | [diff] [blame] | 9086 | &operation, steps[i], |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 9087 | mbedtls_test_parse_binary_string(inputs[i])), |
Kusumit Ghoderao | f28e0f5 | 2023-06-06 15:03:22 +0530 | [diff] [blame] | 9088 | statuses[i]); |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 9089 | if (statuses[i] != PSA_SUCCESS) { |
| 9090 | goto exit; |
| 9091 | } |
| 9092 | break; |
| 9093 | case PSA_KEY_DERIVATION_INPUT_PASSWORD: |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 9094 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9095 | switch (key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9096 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9097 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 9098 | &operation, steps[i], |
| 9099 | inputs[i]->x, inputs[i]->len), |
| 9100 | statuses[i]); |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 9101 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9102 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 9103 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9104 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9105 | break; |
| 9106 | case 1: // input key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9107 | psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE); |
| 9108 | psa_set_key_algorithm(&attributes1, alg); |
| 9109 | psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9110 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9111 | PSA_ASSERT(psa_import_key(&attributes1, |
| 9112 | inputs[i]->x, inputs[i]->len, |
| 9113 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9114 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9115 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 9116 | PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1)); |
| 9117 | TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)), |
| 9118 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9119 | } |
| 9120 | |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 9121 | TEST_EQUAL(psa_key_derivation_input_key(&operation, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9122 | steps[i], |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 9123 | keys[i]), |
| 9124 | statuses[i]); |
| 9125 | |
| 9126 | if (statuses[i] != PSA_SUCCESS) { |
| 9127 | goto exit; |
| 9128 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9129 | break; |
| 9130 | default: |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 9131 | TEST_FAIL("default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9132 | break; |
| 9133 | } |
| 9134 | break; |
| 9135 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9136 | switch (other_key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9137 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9138 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 9139 | steps[i], |
| 9140 | inputs[i]->x, |
| 9141 | inputs[i]->len), |
| 9142 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9143 | break; |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 9144 | case 1: // input key, type DERIVE |
| 9145 | case 11: // input key, type RAW |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9146 | psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE); |
| 9147 | psa_set_key_algorithm(&attributes2, alg); |
| 9148 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9149 | |
| 9150 | // other secret of type RAW_DATA passed with input_key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9151 | if (other_key_input_type == 11) { |
| 9152 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA); |
| 9153 | } |
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(&attributes2, |
| 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_input_key(&operation, |
| 9160 | steps[i], |
| 9161 | keys[i]), |
| 9162 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9163 | break; |
| 9164 | case 2: // key agreement |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9165 | psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE); |
| 9166 | psa_set_key_algorithm(&attributes3, alg); |
| 9167 | psa_set_key_type(&attributes3, |
| 9168 | PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9169 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9170 | PSA_ASSERT(psa_import_key(&attributes3, |
| 9171 | inputs[i]->x, inputs[i]->len, |
| 9172 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9173 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9174 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 9175 | &operation, |
| 9176 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 9177 | keys[i], key_agreement_peer_key->x, |
| 9178 | key_agreement_peer_key->len), statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9179 | break; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9180 | default: |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 9181 | TEST_FAIL("default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9182 | break; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9183 | } |
| 9184 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9185 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9186 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9187 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 9188 | break; |
| 9189 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9190 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 9191 | &operation, steps[i], |
| 9192 | inputs[i]->x, inputs[i]->len), statuses[i]); |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 9193 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9194 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 9195 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9196 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 9197 | break; |
| 9198 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 9199 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 9200 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9201 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 9202 | ¤t_capacity)); |
| 9203 | TEST_EQUAL(current_capacity, requested_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9204 | expected_capacity = requested_capacity; |
| 9205 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9206 | if (derive_type == 1) { // output key |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 9207 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; |
| 9208 | |
| 9209 | /* For output key derivation secret must be provided using |
| 9210 | input key, otherwise operation is not permitted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9211 | if (key_input_type == 1) { |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 9212 | expected_status = PSA_SUCCESS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9213 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9215 | psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT); |
| 9216 | psa_set_key_algorithm(&attributes4, alg); |
| 9217 | psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE); |
| 9218 | psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9219 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9220 | TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation, |
| 9221 | &derived_key), expected_status); |
| 9222 | } else { // output bytes |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9223 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9224 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9225 | /* Read some bytes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9226 | status = psa_key_derivation_output_bytes(&operation, |
| 9227 | output_buffer, output_sizes[i]); |
| 9228 | if (expected_capacity == 0 && output_sizes[i] == 0) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9229 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9230 | TEST_ASSERT(status == PSA_SUCCESS || |
| 9231 | status == PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9232 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9233 | } else if (expected_capacity == 0 || |
| 9234 | output_sizes[i] > expected_capacity) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9235 | /* Capacity exceeded. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9236 | TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9237 | expected_capacity = 0; |
| 9238 | continue; |
| 9239 | } |
| 9240 | /* Success. Check the read data. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9241 | PSA_ASSERT(status); |
| 9242 | if (output_sizes[i] != 0) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9243 | TEST_MEMORY_COMPARE(output_buffer, output_sizes[i], |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9244 | expected_outputs[i], output_sizes[i]); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9245 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9246 | /* Check the operation status. */ |
| 9247 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9248 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 9249 | ¤t_capacity)); |
| 9250 | TEST_EQUAL(expected_capacity, current_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9251 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9252 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9253 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9254 | |
| 9255 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9256 | mbedtls_free(output_buffer); |
| 9257 | psa_key_derivation_abort(&operation); |
| 9258 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 9259 | psa_destroy_key(keys[i]); |
| 9260 | } |
| 9261 | psa_destroy_key(derived_key); |
| 9262 | PSA_DONE(); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9263 | } |
| 9264 | /* END_CASE */ |
| 9265 | |
| 9266 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9267 | void derive_full(int alg_arg, |
| 9268 | data_t *key_data, |
| 9269 | data_t *input1, |
| 9270 | data_t *input2, |
| 9271 | int requested_capacity_arg) |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9272 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9273 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9274 | psa_algorithm_t alg = alg_arg; |
| 9275 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9276 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Kusumit Ghoderao | 9ffd397 | 2023-12-01 16:40:13 +0530 | [diff] [blame] | 9277 | unsigned char output_buffer[32]; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9278 | size_t expected_capacity = requested_capacity; |
| 9279 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9280 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9282 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9283 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9284 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9285 | psa_set_key_algorithm(&attributes, alg); |
| 9286 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9287 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9288 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 9289 | &key)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9290 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9291 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 9292 | input1->x, input1->len, |
| 9293 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9294 | requested_capacity, 0)) { |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 9295 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9296 | } |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 9297 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9298 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 9299 | ¤t_capacity)); |
| 9300 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9301 | |
| 9302 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9303 | while (current_capacity > 0) { |
| 9304 | size_t read_size = sizeof(output_buffer); |
| 9305 | if (read_size > current_capacity) { |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9306 | read_size = current_capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9307 | } |
| 9308 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9309 | output_buffer, |
| 9310 | read_size)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9311 | expected_capacity -= read_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9312 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 9313 | ¤t_capacity)); |
| 9314 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9315 | } |
| 9316 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9317 | /* Check that the operation refuses to go over capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9318 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1), |
| 9319 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9320 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9321 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9322 | |
| 9323 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9324 | psa_key_derivation_abort(&operation); |
| 9325 | psa_destroy_key(key); |
| 9326 | PSA_DONE(); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9327 | } |
| 9328 | /* END_CASE */ |
| 9329 | |
Stephan Koch | 78109f5 | 2023-04-12 14:19:36 +0200 | [diff] [blame] | 9330 | /* 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] | 9331 | void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg, |
| 9332 | int derivation_step, |
| 9333 | int capacity, int expected_capacity_status_arg, |
| 9334 | data_t *expected_output, |
| 9335 | int expected_output_status_arg) |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9336 | { |
| 9337 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 9338 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | d378504 | 2022-09-16 06:45:44 -0400 | [diff] [blame] | 9339 | 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] | 9340 | uint8_t *output_buffer = NULL; |
| 9341 | psa_status_t status; |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 9342 | psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg; |
| 9343 | psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg; |
| 9344 | 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] | 9345 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9346 | TEST_CALLOC(output_buffer, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9347 | PSA_ASSERT(psa_crypto_init()); |
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 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9350 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 9351 | expected_capacity_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9353 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 9354 | step, input->x, input->len), |
| 9355 | expected_input_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9356 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9357 | if (((psa_status_t) expected_input_status) != PSA_SUCCESS) { |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9358 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9359 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9360 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9361 | status = psa_key_derivation_output_bytes(&operation, output_buffer, |
| 9362 | expected_output->len); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9363 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9364 | TEST_EQUAL(status, expected_output_status); |
| 9365 | if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9366 | TEST_MEMORY_COMPARE(output_buffer, expected_output->len, expected_output->x, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9367 | expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9368 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9369 | |
| 9370 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9371 | mbedtls_free(output_buffer); |
| 9372 | psa_key_derivation_abort(&operation); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9373 | PSA_DONE(); |
| 9374 | } |
| 9375 | /* END_CASE */ |
| 9376 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9377 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9378 | void derive_key_exercise(int alg_arg, |
| 9379 | data_t *key_data, |
| 9380 | data_t *input1, |
| 9381 | data_t *input2, |
| 9382 | int derived_type_arg, |
| 9383 | int derived_bits_arg, |
| 9384 | int derived_usage_arg, |
| 9385 | int derived_alg_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9386 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9387 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9388 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9389 | psa_algorithm_t alg = alg_arg; |
| 9390 | psa_key_type_t derived_type = derived_type_arg; |
| 9391 | size_t derived_bits = derived_bits_arg; |
| 9392 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 9393 | psa_algorithm_t derived_alg = derived_alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9394 | size_t capacity = PSA_BITS_TO_BYTES(derived_bits); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9395 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9396 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 9397 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9398 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9399 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9400 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9401 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9402 | psa_set_key_algorithm(&attributes, alg); |
| 9403 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
| 9404 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 9405 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9406 | |
| 9407 | /* Derive a key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9408 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9409 | input1->x, input1->len, |
| 9410 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9411 | capacity, 0)) { |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9412 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9413 | } |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9414 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9415 | psa_set_key_usage_flags(&attributes, derived_usage); |
| 9416 | psa_set_key_algorithm(&attributes, derived_alg); |
| 9417 | psa_set_key_type(&attributes, derived_type); |
| 9418 | psa_set_key_bits(&attributes, derived_bits); |
| 9419 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation, |
| 9420 | &derived_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9421 | |
| 9422 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9423 | PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes)); |
| 9424 | TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type); |
| 9425 | TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9426 | |
| 9427 | /* Exercise the derived key. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 9428 | 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] | 9429 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9430 | } |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9431 | |
| 9432 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9433 | /* |
| 9434 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9435 | * thus reset them as required. |
| 9436 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9437 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9438 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9439 | psa_key_derivation_abort(&operation); |
| 9440 | psa_destroy_key(base_key); |
| 9441 | psa_destroy_key(derived_key); |
| 9442 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9443 | } |
| 9444 | /* END_CASE */ |
| 9445 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9446 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9447 | void derive_key_export(int alg_arg, |
| 9448 | data_t *key_data, |
| 9449 | data_t *input1, |
| 9450 | data_t *input2, |
| 9451 | int bytes1_arg, |
| 9452 | int bytes2_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9453 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9454 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9455 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9456 | psa_algorithm_t alg = alg_arg; |
| 9457 | size_t bytes1 = bytes1_arg; |
| 9458 | size_t bytes2 = bytes2_arg; |
| 9459 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9460 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9461 | uint8_t *output_buffer = NULL; |
| 9462 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9463 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9464 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9465 | size_t length; |
| 9466 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9467 | TEST_CALLOC(output_buffer, capacity); |
| 9468 | TEST_CALLOC(export_buffer, capacity); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9469 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9470 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9471 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9472 | psa_set_key_algorithm(&base_attributes, alg); |
| 9473 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9474 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9475 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9476 | |
| 9477 | /* Derive some material and output it. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9478 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9479 | input1->x, input1->len, |
| 9480 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9481 | capacity, 0)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9482 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9483 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9484 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9485 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9486 | output_buffer, |
| 9487 | capacity)); |
| 9488 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9489 | |
| 9490 | /* 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] | 9491 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9492 | input1->x, input1->len, |
| 9493 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9494 | capacity, 0)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9495 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9496 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9497 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9498 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9499 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9500 | psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA); |
| 9501 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1)); |
| 9502 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9503 | &derived_key)); |
| 9504 | PSA_ASSERT(psa_export_key(derived_key, |
| 9505 | export_buffer, bytes1, |
| 9506 | &length)); |
| 9507 | TEST_EQUAL(length, bytes1); |
| 9508 | PSA_ASSERT(psa_destroy_key(derived_key)); |
| 9509 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2)); |
| 9510 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9511 | &derived_key)); |
| 9512 | PSA_ASSERT(psa_export_key(derived_key, |
| 9513 | export_buffer + bytes1, bytes2, |
| 9514 | &length)); |
| 9515 | TEST_EQUAL(length, bytes2); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9516 | |
| 9517 | /* Compare the outputs from the two runs. */ |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9518 | TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9519 | export_buffer, capacity); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9520 | |
| 9521 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9522 | mbedtls_free(output_buffer); |
| 9523 | mbedtls_free(export_buffer); |
| 9524 | psa_key_derivation_abort(&operation); |
| 9525 | psa_destroy_key(base_key); |
| 9526 | psa_destroy_key(derived_key); |
| 9527 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9528 | } |
| 9529 | /* END_CASE */ |
| 9530 | |
| 9531 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9532 | void derive_key_type(int alg_arg, |
| 9533 | data_t *key_data, |
| 9534 | data_t *input1, |
| 9535 | data_t *input2, |
| 9536 | int key_type_arg, int bits_arg, |
| 9537 | data_t *expected_export) |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9538 | { |
| 9539 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9540 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9541 | const psa_algorithm_t alg = alg_arg; |
| 9542 | const psa_key_type_t key_type = key_type_arg; |
| 9543 | const size_t bits = bits_arg; |
| 9544 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9545 | const size_t export_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9546 | PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9547 | uint8_t *export_buffer = NULL; |
| 9548 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9549 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9550 | size_t export_length; |
| 9551 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9552 | TEST_CALLOC(export_buffer, export_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9553 | PSA_ASSERT(psa_crypto_init()); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9554 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9555 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9556 | psa_set_key_algorithm(&base_attributes, alg); |
| 9557 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9558 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9559 | &base_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 | if (mbedtls_test_psa_setup_key_derivation_wrap( |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9562 | &operation, base_key, alg, |
| 9563 | input1->x, input1->len, |
| 9564 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9565 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY, 0) == 0) { |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9566 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9567 | } |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9568 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9569 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9570 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9571 | psa_set_key_type(&derived_attributes, key_type); |
| 9572 | psa_set_key_bits(&derived_attributes, bits); |
| 9573 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9574 | &derived_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9575 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9576 | PSA_ASSERT(psa_export_key(derived_key, |
| 9577 | export_buffer, export_buffer_size, |
| 9578 | &export_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9579 | TEST_MEMORY_COMPARE(export_buffer, export_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9580 | expected_export->x, expected_export->len); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9581 | |
| 9582 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9583 | mbedtls_free(export_buffer); |
| 9584 | psa_key_derivation_abort(&operation); |
| 9585 | psa_destroy_key(base_key); |
| 9586 | psa_destroy_key(derived_key); |
| 9587 | PSA_DONE(); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9588 | } |
| 9589 | /* END_CASE */ |
| 9590 | |
| 9591 | /* BEGIN_CASE */ |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9592 | void derive_key_ext(int alg_arg, |
| 9593 | data_t *key_data, |
| 9594 | data_t *input1, |
| 9595 | data_t *input2, |
| 9596 | int key_type_arg, int bits_arg, |
Gilles Peskine | c81393b | 2024-02-14 20:51:28 +0100 | [diff] [blame] | 9597 | int flags_arg, |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 9598 | data_t *params_data, |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9599 | psa_status_t expected_status, |
| 9600 | data_t *expected_export) |
| 9601 | { |
| 9602 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9603 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9604 | const psa_algorithm_t alg = alg_arg; |
| 9605 | const psa_key_type_t key_type = key_type_arg; |
| 9606 | const size_t bits = bits_arg; |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 9607 | psa_key_production_parameters_t *params = NULL; |
| 9608 | size_t params_data_length = 0; |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9609 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9610 | const size_t export_buffer_size = |
| 9611 | PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits); |
| 9612 | uint8_t *export_buffer = NULL; |
| 9613 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9614 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9615 | size_t export_length; |
| 9616 | |
| 9617 | TEST_CALLOC(export_buffer, export_buffer_size); |
| 9618 | PSA_ASSERT(psa_crypto_init()); |
| 9619 | |
| 9620 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9621 | psa_set_key_algorithm(&base_attributes, alg); |
| 9622 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9623 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9624 | &base_key)); |
| 9625 | |
| 9626 | if (mbedtls_test_psa_setup_key_derivation_wrap( |
| 9627 | &operation, base_key, alg, |
| 9628 | input1->x, input1->len, |
| 9629 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9630 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY, 0) == 0) { |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9631 | goto exit; |
| 9632 | } |
| 9633 | |
| 9634 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9635 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9636 | psa_set_key_type(&derived_attributes, key_type); |
| 9637 | psa_set_key_bits(&derived_attributes, bits); |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 9638 | if (!setup_key_production_parameters(¶ms, ¶ms_data_length, |
| 9639 | flags_arg, params_data)) { |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9640 | goto exit; |
| 9641 | } |
| 9642 | |
| 9643 | TEST_EQUAL(psa_key_derivation_output_key_ext(&derived_attributes, &operation, |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 9644 | params, params_data_length, |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9645 | &derived_key), |
| 9646 | expected_status); |
| 9647 | |
| 9648 | if (expected_status == PSA_SUCCESS) { |
| 9649 | PSA_ASSERT(psa_export_key(derived_key, |
| 9650 | export_buffer, export_buffer_size, |
| 9651 | &export_length)); |
| 9652 | TEST_MEMORY_COMPARE(export_buffer, export_length, |
| 9653 | expected_export->x, expected_export->len); |
| 9654 | } |
| 9655 | |
| 9656 | exit: |
| 9657 | mbedtls_free(export_buffer); |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 9658 | mbedtls_free(params); |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9659 | psa_key_derivation_abort(&operation); |
| 9660 | psa_destroy_key(base_key); |
| 9661 | psa_destroy_key(derived_key); |
| 9662 | PSA_DONE(); |
| 9663 | } |
| 9664 | /* END_CASE */ |
| 9665 | |
| 9666 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9667 | void derive_key(int alg_arg, |
| 9668 | data_t *key_data, data_t *input1, data_t *input2, |
| 9669 | int type_arg, int bits_arg, |
| 9670 | int expected_status_arg, |
| 9671 | int is_large_output) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9672 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9673 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9674 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9675 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 9676 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9677 | size_t bits = bits_arg; |
| 9678 | psa_status_t expected_status = expected_status_arg; |
| 9679 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9680 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9681 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9682 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9683 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9684 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9685 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9686 | psa_set_key_algorithm(&base_attributes, alg); |
| 9687 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9688 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9689 | &base_key)); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9690 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9691 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9692 | input1->x, input1->len, |
| 9693 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9694 | SIZE_MAX, 0)) { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9695 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9696 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9697 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9698 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9699 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9700 | psa_set_key_type(&derived_attributes, type); |
| 9701 | psa_set_key_bits(&derived_attributes, bits); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9702 | |
| 9703 | psa_status_t status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9704 | psa_key_derivation_output_key(&derived_attributes, |
| 9705 | &operation, |
| 9706 | &derived_key); |
| 9707 | if (is_large_output > 0) { |
| 9708 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9709 | } |
| 9710 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9711 | |
| 9712 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9713 | psa_key_derivation_abort(&operation); |
| 9714 | psa_destroy_key(base_key); |
| 9715 | psa_destroy_key(derived_key); |
| 9716 | PSA_DONE(); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9717 | } |
| 9718 | /* END_CASE */ |
| 9719 | |
| 9720 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9721 | void key_agreement_setup(int alg_arg, |
| 9722 | int our_key_type_arg, int our_key_alg_arg, |
| 9723 | data_t *our_key_data, data_t *peer_key_data, |
| 9724 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9725 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9726 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9727 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 9728 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9729 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9730 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9731 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9732 | psa_status_t expected_status = expected_status_arg; |
| 9733 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9734 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9735 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9736 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9737 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9738 | psa_set_key_algorithm(&attributes, our_key_alg); |
| 9739 | psa_set_key_type(&attributes, our_key_type); |
| 9740 | PSA_ASSERT(psa_import_key(&attributes, |
| 9741 | our_key_data->x, our_key_data->len, |
| 9742 | &our_key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9743 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9744 | /* The tests currently include inputs that should fail at either step. |
| 9745 | * Test cases that fail at the setup step should be changed to call |
| 9746 | * key_derivation_setup instead, and this function should be renamed |
| 9747 | * to key_agreement_fail. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9748 | status = psa_key_derivation_setup(&operation, alg); |
| 9749 | if (status == PSA_SUCCESS) { |
| 9750 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 9751 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 9752 | our_key, |
| 9753 | peer_key_data->x, peer_key_data->len), |
| 9754 | expected_status); |
| 9755 | } else { |
| 9756 | TEST_ASSERT(status == expected_status); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9757 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9758 | |
| 9759 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9760 | psa_key_derivation_abort(&operation); |
| 9761 | psa_destroy_key(our_key); |
| 9762 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9763 | } |
| 9764 | /* END_CASE */ |
| 9765 | |
| 9766 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9767 | void raw_key_agreement(int alg_arg, |
| 9768 | int our_key_type_arg, data_t *our_key_data, |
| 9769 | data_t *peer_key_data, |
| 9770 | data_t *expected_output) |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9771 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9772 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9773 | psa_algorithm_t alg = alg_arg; |
| 9774 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9775 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9776 | unsigned char *output = NULL; |
| 9777 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9778 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9779 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9780 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9781 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9782 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9783 | psa_set_key_algorithm(&attributes, alg); |
| 9784 | psa_set_key_type(&attributes, our_key_type); |
| 9785 | PSA_ASSERT(psa_import_key(&attributes, |
| 9786 | our_key_data->x, our_key_data->len, |
| 9787 | &our_key)); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9788 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9789 | PSA_ASSERT(psa_get_key_attributes(our_key, &attributes)); |
| 9790 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9791 | |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9792 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9793 | TEST_LE_U(expected_output->len, |
| 9794 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits)); |
| 9795 | TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits), |
| 9796 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9797 | |
| 9798 | /* Good case with exact output size */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9799 | TEST_CALLOC(output, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9800 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9801 | peer_key_data->x, peer_key_data->len, |
| 9802 | output, expected_output->len, |
| 9803 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9804 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9805 | expected_output->x, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9806 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9807 | output = NULL; |
| 9808 | output_length = ~0; |
| 9809 | |
| 9810 | /* Larger buffer */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9811 | TEST_CALLOC(output, expected_output->len + 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9812 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9813 | peer_key_data->x, peer_key_data->len, |
| 9814 | output, expected_output->len + 1, |
| 9815 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9816 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9817 | expected_output->x, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9818 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9819 | output = NULL; |
| 9820 | output_length = ~0; |
| 9821 | |
| 9822 | /* Buffer too small */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9823 | TEST_CALLOC(output, expected_output->len - 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9824 | TEST_EQUAL(psa_raw_key_agreement(alg, our_key, |
| 9825 | peer_key_data->x, peer_key_data->len, |
| 9826 | output, expected_output->len - 1, |
| 9827 | &output_length), |
| 9828 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9829 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9830 | TEST_LE_U(output_length, expected_output->len - 1); |
| 9831 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9832 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9833 | |
| 9834 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9835 | mbedtls_free(output); |
| 9836 | psa_destroy_key(our_key); |
| 9837 | PSA_DONE(); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9838 | } |
| 9839 | /* END_CASE */ |
| 9840 | |
| 9841 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9842 | void key_agreement_capacity(int alg_arg, |
| 9843 | int our_key_type_arg, data_t *our_key_data, |
| 9844 | data_t *peer_key_data, |
| 9845 | int expected_capacity_arg) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9846 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9847 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9848 | psa_algorithm_t alg = alg_arg; |
| 9849 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9850 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9851 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9852 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9853 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9854 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9855 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9856 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9857 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9858 | psa_set_key_algorithm(&attributes, alg); |
| 9859 | psa_set_key_type(&attributes, our_key_type); |
| 9860 | PSA_ASSERT(psa_import_key(&attributes, |
| 9861 | our_key_data->x, our_key_data->len, |
| 9862 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9863 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9864 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9865 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9866 | &operation, |
| 9867 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9868 | peer_key_data->x, peer_key_data->len)); |
| 9869 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9870 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9871 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9872 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9873 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9874 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9875 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 9876 | /* Test the advertised capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9877 | PSA_ASSERT(psa_key_derivation_get_capacity( |
| 9878 | &operation, &actual_capacity)); |
| 9879 | TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9880 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9881 | /* Test the actual capacity by reading the output. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9882 | while (actual_capacity > sizeof(output)) { |
| 9883 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9884 | output, sizeof(output))); |
| 9885 | actual_capacity -= sizeof(output); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9886 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9887 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9888 | output, actual_capacity)); |
| 9889 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1), |
| 9890 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9891 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9892 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9893 | psa_key_derivation_abort(&operation); |
| 9894 | psa_destroy_key(our_key); |
| 9895 | PSA_DONE(); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9896 | } |
| 9897 | /* END_CASE */ |
| 9898 | |
Valerio Setti | ad81967 | 2023-12-29 12:14:41 +0100 | [diff] [blame] | 9899 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */ |
| 9900 | 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] | 9901 | { |
| 9902 | mbedtls_ecp_group_id grp_id = grp_id_arg; |
Valerio Setti | ad81967 | 2023-12-29 12:14:41 +0100 | [diff] [blame] | 9903 | psa_ecc_family_t ecc_family = psa_family_arg; |
| 9904 | size_t bits = bits_arg; |
| 9905 | size_t bits_tmp; |
Valerio Setti | bf999cb | 2023-12-28 17:48:13 +0100 | [diff] [blame] | 9906 | |
Valerio Setti | ad81967 | 2023-12-29 12:14:41 +0100 | [diff] [blame] | 9907 | TEST_EQUAL(ecc_family, mbedtls_ecc_group_to_psa(grp_id, &bits_tmp)); |
| 9908 | TEST_EQUAL(bits, bits_tmp); |
Valerio Setti | ac73952 | 2024-01-04 10:22:01 +0100 | [diff] [blame] | 9909 | TEST_EQUAL(grp_id, mbedtls_ecc_group_from_psa(ecc_family, bits)); |
Valerio Setti | bf999cb | 2023-12-28 17:48:13 +0100 | [diff] [blame] | 9910 | } |
| 9911 | /* END_CASE */ |
| 9912 | |
Valerio Setti | ac73952 | 2024-01-04 10:22:01 +0100 | [diff] [blame] | 9913 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */ |
| 9914 | void ecc_conversion_functions_fail() |
| 9915 | { |
| 9916 | size_t bits; |
| 9917 | |
Valerio Setti | db6e029 | 2024-01-05 10:15:45 +0100 | [diff] [blame] | 9918 | /* Invalid legacy curve identifiers. */ |
| 9919 | TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_MAX, &bits)); |
| 9920 | TEST_EQUAL(0, bits); |
Valerio Setti | ac73952 | 2024-01-04 10:22:01 +0100 | [diff] [blame] | 9921 | TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_NONE, &bits)); |
| 9922 | TEST_EQUAL(0, bits); |
| 9923 | |
| 9924 | /* Invalid PSA EC family. */ |
| 9925 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(0, 192)); |
| 9926 | /* Invalid bit-size for a valid EC family. */ |
| 9927 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_SECP_R1, 512)); |
| 9928 | |
| 9929 | /* Twisted-Edward curves are not supported yet. */ |
| 9930 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, |
| 9931 | mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 255)); |
| 9932 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, |
| 9933 | mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 448)); |
| 9934 | } |
| 9935 | /* END_CASE */ |
| 9936 | |
| 9937 | |
Valerio Setti | bf999cb | 2023-12-28 17:48:13 +0100 | [diff] [blame] | 9938 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9939 | void key_agreement_output(int alg_arg, |
| 9940 | int our_key_type_arg, data_t *our_key_data, |
| 9941 | data_t *peer_key_data, |
| 9942 | data_t *expected_output1, data_t *expected_output2) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9943 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9944 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9945 | psa_algorithm_t alg = alg_arg; |
| 9946 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9947 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9948 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9949 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9950 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9951 | TEST_CALLOC(actual_output, MAX(expected_output1->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9952 | expected_output2->len)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9953 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9954 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9955 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9956 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9957 | psa_set_key_algorithm(&attributes, alg); |
| 9958 | psa_set_key_type(&attributes, our_key_type); |
| 9959 | PSA_ASSERT(psa_import_key(&attributes, |
| 9960 | our_key_data->x, our_key_data->len, |
| 9961 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9962 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9963 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9964 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9965 | &operation, |
| 9966 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9967 | peer_key_data->x, peer_key_data->len)); |
| 9968 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9969 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9970 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9971 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9972 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9973 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9974 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9975 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9976 | actual_output, |
| 9977 | expected_output1->len)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9978 | TEST_MEMORY_COMPARE(actual_output, expected_output1->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9979 | expected_output1->x, expected_output1->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9980 | if (expected_output2->len != 0) { |
| 9981 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9982 | actual_output, |
| 9983 | expected_output2->len)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9984 | TEST_MEMORY_COMPARE(actual_output, expected_output2->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9985 | expected_output2->x, expected_output2->len); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9986 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9987 | |
| 9988 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9989 | psa_key_derivation_abort(&operation); |
| 9990 | psa_destroy_key(our_key); |
| 9991 | PSA_DONE(); |
| 9992 | mbedtls_free(actual_output); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9993 | } |
| 9994 | /* END_CASE */ |
| 9995 | |
| 9996 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9997 | void generate_random(int bytes_arg) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9998 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9999 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 10000 | unsigned char *output = NULL; |
| 10001 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10002 | size_t i; |
| 10003 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 10004 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10005 | TEST_ASSERT(bytes_arg >= 0); |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 10006 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 10007 | TEST_CALLOC(output, bytes); |
| 10008 | TEST_CALLOC(changed, bytes); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 10009 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10010 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 10011 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10012 | /* Run several times, to ensure that every output byte will be |
| 10013 | * nonzero at least once with overwhelming probability |
| 10014 | * (2^(-8*number_of_runs)). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10015 | for (run = 0; run < 10; run++) { |
| 10016 | if (bytes != 0) { |
| 10017 | memset(output, 0, bytes); |
| 10018 | } |
| 10019 | PSA_ASSERT(psa_generate_random(output, bytes)); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10020 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10021 | for (i = 0; i < bytes; i++) { |
| 10022 | if (output[i] != 0) { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10023 | ++changed[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10024 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10025 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 10026 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10027 | |
| 10028 | /* Check that every byte was changed to nonzero at least once. This |
| 10029 | * validates that psa_generate_random is overwriting every byte of |
| 10030 | * the output buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10031 | for (i = 0; i < bytes; i++) { |
| 10032 | TEST_ASSERT(changed[i] != 0); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10033 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 10034 | |
| 10035 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10036 | PSA_DONE(); |
| 10037 | mbedtls_free(output); |
| 10038 | mbedtls_free(changed); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 10039 | } |
| 10040 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10041 | |
Ryan | 3a1b786 | 2024-03-01 17:24:04 +0000 | [diff] [blame] | 10042 | #if defined MBEDTLS_THREADING_PTHREAD |
| 10043 | |
| 10044 | /* BEGIN_CASE depends_on:MBEDTLS_THREADING_PTHREAD */ |
| 10045 | void concurrently_generate_keys(int type_arg, |
| 10046 | int bits_arg, |
| 10047 | int usage_arg, |
| 10048 | int alg_arg, |
| 10049 | int expected_status_arg, |
| 10050 | int is_large_key_arg, |
| 10051 | int arg_thread_count, |
| 10052 | int reps_arg) |
| 10053 | { |
| 10054 | size_t thread_count = (size_t) arg_thread_count; |
| 10055 | mbedtls_test_thread_t *threads = NULL; |
| 10056 | generate_key_context gkc; |
| 10057 | gkc.type = type_arg; |
| 10058 | gkc.usage = usage_arg; |
| 10059 | gkc.bits = bits_arg; |
| 10060 | gkc.alg = alg_arg; |
| 10061 | gkc.expected_status = expected_status_arg; |
| 10062 | gkc.is_large_key = is_large_key_arg; |
| 10063 | gkc.reps = reps_arg; |
| 10064 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10065 | |
| 10066 | PSA_ASSERT(psa_crypto_init()); |
| 10067 | |
| 10068 | psa_set_key_usage_flags(&attributes, usage_arg); |
| 10069 | psa_set_key_algorithm(&attributes, alg_arg); |
| 10070 | psa_set_key_type(&attributes, type_arg); |
| 10071 | psa_set_key_bits(&attributes, bits_arg); |
| 10072 | gkc.attributes = &attributes; |
| 10073 | |
| 10074 | TEST_CALLOC(threads, sizeof(mbedtls_test_thread_t) * thread_count); |
| 10075 | |
| 10076 | /* Split threads to generate key then destroy key. */ |
| 10077 | for (size_t i = 0; i < thread_count; i++) { |
| 10078 | TEST_EQUAL( |
| 10079 | mbedtls_test_thread_create(&threads[i], thread_generate_key, |
| 10080 | (void *) &gkc), 0); |
| 10081 | } |
| 10082 | |
| 10083 | /* Join threads. */ |
| 10084 | for (size_t i = 0; i < thread_count; i++) { |
| 10085 | TEST_EQUAL(mbedtls_test_thread_join(&threads[i]), 0); |
| 10086 | } |
| 10087 | |
| 10088 | exit: |
| 10089 | mbedtls_free(threads); |
| 10090 | PSA_DONE(); |
| 10091 | } |
| 10092 | /* END_CASE */ |
| 10093 | #endif |
| 10094 | |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10095 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10096 | void generate_key(int type_arg, |
| 10097 | int bits_arg, |
| 10098 | int usage_arg, |
| 10099 | int alg_arg, |
| 10100 | int expected_status_arg, |
| 10101 | int is_large_key) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10102 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 10103 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10104 | psa_key_type_t type = type_arg; |
| 10105 | psa_key_usage_t usage = usage_arg; |
| 10106 | size_t bits = bits_arg; |
| 10107 | psa_algorithm_t alg = alg_arg; |
| 10108 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 10109 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 10110 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10111 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10112 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10114 | psa_set_key_usage_flags(&attributes, usage); |
| 10115 | psa_set_key_algorithm(&attributes, alg); |
| 10116 | psa_set_key_type(&attributes, type); |
| 10117 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10118 | |
| 10119 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10120 | psa_status_t status = psa_generate_key(&attributes, &key); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 10121 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10122 | if (is_large_key > 0) { |
| 10123 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 10124 | } |
| 10125 | TEST_EQUAL(status, expected_status); |
| 10126 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 10127 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10128 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10129 | |
| 10130 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10131 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 10132 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 10133 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10134 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 10135 | /* Do something with the key according to its type and permitted usage. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 10136 | if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 10137 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10138 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10139 | |
| 10140 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 10141 | /* |
| 10142 | * Key attributes may have been returned by psa_get_key_attributes() |
| 10143 | * thus reset them as required. |
| 10144 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10145 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 10146 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10147 | psa_destroy_key(key); |
| 10148 | PSA_DONE(); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10149 | } |
| 10150 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 10151 | |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10152 | /* BEGIN_CASE */ |
| 10153 | void generate_key_ext(int type_arg, |
| 10154 | int bits_arg, |
| 10155 | int usage_arg, |
| 10156 | int alg_arg, |
Gilles Peskine | c81393b | 2024-02-14 20:51:28 +0100 | [diff] [blame] | 10157 | int flags_arg, |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10158 | data_t *params_data, |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10159 | int expected_status_arg) |
| 10160 | { |
| 10161 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10162 | psa_key_type_t type = type_arg; |
| 10163 | psa_key_usage_t usage = usage_arg; |
| 10164 | size_t bits = bits_arg; |
| 10165 | psa_algorithm_t alg = alg_arg; |
| 10166 | psa_status_t expected_status = expected_status_arg; |
| 10167 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10168 | psa_key_production_parameters_t *params = NULL; |
| 10169 | size_t params_data_length = 0; |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10170 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10171 | |
| 10172 | PSA_ASSERT(psa_crypto_init()); |
| 10173 | |
| 10174 | psa_set_key_usage_flags(&attributes, usage); |
| 10175 | psa_set_key_algorithm(&attributes, alg); |
| 10176 | psa_set_key_type(&attributes, type); |
| 10177 | psa_set_key_bits(&attributes, bits); |
| 10178 | |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10179 | if (!setup_key_production_parameters(¶ms, ¶ms_data_length, |
| 10180 | flags_arg, params_data)) { |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10181 | goto exit; |
| 10182 | } |
| 10183 | |
| 10184 | /* Generate a key */ |
| 10185 | psa_status_t status = psa_generate_key_ext(&attributes, |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10186 | params, params_data_length, |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10187 | &key); |
| 10188 | |
| 10189 | TEST_EQUAL(status, expected_status); |
| 10190 | if (expected_status != PSA_SUCCESS) { |
| 10191 | goto exit; |
| 10192 | } |
| 10193 | |
| 10194 | /* Test the key information */ |
| 10195 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 10196 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 10197 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
| 10198 | |
Gilles Peskine | 7a18f96 | 2024-02-12 16:48:11 +0100 | [diff] [blame] | 10199 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) |
| 10200 | if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10201 | TEST_ASSERT(rsa_test_e(key, bits, params_data)); |
Gilles Peskine | 7a18f96 | 2024-02-12 16:48:11 +0100 | [diff] [blame] | 10202 | } |
| 10203 | #endif |
| 10204 | |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10205 | /* Do something with the key according to its type and permitted usage. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 10206 | if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) { |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10207 | goto exit; |
| 10208 | } |
| 10209 | |
| 10210 | exit: |
| 10211 | /* |
| 10212 | * Key attributes may have been returned by psa_get_key_attributes() |
| 10213 | * thus reset them as required. |
| 10214 | */ |
| 10215 | psa_reset_key_attributes(&got_attributes); |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10216 | mbedtls_free(params); |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10217 | psa_destroy_key(key); |
| 10218 | PSA_DONE(); |
| 10219 | } |
| 10220 | /* END_CASE */ |
| 10221 | |
| 10222 | /* BEGIN_CASE */ |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10223 | void key_production_parameters_init() |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10224 | { |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10225 | psa_key_production_parameters_t init = PSA_KEY_PRODUCTION_PARAMETERS_INIT; |
| 10226 | psa_key_production_parameters_t zero; |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10227 | memset(&zero, 0, sizeof(zero)); |
| 10228 | |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10229 | TEST_EQUAL(init.flags, 0); |
| 10230 | TEST_EQUAL(zero.flags, 0); |
| 10231 | } |
| 10232 | /* END_CASE */ |
| 10233 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10234 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10235 | void persistent_key_load_key_from_storage(data_t *data, |
| 10236 | int type_arg, int bits_arg, |
| 10237 | int usage_flags_arg, int alg_arg, |
| 10238 | int generation_method) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10239 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10240 | 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] | 10241 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 10242 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10243 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10244 | psa_key_type_t type = type_arg; |
| 10245 | size_t bits = bits_arg; |
| 10246 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 10247 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 10248 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10249 | unsigned char *first_export = NULL; |
| 10250 | unsigned char *second_export = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10251 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits); |
Sergey | bef1f63 | 2023-03-06 15:25:06 -0700 | [diff] [blame] | 10252 | size_t first_exported_length = 0; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10253 | size_t second_exported_length; |
| 10254 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10255 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 10256 | TEST_CALLOC(first_export, export_size); |
| 10257 | TEST_CALLOC(second_export, export_size); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10258 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10259 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10260 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10261 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10262 | psa_set_key_id(&attributes, key_id); |
| 10263 | psa_set_key_usage_flags(&attributes, usage_flags); |
| 10264 | psa_set_key_algorithm(&attributes, alg); |
| 10265 | psa_set_key_type(&attributes, type); |
| 10266 | psa_set_key_bits(&attributes, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10267 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10268 | switch (generation_method) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10269 | case IMPORT_KEY: |
| 10270 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10271 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, |
| 10272 | &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10273 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10274 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10275 | case GENERATE_KEY: |
| 10276 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10277 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10278 | break; |
| 10279 | |
| 10280 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 10281 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10282 | { |
| 10283 | /* Create base key */ |
| 10284 | psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256); |
| 10285 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10286 | psa_set_key_usage_flags(&base_attributes, |
| 10287 | PSA_KEY_USAGE_DERIVE); |
| 10288 | psa_set_key_algorithm(&base_attributes, derive_alg); |
| 10289 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 10290 | PSA_ASSERT(psa_import_key(&base_attributes, |
| 10291 | data->x, data->len, |
| 10292 | &base_key)); |
| 10293 | /* Derive a key. */ |
| 10294 | PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg)); |
| 10295 | PSA_ASSERT(psa_key_derivation_input_key( |
| 10296 | &operation, |
| 10297 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key)); |
| 10298 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 10299 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
| 10300 | NULL, 0)); |
| 10301 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, |
| 10302 | &operation, |
| 10303 | &key)); |
| 10304 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
| 10305 | PSA_ASSERT(psa_destroy_key(base_key)); |
| 10306 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10307 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 10308 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10309 | TEST_ASSUME(!"KDF not supported in this configuration"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 10310 | #endif |
| 10311 | break; |
| 10312 | |
| 10313 | default: |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 10314 | TEST_FAIL("generation_method not implemented in test"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 10315 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10316 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10317 | psa_reset_key_attributes(&attributes); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10318 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10319 | /* Export the key if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10320 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 10321 | PSA_ASSERT(psa_export_key(key, |
| 10322 | first_export, export_size, |
| 10323 | &first_exported_length)); |
| 10324 | if (generation_method == IMPORT_KEY) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 10325 | TEST_MEMORY_COMPARE(data->x, data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 10326 | first_export, first_exported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10327 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10328 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10329 | |
| 10330 | /* Shutdown and restart */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10331 | PSA_ASSERT(psa_purge_key(key)); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 10332 | PSA_DONE(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10333 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10334 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10335 | /* Check key slot still contains key data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10336 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 10337 | TEST_ASSERT(mbedtls_svc_key_id_equal( |
| 10338 | psa_get_key_id(&attributes), key_id)); |
| 10339 | TEST_EQUAL(psa_get_key_lifetime(&attributes), |
| 10340 | PSA_KEY_LIFETIME_PERSISTENT); |
| 10341 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 10342 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 10343 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 10344 | mbedtls_test_update_key_usage_flags(usage_flags)); |
| 10345 | TEST_EQUAL(psa_get_key_algorithm(&attributes), alg); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10346 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10347 | /* Export the key again if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10348 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 10349 | PSA_ASSERT(psa_export_key(key, |
| 10350 | second_export, export_size, |
| 10351 | &second_exported_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 10352 | TEST_MEMORY_COMPARE(first_export, first_exported_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 10353 | second_export, second_exported_length); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10354 | } |
| 10355 | |
| 10356 | /* Do something with the key according to its type and permitted usage. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 10357 | if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg, 0)) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10358 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10359 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10360 | |
| 10361 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 10362 | /* |
| 10363 | * Key attributes may have been returned by psa_get_key_attributes() |
| 10364 | * thus reset them as required. |
| 10365 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10366 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 10367 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10368 | mbedtls_free(first_export); |
| 10369 | mbedtls_free(second_export); |
| 10370 | psa_key_derivation_abort(&operation); |
| 10371 | psa_destroy_key(base_key); |
| 10372 | psa_destroy_key(key); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 10373 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10374 | } |
| 10375 | /* END_CASE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10376 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 10377 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10378 | void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg, |
| 10379 | int primitive_arg, int hash_arg, int role_arg, |
| 10380 | int test_input, data_t *pw_data, |
| 10381 | int inj_err_type_arg, |
| 10382 | int expected_error_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10383 | { |
| 10384 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10385 | psa_pake_operation_t operation = psa_pake_operation_init(); |
| 10386 | psa_algorithm_t alg = alg_arg; |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 10387 | psa_pake_primitive_t primitive = primitive_arg; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 10388 | psa_key_type_t key_type_pw = key_type_pw_arg; |
| 10389 | psa_key_usage_t key_usage_pw = key_usage_pw_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10390 | psa_algorithm_t hash_alg = hash_arg; |
| 10391 | psa_pake_role_t role = role_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10392 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10393 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10394 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
| 10395 | psa_status_t expected_error = expected_error_arg; |
| 10396 | psa_status_t status; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10397 | unsigned char *output_buffer = NULL; |
| 10398 | size_t output_len = 0; |
| 10399 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10400 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10401 | |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 10402 | size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10403 | PSA_PAKE_STEP_KEY_SHARE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 10404 | TEST_CALLOC(output_buffer, buf_size); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10405 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10406 | if (pw_data->len > 0) { |
| 10407 | psa_set_key_usage_flags(&attributes, key_usage_pw); |
| 10408 | psa_set_key_algorithm(&attributes, alg); |
| 10409 | psa_set_key_type(&attributes, key_type_pw); |
| 10410 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10411 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10412 | } |
| 10413 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10414 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10415 | psa_pake_cs_set_primitive(&cipher_suite, primitive); |
| 10416 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10417 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10418 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 10419 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10420 | if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) { |
| 10421 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 10422 | expected_error); |
| 10423 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10424 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 10425 | expected_error); |
| 10426 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10427 | TEST_EQUAL(psa_pake_set_password_key(&operation, key), |
| 10428 | expected_error); |
| 10429 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10430 | TEST_EQUAL(psa_pake_set_role(&operation, role), |
| 10431 | expected_error); |
| 10432 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10433 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10434 | NULL, 0, NULL), |
| 10435 | expected_error); |
| 10436 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10437 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0), |
| 10438 | expected_error); |
| 10439 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10440 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10441 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10442 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10443 | status = psa_pake_setup(&operation, &cipher_suite); |
| 10444 | if (status != PSA_SUCCESS) { |
| 10445 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10446 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10447 | } |
| 10448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10449 | if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) { |
| 10450 | TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite), |
| 10451 | expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10452 | goto exit; |
| 10453 | } |
| 10454 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10455 | status = psa_pake_set_role(&operation, role); |
| 10456 | if (status != PSA_SUCCESS) { |
| 10457 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10458 | goto exit; |
| 10459 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10460 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10461 | if (pw_data->len > 0) { |
| 10462 | status = psa_pake_set_password_key(&operation, key); |
| 10463 | if (status != PSA_SUCCESS) { |
| 10464 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10465 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10466 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10467 | } |
| 10468 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10469 | if (inj_err_type == INJECT_ERR_INVALID_USER) { |
| 10470 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 10471 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10472 | goto exit; |
| 10473 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 10474 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10475 | if (inj_err_type == INJECT_ERR_INVALID_PEER) { |
| 10476 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 10477 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10478 | goto exit; |
| 10479 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 10480 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10481 | if (inj_err_type == INJECT_ERR_SET_USER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10482 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10483 | TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4), |
| 10484 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10485 | goto exit; |
| 10486 | } |
| 10487 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10488 | if (inj_err_type == INJECT_ERR_SET_PEER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10489 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10490 | TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4), |
| 10491 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10492 | goto exit; |
| 10493 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 10494 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10495 | const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10496 | PSA_PAKE_STEP_KEY_SHARE); |
| 10497 | const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10498 | PSA_PAKE_STEP_ZK_PUBLIC); |
| 10499 | const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10500 | PSA_PAKE_STEP_ZK_PROOF); |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 10501 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10502 | if (test_input) { |
| 10503 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 10504 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0), |
| 10505 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10506 | goto exit; |
| 10507 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10508 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10509 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 10510 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 10511 | output_buffer, size_zk_proof), |
| 10512 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10513 | goto exit; |
| 10514 | } |
| 10515 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10516 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 10517 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10518 | output_buffer, size_zk_proof), |
| 10519 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10520 | goto exit; |
| 10521 | } |
| 10522 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10523 | status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10524 | output_buffer, size_key_share); |
| 10525 | if (status != PSA_SUCCESS) { |
| 10526 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10527 | goto exit; |
| 10528 | } |
| 10529 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10530 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 10531 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10532 | output_buffer, size_zk_public + 1), |
| 10533 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10534 | goto exit; |
| 10535 | } |
| 10536 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10537 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10538 | // 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] | 10539 | psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10540 | output_buffer, size_zk_public + 1); |
| 10541 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10542 | output_buffer, size_zk_public), |
| 10543 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10544 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10545 | } |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10546 | } else { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10547 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 10548 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10549 | NULL, 0, NULL), |
| 10550 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10551 | goto exit; |
| 10552 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10553 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10554 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 10555 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 10556 | output_buffer, buf_size, &output_len), |
| 10557 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10558 | goto exit; |
| 10559 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10560 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10561 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 10562 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10563 | output_buffer, buf_size, &output_len), |
| 10564 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10565 | goto exit; |
| 10566 | } |
| 10567 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10568 | status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10569 | output_buffer, buf_size, &output_len); |
| 10570 | if (status != PSA_SUCCESS) { |
| 10571 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10572 | goto exit; |
| 10573 | } |
| 10574 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10575 | TEST_ASSERT(output_len > 0); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10576 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10577 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 10578 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10579 | output_buffer, size_zk_public - 1, &output_len), |
| 10580 | PSA_ERROR_BUFFER_TOO_SMALL); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10581 | goto exit; |
| 10582 | } |
| 10583 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10584 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10585 | // 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] | 10586 | psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10587 | output_buffer, size_zk_public - 1, &output_len); |
| 10588 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10589 | output_buffer, buf_size, &output_len), |
| 10590 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10591 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10592 | } |
| 10593 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10594 | |
| 10595 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10596 | PSA_ASSERT(psa_destroy_key(key)); |
| 10597 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10598 | mbedtls_free(output_buffer); |
| 10599 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10600 | } |
| 10601 | /* END_CASE */ |
| 10602 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 10603 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10604 | void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg, |
| 10605 | int client_input_first, int inject_error, |
| 10606 | data_t *pw_data) |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10607 | { |
| 10608 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10609 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10610 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10611 | psa_algorithm_t alg = alg_arg; |
| 10612 | psa_algorithm_t hash_alg = hash_arg; |
| 10613 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10614 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10615 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10616 | PSA_INIT(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10617 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10618 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10619 | psa_set_key_algorithm(&attributes, alg); |
| 10620 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10621 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10622 | &key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10623 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10624 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10625 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10626 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10627 | |
| 10628 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10629 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10630 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10631 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10632 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10633 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10634 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10635 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10636 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10637 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10638 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10639 | client_input_first, 1, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10640 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10641 | if (inject_error == 1 || inject_error == 2) { |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10642 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10643 | } |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10644 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10645 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10646 | client_input_first, 2, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10647 | |
| 10648 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10649 | psa_destroy_key(key); |
| 10650 | psa_pake_abort(&server); |
| 10651 | psa_pake_abort(&client); |
| 10652 | PSA_DONE(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10653 | } |
| 10654 | /* END_CASE */ |
| 10655 | |
| 10656 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10657 | void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg, |
| 10658 | int derive_alg_arg, data_t *pw_data, |
| 10659 | int client_input_first, int inj_err_type_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10660 | { |
| 10661 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10662 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10663 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10664 | psa_algorithm_t alg = alg_arg; |
| 10665 | psa_algorithm_t hash_alg = hash_arg; |
| 10666 | psa_algorithm_t derive_alg = derive_alg_arg; |
| 10667 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10668 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10669 | psa_key_derivation_operation_t server_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10670 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10671 | psa_key_derivation_operation_t client_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10672 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10673 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10674 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10675 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10676 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10677 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10678 | psa_set_key_algorithm(&attributes, alg); |
| 10679 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10680 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10681 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10682 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10683 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10684 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10685 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10686 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10687 | /* Get shared key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10688 | PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg)); |
| 10689 | PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10690 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10691 | if (PSA_ALG_IS_TLS12_PRF(derive_alg) || |
| 10692 | PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) { |
| 10693 | PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive, |
| 10694 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10695 | (const uint8_t *) "", 0)); |
| 10696 | PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive, |
| 10697 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10698 | (const uint8_t *) "", 0)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10699 | } |
| 10700 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10701 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10702 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10703 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10704 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10705 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10706 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10707 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10708 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10709 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10710 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) { |
| 10711 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10712 | PSA_ERROR_BAD_STATE); |
| 10713 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10714 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10715 | goto exit; |
| 10716 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10717 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10718 | /* First round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10719 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10720 | client_input_first, 1, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10721 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10722 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) { |
| 10723 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10724 | PSA_ERROR_BAD_STATE); |
| 10725 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10726 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10727 | goto exit; |
| 10728 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10729 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10730 | /* Second round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10731 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10732 | client_input_first, 2, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10733 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10734 | PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive)); |
| 10735 | PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10736 | |
| 10737 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10738 | psa_key_derivation_abort(&server_derive); |
| 10739 | psa_key_derivation_abort(&client_derive); |
| 10740 | psa_destroy_key(key); |
| 10741 | psa_pake_abort(&server); |
| 10742 | psa_pake_abort(&client); |
| 10743 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10744 | } |
| 10745 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10746 | |
| 10747 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10748 | void ecjpake_size_macros() |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10749 | { |
| 10750 | const psa_algorithm_t alg = PSA_ALG_JPAKE; |
| 10751 | const size_t bits = 256; |
| 10752 | const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10753 | 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] | 10754 | 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] | 10755 | PSA_ECC_FAMILY_SECP_R1); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10756 | |
| 10757 | // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types |
| 10758 | /* 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] | 10759 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10760 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
| 10761 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10762 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10763 | /* The output for ZK_PROOF is the same bitsize as the curve */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10764 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10765 | PSA_BITS_TO_BYTES(bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10766 | |
| 10767 | /* Input sizes are the same as output sizes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10768 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10769 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE)); |
| 10770 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10771 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC)); |
| 10772 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10773 | 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] | 10774 | |
| 10775 | /* These inequalities will always hold even when other PAKEs are added */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10776 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10777 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10778 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10779 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10780 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10781 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10782 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10783 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10784 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10785 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10786 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10787 | PSA_PAKE_INPUT_MAX_SIZE); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10788 | } |
| 10789 | /* END_CASE */ |