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 | |
| 1364 | /* Import the key, exactly one thread must succceed. */ |
| 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. */ |
| 1373 | TEST_EQUAL(skc->key_loaded, 0); |
| 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); |
| 1448 | //Will always fail |
| 1449 | TEST_EQUAL(skc->key_loaded, 1); |
| 1450 | } |
| 1451 | skc->key_loaded = 0; |
| 1452 | mbedtls_mutex_unlock(&skc->key_loaded_mutex); |
| 1453 | } |
| 1454 | } else { |
| 1455 | TEST_EQUAL(status, PSA_ERROR_INVALID_HANDLE); |
| 1456 | } |
| 1457 | |
| 1458 | exit: |
| 1459 | return NULL; |
| 1460 | } |
| 1461 | |
Ryan | 3a1b786 | 2024-03-01 17:24:04 +0000 | [diff] [blame] | 1462 | typedef struct generate_key_context { |
| 1463 | psa_key_type_t type; |
| 1464 | psa_key_usage_t usage; |
| 1465 | size_t bits; |
| 1466 | psa_algorithm_t alg; |
| 1467 | psa_status_t expected_status; |
| 1468 | psa_key_attributes_t *attributes; |
| 1469 | int is_large_key; |
| 1470 | int reps; |
| 1471 | } |
| 1472 | generate_key_context; |
| 1473 | void *thread_generate_key(void *ctx) |
| 1474 | { |
| 1475 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1476 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1477 | generate_key_context *gkc = (struct generate_key_context *) ctx; |
| 1478 | |
| 1479 | /* If there are race conditions, it is likely the case that they do not |
| 1480 | * arise every time the code runs. We repeat the code to increase the |
| 1481 | * chance that any race conditions will be hit. */ |
| 1482 | for (int n = 0; n < gkc->reps; n++) { |
| 1483 | /* Generate a key */ |
| 1484 | psa_status_t status = psa_generate_key(gkc->attributes, &key); |
| 1485 | |
| 1486 | if (gkc->is_large_key > 0) { |
| 1487 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 1488 | } |
| 1489 | |
| 1490 | TEST_EQUAL(status, gkc->expected_status); |
| 1491 | if (gkc->expected_status != PSA_SUCCESS) { |
| 1492 | PSA_ASSERT(psa_destroy_key(key)); |
| 1493 | goto exit; |
| 1494 | } |
| 1495 | |
| 1496 | /* Test the key information */ |
| 1497 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1498 | TEST_EQUAL(psa_get_key_type(&got_attributes), gkc->type); |
| 1499 | TEST_EQUAL(psa_get_key_bits(&got_attributes), gkc->bits); |
| 1500 | |
| 1501 | /* Do something with the key according |
| 1502 | * to its type and permitted usage. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1503 | if (!mbedtls_test_psa_exercise_key(key, gkc->usage, gkc->alg, 0)) { |
Ryan | 3a1b786 | 2024-03-01 17:24:04 +0000 | [diff] [blame] | 1504 | psa_destroy_key(key); |
| 1505 | goto exit; |
| 1506 | } |
| 1507 | psa_reset_key_attributes(&got_attributes); |
| 1508 | |
| 1509 | PSA_ASSERT(psa_destroy_key(key)); |
| 1510 | } |
| 1511 | exit: |
| 1512 | /* |
| 1513 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1514 | * thus reset them as required. |
| 1515 | */ |
| 1516 | psa_reset_key_attributes(&got_attributes); |
| 1517 | return NULL; |
| 1518 | } |
| 1519 | #endif /* MBEDTLS_THREADING_PTHREAD */ |
| 1520 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1521 | /* END_HEADER */ |
| 1522 | |
| 1523 | /* BEGIN_DEPENDENCIES |
| 1524 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1525 | * END_DEPENDENCIES |
| 1526 | */ |
| 1527 | |
| 1528 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 7abdf7e | 2023-03-09 11:17:43 +0100 | [diff] [blame] | 1529 | void psa_can_do_hash() |
| 1530 | { |
| 1531 | /* We can't test that this is specific to drivers until partial init has |
| 1532 | * been implemented, but we can at least test before/after full init. */ |
| 1533 | TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE)); |
| 1534 | PSA_INIT(); |
| 1535 | TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE)); |
| 1536 | PSA_DONE(); |
| 1537 | } |
| 1538 | /* END_CASE */ |
| 1539 | |
| 1540 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1541 | void static_checks() |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1542 | { |
| 1543 | size_t max_truncated_mac_size = |
| 1544 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1545 | |
| 1546 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1547 | * encoding. The shifted mask is the maximum truncated value. The |
| 1548 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1549 | TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size); |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1550 | } |
| 1551 | /* END_CASE */ |
| 1552 | |
| 1553 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1554 | void import_with_policy(int type_arg, |
| 1555 | int usage_arg, int alg_arg, |
| 1556 | int expected_status_arg) |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1557 | { |
| 1558 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1559 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1560 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1561 | psa_key_type_t type = type_arg; |
| 1562 | psa_key_usage_t usage = usage_arg; |
| 1563 | psa_algorithm_t alg = alg_arg; |
| 1564 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1565 | const uint8_t key_material[16] = { 0 }; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1566 | psa_status_t status; |
| 1567 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1568 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1569 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1570 | psa_set_key_type(&attributes, type); |
| 1571 | psa_set_key_usage_flags(&attributes, usage); |
| 1572 | psa_set_key_algorithm(&attributes, alg); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1573 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1574 | status = psa_import_key(&attributes, |
| 1575 | key_material, sizeof(key_material), |
| 1576 | &key); |
| 1577 | TEST_EQUAL(status, expected_status); |
| 1578 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1579 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1580 | } |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1581 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1582 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1583 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1584 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), |
| 1585 | mbedtls_test_update_key_usage_flags(usage)); |
| 1586 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 1587 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1588 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1589 | PSA_ASSERT(psa_destroy_key(key)); |
| 1590 | test_operations_on_invalid_key(key); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1591 | |
| 1592 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1593 | /* |
| 1594 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1595 | * thus reset them as required. |
| 1596 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1597 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1598 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1599 | psa_destroy_key(key); |
| 1600 | PSA_DONE(); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1601 | } |
| 1602 | /* END_CASE */ |
| 1603 | |
| 1604 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1605 | void import_with_data(data_t *data, int type_arg, |
| 1606 | int attr_bits_arg, |
| 1607 | int expected_status_arg) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1608 | { |
| 1609 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1610 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1611 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1612 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1613 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1614 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1615 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1616 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1617 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1618 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1619 | psa_set_key_type(&attributes, type); |
| 1620 | psa_set_key_bits(&attributes, attr_bits); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1621 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1622 | status = psa_import_key(&attributes, data->x, data->len, &key); |
Manuel Pégourié-Gonnard | 0509b58 | 2023-08-08 12:47:56 +0200 | [diff] [blame] | 1623 | /* When expecting INVALID_ARGUMENT, also accept NOT_SUPPORTED. |
| 1624 | * |
| 1625 | * This can happen with a type supported only by a driver: |
| 1626 | * - the driver sees the invalid data (for example wrong size) and thinks |
| 1627 | * "well perhaps this is a key size I don't support" so it returns |
| 1628 | * NOT_SUPPORTED which is correct at this point; |
| 1629 | * - we fallback to built-ins, which don't support this type, so return |
| 1630 | * NOT_SUPPORTED which again is correct at this point. |
| 1631 | */ |
| 1632 | if (expected_status == PSA_ERROR_INVALID_ARGUMENT && |
| 1633 | status == PSA_ERROR_NOT_SUPPORTED) { |
| 1634 | ; // OK |
| 1635 | } else { |
| 1636 | TEST_EQUAL(status, expected_status); |
| 1637 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1638 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1639 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1640 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1642 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1643 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1644 | if (attr_bits != 0) { |
| 1645 | TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes)); |
| 1646 | } |
| 1647 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1648 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1649 | PSA_ASSERT(psa_destroy_key(key)); |
| 1650 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1651 | |
| 1652 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1653 | /* |
| 1654 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1655 | * thus reset them as required. |
| 1656 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1657 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1658 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1659 | psa_destroy_key(key); |
| 1660 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1661 | } |
| 1662 | /* END_CASE */ |
| 1663 | |
| 1664 | /* BEGIN_CASE */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1665 | /* Construct and attempt to import a large unstructured key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1666 | void import_large_key(int type_arg, int byte_size_arg, |
| 1667 | int expected_status_arg) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1668 | { |
| 1669 | psa_key_type_t type = type_arg; |
| 1670 | size_t byte_size = byte_size_arg; |
| 1671 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1672 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1673 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1674 | psa_status_t status; |
| 1675 | uint8_t *buffer = NULL; |
| 1676 | size_t buffer_size = byte_size + 1; |
| 1677 | size_t n; |
| 1678 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1679 | /* Skip the test case if the target running the test cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1680 | * accommodate large keys due to heap size constraints */ |
Tom Cosgrove | 412a813 | 2023-07-20 16:55:14 +0100 | [diff] [blame] | 1681 | TEST_CALLOC_OR_SKIP(buffer, buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1682 | memset(buffer, 'K', byte_size); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1683 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1684 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1685 | |
| 1686 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1687 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1688 | psa_set_key_type(&attributes, type); |
| 1689 | status = psa_import_key(&attributes, buffer, byte_size, &key); |
| 1690 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 1691 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1692 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1693 | if (status == PSA_SUCCESS) { |
| 1694 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1695 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 1696 | TEST_EQUAL(psa_get_key_bits(&attributes), |
| 1697 | PSA_BYTES_TO_BITS(byte_size)); |
| 1698 | ASSERT_NO_SLOT_NUMBER(&attributes); |
| 1699 | memset(buffer, 0, byte_size + 1); |
| 1700 | PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n)); |
| 1701 | for (n = 0; n < byte_size; n++) { |
| 1702 | TEST_EQUAL(buffer[n], 'K'); |
| 1703 | } |
| 1704 | for (n = byte_size; n < buffer_size; n++) { |
| 1705 | TEST_EQUAL(buffer[n], 0); |
| 1706 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1710 | /* |
| 1711 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1712 | * thus reset them as required. |
| 1713 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1714 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1715 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1716 | psa_destroy_key(key); |
| 1717 | PSA_DONE(); |
| 1718 | mbedtls_free(buffer); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1719 | } |
| 1720 | /* END_CASE */ |
| 1721 | |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 1722 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1723 | /* Import an RSA key with a valid structure (but not valid numbers |
| 1724 | * inside, beyond having sensible size and parity). This is expected to |
| 1725 | * fail for large keys. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1726 | 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] | 1727 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1728 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1729 | size_t bits = bits_arg; |
| 1730 | psa_status_t expected_status = expected_status_arg; |
| 1731 | psa_status_t status; |
| 1732 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1733 | 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] | 1734 | size_t buffer_size = /* Slight overapproximations */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1735 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1736 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1737 | unsigned char *p; |
| 1738 | int ret; |
| 1739 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1740 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1741 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1742 | PSA_ASSERT(psa_crypto_init()); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1743 | TEST_CALLOC(buffer, buffer_size); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1744 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1745 | TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p, |
| 1746 | bits, keypair)) >= 0); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1747 | length = ret; |
| 1748 | |
| 1749 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1750 | psa_set_key_type(&attributes, type); |
| 1751 | status = psa_import_key(&attributes, p, length, &key); |
| 1752 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1753 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1754 | if (status == PSA_SUCCESS) { |
| 1755 | PSA_ASSERT(psa_destroy_key(key)); |
| 1756 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1757 | |
| 1758 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1759 | mbedtls_free(buffer); |
| 1760 | PSA_DONE(); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1761 | } |
| 1762 | /* END_CASE */ |
| 1763 | |
| 1764 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1765 | void import_export(data_t *data, |
| 1766 | int type_arg, |
| 1767 | int usage_arg, int alg_arg, |
| 1768 | int lifetime_arg, |
| 1769 | int expected_bits, |
| 1770 | int export_size_delta, |
| 1771 | int expected_export_status_arg, |
| 1772 | /*whether reexport must give the original input exactly*/ |
| 1773 | int canonical_input) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1774 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1775 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1776 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1777 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1778 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1779 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1780 | psa_key_lifetime_t lifetime = lifetime_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1781 | unsigned char *exported = NULL; |
| 1782 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1783 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1784 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1785 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1786 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1787 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1788 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1789 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1790 | TEST_CALLOC(exported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1791 | if (!canonical_input) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1792 | TEST_CALLOC(reexported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1793 | } |
| 1794 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1795 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1796 | psa_set_key_lifetime(&attributes, lifetime); |
| 1797 | psa_set_key_usage_flags(&attributes, usage_arg); |
| 1798 | psa_set_key_algorithm(&attributes, alg); |
| 1799 | psa_set_key_type(&attributes, type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1800 | |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1801 | if (PSA_KEY_TYPE_IS_DH(type) && |
| 1802 | expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) { |
Przemek Stekiel | 654bef0 | 2022-12-15 13:28:02 +0100 | [diff] [blame] | 1803 | /* Simulate that buffer is too small, by decreasing its size by 1 byte. */ |
| 1804 | export_size -= 1; |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1805 | } |
| 1806 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1807 | /* Import the key */ |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1808 | TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key), |
Przemek Stekiel | 2e7c33d | 2023-04-27 12:29:45 +0200 | [diff] [blame] | 1809 | PSA_SUCCESS); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1810 | |
| 1811 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1812 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1813 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1814 | TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits); |
| 1815 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1816 | |
| 1817 | /* Export the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1818 | status = psa_export_key(key, exported, export_size, &exported_length); |
| 1819 | TEST_EQUAL(status, expected_export_status); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1820 | |
| 1821 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1822 | * and export_size. On errors, the exported length must be 0. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1823 | TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH); |
| 1824 | TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0); |
| 1825 | TEST_LE_U(exported_length, export_size); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1826 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1827 | TEST_ASSERT(mem_is_char(exported + exported_length, 0, |
| 1828 | export_size - exported_length)); |
| 1829 | if (status != PSA_SUCCESS) { |
| 1830 | TEST_EQUAL(exported_length, 0); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1831 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1832 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1833 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1834 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1835 | * this validates the canonical representations. For canonical inputs, |
| 1836 | * this doesn't directly validate the implementation, but it still helps |
| 1837 | * by cross-validating the test data with the sanity check code. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1838 | if (!psa_key_lifetime_is_external(lifetime)) { |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1839 | if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0, 0)) { |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1840 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1841 | } |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1842 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1843 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1844 | if (canonical_input) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1845 | TEST_MEMORY_COMPARE(data->x, data->len, exported, exported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1846 | } else { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1847 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1848 | PSA_ASSERT(psa_import_key(&attributes, exported, exported_length, |
| 1849 | &key2)); |
| 1850 | PSA_ASSERT(psa_export_key(key2, |
| 1851 | reexported, |
| 1852 | export_size, |
| 1853 | &reexported_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1854 | TEST_MEMORY_COMPARE(exported, exported_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1855 | reexported, reexported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1856 | PSA_ASSERT(psa_destroy_key(key2)); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1857 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1858 | TEST_LE_U(exported_length, |
| 1859 | PSA_EXPORT_KEY_OUTPUT_SIZE(type, |
| 1860 | psa_get_key_bits(&got_attributes))); |
Valerio Setti | 1eacae8 | 2023-07-28 16:07:03 +0200 | [diff] [blame] | 1861 | if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 1862 | TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
| 1863 | } else if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) { |
| 1864 | TEST_LE_U(exported_length, PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
| 1865 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1866 | |
| 1867 | destroy: |
| 1868 | /* Destroy the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1869 | PSA_ASSERT(psa_destroy_key(key)); |
| 1870 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1871 | |
| 1872 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1873 | /* |
| 1874 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1875 | * thus reset them as required. |
| 1876 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1877 | psa_reset_key_attributes(&got_attributes); |
| 1878 | psa_destroy_key(key); |
| 1879 | mbedtls_free(exported); |
| 1880 | mbedtls_free(reexported); |
| 1881 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1882 | } |
| 1883 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1884 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1885 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1886 | void import_export_public_key(data_t *data, |
| 1887 | int type_arg, // key pair or public key |
| 1888 | int alg_arg, |
| 1889 | int lifetime_arg, |
| 1890 | int export_size_delta, |
| 1891 | int expected_export_status_arg, |
| 1892 | data_t *expected_public_key) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1893 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1894 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1895 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1896 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1897 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1898 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1899 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1900 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1901 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1902 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1903 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1904 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1905 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1906 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1907 | psa_set_key_lifetime(&attributes, lifetime); |
| 1908 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1909 | psa_set_key_algorithm(&attributes, alg); |
| 1910 | psa_set_key_type(&attributes, type); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1911 | |
| 1912 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1913 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1914 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1915 | /* Export the public key */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1916 | TEST_CALLOC(exported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1917 | status = psa_export_public_key(key, |
| 1918 | exported, export_size, |
| 1919 | &exported_length); |
| 1920 | TEST_EQUAL(status, expected_export_status); |
| 1921 | if (status == PSA_SUCCESS) { |
| 1922 | 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] | 1923 | size_t bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1924 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1925 | bits = psa_get_key_bits(&attributes); |
| 1926 | TEST_LE_U(expected_public_key->len, |
| 1927 | PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1928 | TEST_LE_U(expected_public_key->len, |
| 1929 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1930 | TEST_LE_U(expected_public_key->len, |
| 1931 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1932 | TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1933 | exported, exported_length); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1934 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1935 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1936 | /* |
| 1937 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1938 | * thus reset them as required. |
| 1939 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1940 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1941 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1942 | mbedtls_free(exported); |
| 1943 | psa_destroy_key(key); |
| 1944 | PSA_DONE(); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1945 | } |
| 1946 | /* END_CASE */ |
| 1947 | |
Ryan Everett | 5061999 | 2024-03-12 16:55:14 +0000 | [diff] [blame^] | 1948 | |
| 1949 | #if defined(MBEDTLS_THREADING_PTHREAD) |
| 1950 | /* BEGIN_CASE depends_on:MBEDTLS_THREADING_PTHREAD:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
| 1951 | void concurrently_use_same_persistent_key(data_t *data, |
| 1952 | int type_arg, |
| 1953 | int bits_arg, |
| 1954 | int alg_arg, |
| 1955 | int thread_count_arg) |
| 1956 | { |
| 1957 | size_t thread_count = (size_t) thread_count_arg; |
| 1958 | mbedtls_test_thread_t *threads = NULL; |
| 1959 | mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1); |
| 1960 | same_key_context skc; |
| 1961 | skc.data = data; |
| 1962 | skc.key = key_id; |
| 1963 | skc.type = type_arg; |
| 1964 | skc.bits = bits_arg; |
| 1965 | skc.key_loaded = 0; |
| 1966 | mbedtls_mutex_init(&skc.key_loaded_mutex); |
| 1967 | psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(skc.type, alg_arg); |
| 1968 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1969 | |
| 1970 | PSA_ASSERT(psa_crypto_init()); |
| 1971 | |
| 1972 | psa_set_key_id(&attributes, key_id); |
| 1973 | psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_PERSISTENT); |
| 1974 | psa_set_key_usage_flags(&attributes, usage); |
| 1975 | psa_set_key_algorithm(&attributes, alg_arg); |
| 1976 | psa_set_key_type(&attributes, type_arg); |
| 1977 | psa_set_key_bits(&attributes, bits_arg); |
| 1978 | skc.attributes = &attributes; |
| 1979 | |
| 1980 | TEST_CALLOC(threads, sizeof(mbedtls_test_thread_t) * thread_count); |
| 1981 | |
| 1982 | /* Test that when multiple threads import the same key, |
| 1983 | * exactly one thread succeeds and the rest fail with valid errors. |
| 1984 | * Also test that all threads can use the key as soon as it has been |
| 1985 | * imported. */ |
| 1986 | for (size_t i = 0; i < thread_count; i++) { |
| 1987 | TEST_EQUAL( |
| 1988 | mbedtls_test_thread_create(&threads[i], thread_import_key, |
| 1989 | (void *) &skc), 0); |
| 1990 | } |
| 1991 | |
| 1992 | /* Join threads. */ |
| 1993 | for (size_t i = 0; i < thread_count; i++) { |
| 1994 | TEST_EQUAL(mbedtls_test_thread_join(&threads[i]), 0); |
| 1995 | } |
| 1996 | |
| 1997 | /* Test that when multiple threads use and destroy a key no corruption |
| 1998 | * occurs, and exactly one thread succeeds when destroying the key. */ |
| 1999 | for (size_t i = 0; i < thread_count; i++) { |
| 2000 | TEST_EQUAL( |
| 2001 | mbedtls_test_thread_create(&threads[i], thread_use_and_destroy_key, |
| 2002 | (void *) &skc), 0); |
| 2003 | } |
| 2004 | |
| 2005 | /* Join threads. */ |
| 2006 | for (size_t i = 0; i < thread_count; i++) { |
| 2007 | TEST_EQUAL(mbedtls_test_thread_join(&threads[i]), 0); |
| 2008 | } |
| 2009 | /* Ensure that one thread succeeded in destroying the key. */ |
| 2010 | TEST_ASSERT(!skc.key_loaded); |
| 2011 | exit: |
| 2012 | psa_reset_key_attributes(&attributes); |
| 2013 | mbedtls_mutex_free(&skc.key_loaded_mutex); |
| 2014 | mbedtls_free(threads); |
| 2015 | PSA_DONE(); |
| 2016 | } |
| 2017 | /* END_CASE */ |
| 2018 | #endif |
| 2019 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2020 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2021 | void import_and_exercise_key(data_t *data, |
| 2022 | int type_arg, |
| 2023 | int bits_arg, |
| 2024 | int alg_arg) |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2025 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2026 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2027 | psa_key_type_t type = type_arg; |
| 2028 | size_t bits = bits_arg; |
| 2029 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2030 | 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] | 2031 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2032 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2033 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2034 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2035 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2036 | psa_set_key_usage_flags(&attributes, usage); |
| 2037 | psa_set_key_algorithm(&attributes, alg); |
| 2038 | psa_set_key_type(&attributes, type); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2039 | |
| 2040 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2041 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2042 | |
| 2043 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2044 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 2045 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 2046 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2047 | |
| 2048 | /* Do something with the key according to its type and permitted usage. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 2049 | if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 2050 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2051 | } |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2052 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2053 | PSA_ASSERT(psa_destroy_key(key)); |
| 2054 | test_operations_on_invalid_key(key); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 2055 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2056 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2057 | /* |
| 2058 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2059 | * thus reset them as required. |
| 2060 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2061 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2062 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2063 | psa_reset_key_attributes(&attributes); |
| 2064 | psa_destroy_key(key); |
| 2065 | PSA_DONE(); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2066 | } |
| 2067 | /* END_CASE */ |
| 2068 | |
| 2069 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2070 | void effective_key_attributes(int type_arg, int expected_type_arg, |
| 2071 | int bits_arg, int expected_bits_arg, |
| 2072 | int usage_arg, int expected_usage_arg, |
| 2073 | int alg_arg, int expected_alg_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2074 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2075 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 2076 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 2077 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 2078 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 2079 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2080 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 2081 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2082 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 2083 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2084 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2085 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2086 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2087 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2088 | psa_set_key_usage_flags(&attributes, usage); |
| 2089 | psa_set_key_algorithm(&attributes, alg); |
| 2090 | psa_set_key_type(&attributes, key_type); |
| 2091 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2092 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2093 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
| 2094 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2095 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2096 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 2097 | TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type); |
| 2098 | TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits); |
| 2099 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
| 2100 | TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2101 | |
| 2102 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2103 | /* |
| 2104 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2105 | * thus reset them as required. |
| 2106 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2107 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2108 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2109 | psa_destroy_key(key); |
| 2110 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2111 | } |
| 2112 | /* END_CASE */ |
| 2113 | |
| 2114 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2115 | void check_key_policy(int type_arg, int bits_arg, |
| 2116 | int usage_arg, int alg_arg) |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 2117 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2118 | test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg, |
| 2119 | usage_arg, |
| 2120 | mbedtls_test_update_key_usage_flags(usage_arg), |
| 2121 | alg_arg, alg_arg); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 2122 | goto exit; |
| 2123 | } |
| 2124 | /* END_CASE */ |
| 2125 | |
| 2126 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2127 | void key_attributes_init() |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 2128 | { |
| 2129 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2130 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2131 | * 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] | 2132 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2133 | psa_key_attributes_t func = psa_key_attributes_init(); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2134 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 2135 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 2136 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2137 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 2138 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2139 | TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE); |
| 2140 | TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE); |
| 2141 | TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2142 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2143 | TEST_EQUAL(psa_get_key_type(&func), 0); |
| 2144 | TEST_EQUAL(psa_get_key_type(&init), 0); |
| 2145 | TEST_EQUAL(psa_get_key_type(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2146 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2147 | TEST_EQUAL(psa_get_key_bits(&func), 0); |
| 2148 | TEST_EQUAL(psa_get_key_bits(&init), 0); |
| 2149 | TEST_EQUAL(psa_get_key_bits(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2151 | TEST_EQUAL(psa_get_key_usage_flags(&func), 0); |
| 2152 | TEST_EQUAL(psa_get_key_usage_flags(&init), 0); |
| 2153 | TEST_EQUAL(psa_get_key_usage_flags(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2154 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2155 | TEST_EQUAL(psa_get_key_algorithm(&func), 0); |
| 2156 | TEST_EQUAL(psa_get_key_algorithm(&init), 0); |
| 2157 | TEST_EQUAL(psa_get_key_algorithm(&zero), 0); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 2158 | } |
| 2159 | /* END_CASE */ |
| 2160 | |
| 2161 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2162 | void mac_key_policy(int policy_usage_arg, |
| 2163 | int policy_alg_arg, |
| 2164 | int key_type_arg, |
| 2165 | data_t *key_data, |
| 2166 | int exercise_alg_arg, |
| 2167 | int expected_status_sign_arg, |
| 2168 | int expected_status_verify_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2169 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2170 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2171 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2172 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2173 | psa_key_type_t key_type = key_type_arg; |
| 2174 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 2175 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 2176 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2177 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 2178 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 2179 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2180 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2181 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2182 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2183 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2184 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2185 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2186 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2187 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2188 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2189 | &key)); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2190 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2191 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 2192 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2193 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2194 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 2195 | TEST_EQUAL(status, expected_status_sign); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2196 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 2197 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2198 | uint8_t input[128] = { 0 }; |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 2199 | size_t mac_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2200 | TEST_EQUAL(psa_mac_compute(key, exercise_alg, |
| 2201 | input, 128, |
| 2202 | mac, PSA_MAC_MAX_SIZE, &mac_len), |
| 2203 | expected_status_sign); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 2204 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2205 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2206 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 2207 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 2208 | if (status == PSA_SUCCESS) { |
| 2209 | status = psa_mac_update(&operation, input, 128); |
| 2210 | if (status == PSA_SUCCESS) { |
| 2211 | TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE, |
| 2212 | &mac_len), |
| 2213 | expected_status_sign); |
| 2214 | } else { |
| 2215 | TEST_EQUAL(status, expected_status_sign); |
| 2216 | } |
| 2217 | } else { |
| 2218 | TEST_EQUAL(status, expected_status_sign); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2219 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2220 | PSA_ASSERT(psa_mac_abort(&operation)); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2221 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 2222 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2223 | status = psa_mac_verify(key, exercise_alg, input, 128, |
| 2224 | mac, mac_len); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 2225 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2226 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 2227 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2228 | } else { |
| 2229 | TEST_EQUAL(status, expected_status_verify); |
| 2230 | } |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2231 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2232 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2233 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 2234 | if (status == PSA_SUCCESS) { |
| 2235 | status = psa_mac_update(&operation, input, 128); |
| 2236 | if (status == PSA_SUCCESS) { |
| 2237 | status = psa_mac_verify_finish(&operation, mac, mac_len); |
| 2238 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 2239 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2240 | } else { |
| 2241 | TEST_EQUAL(status, expected_status_verify); |
| 2242 | } |
| 2243 | } else { |
| 2244 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2245 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2246 | } else { |
| 2247 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2248 | } |
| 2249 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2250 | psa_mac_abort(&operation); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2252 | memset(mac, 0, sizeof(mac)); |
| 2253 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 2254 | TEST_EQUAL(status, expected_status_verify); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2255 | |
| 2256 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2257 | psa_mac_abort(&operation); |
| 2258 | psa_destroy_key(key); |
| 2259 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2260 | } |
| 2261 | /* END_CASE */ |
| 2262 | |
| 2263 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2264 | void cipher_key_policy(int policy_usage_arg, |
| 2265 | int policy_alg, |
| 2266 | int key_type, |
| 2267 | data_t *key_data, |
| 2268 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2269 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2270 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2271 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2272 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2273 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2274 | size_t output_buffer_size = 0; |
| 2275 | size_t input_buffer_size = 0; |
| 2276 | size_t output_length = 0; |
| 2277 | uint8_t *output = NULL; |
| 2278 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2279 | psa_status_t status; |
| 2280 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2281 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg); |
| 2282 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg, |
| 2283 | input_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2284 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2285 | TEST_CALLOC(input, input_buffer_size); |
| 2286 | TEST_CALLOC(output, output_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2287 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2288 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2289 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2290 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2291 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2292 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2293 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2294 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2295 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2296 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2297 | /* Check if no key usage flag implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2298 | TEST_EQUAL(policy_usage, |
| 2299 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2300 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2301 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2302 | status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size, |
| 2303 | output, output_buffer_size, |
| 2304 | &output_length); |
| 2305 | if (policy_alg == exercise_alg && |
| 2306 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2307 | PSA_ASSERT(status); |
| 2308 | } else { |
| 2309 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2310 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2311 | |
| 2312 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2313 | status = psa_cipher_encrypt_setup(&operation, key, exercise_alg); |
| 2314 | if (policy_alg == exercise_alg && |
| 2315 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2316 | PSA_ASSERT(status); |
| 2317 | } else { |
| 2318 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2319 | } |
| 2320 | psa_cipher_abort(&operation); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2321 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2322 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2323 | status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size, |
| 2324 | input, input_buffer_size, |
| 2325 | &output_length); |
| 2326 | if (policy_alg == exercise_alg && |
| 2327 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2328 | PSA_ASSERT(status); |
| 2329 | } else { |
| 2330 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2331 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2332 | |
| 2333 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2334 | status = psa_cipher_decrypt_setup(&operation, key, exercise_alg); |
| 2335 | if (policy_alg == exercise_alg && |
| 2336 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2337 | PSA_ASSERT(status); |
| 2338 | } else { |
| 2339 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2340 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2341 | |
| 2342 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2343 | psa_cipher_abort(&operation); |
| 2344 | mbedtls_free(input); |
| 2345 | mbedtls_free(output); |
| 2346 | psa_destroy_key(key); |
| 2347 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2348 | } |
| 2349 | /* END_CASE */ |
| 2350 | |
| 2351 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2352 | void aead_key_policy(int policy_usage_arg, |
| 2353 | int policy_alg, |
| 2354 | int key_type, |
| 2355 | data_t *key_data, |
| 2356 | int nonce_length_arg, |
| 2357 | int tag_length_arg, |
| 2358 | int exercise_alg, |
| 2359 | int expected_status_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2360 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2361 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2362 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2363 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2364 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2365 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2366 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2367 | unsigned char nonce[16] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2368 | size_t nonce_length = nonce_length_arg; |
| 2369 | unsigned char tag[16]; |
| 2370 | size_t tag_length = tag_length_arg; |
| 2371 | size_t output_length; |
| 2372 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2373 | TEST_LE_U(nonce_length, sizeof(nonce)); |
| 2374 | TEST_LE_U(tag_length, sizeof(tag)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2375 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2376 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2377 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2378 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2379 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2380 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2381 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2382 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2383 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2384 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2385 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2386 | TEST_EQUAL(policy_usage, |
| 2387 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2388 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2389 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2390 | status = psa_aead_encrypt(key, exercise_alg, |
| 2391 | nonce, nonce_length, |
| 2392 | NULL, 0, |
| 2393 | NULL, 0, |
| 2394 | tag, tag_length, |
| 2395 | &output_length); |
| 2396 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2397 | TEST_EQUAL(status, expected_status); |
| 2398 | } else { |
| 2399 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2400 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2401 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2402 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2403 | status = psa_aead_encrypt_setup(&operation, key, exercise_alg); |
| 2404 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2405 | TEST_EQUAL(status, expected_status); |
| 2406 | } else { |
| 2407 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2408 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2409 | |
| 2410 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2411 | memset(tag, 0, sizeof(tag)); |
| 2412 | status = psa_aead_decrypt(key, exercise_alg, |
| 2413 | nonce, nonce_length, |
| 2414 | NULL, 0, |
| 2415 | tag, tag_length, |
| 2416 | NULL, 0, |
| 2417 | &output_length); |
| 2418 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2419 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2420 | } else if (expected_status == PSA_SUCCESS) { |
| 2421 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2422 | } else { |
| 2423 | TEST_EQUAL(status, expected_status); |
| 2424 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2425 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2426 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2427 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2428 | status = psa_aead_decrypt_setup(&operation, key, exercise_alg); |
| 2429 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2430 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2431 | } else { |
| 2432 | TEST_EQUAL(status, expected_status); |
| 2433 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2434 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2435 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2436 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2437 | psa_destroy_key(key); |
| 2438 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2439 | } |
| 2440 | /* END_CASE */ |
| 2441 | |
| 2442 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2443 | void asymmetric_encryption_key_policy(int policy_usage_arg, |
| 2444 | int policy_alg, |
| 2445 | int key_type, |
| 2446 | data_t *key_data, |
Valerio Setti | f202c29 | 2024-01-15 10:42:37 +0100 | [diff] [blame] | 2447 | int exercise_alg, |
| 2448 | int use_opaque_key) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2449 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2450 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2451 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2452 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2453 | psa_status_t status; |
| 2454 | size_t key_bits; |
| 2455 | size_t buffer_length; |
| 2456 | unsigned char *buffer = NULL; |
| 2457 | size_t output_length; |
| 2458 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2459 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2460 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2461 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2462 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2463 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2464 | |
Valerio Setti | f202c29 | 2024-01-15 10:42:37 +0100 | [diff] [blame] | 2465 | if (use_opaque_key) { |
| 2466 | psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION( |
| 2467 | PSA_KEY_PERSISTENCE_VOLATILE, TEST_DRIVER_LOCATION)); |
| 2468 | } |
| 2469 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2470 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2471 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2472 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2473 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2474 | TEST_EQUAL(policy_usage, |
| 2475 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2476 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2477 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 2478 | key_bits = psa_get_key_bits(&attributes); |
| 2479 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, |
| 2480 | exercise_alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2481 | TEST_CALLOC(buffer, buffer_length); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2482 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2483 | status = psa_asymmetric_encrypt(key, exercise_alg, |
| 2484 | NULL, 0, |
| 2485 | NULL, 0, |
| 2486 | buffer, buffer_length, |
| 2487 | &output_length); |
| 2488 | if (policy_alg == exercise_alg && |
| 2489 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2490 | PSA_ASSERT(status); |
| 2491 | } else { |
| 2492 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2493 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2494 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2495 | if (buffer_length != 0) { |
| 2496 | memset(buffer, 0, buffer_length); |
| 2497 | } |
| 2498 | status = psa_asymmetric_decrypt(key, exercise_alg, |
| 2499 | buffer, buffer_length, |
| 2500 | NULL, 0, |
| 2501 | buffer, buffer_length, |
| 2502 | &output_length); |
| 2503 | if (policy_alg == exercise_alg && |
| 2504 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2505 | TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING); |
| 2506 | } else { |
| 2507 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2508 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2509 | |
| 2510 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2511 | /* |
| 2512 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2513 | * thus reset them as required. |
| 2514 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2515 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2516 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2517 | psa_destroy_key(key); |
| 2518 | PSA_DONE(); |
| 2519 | mbedtls_free(buffer); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2520 | } |
| 2521 | /* END_CASE */ |
| 2522 | |
| 2523 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2524 | void asymmetric_signature_key_policy(int policy_usage_arg, |
| 2525 | int policy_alg, |
| 2526 | int key_type, |
| 2527 | data_t *key_data, |
| 2528 | int exercise_alg, |
| 2529 | int payload_length_arg, |
| 2530 | int expected_usage_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2531 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2532 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2533 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2534 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 2535 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2536 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2537 | unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 }; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2538 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2539 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2540 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2541 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2542 | int compatible_alg = payload_length_arg > 0; |
| 2543 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2544 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2545 | size_t signature_length; |
| 2546 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2547 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2548 | in the expected usage flags. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2549 | TEST_EQUAL(expected_usage, |
| 2550 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2551 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2552 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2553 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2554 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2555 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2556 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2557 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2558 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2559 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2560 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2561 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2562 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2563 | status = psa_sign_hash(key, exercise_alg, |
| 2564 | payload, payload_length, |
| 2565 | signature, sizeof(signature), |
| 2566 | &signature_length); |
| 2567 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) { |
| 2568 | PSA_ASSERT(status); |
| 2569 | } else { |
| 2570 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2571 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2572 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2573 | memset(signature, 0, sizeof(signature)); |
| 2574 | status = psa_verify_hash(key, exercise_alg, |
| 2575 | payload, payload_length, |
| 2576 | signature, sizeof(signature)); |
| 2577 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) { |
| 2578 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2579 | } else { |
| 2580 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2581 | } |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2582 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2583 | if (PSA_ALG_IS_SIGN_HASH(exercise_alg) && |
| 2584 | PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) { |
| 2585 | status = psa_sign_message(key, exercise_alg, |
| 2586 | payload, payload_length, |
| 2587 | signature, sizeof(signature), |
| 2588 | &signature_length); |
| 2589 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) { |
| 2590 | PSA_ASSERT(status); |
| 2591 | } else { |
| 2592 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2593 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2594 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2595 | memset(signature, 0, sizeof(signature)); |
| 2596 | status = psa_verify_message(key, exercise_alg, |
| 2597 | payload, payload_length, |
| 2598 | signature, sizeof(signature)); |
| 2599 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) { |
| 2600 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2601 | } else { |
| 2602 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2603 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2604 | } |
| 2605 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2606 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2607 | psa_destroy_key(key); |
| 2608 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2609 | } |
| 2610 | /* END_CASE */ |
| 2611 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2612 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2613 | void derive_key_policy(int policy_usage, |
| 2614 | int policy_alg, |
| 2615 | int key_type, |
| 2616 | data_t *key_data, |
| 2617 | int exercise_alg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2618 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2619 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2620 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2621 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2622 | psa_status_t status; |
| 2623 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2624 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2625 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2626 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2627 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2628 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2629 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2630 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2631 | &key)); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2632 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2633 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2634 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2635 | if (PSA_ALG_IS_TLS12_PRF(exercise_alg) || |
| 2636 | PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) { |
| 2637 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 2638 | &operation, |
| 2639 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2640 | (const uint8_t *) "", 0)); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2641 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2642 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2643 | status = psa_key_derivation_input_key(&operation, |
| 2644 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 2645 | key); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2646 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2647 | if (policy_alg == exercise_alg && |
| 2648 | (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) { |
| 2649 | PSA_ASSERT(status); |
| 2650 | } else { |
| 2651 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2652 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2653 | |
| 2654 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2655 | psa_key_derivation_abort(&operation); |
| 2656 | psa_destroy_key(key); |
| 2657 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2658 | } |
| 2659 | /* END_CASE */ |
| 2660 | |
| 2661 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2662 | void agreement_key_policy(int policy_usage, |
| 2663 | int policy_alg, |
| 2664 | int key_type_arg, |
| 2665 | data_t *key_data, |
| 2666 | int exercise_alg, |
| 2667 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2668 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2669 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2670 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2671 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2672 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2673 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2674 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2676 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2677 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2678 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2679 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2680 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2681 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2682 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2683 | &key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2684 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2685 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
Ryan Everett | 73e4ea3 | 2024-03-12 16:29:55 +0000 | [diff] [blame] | 2686 | status = mbedtls_test_psa_key_agreement_with_self(&operation, key, 0); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2687 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2688 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2689 | |
| 2690 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2691 | psa_key_derivation_abort(&operation); |
| 2692 | psa_destroy_key(key); |
| 2693 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2694 | } |
| 2695 | /* END_CASE */ |
| 2696 | |
| 2697 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2698 | void key_policy_alg2(int key_type_arg, data_t *key_data, |
| 2699 | int usage_arg, int alg_arg, int alg2_arg) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2700 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2701 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2702 | psa_key_type_t key_type = key_type_arg; |
| 2703 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2704 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2705 | psa_key_usage_t usage = usage_arg; |
| 2706 | psa_algorithm_t alg = alg_arg; |
| 2707 | psa_algorithm_t alg2 = alg2_arg; |
| 2708 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2709 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2710 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2711 | psa_set_key_usage_flags(&attributes, usage); |
| 2712 | psa_set_key_algorithm(&attributes, alg); |
| 2713 | psa_set_key_enrollment_algorithm(&attributes, alg2); |
| 2714 | psa_set_key_type(&attributes, key_type); |
| 2715 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2716 | &key)); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2717 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2718 | /* Update the usage flags to obtain implicit usage flags */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2719 | usage = mbedtls_test_update_key_usage_flags(usage); |
| 2720 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 2721 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage); |
| 2722 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 2723 | TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2724 | |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 2725 | if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2726 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2727 | } |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 2728 | if (!mbedtls_test_psa_exercise_key(key, usage, alg2, 0)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2729 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2730 | } |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2731 | |
| 2732 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2733 | /* |
| 2734 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2735 | * thus reset them as required. |
| 2736 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2737 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2738 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2739 | psa_destroy_key(key); |
| 2740 | PSA_DONE(); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2741 | } |
| 2742 | /* END_CASE */ |
| 2743 | |
| 2744 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2745 | void raw_agreement_key_policy(int policy_usage, |
| 2746 | int policy_alg, |
| 2747 | int key_type_arg, |
| 2748 | data_t *key_data, |
| 2749 | int exercise_alg, |
| 2750 | int expected_status_arg) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2751 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2752 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2753 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2754 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2755 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2756 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2757 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2758 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2759 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2760 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2761 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2762 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2763 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2764 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2765 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2766 | &key)); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2767 | |
Ryan Everett | 8163028 | 2024-03-12 16:21:12 +0000 | [diff] [blame] | 2768 | 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] | 2769 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2770 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2771 | |
| 2772 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2773 | psa_key_derivation_abort(&operation); |
| 2774 | psa_destroy_key(key); |
| 2775 | PSA_DONE(); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2776 | } |
| 2777 | /* END_CASE */ |
| 2778 | |
| 2779 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2780 | void copy_success(int source_usage_arg, |
| 2781 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4ea4ad0 | 2022-12-04 15:11:00 +0100 | [diff] [blame] | 2782 | int source_lifetime_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2783 | int type_arg, data_t *material, |
| 2784 | int copy_attributes, |
| 2785 | int target_usage_arg, |
| 2786 | int target_alg_arg, int target_alg2_arg, |
Gilles Peskine | 4ea4ad0 | 2022-12-04 15:11:00 +0100 | [diff] [blame] | 2787 | int target_lifetime_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2788 | int expected_usage_arg, |
| 2789 | int expected_alg_arg, int expected_alg2_arg) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2790 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2791 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2792 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2793 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2794 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2795 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2796 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 2797 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2798 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2799 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2800 | uint8_t *export_buffer = NULL; |
| 2801 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2802 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2803 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2804 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2805 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2806 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2807 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2808 | psa_set_key_type(&source_attributes, type_arg); |
| 2809 | psa_set_key_lifetime(&source_attributes, source_lifetime); |
| 2810 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2811 | material->x, material->len, |
| 2812 | &source_key)); |
| 2813 | PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2814 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2815 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2816 | if (copy_attributes) { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2817 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2818 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2819 | psa_set_key_lifetime(&target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2820 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2821 | if (target_usage_arg != -1) { |
| 2822 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2823 | } |
| 2824 | if (target_alg_arg != -1) { |
| 2825 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2826 | } |
| 2827 | if (target_alg2_arg != -1) { |
| 2828 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
| 2829 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2830 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2831 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2832 | /* Copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2833 | PSA_ASSERT(psa_copy_key(source_key, |
| 2834 | &target_attributes, &target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2835 | |
| 2836 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2837 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2838 | |
| 2839 | /* Test that the target slot has the expected content and policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2840 | PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes)); |
| 2841 | TEST_EQUAL(psa_get_key_type(&source_attributes), |
| 2842 | psa_get_key_type(&target_attributes)); |
| 2843 | TEST_EQUAL(psa_get_key_bits(&source_attributes), |
| 2844 | psa_get_key_bits(&target_attributes)); |
| 2845 | TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes)); |
| 2846 | TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes)); |
| 2847 | TEST_EQUAL(expected_alg2, |
| 2848 | psa_get_key_enrollment_algorithm(&target_attributes)); |
| 2849 | if (expected_usage & PSA_KEY_USAGE_EXPORT) { |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2850 | size_t length; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2851 | TEST_CALLOC(export_buffer, material->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2852 | PSA_ASSERT(psa_export_key(target_key, export_buffer, |
| 2853 | material->len, &length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2854 | TEST_MEMORY_COMPARE(material->x, material->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2855 | export_buffer, length); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2856 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2857 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2858 | if (!psa_key_lifetime_is_external(target_lifetime)) { |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 2859 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg, 0)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2860 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2861 | } |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 2862 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2, 0)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2863 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2864 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2865 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2866 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2867 | PSA_ASSERT(psa_destroy_key(target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2868 | |
| 2869 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2870 | /* |
| 2871 | * Source and target key attributes may have been returned by |
| 2872 | * psa_get_key_attributes() thus reset them as required. |
| 2873 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2874 | psa_reset_key_attributes(&source_attributes); |
| 2875 | psa_reset_key_attributes(&target_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2876 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2877 | PSA_DONE(); |
| 2878 | mbedtls_free(export_buffer); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2879 | } |
| 2880 | /* END_CASE */ |
| 2881 | |
| 2882 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2883 | void copy_fail(int source_usage_arg, |
| 2884 | int source_alg_arg, int source_alg2_arg, |
| 2885 | int source_lifetime_arg, |
| 2886 | int type_arg, data_t *material, |
| 2887 | int target_type_arg, int target_bits_arg, |
| 2888 | int target_usage_arg, |
| 2889 | int target_alg_arg, int target_alg2_arg, |
| 2890 | int target_id_arg, int target_lifetime_arg, |
| 2891 | int expected_status_arg) |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2892 | { |
| 2893 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2894 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2895 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2896 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2897 | 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] | 2898 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2899 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2900 | |
| 2901 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2902 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2903 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2904 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2905 | psa_set_key_type(&source_attributes, type_arg); |
| 2906 | psa_set_key_lifetime(&source_attributes, source_lifetime_arg); |
| 2907 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2908 | material->x, material->len, |
| 2909 | &source_key)); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2910 | |
| 2911 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2912 | psa_set_key_id(&target_attributes, key_id); |
| 2913 | psa_set_key_lifetime(&target_attributes, target_lifetime_arg); |
| 2914 | psa_set_key_type(&target_attributes, target_type_arg); |
| 2915 | psa_set_key_bits(&target_attributes, target_bits_arg); |
| 2916 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2917 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2918 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2919 | |
| 2920 | /* Try to copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2921 | TEST_EQUAL(psa_copy_key(source_key, |
| 2922 | &target_attributes, &target_key), |
| 2923 | expected_status_arg); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2924 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2925 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2926 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2927 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2928 | psa_reset_key_attributes(&source_attributes); |
| 2929 | psa_reset_key_attributes(&target_attributes); |
| 2930 | PSA_DONE(); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2931 | } |
| 2932 | /* END_CASE */ |
| 2933 | |
| 2934 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2935 | void hash_operation_init() |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2936 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2937 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2938 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2939 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2940 | * 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] | 2941 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2942 | psa_hash_operation_t func = psa_hash_operation_init(); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2943 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2944 | psa_hash_operation_t zero; |
| 2945 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2946 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2947 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2948 | /* A freshly-initialized hash operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2949 | TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)), |
| 2950 | PSA_ERROR_BAD_STATE); |
| 2951 | TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)), |
| 2952 | PSA_ERROR_BAD_STATE); |
| 2953 | TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)), |
| 2954 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2955 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2956 | /* A default hash operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2957 | PSA_ASSERT(psa_hash_abort(&func)); |
| 2958 | PSA_ASSERT(psa_hash_abort(&init)); |
| 2959 | PSA_ASSERT(psa_hash_abort(&zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2960 | } |
| 2961 | /* END_CASE */ |
| 2962 | |
| 2963 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2964 | void hash_setup(int alg_arg, |
| 2965 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2966 | { |
| 2967 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2968 | uint8_t *output = NULL; |
| 2969 | size_t output_size = 0; |
| 2970 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2971 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2972 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2973 | psa_status_t status; |
| 2974 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2975 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2976 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2977 | /* Hash Setup, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2978 | output_size = PSA_HASH_LENGTH(alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2979 | TEST_CALLOC(output, output_size); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2980 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2981 | status = psa_hash_compute(alg, NULL, 0, |
| 2982 | output, output_size, &output_length); |
| 2983 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2984 | |
| 2985 | /* Hash Setup, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2986 | status = psa_hash_setup(&operation, alg); |
| 2987 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2988 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2989 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2990 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2991 | |
| 2992 | /* If setup failed, reproduce the failure, so as to |
| 2993 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2994 | if (status != PSA_SUCCESS) { |
| 2995 | TEST_EQUAL(psa_hash_setup(&operation, alg), status); |
| 2996 | } |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2997 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2998 | /* Now the operation object should be reusable. */ |
| 2999 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3000 | PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG)); |
| 3001 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3002 | #endif |
| 3003 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3004 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3005 | mbedtls_free(output); |
| 3006 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3007 | } |
| 3008 | /* END_CASE */ |
| 3009 | |
| 3010 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3011 | void hash_compute_fail(int alg_arg, data_t *input, |
| 3012 | int output_size_arg, int expected_status_arg) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3013 | { |
| 3014 | psa_algorithm_t alg = alg_arg; |
| 3015 | uint8_t *output = NULL; |
| 3016 | size_t output_size = output_size_arg; |
| 3017 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 3018 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3019 | psa_status_t expected_status = expected_status_arg; |
| 3020 | psa_status_t status; |
| 3021 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3022 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3023 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3024 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3025 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 3026 | /* Hash Compute, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3027 | status = psa_hash_compute(alg, input->x, input->len, |
| 3028 | output, output_size, &output_length); |
| 3029 | TEST_EQUAL(status, expected_status); |
| 3030 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3031 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 3032 | /* Hash Compute, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3033 | status = psa_hash_setup(&operation, alg); |
| 3034 | if (status == PSA_SUCCESS) { |
| 3035 | status = psa_hash_update(&operation, input->x, input->len); |
| 3036 | if (status == PSA_SUCCESS) { |
| 3037 | status = psa_hash_finish(&operation, output, output_size, |
| 3038 | &output_length); |
| 3039 | if (status == PSA_SUCCESS) { |
| 3040 | TEST_LE_U(output_length, output_size); |
| 3041 | } else { |
| 3042 | TEST_EQUAL(status, expected_status); |
| 3043 | } |
| 3044 | } else { |
| 3045 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 3046 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3047 | } else { |
| 3048 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 3049 | } |
| 3050 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3051 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3052 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3053 | mbedtls_free(output); |
| 3054 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3055 | } |
| 3056 | /* END_CASE */ |
| 3057 | |
| 3058 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3059 | void hash_compare_fail(int alg_arg, data_t *input, |
| 3060 | data_t *reference_hash, |
| 3061 | int expected_status_arg) |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 3062 | { |
| 3063 | psa_algorithm_t alg = alg_arg; |
| 3064 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 3065 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 3066 | psa_status_t status; |
| 3067 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3068 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 3069 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 3070 | /* Hash Compare, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3071 | status = psa_hash_compare(alg, input->x, input->len, |
| 3072 | reference_hash->x, reference_hash->len); |
| 3073 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 3074 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 3075 | /* Hash Compare, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3076 | status = psa_hash_setup(&operation, alg); |
| 3077 | if (status == PSA_SUCCESS) { |
| 3078 | status = psa_hash_update(&operation, input->x, input->len); |
| 3079 | if (status == PSA_SUCCESS) { |
| 3080 | status = psa_hash_verify(&operation, reference_hash->x, |
| 3081 | reference_hash->len); |
| 3082 | TEST_EQUAL(status, expected_status); |
| 3083 | } else { |
| 3084 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 3085 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3086 | } else { |
| 3087 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 3088 | } |
| 3089 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 3090 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3091 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3092 | PSA_DONE(); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 3093 | } |
| 3094 | /* END_CASE */ |
| 3095 | |
| 3096 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3097 | void hash_compute_compare(int alg_arg, data_t *input, |
| 3098 | data_t *expected_output) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3099 | { |
| 3100 | psa_algorithm_t alg = alg_arg; |
| 3101 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 3102 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3103 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3104 | size_t i; |
| 3105 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3106 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3107 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3108 | /* Compute with tight buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3109 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 3110 | output, PSA_HASH_LENGTH(alg), |
| 3111 | &output_length)); |
| 3112 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3113 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3114 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3115 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3116 | /* Compute with tight buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3117 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3118 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 3119 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 3120 | PSA_HASH_LENGTH(alg), |
| 3121 | &output_length)); |
| 3122 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3123 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3124 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3125 | |
| 3126 | /* Compute with larger buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3127 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 3128 | output, sizeof(output), |
| 3129 | &output_length)); |
| 3130 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3131 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3132 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3133 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3134 | /* Compute with larger buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3135 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3136 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 3137 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 3138 | sizeof(output), &output_length)); |
| 3139 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3140 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3141 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3142 | |
| 3143 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3144 | PSA_ASSERT(psa_hash_compare(alg, input->x, input->len, |
| 3145 | output, output_length)); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3146 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3147 | /* Compare with correct hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3148 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3149 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 3150 | PSA_ASSERT(psa_hash_verify(&operation, output, |
| 3151 | output_length)); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3152 | |
| 3153 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3154 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 3155 | output, output_length + 1), |
| 3156 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3157 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3158 | /* Compare with trailing garbage, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3159 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3160 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 3161 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1), |
| 3162 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3163 | |
| 3164 | /* Compare with truncated hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3165 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 3166 | output, output_length - 1), |
| 3167 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3168 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3169 | /* Compare with truncated hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3170 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3171 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 3172 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1), |
| 3173 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3174 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3175 | /* Compare with corrupted value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3176 | for (i = 0; i < output_length; i++) { |
| 3177 | mbedtls_test_set_step(i); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3178 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3179 | |
| 3180 | /* One-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3181 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 3182 | output, output_length), |
| 3183 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3184 | |
| 3185 | /* Multi-Part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3186 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3187 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 3188 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length), |
| 3189 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3190 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3191 | output[i] ^= 1; |
| 3192 | } |
| 3193 | |
| 3194 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3195 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3196 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3197 | } |
| 3198 | /* END_CASE */ |
| 3199 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3200 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3201 | void hash_bad_order() |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3202 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3203 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3204 | unsigned char input[] = ""; |
| 3205 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3206 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3207 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 3208 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3209 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 |
| 3210 | }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3211 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3212 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 3213 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3215 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3216 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3217 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3218 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3219 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3220 | TEST_EQUAL(psa_hash_setup(&operation, alg), |
| 3221 | PSA_ERROR_BAD_STATE); |
| 3222 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3223 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3224 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3225 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3226 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3227 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 3228 | PSA_ERROR_BAD_STATE); |
| 3229 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3230 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3231 | /* Check that update calls abort on error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3232 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 3233 | operation.id = UINT_MAX; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3234 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3235 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 3236 | PSA_ERROR_BAD_STATE); |
| 3237 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3238 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3239 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3240 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3241 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3242 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3243 | PSA_ASSERT(psa_hash_finish(&operation, |
| 3244 | hash, sizeof(hash), &hash_len)); |
| 3245 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 3246 | PSA_ERROR_BAD_STATE); |
| 3247 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3248 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3249 | /* Call verify without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3250 | TEST_EQUAL(psa_hash_verify(&operation, |
| 3251 | valid_hash, sizeof(valid_hash)), |
| 3252 | PSA_ERROR_BAD_STATE); |
| 3253 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3254 | |
| 3255 | /* Call verify after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3256 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3257 | PSA_ASSERT(psa_hash_finish(&operation, |
| 3258 | hash, sizeof(hash), &hash_len)); |
| 3259 | TEST_EQUAL(psa_hash_verify(&operation, |
| 3260 | valid_hash, sizeof(valid_hash)), |
| 3261 | PSA_ERROR_BAD_STATE); |
| 3262 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3263 | |
| 3264 | /* Call verify twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3265 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3266 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3267 | PSA_ASSERT(psa_hash_verify(&operation, |
| 3268 | valid_hash, sizeof(valid_hash))); |
| 3269 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3270 | TEST_EQUAL(psa_hash_verify(&operation, |
| 3271 | valid_hash, sizeof(valid_hash)), |
| 3272 | PSA_ERROR_BAD_STATE); |
| 3273 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3274 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3275 | |
| 3276 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3277 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3278 | hash, sizeof(hash), &hash_len), |
| 3279 | PSA_ERROR_BAD_STATE); |
| 3280 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3281 | |
| 3282 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3283 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3284 | PSA_ASSERT(psa_hash_finish(&operation, |
| 3285 | hash, sizeof(hash), &hash_len)); |
| 3286 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3287 | hash, sizeof(hash), &hash_len), |
| 3288 | PSA_ERROR_BAD_STATE); |
| 3289 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3290 | |
| 3291 | /* Call finish after calling verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3292 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3293 | PSA_ASSERT(psa_hash_verify(&operation, |
| 3294 | valid_hash, sizeof(valid_hash))); |
| 3295 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3296 | hash, sizeof(hash), &hash_len), |
| 3297 | PSA_ERROR_BAD_STATE); |
| 3298 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3299 | |
| 3300 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3301 | PSA_DONE(); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3302 | } |
| 3303 | /* END_CASE */ |
| 3304 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3305 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3306 | void hash_verify_bad_args() |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3307 | { |
| 3308 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3309 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 3310 | * appended to it */ |
| 3311 | unsigned char hash[] = { |
| 3312 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 3313 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3314 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb |
| 3315 | }; |
| 3316 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 3317 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3318 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3319 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3320 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3321 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3322 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3323 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3324 | TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1), |
| 3325 | PSA_ERROR_INVALID_SIGNATURE); |
| 3326 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3327 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3328 | ASSERT_OPERATION_IS_INACTIVE(operation); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3329 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3330 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3331 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3332 | TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size), |
| 3333 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3334 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3335 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3336 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3337 | TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)), |
| 3338 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 3339 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3340 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3341 | PSA_DONE(); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3342 | } |
| 3343 | /* END_CASE */ |
| 3344 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3345 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3346 | void hash_finish_bad_args() |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3347 | { |
| 3348 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 3349 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3350 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 3351 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3352 | size_t hash_len; |
| 3353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3354 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3355 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3356 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3357 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3358 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3359 | hash, expected_size - 1, &hash_len), |
| 3360 | PSA_ERROR_BUFFER_TOO_SMALL); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3361 | |
| 3362 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3363 | PSA_DONE(); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3364 | } |
| 3365 | /* END_CASE */ |
| 3366 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3367 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3368 | void hash_clone_source_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3369 | { |
| 3370 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3371 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3372 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 3373 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3374 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3375 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3376 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3377 | size_t hash_len; |
| 3378 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3379 | PSA_ASSERT(psa_crypto_init()); |
| 3380 | PSA_ASSERT(psa_hash_setup(&op_source, alg)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3381 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3382 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3383 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3384 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3385 | hash, sizeof(hash), &hash_len)); |
| 3386 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3387 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3389 | TEST_EQUAL(psa_hash_clone(&op_source, &op_setup), |
| 3390 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3392 | PSA_ASSERT(psa_hash_clone(&op_source, &op_init)); |
| 3393 | PSA_ASSERT(psa_hash_finish(&op_init, |
| 3394 | hash, sizeof(hash), &hash_len)); |
| 3395 | PSA_ASSERT(psa_hash_clone(&op_source, &op_finished)); |
| 3396 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3397 | hash, sizeof(hash), &hash_len)); |
| 3398 | PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted)); |
| 3399 | PSA_ASSERT(psa_hash_finish(&op_aborted, |
| 3400 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3401 | |
| 3402 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3403 | psa_hash_abort(&op_source); |
| 3404 | psa_hash_abort(&op_init); |
| 3405 | psa_hash_abort(&op_setup); |
| 3406 | psa_hash_abort(&op_finished); |
| 3407 | psa_hash_abort(&op_aborted); |
| 3408 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3409 | } |
| 3410 | /* END_CASE */ |
| 3411 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3412 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3413 | void hash_clone_target_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3414 | { |
| 3415 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3416 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3417 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3418 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3419 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3420 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3421 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 3422 | size_t hash_len; |
| 3423 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3424 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3425 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3426 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3427 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3428 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3429 | hash, sizeof(hash), &hash_len)); |
| 3430 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3431 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3433 | PSA_ASSERT(psa_hash_clone(&op_setup, &op_target)); |
| 3434 | PSA_ASSERT(psa_hash_finish(&op_target, |
| 3435 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3436 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3437 | TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE); |
| 3438 | TEST_EQUAL(psa_hash_clone(&op_finished, &op_target), |
| 3439 | PSA_ERROR_BAD_STATE); |
| 3440 | TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target), |
| 3441 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3442 | |
| 3443 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3444 | psa_hash_abort(&op_target); |
| 3445 | psa_hash_abort(&op_init); |
| 3446 | psa_hash_abort(&op_setup); |
| 3447 | psa_hash_abort(&op_finished); |
| 3448 | psa_hash_abort(&op_aborted); |
| 3449 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3450 | } |
| 3451 | /* END_CASE */ |
| 3452 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3453 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3454 | void mac_operation_init() |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3455 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3456 | const uint8_t input[1] = { 0 }; |
| 3457 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3458 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3459 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3460 | * 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] | 3461 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3462 | psa_mac_operation_t func = psa_mac_operation_init(); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3463 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 3464 | psa_mac_operation_t zero; |
| 3465 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3466 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3467 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3468 | /* A freshly-initialized MAC operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3469 | TEST_EQUAL(psa_mac_update(&func, |
| 3470 | input, sizeof(input)), |
| 3471 | PSA_ERROR_BAD_STATE); |
| 3472 | TEST_EQUAL(psa_mac_update(&init, |
| 3473 | input, sizeof(input)), |
| 3474 | PSA_ERROR_BAD_STATE); |
| 3475 | TEST_EQUAL(psa_mac_update(&zero, |
| 3476 | input, sizeof(input)), |
| 3477 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3478 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3479 | /* A default MAC operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3480 | PSA_ASSERT(psa_mac_abort(&func)); |
| 3481 | PSA_ASSERT(psa_mac_abort(&init)); |
| 3482 | PSA_ASSERT(psa_mac_abort(&zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3483 | } |
| 3484 | /* END_CASE */ |
| 3485 | |
| 3486 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3487 | void mac_setup(int key_type_arg, |
| 3488 | data_t *key, |
| 3489 | int alg_arg, |
| 3490 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3491 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3492 | psa_key_type_t key_type = key_type_arg; |
| 3493 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3494 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3495 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3496 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3497 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3498 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3499 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3500 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3501 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3502 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3503 | if (!exercise_mac_setup(key_type, key->x, key->len, alg, |
| 3504 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3505 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3506 | } |
| 3507 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3508 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3509 | /* The operation object should be reusable. */ |
| 3510 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3511 | if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 3512 | smoke_test_key_data, |
| 3513 | sizeof(smoke_test_key_data), |
| 3514 | KNOWN_SUPPORTED_MAC_ALG, |
| 3515 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3516 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3517 | } |
| 3518 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3519 | #endif |
| 3520 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3521 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3522 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3523 | } |
| 3524 | /* END_CASE */ |
| 3525 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3526 | /* 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] | 3527 | void mac_bad_order() |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3528 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3529 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3530 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 3531 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3532 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3533 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3534 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3535 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa |
| 3536 | }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3537 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3538 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3539 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 3540 | size_t sign_mac_length = 0; |
| 3541 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3542 | const uint8_t verify_mac[] = { |
| 3543 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 3544 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3545 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 |
| 3546 | }; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3547 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3548 | PSA_ASSERT(psa_crypto_init()); |
| 3549 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 3550 | psa_set_key_algorithm(&attributes, alg); |
| 3551 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3552 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3553 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3554 | &key)); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3555 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3556 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3557 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3558 | PSA_ERROR_BAD_STATE); |
| 3559 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3560 | |
| 3561 | /* Call sign finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3562 | TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac), |
| 3563 | &sign_mac_length), |
| 3564 | PSA_ERROR_BAD_STATE); |
| 3565 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3566 | |
| 3567 | /* Call verify finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3568 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3569 | verify_mac, sizeof(verify_mac)), |
| 3570 | PSA_ERROR_BAD_STATE); |
| 3571 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3572 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3573 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3574 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3575 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3576 | TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg), |
| 3577 | PSA_ERROR_BAD_STATE); |
| 3578 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3579 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3580 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3581 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3582 | /* Call update after sign finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3583 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3584 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3585 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3586 | sign_mac, sizeof(sign_mac), |
| 3587 | &sign_mac_length)); |
| 3588 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3589 | PSA_ERROR_BAD_STATE); |
| 3590 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3591 | |
| 3592 | /* Call update after verify finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3593 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3594 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3595 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3596 | verify_mac, sizeof(verify_mac))); |
| 3597 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3598 | PSA_ERROR_BAD_STATE); |
| 3599 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3600 | |
| 3601 | /* Call sign finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3602 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3603 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3604 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3605 | sign_mac, sizeof(sign_mac), |
| 3606 | &sign_mac_length)); |
| 3607 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3608 | sign_mac, sizeof(sign_mac), |
| 3609 | &sign_mac_length), |
| 3610 | PSA_ERROR_BAD_STATE); |
| 3611 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3612 | |
| 3613 | /* Call verify finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3614 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3615 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3616 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3617 | verify_mac, sizeof(verify_mac))); |
| 3618 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3619 | verify_mac, sizeof(verify_mac)), |
| 3620 | PSA_ERROR_BAD_STATE); |
| 3621 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3622 | |
| 3623 | /* Setup sign but try verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3624 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3625 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3626 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3627 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3628 | verify_mac, sizeof(verify_mac)), |
| 3629 | PSA_ERROR_BAD_STATE); |
| 3630 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3631 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3632 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3633 | |
| 3634 | /* Setup verify but try sign. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3635 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3636 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3637 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3638 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3639 | sign_mac, sizeof(sign_mac), |
| 3640 | &sign_mac_length), |
| 3641 | PSA_ERROR_BAD_STATE); |
| 3642 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3643 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3644 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3645 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3646 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3647 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3648 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3649 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3650 | } |
| 3651 | /* END_CASE */ |
| 3652 | |
| 3653 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3654 | void mac_sign_verify_multi(int key_type_arg, |
| 3655 | data_t *key_data, |
| 3656 | int alg_arg, |
| 3657 | data_t *input, |
| 3658 | int is_verify, |
| 3659 | data_t *expected_mac) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3660 | { |
| 3661 | size_t data_part_len = 0; |
| 3662 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3663 | 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] | 3664 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3665 | mbedtls_test_set_step(2000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3666 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3667 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3668 | alg_arg, |
| 3669 | input, data_part_len, |
| 3670 | expected_mac, |
| 3671 | is_verify, 0) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3672 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3673 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3674 | |
| 3675 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3676 | mbedtls_test_set_step(3000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3677 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3678 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3679 | alg_arg, |
| 3680 | input, data_part_len, |
| 3681 | expected_mac, |
| 3682 | is_verify, 1) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3683 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3684 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3685 | } |
| 3686 | |
| 3687 | /* Goto is required to silence warnings about unused labels, as we |
| 3688 | * don't actually do any test assertions in this function. */ |
| 3689 | goto exit; |
| 3690 | } |
| 3691 | /* END_CASE */ |
| 3692 | |
| 3693 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3694 | void mac_sign(int key_type_arg, |
| 3695 | data_t *key_data, |
| 3696 | int alg_arg, |
| 3697 | data_t *input, |
| 3698 | data_t *expected_mac) |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3699 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3700 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3701 | psa_key_type_t key_type = key_type_arg; |
| 3702 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3703 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3704 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3705 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3706 | size_t mac_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3707 | 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] | 3708 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3709 | const size_t output_sizes_to_test[] = { |
| 3710 | 0, |
| 3711 | 1, |
| 3712 | expected_mac->len - 1, |
| 3713 | expected_mac->len, |
| 3714 | expected_mac->len + 1, |
| 3715 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3716 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3717 | TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3718 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3719 | TEST_ASSERT(expected_mac->len == mac_buffer_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3720 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3721 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3722 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3723 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 3724 | psa_set_key_algorithm(&attributes, alg); |
| 3725 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3726 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3727 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3728 | &key)); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3729 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3730 | 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] | 3731 | const size_t output_size = output_sizes_to_test[i]; |
| 3732 | psa_status_t expected_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3733 | (output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3734 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3735 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3736 | mbedtls_test_set_step(output_size); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3737 | TEST_CALLOC(actual_mac, output_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3738 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3739 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3740 | TEST_EQUAL(psa_mac_compute(key, alg, |
| 3741 | input->x, input->len, |
| 3742 | actual_mac, output_size, &mac_length), |
| 3743 | expected_status); |
| 3744 | if (expected_status == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3745 | TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3746 | actual_mac, mac_length); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3747 | } |
| 3748 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3749 | if (output_size > 0) { |
| 3750 | memset(actual_mac, 0, output_size); |
| 3751 | } |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3752 | |
| 3753 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3754 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3755 | PSA_ASSERT(psa_mac_update(&operation, |
| 3756 | input->x, input->len)); |
| 3757 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3758 | actual_mac, output_size, |
| 3759 | &mac_length), |
| 3760 | expected_status); |
| 3761 | PSA_ASSERT(psa_mac_abort(&operation)); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3762 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3763 | if (expected_status == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3764 | TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3765 | actual_mac, mac_length); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3766 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3767 | mbedtls_free(actual_mac); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3768 | actual_mac = NULL; |
| 3769 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3770 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3771 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3772 | psa_mac_abort(&operation); |
| 3773 | psa_destroy_key(key); |
| 3774 | PSA_DONE(); |
| 3775 | mbedtls_free(actual_mac); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3776 | } |
| 3777 | /* END_CASE */ |
| 3778 | |
| 3779 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3780 | void mac_verify(int key_type_arg, |
| 3781 | data_t *key_data, |
| 3782 | int alg_arg, |
| 3783 | data_t *input, |
| 3784 | data_t *expected_mac) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3785 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3786 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3787 | psa_key_type_t key_type = key_type_arg; |
| 3788 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3789 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3790 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3791 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3792 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3793 | TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3794 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3795 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3796 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3797 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 3798 | psa_set_key_algorithm(&attributes, alg); |
| 3799 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3800 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3801 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3802 | &key)); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3803 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3804 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3805 | PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len, |
| 3806 | expected_mac->x, expected_mac->len)); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3807 | |
| 3808 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3809 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3810 | PSA_ASSERT(psa_mac_update(&operation, |
| 3811 | input->x, input->len)); |
| 3812 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3813 | expected_mac->x, |
| 3814 | expected_mac->len)); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3815 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3816 | /* Test a MAC that's too short, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3817 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3818 | input->x, input->len, |
| 3819 | expected_mac->x, |
| 3820 | expected_mac->len - 1), |
| 3821 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3822 | |
| 3823 | /* Test a MAC that's too short, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3824 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3825 | PSA_ASSERT(psa_mac_update(&operation, |
| 3826 | input->x, input->len)); |
| 3827 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3828 | expected_mac->x, |
| 3829 | expected_mac->len - 1), |
| 3830 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3831 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3832 | /* Test a MAC that's too long, one-shot case. */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3833 | TEST_CALLOC(perturbed_mac, expected_mac->len + 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3834 | memcpy(perturbed_mac, expected_mac->x, expected_mac->len); |
| 3835 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3836 | input->x, input->len, |
| 3837 | perturbed_mac, expected_mac->len + 1), |
| 3838 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3839 | |
| 3840 | /* Test a MAC that's too long, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3841 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3842 | PSA_ASSERT(psa_mac_update(&operation, |
| 3843 | input->x, input->len)); |
| 3844 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3845 | perturbed_mac, |
| 3846 | expected_mac->len + 1), |
| 3847 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3848 | |
| 3849 | /* Test changing one byte. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3850 | for (size_t i = 0; i < expected_mac->len; i++) { |
| 3851 | mbedtls_test_set_step(i); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3852 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3853 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3854 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3855 | input->x, input->len, |
| 3856 | perturbed_mac, expected_mac->len), |
| 3857 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3858 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3859 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3860 | PSA_ASSERT(psa_mac_update(&operation, |
| 3861 | input->x, input->len)); |
| 3862 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3863 | perturbed_mac, |
| 3864 | expected_mac->len), |
| 3865 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3866 | perturbed_mac[i] ^= 1; |
| 3867 | } |
| 3868 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3869 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3870 | psa_mac_abort(&operation); |
| 3871 | psa_destroy_key(key); |
| 3872 | PSA_DONE(); |
| 3873 | mbedtls_free(perturbed_mac); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3874 | } |
| 3875 | /* END_CASE */ |
| 3876 | |
| 3877 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3878 | void cipher_operation_init() |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3879 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3880 | const uint8_t input[1] = { 0 }; |
| 3881 | unsigned char output[1] = { 0 }; |
| 3882 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3883 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3884 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3885 | * 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] | 3886 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3887 | psa_cipher_operation_t func = psa_cipher_operation_init(); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3888 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3889 | psa_cipher_operation_t zero; |
| 3890 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3891 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3892 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3893 | /* A freshly-initialized cipher operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3894 | TEST_EQUAL(psa_cipher_update(&func, |
| 3895 | input, sizeof(input), |
| 3896 | output, sizeof(output), |
| 3897 | &output_length), |
| 3898 | PSA_ERROR_BAD_STATE); |
| 3899 | TEST_EQUAL(psa_cipher_update(&init, |
| 3900 | input, sizeof(input), |
| 3901 | output, sizeof(output), |
| 3902 | &output_length), |
| 3903 | PSA_ERROR_BAD_STATE); |
| 3904 | TEST_EQUAL(psa_cipher_update(&zero, |
| 3905 | input, sizeof(input), |
| 3906 | output, sizeof(output), |
| 3907 | &output_length), |
| 3908 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3909 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3910 | /* A default cipher operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3911 | PSA_ASSERT(psa_cipher_abort(&func)); |
| 3912 | PSA_ASSERT(psa_cipher_abort(&init)); |
| 3913 | PSA_ASSERT(psa_cipher_abort(&zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3914 | } |
| 3915 | /* END_CASE */ |
| 3916 | |
| 3917 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3918 | void cipher_setup(int key_type_arg, |
| 3919 | data_t *key, |
| 3920 | int alg_arg, |
| 3921 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3922 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3923 | psa_key_type_t key_type = key_type_arg; |
| 3924 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3925 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3926 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3927 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3928 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3929 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3930 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3931 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3932 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3933 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3934 | if (!exercise_cipher_setup(key_type, key->x, key->len, alg, |
| 3935 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3936 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3937 | } |
| 3938 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3939 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3940 | /* The operation object should be reusable. */ |
| 3941 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3942 | if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3943 | smoke_test_key_data, |
| 3944 | sizeof(smoke_test_key_data), |
| 3945 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3946 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3947 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3948 | } |
| 3949 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3950 | #endif |
| 3951 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3952 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3953 | psa_cipher_abort(&operation); |
| 3954 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3955 | } |
| 3956 | /* END_CASE */ |
| 3957 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3958 | /* 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] | 3959 | void cipher_bad_order() |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3960 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3961 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3962 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3963 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3964 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3965 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3966 | 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] | 3967 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3968 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3969 | 0xaa, 0xaa, 0xaa, 0xaa |
| 3970 | }; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3971 | const uint8_t text[] = { |
| 3972 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3973 | 0xbb, 0xbb, 0xbb, 0xbb |
| 3974 | }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3975 | 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] | 3976 | size_t length = 0; |
| 3977 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3978 | PSA_ASSERT(psa_crypto_init()); |
| 3979 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3980 | psa_set_key_algorithm(&attributes, alg); |
| 3981 | psa_set_key_type(&attributes, key_type); |
| 3982 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3983 | &key)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3984 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3985 | /* Call encrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3986 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3987 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3988 | TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg), |
| 3989 | PSA_ERROR_BAD_STATE); |
| 3990 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3991 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3992 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3993 | |
| 3994 | /* Call decrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3995 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3996 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3997 | TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg), |
| 3998 | PSA_ERROR_BAD_STATE); |
| 3999 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 4000 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4001 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 4002 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4003 | /* Generate an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4004 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 4005 | buffer, sizeof(buffer), |
| 4006 | &length), |
| 4007 | PSA_ERROR_BAD_STATE); |
| 4008 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4009 | |
| 4010 | /* Generate an IV twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4011 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4012 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 4013 | buffer, sizeof(buffer), |
| 4014 | &length)); |
| 4015 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 4016 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 4017 | buffer, sizeof(buffer), |
| 4018 | &length), |
| 4019 | PSA_ERROR_BAD_STATE); |
| 4020 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 4021 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4022 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4023 | |
| 4024 | /* Generate an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4025 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4026 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 4027 | iv, sizeof(iv))); |
| 4028 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 4029 | buffer, sizeof(buffer), |
| 4030 | &length), |
| 4031 | PSA_ERROR_BAD_STATE); |
| 4032 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4033 | |
| 4034 | /* Set an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4035 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 4036 | iv, sizeof(iv)), |
| 4037 | PSA_ERROR_BAD_STATE); |
| 4038 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4039 | |
| 4040 | /* Set an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4041 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4042 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 4043 | iv, sizeof(iv))); |
| 4044 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 4045 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 4046 | iv, sizeof(iv)), |
| 4047 | PSA_ERROR_BAD_STATE); |
| 4048 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 4049 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4050 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4051 | |
| 4052 | /* Set an IV after it's already generated. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4053 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4054 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 4055 | buffer, sizeof(buffer), |
| 4056 | &length)); |
| 4057 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 4058 | iv, sizeof(iv)), |
| 4059 | PSA_ERROR_BAD_STATE); |
| 4060 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4061 | |
| 4062 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4063 | TEST_EQUAL(psa_cipher_update(&operation, |
| 4064 | text, sizeof(text), |
| 4065 | buffer, sizeof(buffer), |
| 4066 | &length), |
| 4067 | PSA_ERROR_BAD_STATE); |
| 4068 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4069 | |
| 4070 | /* Call update without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4071 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4072 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 4073 | TEST_EQUAL(psa_cipher_update(&operation, |
| 4074 | text, sizeof(text), |
| 4075 | buffer, sizeof(buffer), |
| 4076 | &length), |
| 4077 | PSA_ERROR_BAD_STATE); |
| 4078 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 4079 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4080 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4081 | |
| 4082 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4083 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4084 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 4085 | iv, sizeof(iv))); |
| 4086 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4087 | buffer, sizeof(buffer), &length)); |
| 4088 | TEST_EQUAL(psa_cipher_update(&operation, |
| 4089 | text, sizeof(text), |
| 4090 | buffer, sizeof(buffer), |
| 4091 | &length), |
| 4092 | PSA_ERROR_BAD_STATE); |
| 4093 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4094 | |
| 4095 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4096 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 4097 | buffer, sizeof(buffer), &length), |
| 4098 | PSA_ERROR_BAD_STATE); |
| 4099 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4100 | |
| 4101 | /* Call finish without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4102 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4103 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 4104 | * for cipher modes with padding. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4105 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 4106 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 4107 | buffer, sizeof(buffer), &length), |
| 4108 | PSA_ERROR_BAD_STATE); |
| 4109 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 4110 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4111 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4112 | |
| 4113 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4114 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4115 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 4116 | iv, sizeof(iv))); |
| 4117 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4118 | buffer, sizeof(buffer), &length)); |
| 4119 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 4120 | buffer, sizeof(buffer), &length), |
| 4121 | PSA_ERROR_BAD_STATE); |
| 4122 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4123 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4124 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 4125 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4126 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4127 | psa_cipher_abort(&operation); |
| 4128 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4129 | } |
| 4130 | /* END_CASE */ |
| 4131 | |
| 4132 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4133 | void cipher_encrypt_fail(int alg_arg, |
| 4134 | int key_type_arg, |
| 4135 | data_t *key_data, |
| 4136 | data_t *input, |
| 4137 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4138 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4139 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4140 | psa_status_t status; |
| 4141 | psa_key_type_t key_type = key_type_arg; |
| 4142 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 4143 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4144 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 }; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4145 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; |
| 4146 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4147 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4148 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4149 | size_t output_length = 0; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4150 | size_t function_output_length; |
| 4151 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4152 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4153 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4154 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 4155 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4156 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4157 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4158 | psa_set_key_algorithm(&attributes, alg); |
| 4159 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4160 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4161 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 4162 | input->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4163 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4164 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4165 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4166 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4167 | } |
| 4168 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4169 | /* Encrypt, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4170 | status = psa_cipher_encrypt(key, alg, input->x, input->len, output, |
| 4171 | output_buffer_size, &output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4172 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4173 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4174 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4175 | /* Encrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4176 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
| 4177 | if (status == PSA_SUCCESS) { |
| 4178 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 4179 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 4180 | iv, iv_size, |
| 4181 | &iv_length)); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4182 | } |
| 4183 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4184 | status = psa_cipher_update(&operation, input->x, input->len, |
| 4185 | output, output_buffer_size, |
| 4186 | &function_output_length); |
| 4187 | if (status == PSA_SUCCESS) { |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4188 | output_length += function_output_length; |
| 4189 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4190 | status = psa_cipher_finish(&operation, output + output_length, |
| 4191 | output_buffer_size - output_length, |
| 4192 | &function_output_length); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4193 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4194 | TEST_EQUAL(status, expected_status); |
| 4195 | } else { |
| 4196 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4197 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4198 | } else { |
| 4199 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4200 | } |
| 4201 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4202 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4203 | psa_cipher_abort(&operation); |
| 4204 | mbedtls_free(output); |
| 4205 | psa_destroy_key(key); |
| 4206 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4207 | } |
| 4208 | /* END_CASE */ |
| 4209 | |
| 4210 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4211 | void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data, |
| 4212 | data_t *input, int iv_length, |
| 4213 | int expected_result) |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4214 | { |
| 4215 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4216 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4217 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4218 | size_t output_buffer_size = 0; |
| 4219 | unsigned char *output = NULL; |
| 4220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4221 | 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] | 4222 | TEST_CALLOC(output, output_buffer_size); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4223 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4224 | PSA_ASSERT(psa_crypto_init()); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4225 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4226 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4227 | psa_set_key_algorithm(&attributes, alg); |
| 4228 | psa_set_key_type(&attributes, key_type); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4230 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4231 | &key)); |
| 4232 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4233 | TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output, |
| 4234 | iv_length)); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4235 | |
| 4236 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4237 | psa_cipher_abort(&operation); |
| 4238 | mbedtls_free(output); |
| 4239 | psa_destroy_key(key); |
| 4240 | PSA_DONE(); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4241 | } |
| 4242 | /* END_CASE */ |
| 4243 | |
| 4244 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4245 | void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data, |
| 4246 | data_t *plaintext, data_t *ciphertext) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4247 | { |
| 4248 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4249 | psa_key_type_t key_type = key_type_arg; |
| 4250 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 4251 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4252 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4253 | unsigned char *output = NULL; |
| 4254 | size_t output_buffer_size = 0; |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4255 | size_t output_length, length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4256 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4257 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4258 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4259 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4260 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4261 | TEST_LE_U(ciphertext->len, |
| 4262 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len)); |
| 4263 | TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len), |
| 4264 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len)); |
| 4265 | TEST_LE_U(plaintext->len, |
| 4266 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len)); |
| 4267 | TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len), |
| 4268 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len)); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4269 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4270 | |
| 4271 | /* Set up key and output buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4272 | psa_set_key_usage_flags(&attributes, |
| 4273 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4274 | psa_set_key_algorithm(&attributes, alg); |
| 4275 | psa_set_key_type(&attributes, key_type); |
| 4276 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4277 | &key)); |
| 4278 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 4279 | plaintext->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4280 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4281 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4282 | /* set_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4283 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4284 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 4285 | PSA_ERROR_BAD_STATE); |
| 4286 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 4287 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 4288 | PSA_ERROR_BAD_STATE); |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 4289 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4290 | /* generate_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4291 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4292 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 4293 | &length), |
| 4294 | PSA_ERROR_BAD_STATE); |
| 4295 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 4296 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 4297 | &length), |
| 4298 | PSA_ERROR_BAD_STATE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4299 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4300 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4301 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4302 | output_length = 0; |
| 4303 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4304 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4305 | plaintext->x, plaintext->len, |
| 4306 | output, output_buffer_size, |
| 4307 | &length)); |
| 4308 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4309 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4310 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4311 | mbedtls_buffer_offset(output, output_length), |
| 4312 | output_buffer_size - output_length, |
| 4313 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4314 | output_length += length; |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4315 | TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4316 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4317 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4318 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4319 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4320 | output_length = 0; |
| 4321 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4322 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4323 | ciphertext->x, ciphertext->len, |
| 4324 | output, output_buffer_size, |
| 4325 | &length)); |
| 4326 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4327 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4328 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4329 | mbedtls_buffer_offset(output, output_length), |
| 4330 | output_buffer_size - output_length, |
| 4331 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4332 | output_length += length; |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4333 | TEST_MEMORY_COMPARE(plaintext->x, plaintext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4334 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4335 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4336 | /* One-shot encryption */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4337 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4338 | PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len, |
| 4339 | output, output_buffer_size, |
| 4340 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4341 | TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4342 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4343 | |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4344 | /* One-shot decryption */ |
| 4345 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4346 | PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len, |
| 4347 | output, output_buffer_size, |
| 4348 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4349 | TEST_MEMORY_COMPARE(plaintext->x, plaintext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4350 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4351 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4352 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4353 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4354 | mbedtls_free(output); |
| 4355 | psa_cipher_abort(&operation); |
| 4356 | psa_destroy_key(key); |
| 4357 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4358 | } |
| 4359 | /* END_CASE */ |
| 4360 | |
| 4361 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4362 | 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] | 4363 | { |
| 4364 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4365 | psa_algorithm_t alg = alg_arg; |
| 4366 | psa_key_type_t key_type = key_type_arg; |
| 4367 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4368 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4369 | psa_status_t status; |
| 4370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4371 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4372 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4373 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4374 | psa_set_key_algorithm(&attributes, alg); |
| 4375 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4376 | |
| 4377 | /* Usage of either of these two size macros would cause divide by zero |
| 4378 | * with incorrect key types previously. Input length should be irrelevant |
| 4379 | * here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4380 | TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16), |
| 4381 | 0); |
| 4382 | TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4383 | |
| 4384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4385 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4386 | &key)); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4387 | |
| 4388 | /* Should fail due to invalid alg type (to support invalid key type). |
| 4389 | * Encrypt or decrypt will end up in the same place. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4390 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4392 | TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4393 | |
| 4394 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4395 | psa_cipher_abort(&operation); |
| 4396 | psa_destroy_key(key); |
| 4397 | PSA_DONE(); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4398 | } |
| 4399 | /* END_CASE */ |
| 4400 | |
| 4401 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4402 | void cipher_encrypt_validation(int alg_arg, |
| 4403 | int key_type_arg, |
| 4404 | data_t *key_data, |
| 4405 | data_t *input) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4406 | { |
| 4407 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4408 | psa_key_type_t key_type = key_type_arg; |
| 4409 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4410 | size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4411 | unsigned char *output1 = NULL; |
| 4412 | size_t output1_buffer_size = 0; |
| 4413 | size_t output1_length = 0; |
| 4414 | unsigned char *output2 = NULL; |
| 4415 | size_t output2_buffer_size = 0; |
| 4416 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4417 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4418 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4419 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4420 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4421 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4422 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4423 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4424 | psa_set_key_algorithm(&attributes, alg); |
| 4425 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4426 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4427 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4428 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4429 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4430 | TEST_CALLOC(output1, output1_buffer_size); |
| 4431 | TEST_CALLOC(output2, output2_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4433 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4434 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4435 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 4436 | /* The one-shot cipher encryption uses generated iv so validating |
| 4437 | the output is not possible. Validating with multipart encryption. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4438 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1, |
| 4439 | output1_buffer_size, &output1_length)); |
| 4440 | TEST_LE_U(output1_length, |
| 4441 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4442 | TEST_LE_U(output1_length, |
| 4443 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4444 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4445 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4446 | PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4447 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4448 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4449 | input->x, input->len, |
| 4450 | output2, output2_buffer_size, |
| 4451 | &function_output_length)); |
| 4452 | TEST_LE_U(function_output_length, |
| 4453 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4454 | TEST_LE_U(function_output_length, |
| 4455 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4456 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4457 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4458 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4459 | output2 + output2_length, |
| 4460 | output2_buffer_size - output2_length, |
| 4461 | &function_output_length)); |
| 4462 | TEST_LE_U(function_output_length, |
| 4463 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4464 | TEST_LE_U(function_output_length, |
| 4465 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4466 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4467 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4468 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4469 | TEST_MEMORY_COMPARE(output1 + iv_size, output1_length - iv_size, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4470 | output2, output2_length); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4471 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4472 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4473 | psa_cipher_abort(&operation); |
| 4474 | mbedtls_free(output1); |
| 4475 | mbedtls_free(output2); |
| 4476 | psa_destroy_key(key); |
| 4477 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4478 | } |
| 4479 | /* END_CASE */ |
| 4480 | |
| 4481 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4482 | void cipher_encrypt_multipart(int alg_arg, int key_type_arg, |
| 4483 | data_t *key_data, data_t *iv, |
| 4484 | data_t *input, |
| 4485 | int first_part_size_arg, |
| 4486 | int output1_length_arg, int output2_length_arg, |
| 4487 | data_t *expected_output, |
| 4488 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4489 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4490 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4491 | psa_key_type_t key_type = key_type_arg; |
| 4492 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4493 | psa_status_t status; |
| 4494 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4495 | size_t first_part_size = first_part_size_arg; |
| 4496 | size_t output1_length = output1_length_arg; |
| 4497 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4498 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4499 | size_t output_buffer_size = 0; |
| 4500 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4501 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4502 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4503 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4504 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4505 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4506 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4507 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4508 | psa_set_key_algorithm(&attributes, alg); |
| 4509 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4510 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4511 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4512 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4513 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4514 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4515 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4516 | if (iv->len > 0) { |
| 4517 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4518 | } |
| 4519 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4520 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4521 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4522 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4523 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4524 | TEST_LE_U(first_part_size, input->len); |
| 4525 | PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size, |
| 4526 | output, output_buffer_size, |
| 4527 | &function_output_length)); |
| 4528 | TEST_ASSERT(function_output_length == output1_length); |
| 4529 | TEST_LE_U(function_output_length, |
| 4530 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4531 | TEST_LE_U(function_output_length, |
| 4532 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4533 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4534 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4535 | if (first_part_size < input->len) { |
| 4536 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4537 | input->x + first_part_size, |
| 4538 | input->len - first_part_size, |
| 4539 | (output_buffer_size == 0 ? NULL : |
| 4540 | output + total_output_length), |
| 4541 | output_buffer_size - total_output_length, |
| 4542 | &function_output_length)); |
| 4543 | TEST_ASSERT(function_output_length == output2_length); |
| 4544 | TEST_LE_U(function_output_length, |
| 4545 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4546 | alg, |
| 4547 | input->len - first_part_size)); |
| 4548 | TEST_LE_U(function_output_length, |
| 4549 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4550 | total_output_length += function_output_length; |
| 4551 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4552 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4553 | status = psa_cipher_finish(&operation, |
| 4554 | (output_buffer_size == 0 ? NULL : |
| 4555 | output + total_output_length), |
| 4556 | output_buffer_size - total_output_length, |
| 4557 | &function_output_length); |
| 4558 | TEST_LE_U(function_output_length, |
| 4559 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4560 | TEST_LE_U(function_output_length, |
| 4561 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4562 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4563 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4564 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4565 | if (expected_status == PSA_SUCCESS) { |
| 4566 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4567 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4568 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4569 | output, total_output_length); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4570 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4571 | |
| 4572 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4573 | psa_cipher_abort(&operation); |
| 4574 | mbedtls_free(output); |
| 4575 | psa_destroy_key(key); |
| 4576 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4577 | } |
| 4578 | /* END_CASE */ |
| 4579 | |
| 4580 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4581 | void cipher_decrypt_multipart(int alg_arg, int key_type_arg, |
| 4582 | data_t *key_data, data_t *iv, |
| 4583 | data_t *input, |
| 4584 | int first_part_size_arg, |
| 4585 | int output1_length_arg, int output2_length_arg, |
| 4586 | data_t *expected_output, |
| 4587 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4588 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4589 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4590 | psa_key_type_t key_type = key_type_arg; |
| 4591 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4592 | psa_status_t status; |
| 4593 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4594 | size_t first_part_size = first_part_size_arg; |
| 4595 | size_t output1_length = output1_length_arg; |
| 4596 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4597 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4598 | size_t output_buffer_size = 0; |
| 4599 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4600 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4601 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4602 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4603 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4604 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4605 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4606 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4607 | psa_set_key_algorithm(&attributes, alg); |
| 4608 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4609 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4610 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4611 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4612 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4613 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4614 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4615 | if (iv->len > 0) { |
| 4616 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4617 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4618 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4619 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4620 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4621 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4622 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4623 | TEST_LE_U(first_part_size, input->len); |
| 4624 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4625 | input->x, first_part_size, |
| 4626 | output, output_buffer_size, |
| 4627 | &function_output_length)); |
| 4628 | TEST_ASSERT(function_output_length == output1_length); |
| 4629 | TEST_LE_U(function_output_length, |
| 4630 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4631 | TEST_LE_U(function_output_length, |
| 4632 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4633 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4634 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4635 | if (first_part_size < input->len) { |
| 4636 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4637 | input->x + first_part_size, |
| 4638 | input->len - first_part_size, |
| 4639 | (output_buffer_size == 0 ? NULL : |
| 4640 | output + total_output_length), |
| 4641 | output_buffer_size - total_output_length, |
| 4642 | &function_output_length)); |
| 4643 | TEST_ASSERT(function_output_length == output2_length); |
| 4644 | TEST_LE_U(function_output_length, |
| 4645 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4646 | alg, |
| 4647 | input->len - first_part_size)); |
| 4648 | TEST_LE_U(function_output_length, |
| 4649 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4650 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4651 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4652 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4653 | status = psa_cipher_finish(&operation, |
| 4654 | (output_buffer_size == 0 ? NULL : |
| 4655 | output + total_output_length), |
| 4656 | output_buffer_size - total_output_length, |
| 4657 | &function_output_length); |
| 4658 | TEST_LE_U(function_output_length, |
| 4659 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4660 | TEST_LE_U(function_output_length, |
| 4661 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4662 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4663 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4664 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4665 | if (expected_status == PSA_SUCCESS) { |
| 4666 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4667 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4668 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4669 | output, total_output_length); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4670 | } |
| 4671 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4672 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4673 | psa_cipher_abort(&operation); |
| 4674 | mbedtls_free(output); |
| 4675 | psa_destroy_key(key); |
| 4676 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4677 | } |
| 4678 | /* END_CASE */ |
| 4679 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4680 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4681 | void cipher_decrypt_fail(int alg_arg, |
| 4682 | int key_type_arg, |
| 4683 | data_t *key_data, |
| 4684 | data_t *iv, |
| 4685 | data_t *input_arg, |
| 4686 | int expected_status_arg) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4687 | { |
| 4688 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4689 | psa_status_t status; |
| 4690 | psa_key_type_t key_type = key_type_arg; |
| 4691 | psa_algorithm_t alg = alg_arg; |
| 4692 | psa_status_t expected_status = expected_status_arg; |
| 4693 | unsigned char *input = NULL; |
| 4694 | size_t input_buffer_size = 0; |
| 4695 | unsigned char *output = NULL; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4696 | unsigned char *output_multi = NULL; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4697 | size_t output_buffer_size = 0; |
| 4698 | size_t output_length = 0; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4699 | size_t function_output_length; |
| 4700 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4701 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4702 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4703 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 4704 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4705 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4706 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4707 | psa_set_key_algorithm(&attributes, alg); |
| 4708 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4709 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4710 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4711 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4712 | } |
| 4713 | |
| 4714 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4715 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4716 | if (input_buffer_size > 0) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4717 | TEST_CALLOC(input, input_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4718 | memcpy(input, iv->x, iv->len); |
| 4719 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4720 | } |
| 4721 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4722 | 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] | 4723 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4724 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4725 | /* Decrypt, one-short */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4726 | status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4727 | output_buffer_size, &output_length); |
| 4728 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4729 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4730 | /* Decrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4731 | status = psa_cipher_decrypt_setup(&operation, key, alg); |
| 4732 | if (status == PSA_SUCCESS) { |
| 4733 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 4734 | input_arg->len) + |
| 4735 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4736 | TEST_CALLOC(output_multi, output_buffer_size); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4737 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4738 | if (iv->len > 0) { |
| 4739 | status = psa_cipher_set_iv(&operation, iv->x, iv->len); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4740 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4741 | if (status != PSA_SUCCESS) { |
| 4742 | TEST_EQUAL(status, expected_status); |
| 4743 | } |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4744 | } |
| 4745 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4746 | if (status == PSA_SUCCESS) { |
| 4747 | status = psa_cipher_update(&operation, |
| 4748 | input_arg->x, input_arg->len, |
| 4749 | output_multi, output_buffer_size, |
| 4750 | &function_output_length); |
| 4751 | if (status == PSA_SUCCESS) { |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4752 | output_length = function_output_length; |
| 4753 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4754 | status = psa_cipher_finish(&operation, |
| 4755 | output_multi + output_length, |
| 4756 | output_buffer_size - output_length, |
| 4757 | &function_output_length); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4758 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4759 | TEST_EQUAL(status, expected_status); |
| 4760 | } else { |
| 4761 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4762 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4763 | } else { |
| 4764 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4765 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4766 | } else { |
| 4767 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4768 | } |
| 4769 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4770 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4771 | psa_cipher_abort(&operation); |
| 4772 | mbedtls_free(input); |
| 4773 | mbedtls_free(output); |
| 4774 | mbedtls_free(output_multi); |
| 4775 | psa_destroy_key(key); |
| 4776 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4777 | } |
| 4778 | /* END_CASE */ |
| 4779 | |
| 4780 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4781 | void cipher_decrypt(int alg_arg, |
| 4782 | int key_type_arg, |
| 4783 | data_t *key_data, |
| 4784 | data_t *iv, |
| 4785 | data_t *input_arg, |
| 4786 | data_t *expected_output) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4787 | { |
| 4788 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4789 | psa_key_type_t key_type = key_type_arg; |
| 4790 | psa_algorithm_t alg = alg_arg; |
| 4791 | unsigned char *input = NULL; |
| 4792 | size_t input_buffer_size = 0; |
| 4793 | unsigned char *output = NULL; |
| 4794 | size_t output_buffer_size = 0; |
| 4795 | size_t output_length = 0; |
| 4796 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4797 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4798 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4799 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4800 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4801 | psa_set_key_algorithm(&attributes, alg); |
| 4802 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4803 | |
| 4804 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4805 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4806 | if (input_buffer_size > 0) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4807 | TEST_CALLOC(input, input_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4808 | memcpy(input, iv->x, iv->len); |
| 4809 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4810 | } |
| 4811 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4812 | 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] | 4813 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4814 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4815 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4816 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4817 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4818 | PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4819 | output_buffer_size, &output_length)); |
| 4820 | TEST_LE_U(output_length, |
| 4821 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size)); |
| 4822 | TEST_LE_U(output_length, |
| 4823 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4824 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4825 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4826 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4827 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4828 | mbedtls_free(input); |
| 4829 | mbedtls_free(output); |
| 4830 | psa_destroy_key(key); |
| 4831 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4832 | } |
| 4833 | /* END_CASE */ |
| 4834 | |
| 4835 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4836 | void cipher_verify_output(int alg_arg, |
| 4837 | int key_type_arg, |
| 4838 | data_t *key_data, |
| 4839 | data_t *input) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4840 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4841 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4842 | psa_key_type_t key_type = key_type_arg; |
| 4843 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4844 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4845 | size_t output1_size = 0; |
| 4846 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4847 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4848 | size_t output2_size = 0; |
| 4849 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4850 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4851 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4852 | PSA_ASSERT(psa_crypto_init()); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4853 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4854 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4855 | psa_set_key_algorithm(&attributes, alg); |
| 4856 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4857 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4858 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4859 | &key)); |
| 4860 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4861 | TEST_CALLOC(output1, output1_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4862 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4863 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, |
| 4864 | output1, output1_size, |
| 4865 | &output1_length)); |
| 4866 | TEST_LE_U(output1_length, |
| 4867 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4868 | TEST_LE_U(output1_length, |
| 4869 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4870 | |
| 4871 | output2_size = output1_length; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4872 | TEST_CALLOC(output2, output2_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4873 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4874 | PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length, |
| 4875 | output2, output2_size, |
| 4876 | &output2_length)); |
| 4877 | TEST_LE_U(output2_length, |
| 4878 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4879 | TEST_LE_U(output2_length, |
| 4880 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4881 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4882 | TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4883 | |
| 4884 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4885 | mbedtls_free(output1); |
| 4886 | mbedtls_free(output2); |
| 4887 | psa_destroy_key(key); |
| 4888 | PSA_DONE(); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4889 | } |
| 4890 | /* END_CASE */ |
| 4891 | |
| 4892 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4893 | void cipher_verify_output_multipart(int alg_arg, |
| 4894 | int key_type_arg, |
| 4895 | data_t *key_data, |
| 4896 | data_t *input, |
| 4897 | int first_part_size_arg) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4898 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4899 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4900 | psa_key_type_t key_type = key_type_arg; |
| 4901 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4902 | size_t first_part_size = first_part_size_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4903 | unsigned char iv[16] = { 0 }; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4904 | size_t iv_size = 16; |
| 4905 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4906 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4907 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4908 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4909 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4910 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4911 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4912 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4913 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 4914 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4915 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4916 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4917 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4918 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4919 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4920 | psa_set_key_algorithm(&attributes, alg); |
| 4921 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4922 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4923 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4924 | &key)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4925 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4926 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg)); |
| 4927 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4928 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4929 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 4930 | PSA_ASSERT(psa_cipher_generate_iv(&operation1, |
| 4931 | iv, iv_size, |
| 4932 | &iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4933 | } |
| 4934 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4935 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4936 | TEST_LE_U(output1_buffer_size, |
| 4937 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4938 | TEST_CALLOC(output1, output1_buffer_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4939 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4940 | TEST_LE_U(first_part_size, input->len); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4941 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4942 | PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size, |
| 4943 | output1, output1_buffer_size, |
| 4944 | &function_output_length)); |
| 4945 | TEST_LE_U(function_output_length, |
| 4946 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4947 | TEST_LE_U(function_output_length, |
| 4948 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4949 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4950 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4951 | PSA_ASSERT(psa_cipher_update(&operation1, |
| 4952 | input->x + first_part_size, |
| 4953 | input->len - first_part_size, |
| 4954 | output1, output1_buffer_size, |
| 4955 | &function_output_length)); |
| 4956 | TEST_LE_U(function_output_length, |
| 4957 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4958 | alg, |
| 4959 | input->len - first_part_size)); |
| 4960 | TEST_LE_U(function_output_length, |
| 4961 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4962 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4963 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4964 | PSA_ASSERT(psa_cipher_finish(&operation1, |
| 4965 | output1 + output1_length, |
| 4966 | output1_buffer_size - output1_length, |
| 4967 | &function_output_length)); |
| 4968 | TEST_LE_U(function_output_length, |
| 4969 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4970 | TEST_LE_U(function_output_length, |
| 4971 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4972 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4973 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4974 | PSA_ASSERT(psa_cipher_abort(&operation1)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4975 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4976 | output2_buffer_size = output1_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4977 | TEST_LE_U(output2_buffer_size, |
| 4978 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4979 | TEST_LE_U(output2_buffer_size, |
| 4980 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4981 | TEST_CALLOC(output2, output2_buffer_size); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4982 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4983 | if (iv_length > 0) { |
| 4984 | PSA_ASSERT(psa_cipher_set_iv(&operation2, |
| 4985 | iv, iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4986 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4987 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4988 | PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size, |
| 4989 | output2, output2_buffer_size, |
| 4990 | &function_output_length)); |
| 4991 | TEST_LE_U(function_output_length, |
| 4992 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4993 | TEST_LE_U(function_output_length, |
| 4994 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4995 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4996 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4997 | PSA_ASSERT(psa_cipher_update(&operation2, |
| 4998 | output1 + first_part_size, |
| 4999 | output1_length - first_part_size, |
| 5000 | output2, output2_buffer_size, |
| 5001 | &function_output_length)); |
| 5002 | TEST_LE_U(function_output_length, |
| 5003 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 5004 | alg, |
| 5005 | output1_length - first_part_size)); |
| 5006 | TEST_LE_U(function_output_length, |
| 5007 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 5008 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 5009 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5010 | PSA_ASSERT(psa_cipher_finish(&operation2, |
| 5011 | output2 + output2_length, |
| 5012 | output2_buffer_size - output2_length, |
| 5013 | &function_output_length)); |
| 5014 | TEST_LE_U(function_output_length, |
| 5015 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 5016 | TEST_LE_U(function_output_length, |
| 5017 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 5018 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 5019 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5020 | PSA_ASSERT(psa_cipher_abort(&operation2)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 5021 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 5022 | TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 5023 | |
| 5024 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5025 | psa_cipher_abort(&operation1); |
| 5026 | psa_cipher_abort(&operation2); |
| 5027 | mbedtls_free(output1); |
| 5028 | mbedtls_free(output2); |
| 5029 | psa_destroy_key(key); |
| 5030 | PSA_DONE(); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 5031 | } |
| 5032 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 5033 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5034 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5035 | void aead_encrypt_decrypt(int key_type_arg, data_t *key_data, |
| 5036 | int alg_arg, |
| 5037 | data_t *nonce, |
| 5038 | data_t *additional_data, |
| 5039 | data_t *input_data, |
| 5040 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5041 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5042 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5043 | psa_key_type_t key_type = key_type_arg; |
| 5044 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5045 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5046 | unsigned char *output_data = NULL; |
| 5047 | size_t output_size = 0; |
| 5048 | size_t output_length = 0; |
| 5049 | unsigned char *output_data2 = NULL; |
| 5050 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 5051 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 5052 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5053 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5054 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5055 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5056 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5057 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5058 | psa_set_key_algorithm(&attributes, alg); |
| 5059 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5060 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5061 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5062 | &key)); |
| 5063 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5064 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5065 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5066 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 5067 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5068 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 5069 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5070 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 5071 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
| 5072 | TEST_EQUAL(output_size, |
| 5073 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 5074 | TEST_LE_U(output_size, |
| 5075 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5076 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5077 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5078 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5079 | status = psa_aead_encrypt(key, alg, |
| 5080 | nonce->x, nonce->len, |
| 5081 | additional_data->x, |
| 5082 | additional_data->len, |
| 5083 | input_data->x, input_data->len, |
| 5084 | output_data, output_size, |
| 5085 | &output_length); |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 5086 | |
| 5087 | /* If the operation is not supported, just skip and not fail in case the |
| 5088 | * encryption involves a common limitation of cryptography hardwares and |
| 5089 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5090 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5091 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5092 | 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] | 5093 | } |
| 5094 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5095 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5096 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5097 | if (PSA_SUCCESS == expected_result) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5098 | TEST_CALLOC(output_data2, output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5099 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 5100 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 5101 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5102 | TEST_EQUAL(input_data->len, |
| 5103 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length)); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 5104 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5105 | TEST_LE_U(input_data->len, |
| 5106 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length)); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5107 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5108 | TEST_EQUAL(psa_aead_decrypt(key, alg, |
| 5109 | nonce->x, nonce->len, |
| 5110 | additional_data->x, |
| 5111 | additional_data->len, |
| 5112 | output_data, output_length, |
| 5113 | output_data2, output_length, |
| 5114 | &output_length2), |
| 5115 | expected_result); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5116 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 5117 | TEST_MEMORY_COMPARE(input_data->x, input_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 5118 | output_data2, output_length2); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5119 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5120 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5121 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5122 | psa_destroy_key(key); |
| 5123 | mbedtls_free(output_data); |
| 5124 | mbedtls_free(output_data2); |
| 5125 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5126 | } |
| 5127 | /* END_CASE */ |
| 5128 | |
| 5129 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5130 | void aead_encrypt(int key_type_arg, data_t *key_data, |
| 5131 | int alg_arg, |
| 5132 | data_t *nonce, |
| 5133 | data_t *additional_data, |
| 5134 | data_t *input_data, |
| 5135 | data_t *expected_result) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5136 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5137 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5138 | psa_key_type_t key_type = key_type_arg; |
| 5139 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5140 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5141 | unsigned char *output_data = NULL; |
| 5142 | size_t output_size = 0; |
| 5143 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5144 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 5145 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5146 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5147 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5149 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5150 | psa_set_key_algorithm(&attributes, alg); |
| 5151 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5153 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5154 | &key)); |
| 5155 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5156 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5157 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5158 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 5159 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5160 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 5161 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5162 | TEST_EQUAL(output_size, |
| 5163 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 5164 | TEST_LE_U(output_size, |
| 5165 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5166 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5167 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5168 | status = psa_aead_encrypt(key, alg, |
| 5169 | nonce->x, nonce->len, |
| 5170 | additional_data->x, additional_data->len, |
| 5171 | input_data->x, input_data->len, |
| 5172 | output_data, output_size, |
| 5173 | &output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5174 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 5175 | /* If the operation is not supported, just skip and not fail in case the |
| 5176 | * encryption involves a common limitation of cryptography hardwares and |
| 5177 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5178 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5179 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5180 | 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] | 5181 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 5182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5183 | PSA_ASSERT(status); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 5184 | TEST_MEMORY_COMPARE(expected_result->x, expected_result->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 5185 | output_data, output_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5186 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5187 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5188 | psa_destroy_key(key); |
| 5189 | mbedtls_free(output_data); |
| 5190 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5191 | } |
| 5192 | /* END_CASE */ |
| 5193 | |
| 5194 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5195 | void aead_decrypt(int key_type_arg, data_t *key_data, |
| 5196 | int alg_arg, |
| 5197 | data_t *nonce, |
| 5198 | data_t *additional_data, |
| 5199 | data_t *input_data, |
| 5200 | data_t *expected_data, |
| 5201 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5202 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5203 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5204 | psa_key_type_t key_type = key_type_arg; |
| 5205 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5206 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5207 | unsigned char *output_data = NULL; |
| 5208 | size_t output_size = 0; |
| 5209 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5210 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 5211 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 5212 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5213 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5214 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5215 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5216 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 5217 | psa_set_key_algorithm(&attributes, alg); |
| 5218 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5219 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5220 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5221 | &key)); |
| 5222 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5223 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5224 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5225 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 5226 | alg); |
| 5227 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 5228 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5229 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 5230 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5231 | TEST_EQUAL(output_size, |
| 5232 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 5233 | TEST_LE_U(output_size, |
| 5234 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5235 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5236 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5237 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5238 | status = psa_aead_decrypt(key, alg, |
| 5239 | nonce->x, nonce->len, |
| 5240 | additional_data->x, |
| 5241 | additional_data->len, |
| 5242 | input_data->x, input_data->len, |
| 5243 | output_data, output_size, |
| 5244 | &output_length); |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 5245 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 5246 | /* If the operation is not supported, just skip and not fail in case the |
| 5247 | * decryption involves a common limitation of cryptography hardwares and |
| 5248 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5249 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5250 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5251 | 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] | 5252 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 5253 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5254 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5256 | if (expected_result == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 5257 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 5258 | output_data, output_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5259 | } |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5260 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5261 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5262 | psa_destroy_key(key); |
| 5263 | mbedtls_free(output_data); |
| 5264 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5265 | } |
| 5266 | /* END_CASE */ |
| 5267 | |
| 5268 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5269 | void aead_multipart_encrypt(int key_type_arg, data_t *key_data, |
| 5270 | int alg_arg, |
| 5271 | data_t *nonce, |
| 5272 | data_t *additional_data, |
| 5273 | data_t *input_data, |
| 5274 | int do_set_lengths, |
| 5275 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5276 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5277 | size_t ad_part_len = 0; |
| 5278 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5279 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5280 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5281 | for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) { |
| 5282 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5283 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5284 | if (do_set_lengths) { |
| 5285 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5286 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5287 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5288 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5289 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5290 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5291 | |
| 5292 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5293 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5294 | alg_arg, nonce, |
| 5295 | additional_data, |
| 5296 | ad_part_len, |
| 5297 | input_data, -1, |
| 5298 | set_lengths_method, |
| 5299 | expected_output, |
| 5300 | 1, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5301 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5302 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5303 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5304 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 5305 | mbedtls_test_set_step(1000 + ad_part_len); |
| 5306 | |
| 5307 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5308 | alg_arg, nonce, |
| 5309 | additional_data, |
| 5310 | ad_part_len, |
| 5311 | input_data, -1, |
| 5312 | set_lengths_method, |
| 5313 | expected_output, |
| 5314 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5315 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5316 | } |
| 5317 | } |
| 5318 | |
| 5319 | for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) { |
| 5320 | /* Split data into length(data_part_len) parts. */ |
| 5321 | mbedtls_test_set_step(2000 + data_part_len); |
| 5322 | |
| 5323 | if (do_set_lengths) { |
| 5324 | if (data_part_len & 0x01) { |
| 5325 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 5326 | } else { |
| 5327 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
| 5328 | } |
| 5329 | } |
| 5330 | |
| 5331 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5332 | alg_arg, nonce, |
| 5333 | additional_data, -1, |
| 5334 | input_data, data_part_len, |
| 5335 | set_lengths_method, |
| 5336 | expected_output, |
| 5337 | 1, 0)) { |
| 5338 | break; |
| 5339 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5340 | |
| 5341 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5342 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5343 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5344 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5345 | alg_arg, nonce, |
| 5346 | additional_data, -1, |
| 5347 | input_data, data_part_len, |
| 5348 | set_lengths_method, |
| 5349 | expected_output, |
| 5350 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5351 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5352 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5353 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5354 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5355 | /* Goto is required to silence warnings about unused labels, as we |
| 5356 | * don't actually do any test assertions in this function. */ |
| 5357 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5358 | } |
| 5359 | /* END_CASE */ |
| 5360 | |
| 5361 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5362 | void aead_multipart_decrypt(int key_type_arg, data_t *key_data, |
| 5363 | int alg_arg, |
| 5364 | data_t *nonce, |
| 5365 | data_t *additional_data, |
| 5366 | data_t *input_data, |
| 5367 | int do_set_lengths, |
| 5368 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5369 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5370 | size_t ad_part_len = 0; |
| 5371 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5372 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5374 | 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] | 5375 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5376 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5377 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5378 | if (do_set_lengths) { |
| 5379 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5380 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5381 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5382 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5383 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5384 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5386 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5387 | alg_arg, nonce, |
| 5388 | additional_data, |
| 5389 | ad_part_len, |
| 5390 | input_data, -1, |
| 5391 | set_lengths_method, |
| 5392 | expected_output, |
| 5393 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5394 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5395 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5396 | |
| 5397 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5398 | mbedtls_test_set_step(1000 + ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5399 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5400 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5401 | alg_arg, nonce, |
| 5402 | additional_data, |
| 5403 | ad_part_len, |
| 5404 | input_data, -1, |
| 5405 | set_lengths_method, |
| 5406 | expected_output, |
| 5407 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5408 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5409 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5410 | } |
| 5411 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5412 | 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] | 5413 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5414 | mbedtls_test_set_step(2000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5415 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5416 | if (do_set_lengths) { |
| 5417 | if (data_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5418 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5419 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5420 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5421 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5422 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5423 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5424 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5425 | alg_arg, nonce, |
| 5426 | additional_data, -1, |
| 5427 | input_data, data_part_len, |
| 5428 | set_lengths_method, |
| 5429 | expected_output, |
| 5430 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5431 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5432 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5433 | |
| 5434 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5435 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5436 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5437 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5438 | alg_arg, nonce, |
| 5439 | additional_data, -1, |
| 5440 | input_data, data_part_len, |
| 5441 | set_lengths_method, |
| 5442 | expected_output, |
| 5443 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5444 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5445 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5446 | } |
| 5447 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5448 | /* Goto is required to silence warnings about unused labels, as we |
| 5449 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5450 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5451 | } |
| 5452 | /* END_CASE */ |
| 5453 | |
| 5454 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5455 | void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data, |
| 5456 | int alg_arg, |
| 5457 | int nonce_length, |
| 5458 | int expected_nonce_length_arg, |
| 5459 | data_t *additional_data, |
| 5460 | data_t *input_data, |
| 5461 | int expected_status_arg) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5462 | { |
| 5463 | |
| 5464 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5465 | psa_key_type_t key_type = key_type_arg; |
| 5466 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5467 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5468 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5469 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5470 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5471 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5472 | size_t actual_nonce_length = 0; |
| 5473 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 5474 | unsigned char *output = NULL; |
| 5475 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5476 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5477 | size_t ciphertext_size = 0; |
| 5478 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5479 | size_t tag_length = 0; |
| 5480 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5481 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5482 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5483 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5484 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5485 | psa_set_key_algorithm(&attributes, alg); |
| 5486 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5487 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5488 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5489 | &key)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5490 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5491 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5492 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5493 | 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] | 5494 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5495 | TEST_CALLOC(output, output_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5496 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5497 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5498 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5499 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5500 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5501 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5502 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5503 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5504 | |
| 5505 | /* If the operation is not supported, just skip and not fail in case the |
| 5506 | * encryption involves a common limitation of cryptography hardwares and |
| 5507 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5508 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5509 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5510 | 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] | 5511 | } |
| 5512 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5513 | PSA_ASSERT(status); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5514 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5515 | status = psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5516 | nonce_length, |
| 5517 | &actual_nonce_length); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5518 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5519 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5520 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5521 | TEST_EQUAL(actual_nonce_length, expected_nonce_length); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 5522 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5523 | if (expected_status == PSA_SUCCESS) { |
| 5524 | TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type, |
| 5525 | alg)); |
| 5526 | } |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 5527 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5528 | TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 5529 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5530 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5531 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5532 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5533 | input_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5534 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5535 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5536 | additional_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5537 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5538 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5539 | output, output_size, |
| 5540 | &ciphertext_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5541 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5542 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5543 | &ciphertext_length, tag_buffer, |
| 5544 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5545 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5546 | |
| 5547 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5548 | psa_destroy_key(key); |
| 5549 | mbedtls_free(output); |
| 5550 | mbedtls_free(ciphertext); |
| 5551 | psa_aead_abort(&operation); |
| 5552 | PSA_DONE(); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5553 | } |
| 5554 | /* END_CASE */ |
| 5555 | |
| 5556 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5557 | void aead_multipart_set_nonce(int key_type_arg, data_t *key_data, |
| 5558 | int alg_arg, |
| 5559 | int nonce_length_arg, |
| 5560 | int set_lengths_method_arg, |
| 5561 | data_t *additional_data, |
| 5562 | data_t *input_data, |
| 5563 | int expected_status_arg) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5564 | { |
| 5565 | |
| 5566 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5567 | psa_key_type_t key_type = key_type_arg; |
| 5568 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5569 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5570 | uint8_t *nonce_buffer = NULL; |
| 5571 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5572 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5573 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5574 | unsigned char *output = NULL; |
| 5575 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5576 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5577 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5578 | size_t ciphertext_size = 0; |
| 5579 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5580 | size_t tag_length = 0; |
| 5581 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5582 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5583 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5584 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5585 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5586 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5587 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5588 | psa_set_key_algorithm(&attributes, alg); |
| 5589 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5590 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5591 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5592 | &key)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5593 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5594 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5595 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5596 | 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] | 5597 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5598 | TEST_CALLOC(output, output_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5599 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5600 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5601 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5602 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5603 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5604 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5605 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5606 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5607 | |
| 5608 | /* If the operation is not supported, just skip and not fail in case the |
| 5609 | * encryption involves a common limitation of cryptography hardwares and |
| 5610 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5611 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5612 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5613 | 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] | 5614 | } |
| 5615 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5616 | PSA_ASSERT(status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5617 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5618 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5619 | if (nonce_length_arg == -1) { |
| 5620 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5621 | TEST_CALLOC(nonce_buffer, 4); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5622 | nonce_length = 0; |
| 5623 | } else { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5624 | /* If length is zero, then this will return NULL. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5625 | nonce_length = (size_t) nonce_length_arg; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5626 | TEST_CALLOC(nonce_buffer, nonce_length); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5627 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5628 | if (nonce_buffer) { |
| 5629 | for (index = 0; index < nonce_length - 1; ++index) { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5630 | nonce_buffer[index] = 'a' + index; |
| 5631 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5632 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5633 | } |
| 5634 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5635 | if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { |
| 5636 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5637 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5638 | } |
| 5639 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5640 | status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5642 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5643 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5644 | if (expected_status == PSA_SUCCESS) { |
| 5645 | if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) { |
| 5646 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5647 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5648 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5649 | 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] | 5650 | expected_status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5651 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5652 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5653 | /* 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] | 5654 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5655 | additional_data->len), |
| 5656 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5657 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5658 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5659 | output, output_size, |
| 5660 | &ciphertext_length), |
| 5661 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5662 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5663 | TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5664 | &ciphertext_length, tag_buffer, |
| 5665 | PSA_AEAD_TAG_MAX_SIZE, &tag_length), |
| 5666 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5667 | } |
| 5668 | |
| 5669 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5670 | psa_destroy_key(key); |
| 5671 | mbedtls_free(output); |
| 5672 | mbedtls_free(ciphertext); |
| 5673 | mbedtls_free(nonce_buffer); |
| 5674 | psa_aead_abort(&operation); |
| 5675 | PSA_DONE(); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5676 | } |
| 5677 | /* END_CASE */ |
| 5678 | |
| 5679 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5680 | 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] | 5681 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5682 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5683 | data_t *nonce, |
| 5684 | data_t *additional_data, |
| 5685 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5686 | int expected_status_arg) |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5687 | { |
| 5688 | |
| 5689 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5690 | psa_key_type_t key_type = key_type_arg; |
| 5691 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5692 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5693 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5694 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5695 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5696 | unsigned char *output = NULL; |
| 5697 | unsigned char *ciphertext = NULL; |
| 5698 | size_t output_size = output_size_arg; |
| 5699 | size_t ciphertext_size = 0; |
| 5700 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5701 | size_t tag_length = 0; |
| 5702 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5703 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5704 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5705 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5706 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5707 | psa_set_key_algorithm(&attributes, alg); |
| 5708 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5709 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5710 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5711 | &key)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5712 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5713 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5714 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5715 | TEST_CALLOC(output, output_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5716 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5717 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5718 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5719 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5720 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5721 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5722 | |
| 5723 | /* If the operation is not supported, just skip and not fail in case the |
| 5724 | * encryption involves a common limitation of cryptography hardwares and |
| 5725 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5726 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5727 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5728 | 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] | 5729 | } |
| 5730 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5731 | PSA_ASSERT(status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5732 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5733 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5734 | input_data->len)); |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5735 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5736 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5737 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5738 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5739 | additional_data->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5740 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5741 | status = psa_aead_update(&operation, input_data->x, input_data->len, |
| 5742 | output, output_size, &ciphertext_length); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5743 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5744 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5745 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5746 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5747 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5748 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5749 | &ciphertext_length, tag_buffer, |
| 5750 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5751 | } |
| 5752 | |
| 5753 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5754 | psa_destroy_key(key); |
| 5755 | mbedtls_free(output); |
| 5756 | mbedtls_free(ciphertext); |
| 5757 | psa_aead_abort(&operation); |
| 5758 | PSA_DONE(); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5759 | } |
| 5760 | /* END_CASE */ |
| 5761 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5762 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5763 | void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data, |
| 5764 | int alg_arg, |
| 5765 | int finish_ciphertext_size_arg, |
| 5766 | int tag_size_arg, |
| 5767 | data_t *nonce, |
| 5768 | data_t *additional_data, |
| 5769 | data_t *input_data, |
| 5770 | int expected_status_arg) |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5771 | { |
| 5772 | |
| 5773 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5774 | psa_key_type_t key_type = key_type_arg; |
| 5775 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5776 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5777 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5778 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5779 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5780 | unsigned char *ciphertext = NULL; |
| 5781 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5782 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5783 | size_t ciphertext_size = 0; |
| 5784 | size_t ciphertext_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5785 | size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg; |
| 5786 | size_t tag_size = (size_t) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5787 | size_t tag_length = 0; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5788 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5789 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5790 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5791 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5792 | psa_set_key_algorithm(&attributes, alg); |
| 5793 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5794 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5795 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5796 | &key)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5797 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5798 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5799 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5800 | 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] | 5801 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5802 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5803 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5804 | TEST_CALLOC(finish_ciphertext, finish_ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5805 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5806 | TEST_CALLOC(tag_buffer, tag_size); |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5807 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5808 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5809 | |
| 5810 | /* If the operation is not supported, just skip and not fail in case the |
| 5811 | * encryption involves a common limitation of cryptography hardwares and |
| 5812 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5813 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5814 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5815 | 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] | 5816 | } |
| 5817 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5818 | PSA_ASSERT(status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5819 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5820 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5821 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5822 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5823 | input_data->len)); |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5824 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5825 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5826 | additional_data->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5827 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5828 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5829 | ciphertext, ciphertext_size, &ciphertext_length)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5830 | |
| 5831 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5832 | status = psa_aead_finish(&operation, finish_ciphertext, |
| 5833 | finish_ciphertext_size, |
| 5834 | &ciphertext_length, tag_buffer, |
| 5835 | tag_size, &tag_length); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5836 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5837 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5838 | |
| 5839 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5840 | psa_destroy_key(key); |
| 5841 | mbedtls_free(ciphertext); |
| 5842 | mbedtls_free(finish_ciphertext); |
| 5843 | mbedtls_free(tag_buffer); |
| 5844 | psa_aead_abort(&operation); |
| 5845 | PSA_DONE(); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5846 | } |
| 5847 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5848 | |
| 5849 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5850 | void aead_multipart_verify(int key_type_arg, data_t *key_data, |
| 5851 | int alg_arg, |
| 5852 | data_t *nonce, |
| 5853 | data_t *additional_data, |
| 5854 | data_t *input_data, |
| 5855 | data_t *tag, |
| 5856 | int tag_usage_arg, |
| 5857 | int expected_setup_status_arg, |
| 5858 | int expected_status_arg) |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5859 | { |
| 5860 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5861 | psa_key_type_t key_type = key_type_arg; |
| 5862 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5863 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5864 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5865 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5866 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5867 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5868 | unsigned char *plaintext = NULL; |
| 5869 | unsigned char *finish_plaintext = NULL; |
| 5870 | size_t plaintext_size = 0; |
| 5871 | size_t plaintext_length = 0; |
| 5872 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5873 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5874 | unsigned char *tag_buffer = NULL; |
| 5875 | size_t tag_size = 0; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5876 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5877 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5878 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5879 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 5880 | psa_set_key_algorithm(&attributes, alg); |
| 5881 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5882 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5883 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5884 | &key)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5885 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5886 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5887 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5888 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 5889 | input_data->len); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5890 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5891 | TEST_CALLOC(plaintext, plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5892 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5893 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5894 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5895 | TEST_CALLOC(finish_plaintext, verify_plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5896 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5897 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5898 | |
| 5899 | /* If the operation is not supported, just skip and not fail in case the |
| 5900 | * encryption involves a common limitation of cryptography hardwares and |
| 5901 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5902 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5903 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5904 | 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] | 5905 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5906 | TEST_EQUAL(status, expected_setup_status); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5907 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5908 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5909 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5910 | } |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5911 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5912 | PSA_ASSERT(status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5913 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5914 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5915 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5916 | status = psa_aead_set_lengths(&operation, additional_data->len, |
| 5917 | input_data->len); |
| 5918 | PSA_ASSERT(status); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5919 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5920 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5921 | additional_data->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5922 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5923 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 5924 | input_data->len, |
| 5925 | plaintext, plaintext_size, |
| 5926 | &plaintext_length)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5927 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5928 | if (tag_usage == USE_GIVEN_TAG) { |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5929 | tag_buffer = tag->x; |
| 5930 | tag_size = tag->len; |
| 5931 | } |
| 5932 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5933 | status = psa_aead_verify(&operation, finish_plaintext, |
| 5934 | verify_plaintext_size, |
| 5935 | &plaintext_length, |
| 5936 | tag_buffer, tag_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5937 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5938 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5939 | |
| 5940 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5941 | psa_destroy_key(key); |
| 5942 | mbedtls_free(plaintext); |
| 5943 | mbedtls_free(finish_plaintext); |
| 5944 | psa_aead_abort(&operation); |
| 5945 | PSA_DONE(); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5946 | } |
| 5947 | /* END_CASE */ |
| 5948 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5949 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5950 | void aead_multipart_setup(int key_type_arg, data_t *key_data, |
| 5951 | int alg_arg, int expected_status_arg) |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5952 | { |
| 5953 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5954 | psa_key_type_t key_type = key_type_arg; |
| 5955 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5956 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5957 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5958 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5959 | psa_status_t expected_status = expected_status_arg; |
| 5960 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5961 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5962 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5963 | psa_set_key_usage_flags(&attributes, |
| 5964 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5965 | psa_set_key_algorithm(&attributes, alg); |
| 5966 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5967 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5968 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5969 | &key)); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5970 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5971 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5972 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5973 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5974 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5975 | psa_aead_abort(&operation); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5976 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5977 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5978 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5979 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5980 | |
| 5981 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5982 | psa_destroy_key(key); |
| 5983 | psa_aead_abort(&operation); |
| 5984 | PSA_DONE(); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5985 | } |
| 5986 | /* END_CASE */ |
| 5987 | |
| 5988 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5989 | void aead_multipart_state_test(int key_type_arg, data_t *key_data, |
| 5990 | int alg_arg, |
| 5991 | data_t *nonce, |
| 5992 | data_t *additional_data, |
| 5993 | data_t *input_data) |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5994 | { |
| 5995 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5996 | psa_key_type_t key_type = key_type_arg; |
| 5997 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5998 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5999 | unsigned char *output_data = NULL; |
| 6000 | unsigned char *final_data = NULL; |
| 6001 | size_t output_size = 0; |
| 6002 | size_t finish_output_size = 0; |
| 6003 | size_t output_length = 0; |
| 6004 | size_t key_bits = 0; |
| 6005 | size_t tag_length = 0; |
| 6006 | size_t tag_size = 0; |
| 6007 | size_t nonce_length = 0; |
| 6008 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 6009 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 6010 | size_t output_part_length = 0; |
| 6011 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6012 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6013 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6014 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6015 | psa_set_key_usage_flags(&attributes, |
| 6016 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 6017 | psa_set_key_algorithm(&attributes, alg); |
| 6018 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6019 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6020 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6021 | &key)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6022 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6023 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6024 | key_bits = psa_get_key_bits(&attributes); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6025 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6026 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6027 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6028 | TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6029 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6030 | 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] | 6031 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6032 | TEST_CALLOC(output_data, output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6033 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6034 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6035 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6036 | TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6037 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6038 | TEST_CALLOC(final_data, finish_output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6039 | |
| 6040 | /* Test all operations error without calling setup first. */ |
| 6041 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6042 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6043 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6044 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6045 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6046 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6047 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6048 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6049 | &nonce_length), |
| 6050 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6051 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6052 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6053 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 6054 | /* ------------------------------------------------------- */ |
| 6055 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6056 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6057 | input_data->len), |
| 6058 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6059 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6060 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6061 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 6062 | /* ------------------------------------------------------- */ |
| 6063 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6064 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6065 | additional_data->len), |
| 6066 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6067 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6068 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6069 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 6070 | /* ------------------------------------------------------- */ |
| 6071 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6072 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6073 | input_data->len, output_data, |
| 6074 | output_size, &output_length), |
| 6075 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6076 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6077 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6078 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 6079 | /* ------------------------------------------------------- */ |
| 6080 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6081 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6082 | finish_output_size, |
| 6083 | &output_part_length, |
| 6084 | tag_buffer, tag_length, |
| 6085 | &tag_size), |
| 6086 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6087 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6088 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6089 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 6090 | /* ------------------------------------------------------- */ |
| 6091 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6092 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6093 | finish_output_size, |
| 6094 | &output_part_length, |
| 6095 | tag_buffer, |
| 6096 | tag_length), |
| 6097 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6098 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6099 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6100 | |
| 6101 | /* Test for double setups. */ |
| 6102 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6103 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6104 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6105 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 6106 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6107 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6108 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6109 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 6110 | /* ------------------------------------------------------- */ |
| 6111 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6112 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6114 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 6115 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6116 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6117 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6118 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6119 | /* ------------------------------------------------------- */ |
| 6120 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6121 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6122 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6123 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 6124 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6125 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6126 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6127 | |
| 6128 | /* ------------------------------------------------------- */ |
| 6129 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6130 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6131 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6132 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 6133 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6135 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6136 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6137 | /* Test for not setting a nonce. */ |
| 6138 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6139 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6140 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6141 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6142 | additional_data->len), |
| 6143 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6144 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6145 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6146 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 6147 | /* ------------------------------------------------------- */ |
| 6148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6149 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 6150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6151 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6152 | input_data->len, output_data, |
| 6153 | output_size, &output_length), |
| 6154 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 6155 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6156 | psa_aead_abort(&operation); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 6157 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6158 | /* ------------------------------------------------------- */ |
| 6159 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6160 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6161 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6162 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6163 | finish_output_size, |
| 6164 | &output_part_length, |
| 6165 | tag_buffer, tag_length, |
| 6166 | &tag_size), |
| 6167 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6168 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6169 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6170 | |
| 6171 | /* ------------------------------------------------------- */ |
| 6172 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6173 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6174 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6175 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6176 | finish_output_size, |
| 6177 | &output_part_length, |
| 6178 | tag_buffer, |
| 6179 | tag_length), |
| 6180 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6181 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6182 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6183 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6184 | /* Test for double setting nonce. */ |
| 6185 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6186 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6187 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6188 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6189 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6190 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6191 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6192 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6193 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6194 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6195 | /* Test for double generating nonce. */ |
| 6196 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6197 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6198 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6199 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6200 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6201 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6202 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6203 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6204 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6205 | &nonce_length), |
| 6206 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6207 | |
| 6208 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6209 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6210 | |
| 6211 | /* Test for generate nonce then set and vice versa */ |
| 6212 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6213 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6215 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6216 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6217 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6218 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6219 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6220 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6221 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6222 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6223 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6224 | /* Test for generating nonce after calling set lengths */ |
| 6225 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6226 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6227 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6228 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6229 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6230 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6231 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6232 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6233 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6234 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6235 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6236 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6237 | /* 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] | 6238 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6239 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6240 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6241 | if (operation.alg == PSA_ALG_CCM) { |
| 6242 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6243 | input_data->len), |
| 6244 | PSA_ERROR_INVALID_ARGUMENT); |
| 6245 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6246 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6247 | &nonce_length), |
| 6248 | PSA_ERROR_BAD_STATE); |
| 6249 | } else { |
| 6250 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6251 | input_data->len)); |
| 6252 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6253 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6254 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6255 | } |
| 6256 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6257 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6258 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6259 | /* 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] | 6260 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6261 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6263 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 6264 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 6265 | input_data->len), |
| 6266 | PSA_ERROR_INVALID_ARGUMENT); |
| 6267 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6268 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6269 | &nonce_length), |
| 6270 | PSA_ERROR_BAD_STATE); |
| 6271 | } else { |
| 6272 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 6273 | input_data->len)); |
| 6274 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6275 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6276 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6277 | } |
| 6278 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6279 | psa_aead_abort(&operation); |
Dave Rodgman | d26d744 | 2023-02-11 17:14:54 +0000 | [diff] [blame] | 6280 | #endif |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6281 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6282 | /* 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] | 6283 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6284 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6285 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6286 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6287 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6288 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6289 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6290 | if (operation.alg == PSA_ALG_CCM) { |
| 6291 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6292 | input_data->len), |
| 6293 | PSA_ERROR_INVALID_ARGUMENT); |
| 6294 | } else { |
| 6295 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6296 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6297 | } |
| 6298 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6299 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6300 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6301 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6302 | /* Test for setting nonce after calling set lengths */ |
| 6303 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6304 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6306 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6307 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6308 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6309 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6310 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6311 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6312 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6313 | /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6314 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6315 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6316 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6317 | if (operation.alg == PSA_ALG_CCM) { |
| 6318 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6319 | input_data->len), |
| 6320 | PSA_ERROR_INVALID_ARGUMENT); |
| 6321 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6322 | PSA_ERROR_BAD_STATE); |
| 6323 | } else { |
| 6324 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6325 | input_data->len)); |
| 6326 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6327 | } |
| 6328 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6329 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6330 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6331 | /* 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] | 6332 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6333 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6334 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6335 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 6336 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 6337 | input_data->len), |
| 6338 | PSA_ERROR_INVALID_ARGUMENT); |
| 6339 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6340 | PSA_ERROR_BAD_STATE); |
| 6341 | } else { |
| 6342 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 6343 | input_data->len)); |
| 6344 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6345 | } |
| 6346 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6347 | psa_aead_abort(&operation); |
Dave Rodgman | a476363 | 2023-02-11 18:36:23 +0000 | [diff] [blame] | 6348 | #endif |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6349 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6350 | /* 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] | 6351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6352 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6354 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6356 | if (operation.alg == PSA_ALG_CCM) { |
| 6357 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6358 | input_data->len), |
| 6359 | PSA_ERROR_INVALID_ARGUMENT); |
| 6360 | } else { |
| 6361 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6362 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6363 | } |
| 6364 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6365 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6366 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6367 | /* 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] | 6368 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6369 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6371 | if (operation.alg == PSA_ALG_GCM) { |
| 6372 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6373 | SIZE_MAX), |
| 6374 | PSA_ERROR_INVALID_ARGUMENT); |
| 6375 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6376 | PSA_ERROR_BAD_STATE); |
| 6377 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6378 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6379 | SIZE_MAX)); |
| 6380 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6381 | } |
| 6382 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6383 | psa_aead_abort(&operation); |
Dave Rodgman | 91e8321 | 2023-02-11 20:07:43 +0000 | [diff] [blame] | 6384 | #endif |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6385 | |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 6386 | /* 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] | 6387 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6388 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6389 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6390 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6392 | if (operation.alg == PSA_ALG_GCM) { |
| 6393 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6394 | SIZE_MAX), |
| 6395 | PSA_ERROR_INVALID_ARGUMENT); |
| 6396 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6397 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6398 | SIZE_MAX)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6399 | } |
| 6400 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6401 | psa_aead_abort(&operation); |
Dave Rodgman | 641288b | 2023-02-11 22:02:04 +0000 | [diff] [blame] | 6402 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6403 | |
| 6404 | /* ------------------------------------------------------- */ |
| 6405 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6406 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6407 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6408 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6409 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6410 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6411 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6412 | &nonce_length), |
| 6413 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6414 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6415 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6416 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6417 | /* Test for generating nonce in decrypt setup. */ |
| 6418 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6419 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6420 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6421 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6422 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6423 | &nonce_length), |
| 6424 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6425 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6426 | psa_aead_abort(&operation); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6427 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6428 | /* Test for setting lengths twice. */ |
| 6429 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6430 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6431 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6432 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6433 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6434 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6435 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6436 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6437 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6438 | input_data->len), |
| 6439 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6440 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6441 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6442 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6443 | /* Test for setting lengths after setting nonce + already starting data. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6444 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6445 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6446 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6447 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6449 | if (operation.alg == PSA_ALG_CCM) { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6450 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6451 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6452 | additional_data->len), |
| 6453 | PSA_ERROR_BAD_STATE); |
| 6454 | } else { |
| 6455 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6456 | additional_data->len)); |
| 6457 | |
| 6458 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6459 | input_data->len), |
| 6460 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6461 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6462 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6463 | |
| 6464 | /* ------------------------------------------------------- */ |
| 6465 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6466 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6467 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6468 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6469 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6470 | if (operation.alg == PSA_ALG_CCM) { |
| 6471 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6472 | input_data->len, output_data, |
| 6473 | output_size, &output_length), |
| 6474 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6475 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6476 | } else { |
| 6477 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6478 | input_data->len, output_data, |
| 6479 | output_size, &output_length)); |
| 6480 | |
| 6481 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6482 | input_data->len), |
| 6483 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6484 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6485 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6486 | |
| 6487 | /* ------------------------------------------------------- */ |
| 6488 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6489 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6490 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6491 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6492 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6493 | if (operation.alg == PSA_ALG_CCM) { |
| 6494 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6495 | finish_output_size, |
| 6496 | &output_part_length, |
| 6497 | tag_buffer, tag_length, |
| 6498 | &tag_size)); |
| 6499 | } else { |
| 6500 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6501 | finish_output_size, |
| 6502 | &output_part_length, |
| 6503 | tag_buffer, tag_length, |
| 6504 | &tag_size)); |
| 6505 | |
| 6506 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6507 | input_data->len), |
| 6508 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6509 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6510 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6511 | |
| 6512 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 6513 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6514 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6515 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6516 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6517 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6518 | &nonce_length)); |
| 6519 | if (operation.alg == PSA_ALG_CCM) { |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6520 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6521 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6522 | additional_data->len), |
| 6523 | PSA_ERROR_BAD_STATE); |
| 6524 | } else { |
| 6525 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6526 | additional_data->len)); |
| 6527 | |
| 6528 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6529 | input_data->len), |
| 6530 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6531 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6532 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6533 | |
| 6534 | /* ------------------------------------------------------- */ |
| 6535 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6536 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6537 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6538 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6539 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6540 | &nonce_length)); |
| 6541 | if (operation.alg == PSA_ALG_CCM) { |
| 6542 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6543 | input_data->len, output_data, |
| 6544 | output_size, &output_length), |
| 6545 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6546 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6547 | } else { |
| 6548 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6549 | input_data->len, output_data, |
| 6550 | output_size, &output_length)); |
| 6551 | |
| 6552 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6553 | input_data->len), |
| 6554 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6555 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6556 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6557 | |
| 6558 | /* ------------------------------------------------------- */ |
| 6559 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6560 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6562 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6563 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6564 | &nonce_length)); |
| 6565 | if (operation.alg == PSA_ALG_CCM) { |
| 6566 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6567 | finish_output_size, |
| 6568 | &output_part_length, |
| 6569 | tag_buffer, tag_length, |
| 6570 | &tag_size)); |
| 6571 | } else { |
| 6572 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6573 | finish_output_size, |
| 6574 | &output_part_length, |
| 6575 | tag_buffer, tag_length, |
| 6576 | &tag_size)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6577 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6578 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6579 | input_data->len), |
| 6580 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6581 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6582 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6583 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6584 | /* Test for not sending any additional data or data after setting non zero |
| 6585 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6586 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6587 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6588 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6589 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6590 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6591 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6592 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6593 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6594 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6595 | finish_output_size, |
| 6596 | &output_part_length, |
| 6597 | tag_buffer, tag_length, |
| 6598 | &tag_size), |
| 6599 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6600 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6601 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6602 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6603 | /* Test for not sending any additional data or data after setting non-zero |
| 6604 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6605 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6606 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6607 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6608 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6609 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6610 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6611 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6612 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6613 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6614 | finish_output_size, |
| 6615 | &output_part_length, |
| 6616 | tag_buffer, |
| 6617 | tag_length), |
| 6618 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6619 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6620 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6621 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6622 | /* Test for not sending any additional data after setting a non-zero length |
| 6623 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6624 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6625 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6626 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6627 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6628 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6629 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6630 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6631 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6632 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6633 | input_data->len, output_data, |
| 6634 | output_size, &output_length), |
| 6635 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6636 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6637 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6638 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6639 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 6640 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6641 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6642 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6643 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6644 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6645 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6646 | input_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6647 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6648 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6649 | additional_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6650 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6651 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6652 | finish_output_size, |
| 6653 | &output_part_length, |
| 6654 | tag_buffer, tag_length, |
| 6655 | &tag_size), |
| 6656 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6657 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6658 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6659 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6660 | /* Test for sending too much additional data after setting lengths. */ |
| 6661 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6662 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6663 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6664 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6665 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6666 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6667 | |
| 6668 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6669 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6670 | additional_data->len), |
| 6671 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6672 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6673 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6674 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6675 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6676 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6677 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6678 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6679 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6680 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6681 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6682 | input_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6683 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6684 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6685 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6686 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6687 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6688 | 1), |
| 6689 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6690 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6691 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6692 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6693 | /* Test for sending too much data after setting lengths. */ |
| 6694 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6695 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6696 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6697 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6698 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6699 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6700 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6701 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6702 | input_data->len, output_data, |
| 6703 | output_size, &output_length), |
| 6704 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6705 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6706 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6707 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6708 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6709 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6710 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6711 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6712 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6713 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6714 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6715 | input_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6716 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6717 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6718 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6719 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6720 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6721 | input_data->len, output_data, |
| 6722 | output_size, &output_length)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6723 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6724 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6725 | 1, output_data, |
| 6726 | output_size, &output_length), |
| 6727 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6728 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6729 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6730 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6731 | /* Test sending additional data after data. */ |
| 6732 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6733 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6734 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6735 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6736 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6737 | if (operation.alg != PSA_ALG_CCM) { |
| 6738 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6739 | input_data->len, output_data, |
| 6740 | output_size, &output_length)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6741 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6742 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6743 | additional_data->len), |
| 6744 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6745 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6746 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6747 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6748 | /* Test calling finish on decryption. */ |
| 6749 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6750 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6751 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6752 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6753 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6754 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6755 | finish_output_size, |
| 6756 | &output_part_length, |
| 6757 | tag_buffer, tag_length, |
| 6758 | &tag_size), |
| 6759 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6760 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6761 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6762 | |
| 6763 | /* Test calling verify on encryption. */ |
| 6764 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6765 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6766 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6767 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6768 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6769 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6770 | finish_output_size, |
| 6771 | &output_part_length, |
| 6772 | tag_buffer, |
| 6773 | tag_length), |
| 6774 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6775 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6776 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6777 | |
| 6778 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6779 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6780 | psa_destroy_key(key); |
| 6781 | psa_aead_abort(&operation); |
| 6782 | mbedtls_free(output_data); |
| 6783 | mbedtls_free(final_data); |
| 6784 | PSA_DONE(); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6785 | } |
| 6786 | /* END_CASE */ |
| 6787 | |
| 6788 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6789 | void signature_size(int type_arg, |
| 6790 | int bits, |
| 6791 | int alg_arg, |
| 6792 | int expected_size_arg) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6793 | { |
| 6794 | psa_key_type_t type = type_arg; |
| 6795 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6796 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6797 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6798 | TEST_EQUAL(actual_size, (size_t) expected_size_arg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6799 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6800 | exit: |
| 6801 | ; |
| 6802 | } |
| 6803 | /* END_CASE */ |
| 6804 | |
| 6805 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6806 | void sign_hash_deterministic(int key_type_arg, data_t *key_data, |
| 6807 | int alg_arg, data_t *input_data, |
| 6808 | data_t *output_data) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6809 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6810 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6811 | psa_key_type_t key_type = key_type_arg; |
| 6812 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6813 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6814 | unsigned char *signature = NULL; |
| 6815 | size_t signature_size; |
| 6816 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6817 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6818 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6819 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6820 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6821 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6822 | psa_set_key_algorithm(&attributes, alg); |
| 6823 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6824 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6825 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6826 | &key)); |
| 6827 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6828 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6829 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6830 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6831 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6832 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6833 | key_bits, alg); |
| 6834 | TEST_ASSERT(signature_size != 0); |
| 6835 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6836 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6837 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6838 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6839 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6840 | input_data->x, input_data->len, |
| 6841 | signature, signature_size, |
| 6842 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6843 | /* Verify that the signature is what is expected. */ |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 6844 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 6845 | signature, signature_length); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6846 | |
| 6847 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6848 | /* |
| 6849 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6850 | * thus reset them as required. |
| 6851 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6852 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6853 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6854 | psa_destroy_key(key); |
| 6855 | mbedtls_free(signature); |
| 6856 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6857 | } |
| 6858 | /* END_CASE */ |
| 6859 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6860 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6861 | /** |
| 6862 | * sign_hash_interruptible() test intentions: |
| 6863 | * |
| 6864 | * Note: This test can currently only handle ECDSA. |
| 6865 | * |
| 6866 | * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 6867 | * and private keys / keypairs only). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6868 | * |
| 6869 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 6870 | * expected for different max_ops values. |
| 6871 | * |
| 6872 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 6873 | * and that each successful stage completes some ops (this is not mandated by |
| 6874 | * the PSA specification, but is currently the case). |
| 6875 | * |
| 6876 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 6877 | * complete() calls does not alter the number of ops returned. |
| 6878 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6879 | void sign_hash_interruptible(int key_type_arg, data_t *key_data, |
| 6880 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6881 | data_t *output_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6882 | { |
| 6883 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6884 | psa_key_type_t key_type = key_type_arg; |
| 6885 | psa_algorithm_t alg = alg_arg; |
| 6886 | size_t key_bits; |
| 6887 | unsigned char *signature = NULL; |
| 6888 | size_t signature_size; |
| 6889 | size_t signature_length = 0xdeadbeef; |
| 6890 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6891 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6892 | uint32_t num_ops = 0; |
| 6893 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6894 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6895 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6896 | size_t min_completes = 0; |
| 6897 | size_t max_completes = 0; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6898 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6899 | psa_sign_hash_interruptible_operation_t operation = |
| 6900 | psa_sign_hash_interruptible_operation_init(); |
| 6901 | |
| 6902 | PSA_ASSERT(psa_crypto_init()); |
| 6903 | |
| 6904 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6905 | psa_set_key_algorithm(&attributes, alg); |
| 6906 | psa_set_key_type(&attributes, key_type); |
| 6907 | |
| 6908 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6909 | &key)); |
| 6910 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6911 | key_bits = psa_get_key_bits(&attributes); |
| 6912 | |
| 6913 | /* Allocate a buffer which has the size advertised by the |
| 6914 | * library. */ |
| 6915 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6916 | key_bits, alg); |
| 6917 | TEST_ASSERT(signature_size != 0); |
| 6918 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6919 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6920 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6921 | psa_interruptible_set_max_ops(max_ops); |
| 6922 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6923 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6924 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6925 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6926 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6927 | TEST_ASSERT(num_ops_prior == 0); |
| 6928 | |
| 6929 | /* Start performing the signature. */ |
| 6930 | PSA_ASSERT(psa_sign_hash_start(&operation, key, alg, |
| 6931 | input_data->x, input_data->len)); |
| 6932 | |
| 6933 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6934 | TEST_ASSERT(num_ops_prior == 0); |
| 6935 | |
| 6936 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6937 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6938 | status = psa_sign_hash_complete(&operation, signature, signature_size, |
| 6939 | &signature_length); |
| 6940 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6941 | num_completes++; |
| 6942 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6943 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 6944 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6945 | /* We are asserting here that every complete makes progress |
| 6946 | * (completes some ops), which is true of the internal |
| 6947 | * implementation and probably any implementation, however this is |
| 6948 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6949 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6950 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6951 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 6952 | |
| 6953 | /* Ensure calling get_num_ops() twice still returns the same |
| 6954 | * number of ops as previously reported. */ |
| 6955 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6956 | |
| 6957 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6958 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6959 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6960 | |
| 6961 | TEST_ASSERT(status == PSA_SUCCESS); |
| 6962 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6963 | TEST_LE_U(min_completes, num_completes); |
| 6964 | TEST_LE_U(num_completes, max_completes); |
| 6965 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6966 | /* Verify that the signature is what is expected. */ |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 6967 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 6968 | signature, signature_length); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6969 | |
| 6970 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6971 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6972 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6973 | TEST_ASSERT(num_ops == 0); |
| 6974 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6975 | exit: |
| 6976 | |
| 6977 | /* |
| 6978 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6979 | * thus reset them as required. |
| 6980 | */ |
| 6981 | psa_reset_key_attributes(&attributes); |
| 6982 | |
| 6983 | psa_destroy_key(key); |
| 6984 | mbedtls_free(signature); |
| 6985 | PSA_DONE(); |
| 6986 | } |
| 6987 | /* END_CASE */ |
| 6988 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6989 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6990 | void sign_hash_fail(int key_type_arg, data_t *key_data, |
| 6991 | int alg_arg, data_t *input_data, |
| 6992 | int signature_size_arg, int expected_status_arg) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6993 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6994 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6995 | psa_key_type_t key_type = key_type_arg; |
| 6996 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6997 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6998 | psa_status_t actual_status; |
| 6999 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 7000 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 7001 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7002 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 7003 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7004 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 7005 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7006 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 7007 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7008 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 7009 | psa_set_key_algorithm(&attributes, alg); |
| 7010 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 7011 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7012 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7013 | &key)); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 7014 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7015 | actual_status = psa_sign_hash(key, alg, |
| 7016 | input_data->x, input_data->len, |
| 7017 | signature, signature_size, |
| 7018 | &signature_length); |
| 7019 | TEST_EQUAL(actual_status, expected_status); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 7020 | /* The value of *signature_length is unspecified on error, but |
| 7021 | * whatever it is, it should be less than signature_size, so that |
| 7022 | * if the caller tries to read *signature_length bytes without |
| 7023 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7024 | TEST_LE_U(signature_length, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 7025 | |
| 7026 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7027 | psa_reset_key_attributes(&attributes); |
| 7028 | psa_destroy_key(key); |
| 7029 | mbedtls_free(signature); |
| 7030 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 7031 | } |
| 7032 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 7033 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7034 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7035 | /** |
| 7036 | * sign_hash_fail_interruptible() test intentions: |
| 7037 | * |
| 7038 | * Note: This test can currently only handle ECDSA. |
| 7039 | * |
| 7040 | * 1. Test that various failure cases for interruptible sign hash fail with the |
| 7041 | * correct error codes, and at the correct point (at start or during |
| 7042 | * complete). |
| 7043 | * |
| 7044 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 7045 | * expected for different max_ops values. |
| 7046 | * |
| 7047 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7048 | * and that each successful stage completes some ops (this is not mandated by |
| 7049 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7050 | * |
| 7051 | * 4. Check that calling complete() when start() fails and complete() |
| 7052 | * after completion results in a BAD_STATE error. |
| 7053 | * |
| 7054 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 7055 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7056 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7057 | void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 7058 | int alg_arg, data_t *input_data, |
| 7059 | int signature_size_arg, |
| 7060 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7061 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7062 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7063 | { |
| 7064 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7065 | psa_key_type_t key_type = key_type_arg; |
| 7066 | psa_algorithm_t alg = alg_arg; |
| 7067 | size_t signature_size = signature_size_arg; |
| 7068 | psa_status_t actual_status; |
| 7069 | psa_status_t expected_start_status = expected_start_status_arg; |
| 7070 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 7071 | unsigned char *signature = NULL; |
| 7072 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7073 | uint32_t num_ops = 0; |
| 7074 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7075 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7076 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7077 | size_t min_completes = 0; |
| 7078 | size_t max_completes = 0; |
| 7079 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7080 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7081 | psa_sign_hash_interruptible_operation_t operation = |
| 7082 | psa_sign_hash_interruptible_operation_init(); |
| 7083 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7084 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7085 | |
| 7086 | PSA_ASSERT(psa_crypto_init()); |
| 7087 | |
| 7088 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 7089 | psa_set_key_algorithm(&attributes, alg); |
| 7090 | psa_set_key_type(&attributes, key_type); |
| 7091 | |
| 7092 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7093 | &key)); |
| 7094 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7095 | psa_interruptible_set_max_ops(max_ops); |
| 7096 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7097 | interruptible_signverify_get_minmax_completes(max_ops, |
| 7098 | expected_complete_status, |
| 7099 | &min_completes, |
| 7100 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7101 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7102 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 7103 | TEST_ASSERT(num_ops_prior == 0); |
| 7104 | |
| 7105 | /* Start performing the signature. */ |
| 7106 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 7107 | input_data->x, input_data->len); |
| 7108 | |
| 7109 | TEST_EQUAL(actual_status, expected_start_status); |
| 7110 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7111 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 7112 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7113 | * start failed. */ |
| 7114 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 7115 | signature_size, |
| 7116 | &signature_length); |
| 7117 | |
| 7118 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7119 | |
| 7120 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7121 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 7122 | input_data->x, input_data->len); |
| 7123 | |
| 7124 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7125 | } |
| 7126 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7127 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 7128 | TEST_ASSERT(num_ops_prior == 0); |
| 7129 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7130 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7131 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7132 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 7133 | signature_size, |
| 7134 | &signature_length); |
| 7135 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7136 | num_completes++; |
| 7137 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7138 | if (actual_status == PSA_SUCCESS || |
| 7139 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7140 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7141 | /* We are asserting here that every complete makes progress |
| 7142 | * (completes some ops), which is true of the internal |
| 7143 | * implementation and probably any implementation, however this is |
| 7144 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7145 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7146 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7147 | num_ops_prior = num_ops; |
| 7148 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7149 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7150 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7151 | TEST_EQUAL(actual_status, expected_complete_status); |
| 7152 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 7153 | /* Check that another complete returns BAD_STATE. */ |
| 7154 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 7155 | signature_size, |
| 7156 | &signature_length); |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7157 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 7158 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7159 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7160 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 7161 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7162 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 7163 | TEST_ASSERT(num_ops == 0); |
| 7164 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7165 | /* The value of *signature_length is unspecified on error, but |
| 7166 | * whatever it is, it should be less than signature_size, so that |
| 7167 | * if the caller tries to read *signature_length bytes without |
| 7168 | * checking the error code then they don't overflow a buffer. */ |
| 7169 | TEST_LE_U(signature_length, signature_size); |
| 7170 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7171 | TEST_LE_U(min_completes, num_completes); |
| 7172 | TEST_LE_U(num_completes, max_completes); |
| 7173 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7174 | exit: |
| 7175 | psa_reset_key_attributes(&attributes); |
| 7176 | psa_destroy_key(key); |
| 7177 | mbedtls_free(signature); |
| 7178 | PSA_DONE(); |
| 7179 | } |
| 7180 | /* END_CASE */ |
| 7181 | |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 7182 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7183 | void sign_verify_hash(int key_type_arg, data_t *key_data, |
| 7184 | int alg_arg, data_t *input_data) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7185 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7186 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7187 | psa_key_type_t key_type = key_type_arg; |
| 7188 | psa_algorithm_t alg = alg_arg; |
| 7189 | size_t key_bits; |
| 7190 | unsigned char *signature = NULL; |
| 7191 | size_t signature_size; |
| 7192 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7193 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7194 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7195 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7196 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7197 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 7198 | psa_set_key_algorithm(&attributes, alg); |
| 7199 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7200 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7201 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7202 | &key)); |
| 7203 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7204 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7205 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 7206 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7207 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7208 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7209 | key_bits, alg); |
| 7210 | TEST_ASSERT(signature_size != 0); |
| 7211 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7212 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7213 | |
| 7214 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7215 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 7216 | input_data->x, input_data->len, |
| 7217 | signature, signature_size, |
| 7218 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7219 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7220 | TEST_LE_U(signature_length, signature_size); |
| 7221 | TEST_ASSERT(signature_length > 0); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7222 | |
| 7223 | /* Use the library to verify that the signature is correct. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7224 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 7225 | input_data->x, input_data->len, |
| 7226 | signature, signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7227 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7228 | if (input_data->len != 0) { |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7229 | /* Flip a bit in the input and verify that the signature is now |
| 7230 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 7231 | * because ECDSA may ignore the last few bits of the input. */ |
| 7232 | input_data->x[0] ^= 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7233 | TEST_EQUAL(psa_verify_hash(key, alg, |
| 7234 | input_data->x, input_data->len, |
| 7235 | signature, signature_length), |
| 7236 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7237 | } |
| 7238 | |
| 7239 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7240 | /* |
| 7241 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7242 | * thus reset them as required. |
| 7243 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7244 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7245 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7246 | psa_destroy_key(key); |
| 7247 | mbedtls_free(signature); |
| 7248 | PSA_DONE(); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7249 | } |
| 7250 | /* END_CASE */ |
| 7251 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7252 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7253 | /** |
| 7254 | * sign_verify_hash_interruptible() test intentions: |
| 7255 | * |
| 7256 | * Note: This test can currently only handle ECDSA. |
| 7257 | * |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 7258 | * 1. Test that we can sign an input hash with the given keypair and then |
| 7259 | * afterwards verify that signature. This is currently the only way to test |
| 7260 | * non deterministic ECDSA, but this test can also handle deterministic. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7261 | * |
| 7262 | * 2. Test that after corrupting the hash, the verification detects an invalid |
| 7263 | * signature. |
| 7264 | * |
| 7265 | * 3. Test the number of calls to psa_sign_hash_complete() required are as |
| 7266 | * expected for different max_ops values. |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7267 | * |
| 7268 | * 4. Test that the number of ops done prior to starting signing and after abort |
| 7269 | * is zero and that each successful signing stage completes some ops (this is |
| 7270 | * not mandated by the PSA specification, but is currently the case). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7271 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7272 | 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] | 7273 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7274 | int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7275 | { |
| 7276 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7277 | psa_key_type_t key_type = key_type_arg; |
| 7278 | psa_algorithm_t alg = alg_arg; |
| 7279 | size_t key_bits; |
| 7280 | unsigned char *signature = NULL; |
| 7281 | size_t signature_size; |
| 7282 | size_t signature_length = 0xdeadbeef; |
| 7283 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7284 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7285 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7286 | uint32_t num_ops = 0; |
| 7287 | uint32_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7288 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7289 | size_t min_completes = 0; |
| 7290 | size_t max_completes = 0; |
| 7291 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7292 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7293 | psa_sign_hash_interruptible_operation_init(); |
| 7294 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7295 | psa_verify_hash_interruptible_operation_init(); |
| 7296 | |
| 7297 | PSA_ASSERT(psa_crypto_init()); |
| 7298 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7299 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7300 | PSA_KEY_USAGE_VERIFY_HASH); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7301 | psa_set_key_algorithm(&attributes, alg); |
| 7302 | psa_set_key_type(&attributes, key_type); |
| 7303 | |
| 7304 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7305 | &key)); |
| 7306 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7307 | key_bits = psa_get_key_bits(&attributes); |
| 7308 | |
| 7309 | /* Allocate a buffer which has the size advertised by the |
| 7310 | * library. */ |
| 7311 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7312 | key_bits, alg); |
| 7313 | TEST_ASSERT(signature_size != 0); |
| 7314 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7315 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7316 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7317 | psa_interruptible_set_max_ops(max_ops); |
| 7318 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7319 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 7320 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7321 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7322 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); |
| 7323 | TEST_ASSERT(num_ops_prior == 0); |
| 7324 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7325 | /* Start performing the signature. */ |
| 7326 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7327 | input_data->x, input_data->len)); |
| 7328 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7329 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); |
| 7330 | TEST_ASSERT(num_ops_prior == 0); |
| 7331 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7332 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7333 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7334 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7335 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7336 | signature_size, |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7337 | &signature_length); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7338 | |
| 7339 | num_completes++; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7340 | |
| 7341 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 7342 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7343 | /* We are asserting here that every complete makes progress |
| 7344 | * (completes some ops), which is true of the internal |
| 7345 | * implementation and probably any implementation, however this is |
| 7346 | * not mandated by the PSA specification. */ |
| 7347 | TEST_ASSERT(num_ops > num_ops_prior); |
| 7348 | |
| 7349 | num_ops_prior = num_ops; |
| 7350 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7351 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7352 | |
| 7353 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7354 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7355 | TEST_LE_U(min_completes, num_completes); |
| 7356 | TEST_LE_U(num_completes, max_completes); |
| 7357 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7358 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7359 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7360 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7361 | TEST_ASSERT(num_ops == 0); |
| 7362 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7363 | /* Check that the signature length looks sensible. */ |
| 7364 | TEST_LE_U(signature_length, signature_size); |
| 7365 | TEST_ASSERT(signature_length > 0); |
| 7366 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7367 | num_completes = 0; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7368 | |
| 7369 | /* Start verification. */ |
| 7370 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7371 | input_data->x, input_data->len, |
| 7372 | signature, signature_length)); |
| 7373 | |
| 7374 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7375 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7376 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7377 | |
| 7378 | num_completes++; |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7379 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7380 | |
| 7381 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7382 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7383 | TEST_LE_U(min_completes, num_completes); |
| 7384 | TEST_LE_U(num_completes, max_completes); |
| 7385 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7386 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7387 | |
| 7388 | verify_operation = psa_verify_hash_interruptible_operation_init(); |
| 7389 | |
| 7390 | if (input_data->len != 0) { |
| 7391 | /* Flip a bit in the input and verify that the signature is now |
| 7392 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 7393 | * because ECDSA may ignore the last few bits of the input. */ |
| 7394 | input_data->x[0] ^= 1; |
| 7395 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7396 | /* Start verification. */ |
| 7397 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7398 | input_data->x, input_data->len, |
| 7399 | signature, signature_length)); |
| 7400 | |
| 7401 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7402 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7403 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7404 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7405 | |
| 7406 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7407 | } |
| 7408 | |
| 7409 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7410 | |
| 7411 | exit: |
| 7412 | /* |
| 7413 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7414 | * thus reset them as required. |
| 7415 | */ |
| 7416 | psa_reset_key_attributes(&attributes); |
| 7417 | |
| 7418 | psa_destroy_key(key); |
| 7419 | mbedtls_free(signature); |
| 7420 | PSA_DONE(); |
| 7421 | } |
| 7422 | /* END_CASE */ |
| 7423 | |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7424 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7425 | void verify_hash(int key_type_arg, data_t *key_data, |
| 7426 | int alg_arg, data_t *hash_data, |
| 7427 | data_t *signature_data) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7428 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7429 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7430 | psa_key_type_t key_type = key_type_arg; |
| 7431 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7432 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7433 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7434 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 7435 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7436 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7437 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7438 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7439 | psa_set_key_algorithm(&attributes, alg); |
| 7440 | psa_set_key_type(&attributes, key_type); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7441 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7442 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7443 | &key)); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7444 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7445 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 7446 | hash_data->x, hash_data->len, |
| 7447 | signature_data->x, signature_data->len)); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 7448 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7449 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7450 | psa_reset_key_attributes(&attributes); |
| 7451 | psa_destroy_key(key); |
| 7452 | PSA_DONE(); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7453 | } |
| 7454 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7455 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7456 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7457 | /** |
| 7458 | * verify_hash_interruptible() test intentions: |
| 7459 | * |
| 7460 | * Note: This test can currently only handle ECDSA. |
| 7461 | * |
| 7462 | * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 7463 | * only). Given this test only does verification it can accept public keys as |
| 7464 | * well as private keys / keypairs. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7465 | * |
| 7466 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7467 | * expected for different max_ops values. |
| 7468 | * |
| 7469 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7470 | * and that each successful stage completes some ops (this is not mandated by |
| 7471 | * the PSA specification, but is currently the case). |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7472 | * |
| 7473 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 7474 | * complete() calls does not alter the number of ops returned. |
| 7475 | * |
| 7476 | * 5. Test that after corrupting the hash, the verification detects an invalid |
| 7477 | * signature. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7478 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7479 | void verify_hash_interruptible(int key_type_arg, data_t *key_data, |
| 7480 | int alg_arg, data_t *hash_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7481 | data_t *signature_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7482 | { |
| 7483 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7484 | psa_key_type_t key_type = key_type_arg; |
| 7485 | psa_algorithm_t alg = alg_arg; |
| 7486 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7487 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7488 | uint32_t num_ops = 0; |
| 7489 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7490 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7491 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7492 | size_t min_completes = 0; |
| 7493 | size_t max_completes = 0; |
| 7494 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7495 | psa_verify_hash_interruptible_operation_t operation = |
| 7496 | psa_verify_hash_interruptible_operation_init(); |
| 7497 | |
| 7498 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 7499 | |
| 7500 | PSA_ASSERT(psa_crypto_init()); |
| 7501 | |
| 7502 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7503 | psa_set_key_algorithm(&attributes, alg); |
| 7504 | psa_set_key_type(&attributes, key_type); |
| 7505 | |
| 7506 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7507 | &key)); |
| 7508 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7509 | psa_interruptible_set_max_ops(max_ops); |
| 7510 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7511 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 7512 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7513 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7514 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7515 | |
| 7516 | TEST_ASSERT(num_ops_prior == 0); |
| 7517 | |
| 7518 | /* Start verification. */ |
| 7519 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7520 | hash_data->x, hash_data->len, |
| 7521 | signature_data->x, signature_data->len) |
| 7522 | ); |
| 7523 | |
| 7524 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7525 | |
| 7526 | TEST_ASSERT(num_ops_prior == 0); |
| 7527 | |
| 7528 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7529 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7530 | status = psa_verify_hash_complete(&operation); |
| 7531 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7532 | num_completes++; |
| 7533 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7534 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 7535 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7536 | /* We are asserting here that every complete makes progress |
| 7537 | * (completes some ops), which is true of the internal |
| 7538 | * implementation and probably any implementation, however this is |
| 7539 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7540 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7541 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7542 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 7543 | |
| 7544 | /* Ensure calling get_num_ops() twice still returns the same |
| 7545 | * number of ops as previously reported. */ |
| 7546 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7547 | |
| 7548 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7549 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7550 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7551 | |
| 7552 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7553 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7554 | TEST_LE_U(min_completes, num_completes); |
| 7555 | TEST_LE_U(num_completes, max_completes); |
| 7556 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7557 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7558 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7559 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7560 | TEST_ASSERT(num_ops == 0); |
| 7561 | |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7562 | if (hash_data->len != 0) { |
| 7563 | /* Flip a bit in the hash and verify that the signature is now detected |
| 7564 | * as invalid. Flip a bit at the beginning, not at the end, because |
| 7565 | * ECDSA may ignore the last few bits of the input. */ |
| 7566 | hash_data->x[0] ^= 1; |
| 7567 | |
| 7568 | /* Start verification. */ |
| 7569 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7570 | hash_data->x, hash_data->len, |
| 7571 | signature_data->x, signature_data->len)); |
| 7572 | |
| 7573 | /* Continue performing the signature until complete. */ |
| 7574 | do { |
| 7575 | status = psa_verify_hash_complete(&operation); |
| 7576 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7577 | |
| 7578 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7579 | } |
| 7580 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7581 | exit: |
| 7582 | psa_reset_key_attributes(&attributes); |
| 7583 | psa_destroy_key(key); |
| 7584 | PSA_DONE(); |
| 7585 | } |
| 7586 | /* END_CASE */ |
| 7587 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7588 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7589 | void verify_hash_fail(int key_type_arg, data_t *key_data, |
| 7590 | int alg_arg, data_t *hash_data, |
| 7591 | data_t *signature_data, |
| 7592 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7593 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7594 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7595 | psa_key_type_t key_type = key_type_arg; |
| 7596 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7597 | psa_status_t actual_status; |
| 7598 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7599 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7600 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7601 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7602 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7603 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7604 | psa_set_key_algorithm(&attributes, alg); |
| 7605 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7606 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7607 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7608 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7609 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7610 | actual_status = psa_verify_hash(key, alg, |
| 7611 | hash_data->x, hash_data->len, |
| 7612 | signature_data->x, signature_data->len); |
| 7613 | TEST_EQUAL(actual_status, expected_status); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7614 | |
| 7615 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7616 | psa_reset_key_attributes(&attributes); |
| 7617 | psa_destroy_key(key); |
| 7618 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7619 | } |
| 7620 | /* END_CASE */ |
| 7621 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7622 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7623 | /** |
| 7624 | * verify_hash_fail_interruptible() test intentions: |
| 7625 | * |
| 7626 | * Note: This test can currently only handle ECDSA. |
| 7627 | * |
| 7628 | * 1. Test that various failure cases for interruptible verify hash fail with |
| 7629 | * the correct error codes, and at the correct point (at start or during |
| 7630 | * complete). |
| 7631 | * |
| 7632 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7633 | * expected for different max_ops values. |
| 7634 | * |
| 7635 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7636 | * and that each successful stage completes some ops (this is not mandated by |
| 7637 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7638 | * |
| 7639 | * 4. Check that calling complete() when start() fails and complete() |
| 7640 | * after completion results in a BAD_STATE error. |
| 7641 | * |
| 7642 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 7643 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7644 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7645 | void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 7646 | int alg_arg, data_t *hash_data, |
| 7647 | data_t *signature_data, |
| 7648 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7649 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7650 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7651 | { |
| 7652 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7653 | psa_key_type_t key_type = key_type_arg; |
| 7654 | psa_algorithm_t alg = alg_arg; |
| 7655 | psa_status_t actual_status; |
| 7656 | psa_status_t expected_start_status = expected_start_status_arg; |
| 7657 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 7658 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7659 | uint32_t num_ops = 0; |
| 7660 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7661 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7662 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7663 | size_t min_completes = 0; |
| 7664 | size_t max_completes = 0; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7665 | psa_verify_hash_interruptible_operation_t operation = |
| 7666 | psa_verify_hash_interruptible_operation_init(); |
| 7667 | |
| 7668 | PSA_ASSERT(psa_crypto_init()); |
| 7669 | |
| 7670 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7671 | psa_set_key_algorithm(&attributes, alg); |
| 7672 | psa_set_key_type(&attributes, key_type); |
| 7673 | |
| 7674 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7675 | &key)); |
| 7676 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7677 | psa_interruptible_set_max_ops(max_ops); |
| 7678 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7679 | interruptible_signverify_get_minmax_completes(max_ops, |
| 7680 | expected_complete_status, |
| 7681 | &min_completes, |
| 7682 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7683 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7684 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7685 | TEST_ASSERT(num_ops_prior == 0); |
| 7686 | |
| 7687 | /* Start verification. */ |
| 7688 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7689 | hash_data->x, hash_data->len, |
| 7690 | signature_data->x, |
| 7691 | signature_data->len); |
| 7692 | |
| 7693 | TEST_EQUAL(actual_status, expected_start_status); |
| 7694 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7695 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 7696 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7697 | * start failed. */ |
| 7698 | actual_status = psa_verify_hash_complete(&operation); |
| 7699 | |
| 7700 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7701 | |
| 7702 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7703 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7704 | hash_data->x, hash_data->len, |
| 7705 | signature_data->x, |
| 7706 | signature_data->len); |
| 7707 | |
| 7708 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7709 | } |
| 7710 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7711 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7712 | TEST_ASSERT(num_ops_prior == 0); |
| 7713 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7714 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7715 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7716 | actual_status = psa_verify_hash_complete(&operation); |
| 7717 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7718 | num_completes++; |
| 7719 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7720 | if (actual_status == PSA_SUCCESS || |
| 7721 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7722 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7723 | /* We are asserting here that every complete makes progress |
| 7724 | * (completes some ops), which is true of the internal |
| 7725 | * implementation and probably any implementation, however this is |
| 7726 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7727 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7728 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7729 | num_ops_prior = num_ops; |
| 7730 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7731 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7732 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7733 | TEST_EQUAL(actual_status, expected_complete_status); |
| 7734 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 7735 | /* Check that another complete returns BAD_STATE. */ |
| 7736 | actual_status = psa_verify_hash_complete(&operation); |
| 7737 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7738 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7739 | TEST_LE_U(min_completes, num_completes); |
| 7740 | TEST_LE_U(num_completes, max_completes); |
| 7741 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7742 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7743 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7744 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7745 | TEST_ASSERT(num_ops == 0); |
| 7746 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7747 | exit: |
| 7748 | psa_reset_key_attributes(&attributes); |
| 7749 | psa_destroy_key(key); |
| 7750 | PSA_DONE(); |
| 7751 | } |
| 7752 | /* END_CASE */ |
| 7753 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7754 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7755 | /** |
| 7756 | * interruptible_signverify_hash_state_test() test intentions: |
| 7757 | * |
| 7758 | * Note: This test can currently only handle ECDSA. |
| 7759 | * |
| 7760 | * 1. Test that calling the various interruptible sign and verify hash functions |
| 7761 | * in incorrect orders returns BAD_STATE errors. |
| 7762 | */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7763 | void interruptible_signverify_hash_state_test(int key_type_arg, |
| 7764 | data_t *key_data, int alg_arg, data_t *input_data) |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7765 | { |
| 7766 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7767 | psa_key_type_t key_type = key_type_arg; |
| 7768 | psa_algorithm_t alg = alg_arg; |
| 7769 | size_t key_bits; |
| 7770 | unsigned char *signature = NULL; |
| 7771 | size_t signature_size; |
| 7772 | size_t signature_length = 0xdeadbeef; |
| 7773 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7774 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7775 | psa_sign_hash_interruptible_operation_init(); |
| 7776 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7777 | psa_verify_hash_interruptible_operation_init(); |
| 7778 | |
| 7779 | PSA_ASSERT(psa_crypto_init()); |
| 7780 | |
| 7781 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7782 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7783 | psa_set_key_algorithm(&attributes, alg); |
| 7784 | psa_set_key_type(&attributes, key_type); |
| 7785 | |
| 7786 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7787 | &key)); |
| 7788 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7789 | key_bits = psa_get_key_bits(&attributes); |
| 7790 | |
| 7791 | /* Allocate a buffer which has the size advertised by the |
| 7792 | * library. */ |
| 7793 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7794 | key_bits, alg); |
| 7795 | TEST_ASSERT(signature_size != 0); |
| 7796 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7797 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7798 | |
| 7799 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7800 | |
| 7801 | /* --- Attempt completes prior to starts --- */ |
| 7802 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7803 | signature_size, |
| 7804 | &signature_length), |
| 7805 | PSA_ERROR_BAD_STATE); |
| 7806 | |
| 7807 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7808 | |
| 7809 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7810 | PSA_ERROR_BAD_STATE); |
| 7811 | |
| 7812 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7813 | |
| 7814 | /* --- Aborts in all other places. --- */ |
| 7815 | psa_sign_hash_abort(&sign_operation); |
| 7816 | |
| 7817 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7818 | input_data->x, input_data->len)); |
| 7819 | |
| 7820 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7821 | |
| 7822 | psa_interruptible_set_max_ops(1); |
| 7823 | |
| 7824 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7825 | input_data->x, input_data->len)); |
| 7826 | |
| 7827 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7828 | signature_size, |
| 7829 | &signature_length), |
| 7830 | PSA_OPERATION_INCOMPLETE); |
| 7831 | |
| 7832 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7833 | |
| 7834 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7835 | |
| 7836 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7837 | input_data->x, input_data->len)); |
| 7838 | |
| 7839 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7840 | signature_size, |
| 7841 | &signature_length)); |
| 7842 | |
| 7843 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7844 | |
| 7845 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7846 | |
| 7847 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7848 | input_data->x, input_data->len, |
| 7849 | signature, signature_length)); |
| 7850 | |
| 7851 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7852 | |
| 7853 | psa_interruptible_set_max_ops(1); |
| 7854 | |
| 7855 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7856 | input_data->x, input_data->len, |
| 7857 | signature, signature_length)); |
| 7858 | |
| 7859 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7860 | PSA_OPERATION_INCOMPLETE); |
| 7861 | |
| 7862 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7863 | |
| 7864 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7865 | |
| 7866 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7867 | input_data->x, input_data->len, |
| 7868 | signature, signature_length)); |
| 7869 | |
| 7870 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7871 | |
| 7872 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7873 | |
| 7874 | /* --- Attempt double starts. --- */ |
| 7875 | |
| 7876 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7877 | input_data->x, input_data->len)); |
| 7878 | |
| 7879 | TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg, |
| 7880 | input_data->x, input_data->len), |
| 7881 | PSA_ERROR_BAD_STATE); |
| 7882 | |
| 7883 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7884 | |
| 7885 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7886 | input_data->x, input_data->len, |
| 7887 | signature, signature_length)); |
| 7888 | |
| 7889 | TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg, |
| 7890 | input_data->x, input_data->len, |
| 7891 | signature, signature_length), |
| 7892 | PSA_ERROR_BAD_STATE); |
| 7893 | |
| 7894 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7895 | |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7896 | exit: |
| 7897 | /* |
| 7898 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7899 | * thus reset them as required. |
| 7900 | */ |
| 7901 | psa_reset_key_attributes(&attributes); |
| 7902 | |
| 7903 | psa_destroy_key(key); |
| 7904 | mbedtls_free(signature); |
| 7905 | PSA_DONE(); |
| 7906 | } |
| 7907 | /* END_CASE */ |
| 7908 | |
| 7909 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7910 | /** |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7911 | * interruptible_signverify_hash_edgecase_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7912 | * |
| 7913 | * Note: This test can currently only handle ECDSA. |
| 7914 | * |
| 7915 | * 1. Test various edge cases in the interruptible sign and verify hash |
| 7916 | * interfaces. |
| 7917 | */ |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7918 | void interruptible_signverify_hash_edgecase_tests(int key_type_arg, |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7919 | data_t *key_data, int alg_arg, data_t *input_data) |
| 7920 | { |
| 7921 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7922 | psa_key_type_t key_type = key_type_arg; |
| 7923 | psa_algorithm_t alg = alg_arg; |
| 7924 | size_t key_bits; |
| 7925 | unsigned char *signature = NULL; |
| 7926 | size_t signature_size; |
| 7927 | size_t signature_length = 0xdeadbeef; |
| 7928 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7929 | uint8_t *input_buffer = NULL; |
| 7930 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7931 | psa_sign_hash_interruptible_operation_init(); |
| 7932 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7933 | psa_verify_hash_interruptible_operation_init(); |
| 7934 | |
| 7935 | PSA_ASSERT(psa_crypto_init()); |
| 7936 | |
| 7937 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7938 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7939 | psa_set_key_algorithm(&attributes, alg); |
| 7940 | psa_set_key_type(&attributes, key_type); |
| 7941 | |
| 7942 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7943 | &key)); |
| 7944 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7945 | key_bits = psa_get_key_bits(&attributes); |
| 7946 | |
| 7947 | /* Allocate a buffer which has the size advertised by the |
| 7948 | * library. */ |
| 7949 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7950 | key_bits, alg); |
| 7951 | TEST_ASSERT(signature_size != 0); |
| 7952 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7953 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7954 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7955 | /* --- Change function inputs mid run, to cause an error (sign only, |
| 7956 | * verify passes all inputs to start. --- */ |
| 7957 | |
| 7958 | psa_interruptible_set_max_ops(1); |
| 7959 | |
| 7960 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7961 | input_data->x, input_data->len)); |
| 7962 | |
| 7963 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7964 | signature_size, |
| 7965 | &signature_length), |
| 7966 | PSA_OPERATION_INCOMPLETE); |
| 7967 | |
| 7968 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7969 | 0, |
| 7970 | &signature_length), |
| 7971 | PSA_ERROR_BUFFER_TOO_SMALL); |
| 7972 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7973 | /* And test that this invalidates the operation. */ |
| 7974 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7975 | 0, |
| 7976 | &signature_length), |
| 7977 | PSA_ERROR_BAD_STATE); |
| 7978 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7979 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7980 | |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7981 | /* Trash the hash buffer in between start and complete, to ensure |
| 7982 | * no reliance on external buffers. */ |
| 7983 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7984 | |
Paul Elliott | 6c68df4 | 2023-10-23 15:33:37 +0100 | [diff] [blame] | 7985 | TEST_CALLOC(input_buffer, input_data->len); |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7986 | |
| 7987 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7988 | |
| 7989 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7990 | input_buffer, input_data->len)); |
| 7991 | |
| 7992 | memset(input_buffer, '!', input_data->len); |
| 7993 | mbedtls_free(input_buffer); |
| 7994 | input_buffer = NULL; |
| 7995 | |
| 7996 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7997 | signature_size, |
| 7998 | &signature_length)); |
| 7999 | |
| 8000 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 8001 | |
Paul Elliott | 6c68df4 | 2023-10-23 15:33:37 +0100 | [diff] [blame] | 8002 | TEST_CALLOC(input_buffer, input_data->len); |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 8003 | |
| 8004 | memcpy(input_buffer, input_data->x, input_data->len); |
| 8005 | |
| 8006 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 8007 | input_buffer, input_data->len, |
| 8008 | signature, signature_length)); |
| 8009 | |
| 8010 | memset(input_buffer, '!', input_data->len); |
| 8011 | mbedtls_free(input_buffer); |
| 8012 | input_buffer = NULL; |
| 8013 | |
| 8014 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 8015 | |
| 8016 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 8017 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 8018 | exit: |
| 8019 | /* |
| 8020 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8021 | * thus reset them as required. |
| 8022 | */ |
| 8023 | psa_reset_key_attributes(&attributes); |
| 8024 | |
| 8025 | psa_destroy_key(key); |
| 8026 | mbedtls_free(signature); |
Paul Elliott | 6c68df4 | 2023-10-23 15:33:37 +0100 | [diff] [blame] | 8027 | mbedtls_free(input_buffer); |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 8028 | PSA_DONE(); |
| 8029 | } |
| 8030 | /* END_CASE */ |
| 8031 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8032 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 8033 | /** |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 8034 | * interruptible_signverify_hash_ops_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 8035 | * |
| 8036 | * Note: This test can currently only handle ECDSA. |
| 8037 | * |
| 8038 | * 1. Test that setting max ops is reflected in both interruptible sign and |
| 8039 | * verify hash |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 8040 | * 2. Test that changing the value of max_ops to unlimited during an operation |
| 8041 | * causes that operation to complete in the next call. |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 8042 | * |
| 8043 | * 3. Test that calling get_num_ops() between complete calls gives the same |
| 8044 | * 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] | 8045 | */ |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 8046 | void interruptible_signverify_hash_ops_tests(int key_type_arg, |
| 8047 | data_t *key_data, int alg_arg, |
| 8048 | data_t *input_data) |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8049 | { |
| 8050 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8051 | psa_key_type_t key_type = key_type_arg; |
| 8052 | psa_algorithm_t alg = alg_arg; |
| 8053 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 8054 | size_t key_bits; |
| 8055 | unsigned char *signature = NULL; |
| 8056 | size_t signature_size; |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 8057 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 8058 | uint32_t num_ops = 0; |
| 8059 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 8060 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8061 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 8062 | psa_sign_hash_interruptible_operation_init(); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 8063 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 8064 | psa_verify_hash_interruptible_operation_init(); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8065 | |
| 8066 | PSA_ASSERT(psa_crypto_init()); |
| 8067 | |
| 8068 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 8069 | PSA_KEY_USAGE_VERIFY_HASH); |
| 8070 | psa_set_key_algorithm(&attributes, alg); |
| 8071 | psa_set_key_type(&attributes, key_type); |
| 8072 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 8073 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key)); |
| 8074 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8075 | key_bits = psa_get_key_bits(&attributes); |
| 8076 | |
| 8077 | /* Allocate a buffer which has the size advertised by the |
| 8078 | * library. */ |
| 8079 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8080 | |
| 8081 | TEST_ASSERT(signature_size != 0); |
| 8082 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8083 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8084 | |
| 8085 | /* Check that default max ops gets set if we don't set it. */ |
| 8086 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 8087 | input_data->x, input_data->len)); |
| 8088 | |
| 8089 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 8090 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 8091 | |
| 8092 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 8093 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 8094 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 8095 | input_data->x, input_data->len, |
| 8096 | signature, signature_size)); |
| 8097 | |
| 8098 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 8099 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 8100 | |
| 8101 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 8102 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8103 | /* Check that max ops gets set properly. */ |
| 8104 | |
| 8105 | psa_interruptible_set_max_ops(0xbeef); |
| 8106 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 8107 | TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8108 | |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 8109 | /* --- Ensure changing the max ops mid operation works (operation should |
| 8110 | * complete successfully after setting max ops to unlimited --- */ |
| 8111 | psa_interruptible_set_max_ops(1); |
| 8112 | |
| 8113 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 8114 | input_data->x, input_data->len)); |
| 8115 | |
| 8116 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 8117 | signature_size, |
| 8118 | &signature_length), |
| 8119 | PSA_OPERATION_INCOMPLETE); |
| 8120 | |
| 8121 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 8122 | |
| 8123 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 8124 | signature_size, |
| 8125 | &signature_length)); |
| 8126 | |
| 8127 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 8128 | |
| 8129 | psa_interruptible_set_max_ops(1); |
| 8130 | |
| 8131 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 8132 | input_data->x, input_data->len, |
| 8133 | signature, signature_length)); |
| 8134 | |
| 8135 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 8136 | PSA_OPERATION_INCOMPLETE); |
| 8137 | |
| 8138 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 8139 | |
| 8140 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 8141 | |
| 8142 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 8143 | |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 8144 | /* --- Test that not calling get_num_ops inbetween complete calls does not |
| 8145 | * result in lost ops. ---*/ |
| 8146 | |
| 8147 | psa_interruptible_set_max_ops(1); |
| 8148 | |
| 8149 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 8150 | input_data->x, input_data->len)); |
| 8151 | |
| 8152 | /* Continue performing the signature until complete. */ |
| 8153 | do { |
| 8154 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 8155 | signature_size, |
| 8156 | &signature_length); |
| 8157 | |
| 8158 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 8159 | |
| 8160 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 8161 | |
| 8162 | PSA_ASSERT(status); |
| 8163 | |
| 8164 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 8165 | |
| 8166 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 8167 | input_data->x, input_data->len)); |
| 8168 | |
| 8169 | /* Continue performing the signature until complete. */ |
| 8170 | do { |
| 8171 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 8172 | signature_size, |
| 8173 | &signature_length); |
| 8174 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 8175 | |
| 8176 | PSA_ASSERT(status); |
| 8177 | |
| 8178 | TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation)); |
| 8179 | |
| 8180 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 8181 | |
| 8182 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 8183 | input_data->x, input_data->len, |
| 8184 | signature, signature_length)); |
| 8185 | |
| 8186 | /* Continue performing the verification until complete. */ |
| 8187 | do { |
| 8188 | status = psa_verify_hash_complete(&verify_operation); |
| 8189 | |
| 8190 | num_ops = psa_verify_hash_get_num_ops(&verify_operation); |
| 8191 | |
| 8192 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 8193 | |
| 8194 | PSA_ASSERT(status); |
| 8195 | |
| 8196 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 8197 | |
| 8198 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 8199 | input_data->x, input_data->len, |
| 8200 | signature, signature_length)); |
| 8201 | |
| 8202 | /* Continue performing the verification until complete. */ |
| 8203 | do { |
| 8204 | status = psa_verify_hash_complete(&verify_operation); |
| 8205 | |
| 8206 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 8207 | |
| 8208 | PSA_ASSERT(status); |
| 8209 | |
| 8210 | TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation)); |
| 8211 | |
| 8212 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 8213 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8214 | exit: |
| 8215 | /* |
| 8216 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8217 | * thus reset them as required. |
| 8218 | */ |
| 8219 | psa_reset_key_attributes(&attributes); |
| 8220 | |
| 8221 | psa_destroy_key(key); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 8222 | mbedtls_free(signature); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8223 | PSA_DONE(); |
| 8224 | } |
| 8225 | /* END_CASE */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 8226 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8227 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8228 | void sign_message_deterministic(int key_type_arg, |
| 8229 | data_t *key_data, |
| 8230 | int alg_arg, |
| 8231 | data_t *input_data, |
| 8232 | data_t *output_data) |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8233 | { |
| 8234 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8235 | psa_key_type_t key_type = key_type_arg; |
| 8236 | psa_algorithm_t alg = alg_arg; |
| 8237 | size_t key_bits; |
| 8238 | unsigned char *signature = NULL; |
| 8239 | size_t signature_size; |
| 8240 | size_t signature_length = 0xdeadbeef; |
| 8241 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8242 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8243 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8244 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8245 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 8246 | psa_set_key_algorithm(&attributes, alg); |
| 8247 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8248 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8249 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8250 | &key)); |
| 8251 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8252 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8253 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8254 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8255 | TEST_ASSERT(signature_size != 0); |
| 8256 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8257 | TEST_CALLOC(signature, signature_size); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8258 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8259 | PSA_ASSERT(psa_sign_message(key, alg, |
| 8260 | input_data->x, input_data->len, |
| 8261 | signature, signature_size, |
| 8262 | &signature_length)); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8263 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8264 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8265 | signature, signature_length); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8266 | |
| 8267 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8268 | psa_reset_key_attributes(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8269 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8270 | psa_destroy_key(key); |
| 8271 | mbedtls_free(signature); |
| 8272 | PSA_DONE(); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8273 | |
| 8274 | } |
| 8275 | /* END_CASE */ |
| 8276 | |
| 8277 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8278 | void sign_message_fail(int key_type_arg, |
| 8279 | data_t *key_data, |
| 8280 | int alg_arg, |
| 8281 | data_t *input_data, |
| 8282 | int signature_size_arg, |
| 8283 | int expected_status_arg) |
| 8284 | { |
| 8285 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8286 | psa_key_type_t key_type = key_type_arg; |
| 8287 | psa_algorithm_t alg = alg_arg; |
| 8288 | size_t signature_size = signature_size_arg; |
| 8289 | psa_status_t actual_status; |
| 8290 | psa_status_t expected_status = expected_status_arg; |
| 8291 | unsigned char *signature = NULL; |
| 8292 | size_t signature_length = 0xdeadbeef; |
| 8293 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8294 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8295 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8296 | |
| 8297 | PSA_ASSERT(psa_crypto_init()); |
| 8298 | |
| 8299 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 8300 | psa_set_key_algorithm(&attributes, alg); |
| 8301 | psa_set_key_type(&attributes, key_type); |
| 8302 | |
| 8303 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8304 | &key)); |
| 8305 | |
| 8306 | actual_status = psa_sign_message(key, alg, |
| 8307 | input_data->x, input_data->len, |
| 8308 | signature, signature_size, |
| 8309 | &signature_length); |
| 8310 | TEST_EQUAL(actual_status, expected_status); |
| 8311 | /* The value of *signature_length is unspecified on error, but |
| 8312 | * whatever it is, it should be less than signature_size, so that |
| 8313 | * if the caller tries to read *signature_length bytes without |
| 8314 | * checking the error code then they don't overflow a buffer. */ |
| 8315 | TEST_LE_U(signature_length, signature_size); |
| 8316 | |
| 8317 | exit: |
| 8318 | psa_reset_key_attributes(&attributes); |
| 8319 | psa_destroy_key(key); |
| 8320 | mbedtls_free(signature); |
| 8321 | PSA_DONE(); |
| 8322 | } |
| 8323 | /* END_CASE */ |
| 8324 | |
| 8325 | /* BEGIN_CASE */ |
| 8326 | void sign_verify_message(int key_type_arg, |
| 8327 | data_t *key_data, |
| 8328 | int alg_arg, |
| 8329 | data_t *input_data) |
| 8330 | { |
| 8331 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8332 | psa_key_type_t key_type = key_type_arg; |
| 8333 | psa_algorithm_t alg = alg_arg; |
| 8334 | size_t key_bits; |
| 8335 | unsigned char *signature = NULL; |
| 8336 | size_t signature_size; |
| 8337 | size_t signature_length = 0xdeadbeef; |
| 8338 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8339 | |
| 8340 | PSA_ASSERT(psa_crypto_init()); |
| 8341 | |
| 8342 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 8343 | PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8344 | psa_set_key_algorithm(&attributes, alg); |
| 8345 | psa_set_key_type(&attributes, key_type); |
| 8346 | |
| 8347 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8348 | &key)); |
| 8349 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8350 | key_bits = psa_get_key_bits(&attributes); |
| 8351 | |
| 8352 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8353 | TEST_ASSERT(signature_size != 0); |
| 8354 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8355 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8356 | |
| 8357 | PSA_ASSERT(psa_sign_message(key, alg, |
| 8358 | input_data->x, input_data->len, |
| 8359 | signature, signature_size, |
| 8360 | &signature_length)); |
| 8361 | TEST_LE_U(signature_length, signature_size); |
| 8362 | TEST_ASSERT(signature_length > 0); |
| 8363 | |
| 8364 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8365 | input_data->x, input_data->len, |
| 8366 | signature, signature_length)); |
| 8367 | |
| 8368 | if (input_data->len != 0) { |
| 8369 | /* Flip a bit in the input and verify that the signature is now |
| 8370 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 8371 | * because ECDSA may ignore the last few bits of the input. */ |
| 8372 | input_data->x[0] ^= 1; |
| 8373 | TEST_EQUAL(psa_verify_message(key, alg, |
| 8374 | input_data->x, input_data->len, |
| 8375 | signature, signature_length), |
| 8376 | PSA_ERROR_INVALID_SIGNATURE); |
| 8377 | } |
| 8378 | |
| 8379 | exit: |
| 8380 | psa_reset_key_attributes(&attributes); |
| 8381 | |
| 8382 | psa_destroy_key(key); |
| 8383 | mbedtls_free(signature); |
| 8384 | PSA_DONE(); |
| 8385 | } |
| 8386 | /* END_CASE */ |
| 8387 | |
| 8388 | /* BEGIN_CASE */ |
| 8389 | void verify_message(int key_type_arg, |
| 8390 | data_t *key_data, |
| 8391 | int alg_arg, |
| 8392 | data_t *input_data, |
| 8393 | data_t *signature_data) |
| 8394 | { |
| 8395 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8396 | psa_key_type_t key_type = key_type_arg; |
| 8397 | psa_algorithm_t alg = alg_arg; |
| 8398 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8399 | |
| 8400 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 8401 | |
| 8402 | PSA_ASSERT(psa_crypto_init()); |
| 8403 | |
| 8404 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8405 | psa_set_key_algorithm(&attributes, alg); |
| 8406 | psa_set_key_type(&attributes, key_type); |
| 8407 | |
| 8408 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8409 | &key)); |
| 8410 | |
| 8411 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8412 | input_data->x, input_data->len, |
| 8413 | signature_data->x, signature_data->len)); |
| 8414 | |
| 8415 | exit: |
| 8416 | psa_reset_key_attributes(&attributes); |
| 8417 | psa_destroy_key(key); |
| 8418 | PSA_DONE(); |
| 8419 | } |
| 8420 | /* END_CASE */ |
| 8421 | |
| 8422 | /* BEGIN_CASE */ |
| 8423 | void verify_message_fail(int key_type_arg, |
| 8424 | data_t *key_data, |
| 8425 | int alg_arg, |
| 8426 | data_t *hash_data, |
| 8427 | data_t *signature_data, |
| 8428 | int expected_status_arg) |
| 8429 | { |
| 8430 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8431 | psa_key_type_t key_type = key_type_arg; |
| 8432 | psa_algorithm_t alg = alg_arg; |
| 8433 | psa_status_t actual_status; |
| 8434 | psa_status_t expected_status = expected_status_arg; |
| 8435 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8436 | |
| 8437 | PSA_ASSERT(psa_crypto_init()); |
| 8438 | |
| 8439 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8440 | psa_set_key_algorithm(&attributes, alg); |
| 8441 | psa_set_key_type(&attributes, key_type); |
| 8442 | |
| 8443 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8444 | &key)); |
| 8445 | |
| 8446 | actual_status = psa_verify_message(key, alg, |
| 8447 | hash_data->x, hash_data->len, |
| 8448 | signature_data->x, |
| 8449 | signature_data->len); |
| 8450 | TEST_EQUAL(actual_status, expected_status); |
| 8451 | |
| 8452 | exit: |
| 8453 | psa_reset_key_attributes(&attributes); |
| 8454 | psa_destroy_key(key); |
| 8455 | PSA_DONE(); |
| 8456 | } |
| 8457 | /* END_CASE */ |
| 8458 | |
| 8459 | /* BEGIN_CASE */ |
| 8460 | void asymmetric_encrypt(int key_type_arg, |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8461 | data_t *key_data, |
| 8462 | int alg_arg, |
| 8463 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8464 | data_t *label, |
| 8465 | int expected_output_length_arg, |
| 8466 | int expected_status_arg) |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8467 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8468 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8469 | psa_key_type_t key_type = key_type_arg; |
| 8470 | psa_algorithm_t alg = alg_arg; |
| 8471 | size_t expected_output_length = expected_output_length_arg; |
| 8472 | size_t key_bits; |
| 8473 | unsigned char *output = NULL; |
| 8474 | size_t output_size; |
| 8475 | size_t output_length = ~0; |
| 8476 | psa_status_t actual_status; |
| 8477 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8478 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8479 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8480 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 8481 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8482 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8483 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 8484 | psa_set_key_algorithm(&attributes, alg); |
| 8485 | psa_set_key_type(&attributes, key_type); |
| 8486 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8487 | &key)); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8488 | |
| 8489 | /* Determine the maximum output length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8490 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8491 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8492 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8493 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8494 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8495 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8496 | |
| 8497 | /* Encrypt the input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8498 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8499 | input_data->x, input_data->len, |
| 8500 | label->x, label->len, |
| 8501 | output, output_size, |
| 8502 | &output_length); |
| 8503 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8504 | if (actual_status == PSA_SUCCESS) { |
| 8505 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8506 | } else { |
| 8507 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8508 | } |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8509 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8510 | /* If the label is empty, the test framework puts a non-null pointer |
| 8511 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8512 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8513 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8514 | if (output_size != 0) { |
| 8515 | memset(output, 0, output_size); |
| 8516 | } |
| 8517 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8518 | input_data->x, input_data->len, |
| 8519 | NULL, label->len, |
| 8520 | output, output_size, |
| 8521 | &output_length); |
| 8522 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8523 | if (actual_status == PSA_SUCCESS) { |
| 8524 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8525 | } else { |
| 8526 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8527 | } |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8528 | } |
| 8529 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8530 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8531 | /* |
| 8532 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8533 | * thus reset them as required. |
| 8534 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8535 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8536 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8537 | psa_destroy_key(key); |
| 8538 | mbedtls_free(output); |
| 8539 | PSA_DONE(); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8540 | } |
| 8541 | /* END_CASE */ |
| 8542 | |
| 8543 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8544 | void asymmetric_encrypt_decrypt(int key_type_arg, |
| 8545 | data_t *key_data, |
| 8546 | int alg_arg, |
| 8547 | data_t *input_data, |
| 8548 | data_t *label) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8549 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8550 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8551 | psa_key_type_t key_type = key_type_arg; |
| 8552 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8553 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8554 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8555 | size_t output_size; |
| 8556 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8557 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8558 | size_t output2_size; |
| 8559 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8560 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8562 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8563 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8564 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 8565 | psa_set_key_algorithm(&attributes, alg); |
| 8566 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8567 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8568 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8569 | &key)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8570 | |
| 8571 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8572 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8573 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8574 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8575 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8576 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8577 | TEST_CALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8578 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8579 | output2_size = input_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8580 | TEST_LE_U(output2_size, |
| 8581 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)); |
| 8582 | TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8583 | TEST_CALLOC(output2, output2_size); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8584 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 8585 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 8586 | * the original plaintext because of the non-optional random |
| 8587 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8588 | PSA_ASSERT(psa_asymmetric_encrypt(key, alg, |
| 8589 | input_data->x, input_data->len, |
| 8590 | label->x, label->len, |
| 8591 | output, output_size, |
| 8592 | &output_length)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8593 | /* We don't know what ciphertext length to expect, but check that |
| 8594 | * it looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8595 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8596 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8597 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8598 | output, output_length, |
| 8599 | label->x, label->len, |
| 8600 | output2, output2_size, |
| 8601 | &output2_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8602 | TEST_MEMORY_COMPARE(input_data->x, input_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8603 | output2, output2_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8604 | |
| 8605 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8606 | /* |
| 8607 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8608 | * thus reset them as required. |
| 8609 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8610 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8611 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8612 | psa_destroy_key(key); |
| 8613 | mbedtls_free(output); |
| 8614 | mbedtls_free(output2); |
| 8615 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8616 | } |
| 8617 | /* END_CASE */ |
| 8618 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8619 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8620 | void asymmetric_decrypt(int key_type_arg, |
| 8621 | data_t *key_data, |
| 8622 | int alg_arg, |
| 8623 | data_t *input_data, |
| 8624 | data_t *label, |
| 8625 | data_t *expected_data) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8626 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8627 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8628 | psa_key_type_t key_type = key_type_arg; |
| 8629 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8630 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8631 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 8632 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8633 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8634 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8635 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8636 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8637 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8638 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8639 | psa_set_key_algorithm(&attributes, alg); |
| 8640 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8642 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8643 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8644 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8645 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8646 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8647 | |
| 8648 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8649 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8650 | TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8651 | TEST_CALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8652 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8653 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8654 | input_data->x, input_data->len, |
| 8655 | label->x, label->len, |
| 8656 | output, |
| 8657 | output_size, |
| 8658 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8659 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8660 | output, output_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8661 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8662 | /* If the label is empty, the test framework puts a non-null pointer |
| 8663 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8664 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8665 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8666 | if (output_size != 0) { |
| 8667 | memset(output, 0, output_size); |
| 8668 | } |
| 8669 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8670 | input_data->x, input_data->len, |
| 8671 | NULL, label->len, |
| 8672 | output, |
| 8673 | output_size, |
| 8674 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8675 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8676 | output, output_length); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8677 | } |
| 8678 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8679 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8680 | psa_reset_key_attributes(&attributes); |
| 8681 | psa_destroy_key(key); |
| 8682 | mbedtls_free(output); |
| 8683 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8684 | } |
| 8685 | /* END_CASE */ |
| 8686 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8687 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8688 | void asymmetric_decrypt_fail(int key_type_arg, |
| 8689 | data_t *key_data, |
| 8690 | int alg_arg, |
| 8691 | data_t *input_data, |
| 8692 | data_t *label, |
| 8693 | int output_size_arg, |
| 8694 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8695 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8696 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8697 | psa_key_type_t key_type = key_type_arg; |
| 8698 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8699 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 8700 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8701 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8702 | psa_status_t actual_status; |
| 8703 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8704 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8705 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8706 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8707 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8708 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8709 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8710 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8711 | psa_set_key_algorithm(&attributes, alg); |
| 8712 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8713 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8714 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8715 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8716 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8717 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8718 | input_data->x, input_data->len, |
| 8719 | label->x, label->len, |
| 8720 | output, output_size, |
| 8721 | &output_length); |
| 8722 | TEST_EQUAL(actual_status, expected_status); |
| 8723 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8724 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8725 | /* If the label is empty, the test framework puts a non-null pointer |
| 8726 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8727 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8728 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8729 | if (output_size != 0) { |
| 8730 | memset(output, 0, output_size); |
| 8731 | } |
| 8732 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8733 | input_data->x, input_data->len, |
| 8734 | NULL, label->len, |
| 8735 | output, output_size, |
| 8736 | &output_length); |
| 8737 | TEST_EQUAL(actual_status, expected_status); |
| 8738 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8739 | } |
| 8740 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8741 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8742 | psa_reset_key_attributes(&attributes); |
| 8743 | psa_destroy_key(key); |
| 8744 | mbedtls_free(output); |
| 8745 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8746 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8747 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8748 | |
| 8749 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8750 | void key_derivation_init() |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8751 | { |
| 8752 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 8753 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 8754 | * 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] | 8755 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8756 | size_t capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8757 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init(); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 8758 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8759 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8760 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8761 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8762 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8763 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8764 | TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity), |
| 8765 | PSA_ERROR_BAD_STATE); |
| 8766 | TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity), |
| 8767 | PSA_ERROR_BAD_STATE); |
| 8768 | TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity), |
| 8769 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8770 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8771 | /* A default operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8772 | PSA_ASSERT(psa_key_derivation_abort(&func)); |
| 8773 | PSA_ASSERT(psa_key_derivation_abort(&init)); |
| 8774 | PSA_ASSERT(psa_key_derivation_abort(&zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8775 | } |
| 8776 | /* END_CASE */ |
| 8777 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 8778 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8779 | void derive_setup(int alg_arg, int expected_status_arg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8780 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8781 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8782 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8783 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8784 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8785 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8786 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8787 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8788 | expected_status); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8789 | |
| 8790 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8791 | psa_key_derivation_abort(&operation); |
| 8792 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8793 | } |
| 8794 | /* END_CASE */ |
| 8795 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8796 | /* BEGIN_CASE */ |
Kusumit Ghoderao | 9ffd397 | 2023-12-01 16:40:13 +0530 | [diff] [blame] | 8797 | void derive_set_capacity(int alg_arg, int64_t capacity_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8798 | int expected_status_arg) |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8799 | { |
| 8800 | psa_algorithm_t alg = alg_arg; |
| 8801 | size_t capacity = capacity_arg; |
| 8802 | psa_status_t expected_status = expected_status_arg; |
| 8803 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8804 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8805 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8806 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8807 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8808 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8809 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 8810 | expected_status); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8811 | |
| 8812 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8813 | psa_key_derivation_abort(&operation); |
| 8814 | PSA_DONE(); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8815 | } |
| 8816 | /* END_CASE */ |
| 8817 | |
| 8818 | /* BEGIN_CASE */ |
Kusumit Ghoderao | d60dfc0 | 2023-05-01 17:39:27 +0530 | [diff] [blame] | 8819 | void parse_binary_string_test(data_t *input, int output) |
| 8820 | { |
| 8821 | uint64_t value; |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 8822 | value = mbedtls_test_parse_binary_string(input); |
Kusumit Ghoderao | d60dfc0 | 2023-05-01 17:39:27 +0530 | [diff] [blame] | 8823 | TEST_EQUAL(value, output); |
| 8824 | } |
| 8825 | /* END_CASE */ |
| 8826 | |
| 8827 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8828 | void derive_input(int alg_arg, |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8829 | int step_arg1, int key_type_arg1, data_t *input1, |
| 8830 | int expected_status_arg1, |
| 8831 | int step_arg2, int key_type_arg2, data_t *input2, |
| 8832 | int expected_status_arg2, |
| 8833 | int step_arg3, int key_type_arg3, data_t *input3, |
| 8834 | int expected_status_arg3, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8835 | int output_key_type_arg, int expected_output_status_arg) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8836 | { |
| 8837 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8838 | psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 }; |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8839 | 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] | 8840 | psa_status_t expected_statuses[] = { expected_status_arg1, |
| 8841 | expected_status_arg2, |
| 8842 | expected_status_arg3 }; |
| 8843 | data_t *inputs[] = { input1, input2, input3 }; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8844 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8845 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8846 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8847 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8848 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8849 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8850 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8851 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8852 | psa_status_t expected_output_status = expected_output_status_arg; |
| 8853 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8854 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8855 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8856 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8857 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8858 | psa_set_key_algorithm(&attributes, alg); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8859 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8860 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8861 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8862 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8863 | mbedtls_test_set_step(i); |
| 8864 | if (steps[i] == 0) { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 8865 | /* Skip this step */ |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8866 | } else if (((psa_key_type_t) key_types[i]) != PSA_KEY_TYPE_NONE && |
| 8867 | key_types[i] != INPUT_INTEGER) { |
| 8868 | psa_set_key_type(&attributes, ((psa_key_type_t) key_types[i])); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8869 | PSA_ASSERT(psa_import_key(&attributes, |
| 8870 | inputs[i]->x, inputs[i]->len, |
| 8871 | &keys[i])); |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8872 | 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] | 8873 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8874 | // When taking a private key as secret input, use key agreement |
| 8875 | // to add the shared secret to the derivation |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8876 | TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self( |
Ryan Everett | 73e4ea3 | 2024-03-12 16:29:55 +0000 | [diff] [blame] | 8877 | &operation, keys[i], 0), |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8878 | expected_statuses[i]); |
| 8879 | } else { |
| 8880 | TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i], |
| 8881 | keys[i]), |
| 8882 | expected_statuses[i]); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8883 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8884 | } else { |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8885 | if (key_types[i] == INPUT_INTEGER) { |
| 8886 | TEST_EQUAL(psa_key_derivation_input_integer( |
| 8887 | &operation, steps[i], |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 8888 | mbedtls_test_parse_binary_string(inputs[i])), |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8889 | expected_statuses[i]); |
| 8890 | } else { |
Kusumit Ghoderao | 02326d5 | 2023-04-06 17:47:59 +0530 | [diff] [blame] | 8891 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8892 | &operation, steps[i], |
| 8893 | inputs[i]->x, inputs[i]->len), |
| 8894 | expected_statuses[i]); |
Kusumit Ghoderao | 02326d5 | 2023-04-06 17:47:59 +0530 | [diff] [blame] | 8895 | } |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8896 | } |
| 8897 | } |
| 8898 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8899 | if (output_key_type != PSA_KEY_TYPE_NONE) { |
| 8900 | psa_reset_key_attributes(&attributes); |
| 8901 | psa_set_key_type(&attributes, output_key_type); |
| 8902 | psa_set_key_bits(&attributes, 8); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8903 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8904 | psa_key_derivation_output_key(&attributes, &operation, |
| 8905 | &output_key); |
| 8906 | } else { |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8907 | uint8_t buffer[1]; |
| 8908 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8909 | psa_key_derivation_output_bytes(&operation, |
| 8910 | buffer, sizeof(buffer)); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8911 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8912 | TEST_EQUAL(actual_output_status, expected_output_status); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8913 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8914 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8915 | psa_key_derivation_abort(&operation); |
| 8916 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8917 | psa_destroy_key(keys[i]); |
| 8918 | } |
| 8919 | psa_destroy_key(output_key); |
| 8920 | PSA_DONE(); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8921 | } |
| 8922 | /* END_CASE */ |
| 8923 | |
Kusumit Ghoderao | 42b02b9 | 2023-06-06 16:48:46 +0530 | [diff] [blame] | 8924 | /* BEGIN_CASE*/ |
| 8925 | void derive_input_invalid_cost(int alg_arg, int64_t cost) |
| 8926 | { |
| 8927 | psa_algorithm_t alg = alg_arg; |
| 8928 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8929 | |
| 8930 | PSA_ASSERT(psa_crypto_init()); |
| 8931 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8932 | |
| 8933 | TEST_EQUAL(psa_key_derivation_input_integer(&operation, |
| 8934 | PSA_KEY_DERIVATION_INPUT_COST, |
| 8935 | cost), |
| 8936 | PSA_ERROR_NOT_SUPPORTED); |
| 8937 | |
| 8938 | exit: |
| 8939 | psa_key_derivation_abort(&operation); |
| 8940 | PSA_DONE(); |
| 8941 | } |
| 8942 | /* END_CASE*/ |
| 8943 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8944 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8945 | void derive_over_capacity(int alg_arg) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8946 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8947 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8948 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 8949 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8950 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8951 | unsigned char input1[] = "Input 1"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8952 | size_t input1_length = sizeof(input1); |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8953 | unsigned char input2[] = "Input 2"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8954 | size_t input2_length = sizeof(input2); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8955 | uint8_t buffer[42]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8956 | size_t capacity = sizeof(buffer); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 8957 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 8958 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8959 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8960 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8961 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8962 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8963 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8964 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8965 | psa_set_key_algorithm(&attributes, alg); |
| 8966 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8967 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8968 | PSA_ASSERT(psa_import_key(&attributes, |
| 8969 | key_data, sizeof(key_data), |
| 8970 | &key)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8971 | |
| 8972 | /* valid key derivation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8973 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8974 | input1, input1_length, |
| 8975 | input2, input2_length, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 8976 | capacity, 0)) { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8977 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8978 | } |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8979 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8980 | /* state of operation shouldn't allow additional generation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8981 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8982 | PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8983 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8984 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8985 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8986 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity), |
| 8987 | PSA_ERROR_INSUFFICIENT_DATA); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8988 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8989 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8990 | psa_key_derivation_abort(&operation); |
| 8991 | psa_destroy_key(key); |
| 8992 | PSA_DONE(); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8993 | } |
| 8994 | /* END_CASE */ |
| 8995 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8996 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8997 | void derive_actions_without_setup() |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8998 | { |
| 8999 | uint8_t output_buffer[16]; |
| 9000 | size_t buffer_size = 16; |
| 9001 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9002 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 9003 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9004 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9005 | output_buffer, buffer_size) |
| 9006 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 9007 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9008 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 9009 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 9010 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9011 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 9012 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9013 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9014 | output_buffer, buffer_size) |
| 9015 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 9016 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9017 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 9018 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 9019 | |
| 9020 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9021 | psa_key_derivation_abort(&operation); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 9022 | } |
| 9023 | /* END_CASE */ |
| 9024 | |
| 9025 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9026 | void derive_output(int alg_arg, |
| 9027 | int step1_arg, data_t *input1, int expected_status_arg1, |
| 9028 | int step2_arg, data_t *input2, int expected_status_arg2, |
| 9029 | int step3_arg, data_t *input3, int expected_status_arg3, |
| 9030 | int step4_arg, data_t *input4, int expected_status_arg4, |
| 9031 | data_t *key_agreement_peer_key, |
| 9032 | int requested_capacity_arg, |
| 9033 | data_t *expected_output1, |
| 9034 | data_t *expected_output2, |
| 9035 | int other_key_input_type, |
| 9036 | int key_input_type, |
| 9037 | int derive_type) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9038 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9039 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9040 | psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg }; |
| 9041 | data_t *inputs[] = { input1, input2, input3, input4 }; |
| 9042 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 9043 | MBEDTLS_SVC_KEY_ID_INIT, |
| 9044 | MBEDTLS_SVC_KEY_ID_INIT, |
| 9045 | MBEDTLS_SVC_KEY_ID_INIT }; |
| 9046 | psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2, |
| 9047 | expected_status_arg3, expected_status_arg4 }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9048 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9049 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9050 | uint8_t *expected_outputs[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9051 | { expected_output1->x, expected_output2->x }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9052 | size_t output_sizes[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9053 | { expected_output1->len, expected_output2->len }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9054 | size_t output_buffer_size = 0; |
| 9055 | uint8_t *output_buffer = NULL; |
| 9056 | size_t expected_capacity; |
| 9057 | size_t current_capacity; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9058 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; |
| 9059 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; |
| 9060 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; |
| 9061 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 9062 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9063 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 9064 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9065 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9066 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
| 9067 | if (output_sizes[i] > output_buffer_size) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9068 | output_buffer_size = output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9069 | } |
| 9070 | if (output_sizes[i] == 0) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9071 | expected_outputs[i] = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9072 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9073 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9074 | TEST_CALLOC(output_buffer, output_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9075 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9076 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9077 | /* Extraction phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9078 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9079 | PSA_ASSERT(psa_key_derivation_set_capacity(&operation, |
| 9080 | requested_capacity)); |
| 9081 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 9082 | switch (steps[i]) { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 9083 | case 0: |
| 9084 | break; |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 9085 | case PSA_KEY_DERIVATION_INPUT_COST: |
| 9086 | TEST_EQUAL(psa_key_derivation_input_integer( |
Kusumit Ghoderao | f28e0f5 | 2023-06-06 15:03:22 +0530 | [diff] [blame] | 9087 | &operation, steps[i], |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 9088 | mbedtls_test_parse_binary_string(inputs[i])), |
Kusumit Ghoderao | f28e0f5 | 2023-06-06 15:03:22 +0530 | [diff] [blame] | 9089 | statuses[i]); |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 9090 | if (statuses[i] != PSA_SUCCESS) { |
| 9091 | goto exit; |
| 9092 | } |
| 9093 | break; |
| 9094 | case PSA_KEY_DERIVATION_INPUT_PASSWORD: |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 9095 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9096 | switch (key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9097 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9098 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 9099 | &operation, steps[i], |
| 9100 | inputs[i]->x, inputs[i]->len), |
| 9101 | statuses[i]); |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 9102 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9103 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 9104 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9105 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9106 | break; |
| 9107 | case 1: // input key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9108 | psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE); |
| 9109 | psa_set_key_algorithm(&attributes1, alg); |
| 9110 | psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9111 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9112 | PSA_ASSERT(psa_import_key(&attributes1, |
| 9113 | inputs[i]->x, inputs[i]->len, |
| 9114 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9115 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9116 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 9117 | PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1)); |
| 9118 | TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)), |
| 9119 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9120 | } |
| 9121 | |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 9122 | TEST_EQUAL(psa_key_derivation_input_key(&operation, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9123 | steps[i], |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 9124 | keys[i]), |
| 9125 | statuses[i]); |
| 9126 | |
| 9127 | if (statuses[i] != PSA_SUCCESS) { |
| 9128 | goto exit; |
| 9129 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9130 | break; |
| 9131 | default: |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 9132 | TEST_FAIL("default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9133 | break; |
| 9134 | } |
| 9135 | break; |
| 9136 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9137 | switch (other_key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9138 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9139 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 9140 | steps[i], |
| 9141 | inputs[i]->x, |
| 9142 | inputs[i]->len), |
| 9143 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9144 | break; |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 9145 | case 1: // input key, type DERIVE |
| 9146 | case 11: // input key, type RAW |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9147 | psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE); |
| 9148 | psa_set_key_algorithm(&attributes2, alg); |
| 9149 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9150 | |
| 9151 | // other secret of type RAW_DATA passed with input_key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9152 | if (other_key_input_type == 11) { |
| 9153 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA); |
| 9154 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9155 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9156 | PSA_ASSERT(psa_import_key(&attributes2, |
| 9157 | inputs[i]->x, inputs[i]->len, |
| 9158 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9159 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9160 | TEST_EQUAL(psa_key_derivation_input_key(&operation, |
| 9161 | steps[i], |
| 9162 | keys[i]), |
| 9163 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9164 | break; |
| 9165 | case 2: // key agreement |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9166 | psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE); |
| 9167 | psa_set_key_algorithm(&attributes3, alg); |
| 9168 | psa_set_key_type(&attributes3, |
| 9169 | PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9170 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9171 | PSA_ASSERT(psa_import_key(&attributes3, |
| 9172 | inputs[i]->x, inputs[i]->len, |
| 9173 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9174 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9175 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 9176 | &operation, |
| 9177 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 9178 | keys[i], key_agreement_peer_key->x, |
| 9179 | key_agreement_peer_key->len), statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9180 | break; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9181 | default: |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 9182 | TEST_FAIL("default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9183 | break; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9184 | } |
| 9185 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9186 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9187 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9188 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 9189 | break; |
| 9190 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9191 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 9192 | &operation, steps[i], |
| 9193 | inputs[i]->x, inputs[i]->len), statuses[i]); |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 9194 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9195 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 9196 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9197 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 9198 | break; |
| 9199 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 9200 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 9201 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9202 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 9203 | ¤t_capacity)); |
| 9204 | TEST_EQUAL(current_capacity, requested_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9205 | expected_capacity = requested_capacity; |
| 9206 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9207 | if (derive_type == 1) { // output key |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 9208 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; |
| 9209 | |
| 9210 | /* For output key derivation secret must be provided using |
| 9211 | input key, otherwise operation is not permitted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9212 | if (key_input_type == 1) { |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 9213 | expected_status = PSA_SUCCESS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9214 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9215 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9216 | psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT); |
| 9217 | psa_set_key_algorithm(&attributes4, alg); |
| 9218 | psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE); |
| 9219 | psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9221 | TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation, |
| 9222 | &derived_key), expected_status); |
| 9223 | } else { // output bytes |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9224 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9225 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9226 | /* Read some bytes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9227 | status = psa_key_derivation_output_bytes(&operation, |
| 9228 | output_buffer, output_sizes[i]); |
| 9229 | if (expected_capacity == 0 && output_sizes[i] == 0) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9230 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9231 | TEST_ASSERT(status == PSA_SUCCESS || |
| 9232 | status == PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9233 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9234 | } else if (expected_capacity == 0 || |
| 9235 | output_sizes[i] > expected_capacity) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9236 | /* Capacity exceeded. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9237 | TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9238 | expected_capacity = 0; |
| 9239 | continue; |
| 9240 | } |
| 9241 | /* Success. Check the read data. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9242 | PSA_ASSERT(status); |
| 9243 | if (output_sizes[i] != 0) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9244 | TEST_MEMORY_COMPARE(output_buffer, output_sizes[i], |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9245 | expected_outputs[i], output_sizes[i]); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9246 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9247 | /* Check the operation status. */ |
| 9248 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9249 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 9250 | ¤t_capacity)); |
| 9251 | TEST_EQUAL(expected_capacity, current_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9252 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9253 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9254 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9255 | |
| 9256 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9257 | mbedtls_free(output_buffer); |
| 9258 | psa_key_derivation_abort(&operation); |
| 9259 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 9260 | psa_destroy_key(keys[i]); |
| 9261 | } |
| 9262 | psa_destroy_key(derived_key); |
| 9263 | PSA_DONE(); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9264 | } |
| 9265 | /* END_CASE */ |
| 9266 | |
| 9267 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9268 | void derive_full(int alg_arg, |
| 9269 | data_t *key_data, |
| 9270 | data_t *input1, |
| 9271 | data_t *input2, |
| 9272 | int requested_capacity_arg) |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9273 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9274 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9275 | psa_algorithm_t alg = alg_arg; |
| 9276 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9277 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Kusumit Ghoderao | 9ffd397 | 2023-12-01 16:40:13 +0530 | [diff] [blame] | 9278 | unsigned char output_buffer[32]; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9279 | size_t expected_capacity = requested_capacity; |
| 9280 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9281 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9282 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9283 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9284 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9285 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9286 | psa_set_key_algorithm(&attributes, alg); |
| 9287 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9288 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9289 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 9290 | &key)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9291 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9292 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 9293 | input1->x, input1->len, |
| 9294 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9295 | requested_capacity, 0)) { |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 9296 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9297 | } |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 9298 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9299 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 9300 | ¤t_capacity)); |
| 9301 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9302 | |
| 9303 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9304 | while (current_capacity > 0) { |
| 9305 | size_t read_size = sizeof(output_buffer); |
| 9306 | if (read_size > current_capacity) { |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9307 | read_size = current_capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9308 | } |
| 9309 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9310 | output_buffer, |
| 9311 | read_size)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9312 | expected_capacity -= read_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9313 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 9314 | ¤t_capacity)); |
| 9315 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9316 | } |
| 9317 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9318 | /* Check that the operation refuses to go over capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9319 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1), |
| 9320 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9321 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9322 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9323 | |
| 9324 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9325 | psa_key_derivation_abort(&operation); |
| 9326 | psa_destroy_key(key); |
| 9327 | PSA_DONE(); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9328 | } |
| 9329 | /* END_CASE */ |
| 9330 | |
Stephan Koch | 78109f5 | 2023-04-12 14:19:36 +0200 | [diff] [blame] | 9331 | /* 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] | 9332 | void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg, |
| 9333 | int derivation_step, |
| 9334 | int capacity, int expected_capacity_status_arg, |
| 9335 | data_t *expected_output, |
| 9336 | int expected_output_status_arg) |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9337 | { |
| 9338 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 9339 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | d378504 | 2022-09-16 06:45:44 -0400 | [diff] [blame] | 9340 | 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] | 9341 | uint8_t *output_buffer = NULL; |
| 9342 | psa_status_t status; |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 9343 | psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg; |
| 9344 | psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg; |
| 9345 | 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] | 9346 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9347 | TEST_CALLOC(output_buffer, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9348 | PSA_ASSERT(psa_crypto_init()); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9349 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9350 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9351 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 9352 | expected_capacity_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9354 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 9355 | step, input->x, input->len), |
| 9356 | expected_input_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9358 | if (((psa_status_t) expected_input_status) != PSA_SUCCESS) { |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9359 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9360 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9361 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9362 | status = psa_key_derivation_output_bytes(&operation, output_buffer, |
| 9363 | expected_output->len); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9364 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9365 | TEST_EQUAL(status, expected_output_status); |
| 9366 | if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9367 | TEST_MEMORY_COMPARE(output_buffer, expected_output->len, expected_output->x, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9368 | expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9369 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9370 | |
| 9371 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9372 | mbedtls_free(output_buffer); |
| 9373 | psa_key_derivation_abort(&operation); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9374 | PSA_DONE(); |
| 9375 | } |
| 9376 | /* END_CASE */ |
| 9377 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9378 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9379 | void derive_key_exercise(int alg_arg, |
| 9380 | data_t *key_data, |
| 9381 | data_t *input1, |
| 9382 | data_t *input2, |
| 9383 | int derived_type_arg, |
| 9384 | int derived_bits_arg, |
| 9385 | int derived_usage_arg, |
| 9386 | int derived_alg_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9387 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9388 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9389 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9390 | psa_algorithm_t alg = alg_arg; |
| 9391 | psa_key_type_t derived_type = derived_type_arg; |
| 9392 | size_t derived_bits = derived_bits_arg; |
| 9393 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 9394 | psa_algorithm_t derived_alg = derived_alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9395 | size_t capacity = PSA_BITS_TO_BYTES(derived_bits); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9396 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9397 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 9398 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9399 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9400 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9401 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9402 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9403 | psa_set_key_algorithm(&attributes, alg); |
| 9404 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
| 9405 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 9406 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9407 | |
| 9408 | /* Derive a key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9409 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9410 | input1->x, input1->len, |
| 9411 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9412 | capacity, 0)) { |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9413 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9414 | } |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9415 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9416 | psa_set_key_usage_flags(&attributes, derived_usage); |
| 9417 | psa_set_key_algorithm(&attributes, derived_alg); |
| 9418 | psa_set_key_type(&attributes, derived_type); |
| 9419 | psa_set_key_bits(&attributes, derived_bits); |
| 9420 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation, |
| 9421 | &derived_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9422 | |
| 9423 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9424 | PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes)); |
| 9425 | TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type); |
| 9426 | TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9427 | |
| 9428 | /* Exercise the derived key. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 9429 | 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] | 9430 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9431 | } |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9432 | |
| 9433 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9434 | /* |
| 9435 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9436 | * thus reset them as required. |
| 9437 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9438 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9439 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9440 | psa_key_derivation_abort(&operation); |
| 9441 | psa_destroy_key(base_key); |
| 9442 | psa_destroy_key(derived_key); |
| 9443 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9444 | } |
| 9445 | /* END_CASE */ |
| 9446 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9447 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9448 | void derive_key_export(int alg_arg, |
| 9449 | data_t *key_data, |
| 9450 | data_t *input1, |
| 9451 | data_t *input2, |
| 9452 | int bytes1_arg, |
| 9453 | int bytes2_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9454 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9455 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9456 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9457 | psa_algorithm_t alg = alg_arg; |
| 9458 | size_t bytes1 = bytes1_arg; |
| 9459 | size_t bytes2 = bytes2_arg; |
| 9460 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9461 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9462 | uint8_t *output_buffer = NULL; |
| 9463 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9464 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9465 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9466 | size_t length; |
| 9467 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9468 | TEST_CALLOC(output_buffer, capacity); |
| 9469 | TEST_CALLOC(export_buffer, capacity); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9470 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9471 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9472 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9473 | psa_set_key_algorithm(&base_attributes, alg); |
| 9474 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9475 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9476 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9477 | |
| 9478 | /* Derive some material and output it. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9479 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9480 | input1->x, input1->len, |
| 9481 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9482 | capacity, 0)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9483 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9484 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9485 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9486 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9487 | output_buffer, |
| 9488 | capacity)); |
| 9489 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9490 | |
| 9491 | /* 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] | 9492 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9493 | input1->x, input1->len, |
| 9494 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9495 | capacity, 0)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9496 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9497 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9498 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9499 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9500 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9501 | psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA); |
| 9502 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1)); |
| 9503 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9504 | &derived_key)); |
| 9505 | PSA_ASSERT(psa_export_key(derived_key, |
| 9506 | export_buffer, bytes1, |
| 9507 | &length)); |
| 9508 | TEST_EQUAL(length, bytes1); |
| 9509 | PSA_ASSERT(psa_destroy_key(derived_key)); |
| 9510 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2)); |
| 9511 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9512 | &derived_key)); |
| 9513 | PSA_ASSERT(psa_export_key(derived_key, |
| 9514 | export_buffer + bytes1, bytes2, |
| 9515 | &length)); |
| 9516 | TEST_EQUAL(length, bytes2); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9517 | |
| 9518 | /* Compare the outputs from the two runs. */ |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9519 | TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9520 | export_buffer, capacity); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9521 | |
| 9522 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9523 | mbedtls_free(output_buffer); |
| 9524 | mbedtls_free(export_buffer); |
| 9525 | psa_key_derivation_abort(&operation); |
| 9526 | psa_destroy_key(base_key); |
| 9527 | psa_destroy_key(derived_key); |
| 9528 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9529 | } |
| 9530 | /* END_CASE */ |
| 9531 | |
| 9532 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9533 | void derive_key_type(int alg_arg, |
| 9534 | data_t *key_data, |
| 9535 | data_t *input1, |
| 9536 | data_t *input2, |
| 9537 | int key_type_arg, int bits_arg, |
| 9538 | data_t *expected_export) |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9539 | { |
| 9540 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9541 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9542 | const psa_algorithm_t alg = alg_arg; |
| 9543 | const psa_key_type_t key_type = key_type_arg; |
| 9544 | const size_t bits = bits_arg; |
| 9545 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9546 | const size_t export_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9547 | PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9548 | uint8_t *export_buffer = NULL; |
| 9549 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9550 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9551 | size_t export_length; |
| 9552 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9553 | TEST_CALLOC(export_buffer, export_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9554 | PSA_ASSERT(psa_crypto_init()); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9555 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9556 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9557 | psa_set_key_algorithm(&base_attributes, alg); |
| 9558 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9559 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9560 | &base_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9562 | if (mbedtls_test_psa_setup_key_derivation_wrap( |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9563 | &operation, base_key, alg, |
| 9564 | input1->x, input1->len, |
| 9565 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9566 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY, 0) == 0) { |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9567 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9568 | } |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9569 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9570 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9571 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9572 | psa_set_key_type(&derived_attributes, key_type); |
| 9573 | psa_set_key_bits(&derived_attributes, bits); |
| 9574 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9575 | &derived_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9576 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9577 | PSA_ASSERT(psa_export_key(derived_key, |
| 9578 | export_buffer, export_buffer_size, |
| 9579 | &export_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9580 | TEST_MEMORY_COMPARE(export_buffer, export_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9581 | expected_export->x, expected_export->len); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9582 | |
| 9583 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9584 | mbedtls_free(export_buffer); |
| 9585 | psa_key_derivation_abort(&operation); |
| 9586 | psa_destroy_key(base_key); |
| 9587 | psa_destroy_key(derived_key); |
| 9588 | PSA_DONE(); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9589 | } |
| 9590 | /* END_CASE */ |
| 9591 | |
| 9592 | /* BEGIN_CASE */ |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9593 | void derive_key_ext(int alg_arg, |
| 9594 | data_t *key_data, |
| 9595 | data_t *input1, |
| 9596 | data_t *input2, |
| 9597 | int key_type_arg, int bits_arg, |
Gilles Peskine | c81393b | 2024-02-14 20:51:28 +0100 | [diff] [blame] | 9598 | int flags_arg, |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 9599 | data_t *params_data, |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9600 | psa_status_t expected_status, |
| 9601 | data_t *expected_export) |
| 9602 | { |
| 9603 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9604 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9605 | const psa_algorithm_t alg = alg_arg; |
| 9606 | const psa_key_type_t key_type = key_type_arg; |
| 9607 | const size_t bits = bits_arg; |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 9608 | psa_key_production_parameters_t *params = NULL; |
| 9609 | size_t params_data_length = 0; |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9610 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9611 | const size_t export_buffer_size = |
| 9612 | PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits); |
| 9613 | uint8_t *export_buffer = NULL; |
| 9614 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9615 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9616 | size_t export_length; |
| 9617 | |
| 9618 | TEST_CALLOC(export_buffer, export_buffer_size); |
| 9619 | PSA_ASSERT(psa_crypto_init()); |
| 9620 | |
| 9621 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9622 | psa_set_key_algorithm(&base_attributes, alg); |
| 9623 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9624 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9625 | &base_key)); |
| 9626 | |
| 9627 | if (mbedtls_test_psa_setup_key_derivation_wrap( |
| 9628 | &operation, base_key, alg, |
| 9629 | input1->x, input1->len, |
| 9630 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9631 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY, 0) == 0) { |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9632 | goto exit; |
| 9633 | } |
| 9634 | |
| 9635 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9636 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9637 | psa_set_key_type(&derived_attributes, key_type); |
| 9638 | psa_set_key_bits(&derived_attributes, bits); |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 9639 | if (!setup_key_production_parameters(¶ms, ¶ms_data_length, |
| 9640 | flags_arg, params_data)) { |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9641 | goto exit; |
| 9642 | } |
| 9643 | |
| 9644 | TEST_EQUAL(psa_key_derivation_output_key_ext(&derived_attributes, &operation, |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 9645 | params, params_data_length, |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9646 | &derived_key), |
| 9647 | expected_status); |
| 9648 | |
| 9649 | if (expected_status == PSA_SUCCESS) { |
| 9650 | PSA_ASSERT(psa_export_key(derived_key, |
| 9651 | export_buffer, export_buffer_size, |
| 9652 | &export_length)); |
| 9653 | TEST_MEMORY_COMPARE(export_buffer, export_length, |
| 9654 | expected_export->x, expected_export->len); |
| 9655 | } |
| 9656 | |
| 9657 | exit: |
| 9658 | mbedtls_free(export_buffer); |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 9659 | mbedtls_free(params); |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9660 | psa_key_derivation_abort(&operation); |
| 9661 | psa_destroy_key(base_key); |
| 9662 | psa_destroy_key(derived_key); |
| 9663 | PSA_DONE(); |
| 9664 | } |
| 9665 | /* END_CASE */ |
| 9666 | |
| 9667 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9668 | void derive_key(int alg_arg, |
| 9669 | data_t *key_data, data_t *input1, data_t *input2, |
| 9670 | int type_arg, int bits_arg, |
| 9671 | int expected_status_arg, |
| 9672 | int is_large_output) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9673 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9674 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9675 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9676 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 9677 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9678 | size_t bits = bits_arg; |
| 9679 | psa_status_t expected_status = expected_status_arg; |
| 9680 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9681 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9682 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9683 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9684 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9685 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9686 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9687 | psa_set_key_algorithm(&base_attributes, alg); |
| 9688 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9689 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9690 | &base_key)); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9691 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9692 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9693 | input1->x, input1->len, |
| 9694 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9695 | SIZE_MAX, 0)) { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9696 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9697 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9698 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9699 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9700 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9701 | psa_set_key_type(&derived_attributes, type); |
| 9702 | psa_set_key_bits(&derived_attributes, bits); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9703 | |
| 9704 | psa_status_t status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9705 | psa_key_derivation_output_key(&derived_attributes, |
| 9706 | &operation, |
| 9707 | &derived_key); |
| 9708 | if (is_large_output > 0) { |
| 9709 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9710 | } |
| 9711 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9712 | |
| 9713 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9714 | psa_key_derivation_abort(&operation); |
| 9715 | psa_destroy_key(base_key); |
| 9716 | psa_destroy_key(derived_key); |
| 9717 | PSA_DONE(); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9718 | } |
| 9719 | /* END_CASE */ |
| 9720 | |
| 9721 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9722 | void key_agreement_setup(int alg_arg, |
| 9723 | int our_key_type_arg, int our_key_alg_arg, |
| 9724 | data_t *our_key_data, data_t *peer_key_data, |
| 9725 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9726 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9727 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9728 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 9729 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9730 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9731 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9732 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9733 | psa_status_t expected_status = expected_status_arg; |
| 9734 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9735 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9736 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9737 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9738 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9739 | psa_set_key_algorithm(&attributes, our_key_alg); |
| 9740 | psa_set_key_type(&attributes, our_key_type); |
| 9741 | PSA_ASSERT(psa_import_key(&attributes, |
| 9742 | our_key_data->x, our_key_data->len, |
| 9743 | &our_key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9744 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9745 | /* The tests currently include inputs that should fail at either step. |
| 9746 | * Test cases that fail at the setup step should be changed to call |
| 9747 | * key_derivation_setup instead, and this function should be renamed |
| 9748 | * to key_agreement_fail. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9749 | status = psa_key_derivation_setup(&operation, alg); |
| 9750 | if (status == PSA_SUCCESS) { |
| 9751 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 9752 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 9753 | our_key, |
| 9754 | peer_key_data->x, peer_key_data->len), |
| 9755 | expected_status); |
| 9756 | } else { |
| 9757 | TEST_ASSERT(status == expected_status); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9758 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9759 | |
| 9760 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9761 | psa_key_derivation_abort(&operation); |
| 9762 | psa_destroy_key(our_key); |
| 9763 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9764 | } |
| 9765 | /* END_CASE */ |
| 9766 | |
| 9767 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9768 | void raw_key_agreement(int alg_arg, |
| 9769 | int our_key_type_arg, data_t *our_key_data, |
| 9770 | data_t *peer_key_data, |
| 9771 | data_t *expected_output) |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9772 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9773 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9774 | psa_algorithm_t alg = alg_arg; |
| 9775 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9776 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9777 | unsigned char *output = NULL; |
| 9778 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9779 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9780 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9781 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9782 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9783 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9784 | psa_set_key_algorithm(&attributes, alg); |
| 9785 | psa_set_key_type(&attributes, our_key_type); |
| 9786 | PSA_ASSERT(psa_import_key(&attributes, |
| 9787 | our_key_data->x, our_key_data->len, |
| 9788 | &our_key)); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9789 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9790 | PSA_ASSERT(psa_get_key_attributes(our_key, &attributes)); |
| 9791 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9792 | |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9793 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9794 | TEST_LE_U(expected_output->len, |
| 9795 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits)); |
| 9796 | TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits), |
| 9797 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9798 | |
| 9799 | /* Good case with exact output size */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9800 | TEST_CALLOC(output, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9801 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9802 | peer_key_data->x, peer_key_data->len, |
| 9803 | output, expected_output->len, |
| 9804 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9805 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9806 | expected_output->x, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9807 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9808 | output = NULL; |
| 9809 | output_length = ~0; |
| 9810 | |
| 9811 | /* Larger buffer */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9812 | TEST_CALLOC(output, expected_output->len + 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9813 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9814 | peer_key_data->x, peer_key_data->len, |
| 9815 | output, expected_output->len + 1, |
| 9816 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9817 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9818 | expected_output->x, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9819 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9820 | output = NULL; |
| 9821 | output_length = ~0; |
| 9822 | |
| 9823 | /* Buffer too small */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9824 | TEST_CALLOC(output, expected_output->len - 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9825 | TEST_EQUAL(psa_raw_key_agreement(alg, our_key, |
| 9826 | peer_key_data->x, peer_key_data->len, |
| 9827 | output, expected_output->len - 1, |
| 9828 | &output_length), |
| 9829 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9830 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9831 | TEST_LE_U(output_length, expected_output->len - 1); |
| 9832 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9833 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9834 | |
| 9835 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9836 | mbedtls_free(output); |
| 9837 | psa_destroy_key(our_key); |
| 9838 | PSA_DONE(); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9839 | } |
| 9840 | /* END_CASE */ |
| 9841 | |
| 9842 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9843 | void key_agreement_capacity(int alg_arg, |
| 9844 | int our_key_type_arg, data_t *our_key_data, |
| 9845 | data_t *peer_key_data, |
| 9846 | int expected_capacity_arg) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9847 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9848 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9849 | psa_algorithm_t alg = alg_arg; |
| 9850 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9851 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9852 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9853 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9854 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9855 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9856 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9857 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9858 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9859 | psa_set_key_algorithm(&attributes, alg); |
| 9860 | psa_set_key_type(&attributes, our_key_type); |
| 9861 | PSA_ASSERT(psa_import_key(&attributes, |
| 9862 | our_key_data->x, our_key_data->len, |
| 9863 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9864 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9865 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9866 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9867 | &operation, |
| 9868 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9869 | peer_key_data->x, peer_key_data->len)); |
| 9870 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9871 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9872 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9873 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9874 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9875 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9876 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 9877 | /* Test the advertised capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9878 | PSA_ASSERT(psa_key_derivation_get_capacity( |
| 9879 | &operation, &actual_capacity)); |
| 9880 | TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9881 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9882 | /* Test the actual capacity by reading the output. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9883 | while (actual_capacity > sizeof(output)) { |
| 9884 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9885 | output, sizeof(output))); |
| 9886 | actual_capacity -= sizeof(output); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9887 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9888 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9889 | output, actual_capacity)); |
| 9890 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1), |
| 9891 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9892 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9893 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9894 | psa_key_derivation_abort(&operation); |
| 9895 | psa_destroy_key(our_key); |
| 9896 | PSA_DONE(); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9897 | } |
| 9898 | /* END_CASE */ |
| 9899 | |
Valerio Setti | ad81967 | 2023-12-29 12:14:41 +0100 | [diff] [blame] | 9900 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */ |
| 9901 | 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] | 9902 | { |
| 9903 | mbedtls_ecp_group_id grp_id = grp_id_arg; |
Valerio Setti | ad81967 | 2023-12-29 12:14:41 +0100 | [diff] [blame] | 9904 | psa_ecc_family_t ecc_family = psa_family_arg; |
| 9905 | size_t bits = bits_arg; |
| 9906 | size_t bits_tmp; |
Valerio Setti | bf999cb | 2023-12-28 17:48:13 +0100 | [diff] [blame] | 9907 | |
Valerio Setti | ad81967 | 2023-12-29 12:14:41 +0100 | [diff] [blame] | 9908 | TEST_EQUAL(ecc_family, mbedtls_ecc_group_to_psa(grp_id, &bits_tmp)); |
| 9909 | TEST_EQUAL(bits, bits_tmp); |
Valerio Setti | ac73952 | 2024-01-04 10:22:01 +0100 | [diff] [blame] | 9910 | TEST_EQUAL(grp_id, mbedtls_ecc_group_from_psa(ecc_family, bits)); |
Valerio Setti | bf999cb | 2023-12-28 17:48:13 +0100 | [diff] [blame] | 9911 | } |
| 9912 | /* END_CASE */ |
| 9913 | |
Valerio Setti | ac73952 | 2024-01-04 10:22:01 +0100 | [diff] [blame] | 9914 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */ |
| 9915 | void ecc_conversion_functions_fail() |
| 9916 | { |
| 9917 | size_t bits; |
| 9918 | |
Valerio Setti | db6e029 | 2024-01-05 10:15:45 +0100 | [diff] [blame] | 9919 | /* Invalid legacy curve identifiers. */ |
| 9920 | TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_MAX, &bits)); |
| 9921 | TEST_EQUAL(0, bits); |
Valerio Setti | ac73952 | 2024-01-04 10:22:01 +0100 | [diff] [blame] | 9922 | TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_NONE, &bits)); |
| 9923 | TEST_EQUAL(0, bits); |
| 9924 | |
| 9925 | /* Invalid PSA EC family. */ |
| 9926 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(0, 192)); |
| 9927 | /* Invalid bit-size for a valid EC family. */ |
| 9928 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_SECP_R1, 512)); |
| 9929 | |
| 9930 | /* Twisted-Edward curves are not supported yet. */ |
| 9931 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, |
| 9932 | mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 255)); |
| 9933 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, |
| 9934 | mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 448)); |
| 9935 | } |
| 9936 | /* END_CASE */ |
| 9937 | |
| 9938 | |
Valerio Setti | bf999cb | 2023-12-28 17:48:13 +0100 | [diff] [blame] | 9939 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9940 | void key_agreement_output(int alg_arg, |
| 9941 | int our_key_type_arg, data_t *our_key_data, |
| 9942 | data_t *peer_key_data, |
| 9943 | data_t *expected_output1, data_t *expected_output2) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9944 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9945 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9946 | psa_algorithm_t alg = alg_arg; |
| 9947 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9948 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9949 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9950 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9951 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9952 | TEST_CALLOC(actual_output, MAX(expected_output1->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9953 | expected_output2->len)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9954 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9955 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9956 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9957 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9958 | psa_set_key_algorithm(&attributes, alg); |
| 9959 | psa_set_key_type(&attributes, our_key_type); |
| 9960 | PSA_ASSERT(psa_import_key(&attributes, |
| 9961 | our_key_data->x, our_key_data->len, |
| 9962 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9963 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9964 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9965 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9966 | &operation, |
| 9967 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9968 | peer_key_data->x, peer_key_data->len)); |
| 9969 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9970 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9971 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9972 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9973 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9974 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9975 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9976 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9977 | actual_output, |
| 9978 | expected_output1->len)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9979 | TEST_MEMORY_COMPARE(actual_output, expected_output1->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9980 | expected_output1->x, expected_output1->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9981 | if (expected_output2->len != 0) { |
| 9982 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9983 | actual_output, |
| 9984 | expected_output2->len)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9985 | TEST_MEMORY_COMPARE(actual_output, expected_output2->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9986 | expected_output2->x, expected_output2->len); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9987 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9988 | |
| 9989 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9990 | psa_key_derivation_abort(&operation); |
| 9991 | psa_destroy_key(our_key); |
| 9992 | PSA_DONE(); |
| 9993 | mbedtls_free(actual_output); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9994 | } |
| 9995 | /* END_CASE */ |
| 9996 | |
| 9997 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9998 | void generate_random(int bytes_arg) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9999 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10000 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 10001 | unsigned char *output = NULL; |
| 10002 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10003 | size_t i; |
| 10004 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 10005 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10006 | TEST_ASSERT(bytes_arg >= 0); |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 10007 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 10008 | TEST_CALLOC(output, bytes); |
| 10009 | TEST_CALLOC(changed, bytes); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 10010 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10011 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 10012 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10013 | /* Run several times, to ensure that every output byte will be |
| 10014 | * nonzero at least once with overwhelming probability |
| 10015 | * (2^(-8*number_of_runs)). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10016 | for (run = 0; run < 10; run++) { |
| 10017 | if (bytes != 0) { |
| 10018 | memset(output, 0, bytes); |
| 10019 | } |
| 10020 | PSA_ASSERT(psa_generate_random(output, bytes)); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10021 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10022 | for (i = 0; i < bytes; i++) { |
| 10023 | if (output[i] != 0) { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10024 | ++changed[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10025 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10026 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 10027 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10028 | |
| 10029 | /* Check that every byte was changed to nonzero at least once. This |
| 10030 | * validates that psa_generate_random is overwriting every byte of |
| 10031 | * the output buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10032 | for (i = 0; i < bytes; i++) { |
| 10033 | TEST_ASSERT(changed[i] != 0); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10034 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 10035 | |
| 10036 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10037 | PSA_DONE(); |
| 10038 | mbedtls_free(output); |
| 10039 | mbedtls_free(changed); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 10040 | } |
| 10041 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10042 | |
Ryan | 3a1b786 | 2024-03-01 17:24:04 +0000 | [diff] [blame] | 10043 | #if defined MBEDTLS_THREADING_PTHREAD |
| 10044 | |
| 10045 | /* BEGIN_CASE depends_on:MBEDTLS_THREADING_PTHREAD */ |
| 10046 | void concurrently_generate_keys(int type_arg, |
| 10047 | int bits_arg, |
| 10048 | int usage_arg, |
| 10049 | int alg_arg, |
| 10050 | int expected_status_arg, |
| 10051 | int is_large_key_arg, |
| 10052 | int arg_thread_count, |
| 10053 | int reps_arg) |
| 10054 | { |
| 10055 | size_t thread_count = (size_t) arg_thread_count; |
| 10056 | mbedtls_test_thread_t *threads = NULL; |
| 10057 | generate_key_context gkc; |
| 10058 | gkc.type = type_arg; |
| 10059 | gkc.usage = usage_arg; |
| 10060 | gkc.bits = bits_arg; |
| 10061 | gkc.alg = alg_arg; |
| 10062 | gkc.expected_status = expected_status_arg; |
| 10063 | gkc.is_large_key = is_large_key_arg; |
| 10064 | gkc.reps = reps_arg; |
| 10065 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10066 | |
| 10067 | PSA_ASSERT(psa_crypto_init()); |
| 10068 | |
| 10069 | psa_set_key_usage_flags(&attributes, usage_arg); |
| 10070 | psa_set_key_algorithm(&attributes, alg_arg); |
| 10071 | psa_set_key_type(&attributes, type_arg); |
| 10072 | psa_set_key_bits(&attributes, bits_arg); |
| 10073 | gkc.attributes = &attributes; |
| 10074 | |
| 10075 | TEST_CALLOC(threads, sizeof(mbedtls_test_thread_t) * thread_count); |
| 10076 | |
| 10077 | /* Split threads to generate key then destroy key. */ |
| 10078 | for (size_t i = 0; i < thread_count; i++) { |
| 10079 | TEST_EQUAL( |
| 10080 | mbedtls_test_thread_create(&threads[i], thread_generate_key, |
| 10081 | (void *) &gkc), 0); |
| 10082 | } |
| 10083 | |
| 10084 | /* Join threads. */ |
| 10085 | for (size_t i = 0; i < thread_count; i++) { |
| 10086 | TEST_EQUAL(mbedtls_test_thread_join(&threads[i]), 0); |
| 10087 | } |
| 10088 | |
| 10089 | exit: |
| 10090 | mbedtls_free(threads); |
| 10091 | PSA_DONE(); |
| 10092 | } |
| 10093 | /* END_CASE */ |
| 10094 | #endif |
| 10095 | |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10096 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10097 | void generate_key(int type_arg, |
| 10098 | int bits_arg, |
| 10099 | int usage_arg, |
| 10100 | int alg_arg, |
| 10101 | int expected_status_arg, |
| 10102 | int is_large_key) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10103 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 10104 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10105 | psa_key_type_t type = type_arg; |
| 10106 | psa_key_usage_t usage = usage_arg; |
| 10107 | size_t bits = bits_arg; |
| 10108 | psa_algorithm_t alg = alg_arg; |
| 10109 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 10110 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 10111 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10112 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10113 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10114 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10115 | psa_set_key_usage_flags(&attributes, usage); |
| 10116 | psa_set_key_algorithm(&attributes, alg); |
| 10117 | psa_set_key_type(&attributes, type); |
| 10118 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10119 | |
| 10120 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10121 | psa_status_t status = psa_generate_key(&attributes, &key); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 10122 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10123 | if (is_large_key > 0) { |
| 10124 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 10125 | } |
| 10126 | TEST_EQUAL(status, expected_status); |
| 10127 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 10128 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10129 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10130 | |
| 10131 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10132 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 10133 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 10134 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10135 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 10136 | /* Do something with the key according to its type and permitted usage. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 10137 | if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 10138 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10139 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10140 | |
| 10141 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 10142 | /* |
| 10143 | * Key attributes may have been returned by psa_get_key_attributes() |
| 10144 | * thus reset them as required. |
| 10145 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10146 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 10147 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10148 | psa_destroy_key(key); |
| 10149 | PSA_DONE(); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10150 | } |
| 10151 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 10152 | |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10153 | /* BEGIN_CASE */ |
| 10154 | void generate_key_ext(int type_arg, |
| 10155 | int bits_arg, |
| 10156 | int usage_arg, |
| 10157 | int alg_arg, |
Gilles Peskine | c81393b | 2024-02-14 20:51:28 +0100 | [diff] [blame] | 10158 | int flags_arg, |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10159 | data_t *params_data, |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10160 | int expected_status_arg) |
| 10161 | { |
| 10162 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10163 | psa_key_type_t type = type_arg; |
| 10164 | psa_key_usage_t usage = usage_arg; |
| 10165 | size_t bits = bits_arg; |
| 10166 | psa_algorithm_t alg = alg_arg; |
| 10167 | psa_status_t expected_status = expected_status_arg; |
| 10168 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10169 | psa_key_production_parameters_t *params = NULL; |
| 10170 | size_t params_data_length = 0; |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10171 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10172 | |
| 10173 | PSA_ASSERT(psa_crypto_init()); |
| 10174 | |
| 10175 | psa_set_key_usage_flags(&attributes, usage); |
| 10176 | psa_set_key_algorithm(&attributes, alg); |
| 10177 | psa_set_key_type(&attributes, type); |
| 10178 | psa_set_key_bits(&attributes, bits); |
| 10179 | |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10180 | if (!setup_key_production_parameters(¶ms, ¶ms_data_length, |
| 10181 | flags_arg, params_data)) { |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10182 | goto exit; |
| 10183 | } |
| 10184 | |
| 10185 | /* Generate a key */ |
| 10186 | psa_status_t status = psa_generate_key_ext(&attributes, |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10187 | params, params_data_length, |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10188 | &key); |
| 10189 | |
| 10190 | TEST_EQUAL(status, expected_status); |
| 10191 | if (expected_status != PSA_SUCCESS) { |
| 10192 | goto exit; |
| 10193 | } |
| 10194 | |
| 10195 | /* Test the key information */ |
| 10196 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 10197 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 10198 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
| 10199 | |
Gilles Peskine | 7a18f96 | 2024-02-12 16:48:11 +0100 | [diff] [blame] | 10200 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) |
| 10201 | if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10202 | TEST_ASSERT(rsa_test_e(key, bits, params_data)); |
Gilles Peskine | 7a18f96 | 2024-02-12 16:48:11 +0100 | [diff] [blame] | 10203 | } |
| 10204 | #endif |
| 10205 | |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10206 | /* Do something with the key according to its type and permitted usage. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 10207 | if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) { |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10208 | goto exit; |
| 10209 | } |
| 10210 | |
| 10211 | exit: |
| 10212 | /* |
| 10213 | * Key attributes may have been returned by psa_get_key_attributes() |
| 10214 | * thus reset them as required. |
| 10215 | */ |
| 10216 | psa_reset_key_attributes(&got_attributes); |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10217 | mbedtls_free(params); |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10218 | psa_destroy_key(key); |
| 10219 | PSA_DONE(); |
| 10220 | } |
| 10221 | /* END_CASE */ |
| 10222 | |
| 10223 | /* BEGIN_CASE */ |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10224 | void key_production_parameters_init() |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10225 | { |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10226 | psa_key_production_parameters_t init = PSA_KEY_PRODUCTION_PARAMETERS_INIT; |
| 10227 | psa_key_production_parameters_t zero; |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10228 | memset(&zero, 0, sizeof(zero)); |
| 10229 | |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10230 | TEST_EQUAL(init.flags, 0); |
| 10231 | TEST_EQUAL(zero.flags, 0); |
| 10232 | } |
| 10233 | /* END_CASE */ |
| 10234 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10235 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10236 | void persistent_key_load_key_from_storage(data_t *data, |
| 10237 | int type_arg, int bits_arg, |
| 10238 | int usage_flags_arg, int alg_arg, |
| 10239 | int generation_method) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10240 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10241 | 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] | 10242 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 10243 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10244 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10245 | psa_key_type_t type = type_arg; |
| 10246 | size_t bits = bits_arg; |
| 10247 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 10248 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 10249 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10250 | unsigned char *first_export = NULL; |
| 10251 | unsigned char *second_export = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10252 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits); |
Sergey | bef1f63 | 2023-03-06 15:25:06 -0700 | [diff] [blame] | 10253 | size_t first_exported_length = 0; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10254 | size_t second_exported_length; |
| 10255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10256 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 10257 | TEST_CALLOC(first_export, export_size); |
| 10258 | TEST_CALLOC(second_export, export_size); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10259 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10260 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10261 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10263 | psa_set_key_id(&attributes, key_id); |
| 10264 | psa_set_key_usage_flags(&attributes, usage_flags); |
| 10265 | psa_set_key_algorithm(&attributes, alg); |
| 10266 | psa_set_key_type(&attributes, type); |
| 10267 | psa_set_key_bits(&attributes, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10268 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10269 | switch (generation_method) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10270 | case IMPORT_KEY: |
| 10271 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10272 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, |
| 10273 | &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10274 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10275 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10276 | case GENERATE_KEY: |
| 10277 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10278 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10279 | break; |
| 10280 | |
| 10281 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 10282 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10283 | { |
| 10284 | /* Create base key */ |
| 10285 | psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256); |
| 10286 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10287 | psa_set_key_usage_flags(&base_attributes, |
| 10288 | PSA_KEY_USAGE_DERIVE); |
| 10289 | psa_set_key_algorithm(&base_attributes, derive_alg); |
| 10290 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 10291 | PSA_ASSERT(psa_import_key(&base_attributes, |
| 10292 | data->x, data->len, |
| 10293 | &base_key)); |
| 10294 | /* Derive a key. */ |
| 10295 | PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg)); |
| 10296 | PSA_ASSERT(psa_key_derivation_input_key( |
| 10297 | &operation, |
| 10298 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key)); |
| 10299 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 10300 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
| 10301 | NULL, 0)); |
| 10302 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, |
| 10303 | &operation, |
| 10304 | &key)); |
| 10305 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
| 10306 | PSA_ASSERT(psa_destroy_key(base_key)); |
| 10307 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10308 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 10309 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10310 | TEST_ASSUME(!"KDF not supported in this configuration"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 10311 | #endif |
| 10312 | break; |
| 10313 | |
| 10314 | default: |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 10315 | TEST_FAIL("generation_method not implemented in test"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 10316 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10317 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10318 | psa_reset_key_attributes(&attributes); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10319 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10320 | /* Export the key if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10321 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 10322 | PSA_ASSERT(psa_export_key(key, |
| 10323 | first_export, export_size, |
| 10324 | &first_exported_length)); |
| 10325 | if (generation_method == IMPORT_KEY) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 10326 | TEST_MEMORY_COMPARE(data->x, data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 10327 | first_export, first_exported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10328 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10329 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10330 | |
| 10331 | /* Shutdown and restart */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10332 | PSA_ASSERT(psa_purge_key(key)); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 10333 | PSA_DONE(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10334 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10335 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10336 | /* Check key slot still contains key data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10337 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 10338 | TEST_ASSERT(mbedtls_svc_key_id_equal( |
| 10339 | psa_get_key_id(&attributes), key_id)); |
| 10340 | TEST_EQUAL(psa_get_key_lifetime(&attributes), |
| 10341 | PSA_KEY_LIFETIME_PERSISTENT); |
| 10342 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 10343 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 10344 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 10345 | mbedtls_test_update_key_usage_flags(usage_flags)); |
| 10346 | TEST_EQUAL(psa_get_key_algorithm(&attributes), alg); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10347 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10348 | /* Export the key again if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10349 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 10350 | PSA_ASSERT(psa_export_key(key, |
| 10351 | second_export, export_size, |
| 10352 | &second_exported_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 10353 | TEST_MEMORY_COMPARE(first_export, first_exported_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 10354 | second_export, second_exported_length); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10355 | } |
| 10356 | |
| 10357 | /* Do something with the key according to its type and permitted usage. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 10358 | if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg, 0)) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10359 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10360 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10361 | |
| 10362 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 10363 | /* |
| 10364 | * Key attributes may have been returned by psa_get_key_attributes() |
| 10365 | * thus reset them as required. |
| 10366 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10367 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 10368 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10369 | mbedtls_free(first_export); |
| 10370 | mbedtls_free(second_export); |
| 10371 | psa_key_derivation_abort(&operation); |
| 10372 | psa_destroy_key(base_key); |
| 10373 | psa_destroy_key(key); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 10374 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10375 | } |
| 10376 | /* END_CASE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10377 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 10378 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10379 | void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg, |
| 10380 | int primitive_arg, int hash_arg, int role_arg, |
| 10381 | int test_input, data_t *pw_data, |
| 10382 | int inj_err_type_arg, |
| 10383 | int expected_error_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10384 | { |
| 10385 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10386 | psa_pake_operation_t operation = psa_pake_operation_init(); |
| 10387 | psa_algorithm_t alg = alg_arg; |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 10388 | psa_pake_primitive_t primitive = primitive_arg; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 10389 | psa_key_type_t key_type_pw = key_type_pw_arg; |
| 10390 | psa_key_usage_t key_usage_pw = key_usage_pw_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10391 | psa_algorithm_t hash_alg = hash_arg; |
| 10392 | psa_pake_role_t role = role_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10393 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10394 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10395 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
| 10396 | psa_status_t expected_error = expected_error_arg; |
| 10397 | psa_status_t status; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10398 | unsigned char *output_buffer = NULL; |
| 10399 | size_t output_len = 0; |
| 10400 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10401 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10402 | |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 10403 | size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10404 | PSA_PAKE_STEP_KEY_SHARE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 10405 | TEST_CALLOC(output_buffer, buf_size); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10406 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10407 | if (pw_data->len > 0) { |
| 10408 | psa_set_key_usage_flags(&attributes, key_usage_pw); |
| 10409 | psa_set_key_algorithm(&attributes, alg); |
| 10410 | psa_set_key_type(&attributes, key_type_pw); |
| 10411 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10412 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10413 | } |
| 10414 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10415 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10416 | psa_pake_cs_set_primitive(&cipher_suite, primitive); |
| 10417 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10418 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10419 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 10420 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10421 | if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) { |
| 10422 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 10423 | expected_error); |
| 10424 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10425 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 10426 | expected_error); |
| 10427 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10428 | TEST_EQUAL(psa_pake_set_password_key(&operation, key), |
| 10429 | expected_error); |
| 10430 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10431 | TEST_EQUAL(psa_pake_set_role(&operation, role), |
| 10432 | expected_error); |
| 10433 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10434 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10435 | NULL, 0, NULL), |
| 10436 | expected_error); |
| 10437 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10438 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0), |
| 10439 | expected_error); |
| 10440 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10441 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10442 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10444 | status = psa_pake_setup(&operation, &cipher_suite); |
| 10445 | if (status != PSA_SUCCESS) { |
| 10446 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10447 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10448 | } |
| 10449 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10450 | if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) { |
| 10451 | TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite), |
| 10452 | expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10453 | goto exit; |
| 10454 | } |
| 10455 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10456 | status = psa_pake_set_role(&operation, role); |
| 10457 | if (status != PSA_SUCCESS) { |
| 10458 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10459 | goto exit; |
| 10460 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10461 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10462 | if (pw_data->len > 0) { |
| 10463 | status = psa_pake_set_password_key(&operation, key); |
| 10464 | if (status != PSA_SUCCESS) { |
| 10465 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10466 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10467 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10468 | } |
| 10469 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10470 | if (inj_err_type == INJECT_ERR_INVALID_USER) { |
| 10471 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 10472 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10473 | goto exit; |
| 10474 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 10475 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10476 | if (inj_err_type == INJECT_ERR_INVALID_PEER) { |
| 10477 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 10478 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10479 | goto exit; |
| 10480 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 10481 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10482 | if (inj_err_type == INJECT_ERR_SET_USER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10483 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10484 | TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4), |
| 10485 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10486 | goto exit; |
| 10487 | } |
| 10488 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10489 | if (inj_err_type == INJECT_ERR_SET_PEER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10490 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10491 | TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4), |
| 10492 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10493 | goto exit; |
| 10494 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 10495 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10496 | const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10497 | PSA_PAKE_STEP_KEY_SHARE); |
| 10498 | const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10499 | PSA_PAKE_STEP_ZK_PUBLIC); |
| 10500 | const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10501 | PSA_PAKE_STEP_ZK_PROOF); |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 10502 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10503 | if (test_input) { |
| 10504 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 10505 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0), |
| 10506 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10507 | goto exit; |
| 10508 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10509 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10510 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 10511 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 10512 | output_buffer, size_zk_proof), |
| 10513 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10514 | goto exit; |
| 10515 | } |
| 10516 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10517 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 10518 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10519 | output_buffer, size_zk_proof), |
| 10520 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10521 | goto exit; |
| 10522 | } |
| 10523 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10524 | status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10525 | output_buffer, size_key_share); |
| 10526 | if (status != PSA_SUCCESS) { |
| 10527 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10528 | goto exit; |
| 10529 | } |
| 10530 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10531 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 10532 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10533 | output_buffer, size_zk_public + 1), |
| 10534 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10535 | goto exit; |
| 10536 | } |
| 10537 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10538 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10539 | // 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] | 10540 | psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10541 | output_buffer, size_zk_public + 1); |
| 10542 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10543 | output_buffer, size_zk_public), |
| 10544 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10545 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10546 | } |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10547 | } else { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10548 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 10549 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10550 | NULL, 0, NULL), |
| 10551 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10552 | goto exit; |
| 10553 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10554 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10555 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 10556 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 10557 | output_buffer, buf_size, &output_len), |
| 10558 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10559 | goto exit; |
| 10560 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10562 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 10563 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10564 | output_buffer, buf_size, &output_len), |
| 10565 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10566 | goto exit; |
| 10567 | } |
| 10568 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10569 | status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10570 | output_buffer, buf_size, &output_len); |
| 10571 | if (status != PSA_SUCCESS) { |
| 10572 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10573 | goto exit; |
| 10574 | } |
| 10575 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10576 | TEST_ASSERT(output_len > 0); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10577 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10578 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 10579 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10580 | output_buffer, size_zk_public - 1, &output_len), |
| 10581 | PSA_ERROR_BUFFER_TOO_SMALL); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10582 | goto exit; |
| 10583 | } |
| 10584 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10585 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10586 | // 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] | 10587 | psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10588 | output_buffer, size_zk_public - 1, &output_len); |
| 10589 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10590 | output_buffer, buf_size, &output_len), |
| 10591 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10592 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10593 | } |
| 10594 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10595 | |
| 10596 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10597 | PSA_ASSERT(psa_destroy_key(key)); |
| 10598 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10599 | mbedtls_free(output_buffer); |
| 10600 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10601 | } |
| 10602 | /* END_CASE */ |
| 10603 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 10604 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10605 | void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg, |
| 10606 | int client_input_first, int inject_error, |
| 10607 | data_t *pw_data) |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10608 | { |
| 10609 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10610 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10611 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10612 | psa_algorithm_t alg = alg_arg; |
| 10613 | psa_algorithm_t hash_alg = hash_arg; |
| 10614 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10615 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10616 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10617 | PSA_INIT(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10618 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10619 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10620 | psa_set_key_algorithm(&attributes, alg); |
| 10621 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10622 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10623 | &key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10624 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10625 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10626 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10627 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10628 | |
| 10629 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10630 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10631 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10632 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10633 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10634 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10635 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10636 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10637 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10638 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10639 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10640 | client_input_first, 1, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10642 | if (inject_error == 1 || inject_error == 2) { |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10643 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10644 | } |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10645 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10646 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10647 | client_input_first, 2, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10648 | |
| 10649 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10650 | psa_destroy_key(key); |
| 10651 | psa_pake_abort(&server); |
| 10652 | psa_pake_abort(&client); |
| 10653 | PSA_DONE(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10654 | } |
| 10655 | /* END_CASE */ |
| 10656 | |
| 10657 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10658 | void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg, |
| 10659 | int derive_alg_arg, data_t *pw_data, |
| 10660 | int client_input_first, int inj_err_type_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10661 | { |
| 10662 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10663 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10664 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10665 | psa_algorithm_t alg = alg_arg; |
| 10666 | psa_algorithm_t hash_alg = hash_arg; |
| 10667 | psa_algorithm_t derive_alg = derive_alg_arg; |
| 10668 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10669 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10670 | psa_key_derivation_operation_t server_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10671 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10672 | psa_key_derivation_operation_t client_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10673 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10674 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10676 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10677 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10678 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10679 | psa_set_key_algorithm(&attributes, alg); |
| 10680 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10681 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10682 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10683 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10684 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10685 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10686 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10687 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10688 | /* Get shared key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10689 | PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg)); |
| 10690 | PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10691 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10692 | if (PSA_ALG_IS_TLS12_PRF(derive_alg) || |
| 10693 | PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) { |
| 10694 | PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive, |
| 10695 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10696 | (const uint8_t *) "", 0)); |
| 10697 | PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive, |
| 10698 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10699 | (const uint8_t *) "", 0)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10700 | } |
| 10701 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10702 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10703 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10704 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10705 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10706 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10707 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10708 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10709 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10710 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10711 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) { |
| 10712 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10713 | PSA_ERROR_BAD_STATE); |
| 10714 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10715 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10716 | goto exit; |
| 10717 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10718 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10719 | /* First round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10720 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10721 | client_input_first, 1, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10722 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10723 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) { |
| 10724 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10725 | PSA_ERROR_BAD_STATE); |
| 10726 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10727 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10728 | goto exit; |
| 10729 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10730 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10731 | /* Second round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10732 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10733 | client_input_first, 2, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10734 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10735 | PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive)); |
| 10736 | PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10737 | |
| 10738 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10739 | psa_key_derivation_abort(&server_derive); |
| 10740 | psa_key_derivation_abort(&client_derive); |
| 10741 | psa_destroy_key(key); |
| 10742 | psa_pake_abort(&server); |
| 10743 | psa_pake_abort(&client); |
| 10744 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10745 | } |
| 10746 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10747 | |
| 10748 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10749 | void ecjpake_size_macros() |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10750 | { |
| 10751 | const psa_algorithm_t alg = PSA_ALG_JPAKE; |
| 10752 | const size_t bits = 256; |
| 10753 | const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10754 | 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] | 10755 | 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] | 10756 | PSA_ECC_FAMILY_SECP_R1); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10757 | |
| 10758 | // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types |
| 10759 | /* 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] | 10760 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10761 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
| 10762 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10763 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10764 | /* The output for ZK_PROOF is the same bitsize as the curve */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10765 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10766 | PSA_BITS_TO_BYTES(bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10767 | |
| 10768 | /* Input sizes are the same as output sizes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10769 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10770 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE)); |
| 10771 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10772 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC)); |
| 10773 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10774 | 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] | 10775 | |
| 10776 | /* These inequalities will always hold even when other PAKEs are added */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10777 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10778 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10779 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10780 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10781 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10782 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10783 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10784 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10785 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10786 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10787 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10788 | PSA_PAKE_INPUT_MAX_SIZE); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10789 | } |
| 10790 | /* END_CASE */ |