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) |
| 1341 | typedef struct generate_key_context { |
| 1342 | psa_key_type_t type; |
| 1343 | psa_key_usage_t usage; |
| 1344 | size_t bits; |
| 1345 | psa_algorithm_t alg; |
| 1346 | psa_status_t expected_status; |
| 1347 | psa_key_attributes_t *attributes; |
| 1348 | int is_large_key; |
| 1349 | int reps; |
| 1350 | } |
| 1351 | generate_key_context; |
| 1352 | void *thread_generate_key(void *ctx) |
| 1353 | { |
| 1354 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1355 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1356 | generate_key_context *gkc = (struct generate_key_context *) ctx; |
| 1357 | |
| 1358 | /* If there are race conditions, it is likely the case that they do not |
| 1359 | * arise every time the code runs. We repeat the code to increase the |
| 1360 | * chance that any race conditions will be hit. */ |
| 1361 | for (int n = 0; n < gkc->reps; n++) { |
| 1362 | /* Generate a key */ |
| 1363 | psa_status_t status = psa_generate_key(gkc->attributes, &key); |
| 1364 | |
| 1365 | if (gkc->is_large_key > 0) { |
| 1366 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 1367 | } |
| 1368 | |
| 1369 | TEST_EQUAL(status, gkc->expected_status); |
| 1370 | if (gkc->expected_status != PSA_SUCCESS) { |
| 1371 | PSA_ASSERT(psa_destroy_key(key)); |
| 1372 | goto exit; |
| 1373 | } |
| 1374 | |
| 1375 | /* Test the key information */ |
| 1376 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1377 | TEST_EQUAL(psa_get_key_type(&got_attributes), gkc->type); |
| 1378 | TEST_EQUAL(psa_get_key_bits(&got_attributes), gkc->bits); |
| 1379 | |
| 1380 | /* Do something with the key according |
| 1381 | * to its type and permitted usage. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1382 | if (!mbedtls_test_psa_exercise_key(key, gkc->usage, gkc->alg, 0)) { |
Ryan | 3a1b786 | 2024-03-01 17:24:04 +0000 | [diff] [blame] | 1383 | psa_destroy_key(key); |
| 1384 | goto exit; |
| 1385 | } |
| 1386 | psa_reset_key_attributes(&got_attributes); |
| 1387 | |
| 1388 | PSA_ASSERT(psa_destroy_key(key)); |
| 1389 | } |
| 1390 | exit: |
| 1391 | /* |
| 1392 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1393 | * thus reset them as required. |
| 1394 | */ |
| 1395 | psa_reset_key_attributes(&got_attributes); |
| 1396 | return NULL; |
| 1397 | } |
| 1398 | #endif /* MBEDTLS_THREADING_PTHREAD */ |
| 1399 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1400 | /* END_HEADER */ |
| 1401 | |
| 1402 | /* BEGIN_DEPENDENCIES |
| 1403 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1404 | * END_DEPENDENCIES |
| 1405 | */ |
| 1406 | |
| 1407 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 7abdf7e | 2023-03-09 11:17:43 +0100 | [diff] [blame] | 1408 | void psa_can_do_hash() |
| 1409 | { |
| 1410 | /* We can't test that this is specific to drivers until partial init has |
| 1411 | * been implemented, but we can at least test before/after full init. */ |
| 1412 | TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE)); |
| 1413 | PSA_INIT(); |
| 1414 | TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE)); |
| 1415 | PSA_DONE(); |
| 1416 | } |
| 1417 | /* END_CASE */ |
| 1418 | |
| 1419 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1420 | void static_checks() |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1421 | { |
| 1422 | size_t max_truncated_mac_size = |
| 1423 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1424 | |
| 1425 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1426 | * encoding. The shifted mask is the maximum truncated value. The |
| 1427 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1428 | TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size); |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1429 | } |
| 1430 | /* END_CASE */ |
| 1431 | |
| 1432 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1433 | void import_with_policy(int type_arg, |
| 1434 | int usage_arg, int alg_arg, |
| 1435 | int expected_status_arg) |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1436 | { |
| 1437 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1438 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1439 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1440 | psa_key_type_t type = type_arg; |
| 1441 | psa_key_usage_t usage = usage_arg; |
| 1442 | psa_algorithm_t alg = alg_arg; |
| 1443 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1444 | const uint8_t key_material[16] = { 0 }; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1445 | psa_status_t status; |
| 1446 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1447 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1449 | psa_set_key_type(&attributes, type); |
| 1450 | psa_set_key_usage_flags(&attributes, usage); |
| 1451 | psa_set_key_algorithm(&attributes, alg); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1452 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1453 | status = psa_import_key(&attributes, |
| 1454 | key_material, sizeof(key_material), |
| 1455 | &key); |
| 1456 | TEST_EQUAL(status, expected_status); |
| 1457 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1458 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1459 | } |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1460 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1461 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1462 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1463 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), |
| 1464 | mbedtls_test_update_key_usage_flags(usage)); |
| 1465 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 1466 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1467 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1468 | PSA_ASSERT(psa_destroy_key(key)); |
| 1469 | test_operations_on_invalid_key(key); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1470 | |
| 1471 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1472 | /* |
| 1473 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1474 | * thus reset them as required. |
| 1475 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1476 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1477 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1478 | psa_destroy_key(key); |
| 1479 | PSA_DONE(); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1480 | } |
| 1481 | /* END_CASE */ |
| 1482 | |
| 1483 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1484 | void import_with_data(data_t *data, int type_arg, |
| 1485 | int attr_bits_arg, |
| 1486 | int expected_status_arg) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1487 | { |
| 1488 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1489 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1490 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1491 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1492 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1493 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1494 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1495 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1496 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1497 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1498 | psa_set_key_type(&attributes, type); |
| 1499 | psa_set_key_bits(&attributes, attr_bits); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1500 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1501 | status = psa_import_key(&attributes, data->x, data->len, &key); |
Manuel Pégourié-Gonnard | 0509b58 | 2023-08-08 12:47:56 +0200 | [diff] [blame] | 1502 | /* When expecting INVALID_ARGUMENT, also accept NOT_SUPPORTED. |
| 1503 | * |
| 1504 | * This can happen with a type supported only by a driver: |
| 1505 | * - the driver sees the invalid data (for example wrong size) and thinks |
| 1506 | * "well perhaps this is a key size I don't support" so it returns |
| 1507 | * NOT_SUPPORTED which is correct at this point; |
| 1508 | * - we fallback to built-ins, which don't support this type, so return |
| 1509 | * NOT_SUPPORTED which again is correct at this point. |
| 1510 | */ |
| 1511 | if (expected_status == PSA_ERROR_INVALID_ARGUMENT && |
| 1512 | status == PSA_ERROR_NOT_SUPPORTED) { |
| 1513 | ; // OK |
| 1514 | } else { |
| 1515 | TEST_EQUAL(status, expected_status); |
| 1516 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1517 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1518 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1519 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1520 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1521 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1522 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1523 | if (attr_bits != 0) { |
| 1524 | TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes)); |
| 1525 | } |
| 1526 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1527 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1528 | PSA_ASSERT(psa_destroy_key(key)); |
| 1529 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1530 | |
| 1531 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1532 | /* |
| 1533 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1534 | * thus reset them as required. |
| 1535 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1536 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1537 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1538 | psa_destroy_key(key); |
| 1539 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1540 | } |
| 1541 | /* END_CASE */ |
| 1542 | |
| 1543 | /* BEGIN_CASE */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1544 | /* Construct and attempt to import a large unstructured key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1545 | void import_large_key(int type_arg, int byte_size_arg, |
| 1546 | int expected_status_arg) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1547 | { |
| 1548 | psa_key_type_t type = type_arg; |
| 1549 | size_t byte_size = byte_size_arg; |
| 1550 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1551 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1552 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1553 | psa_status_t status; |
| 1554 | uint8_t *buffer = NULL; |
| 1555 | size_t buffer_size = byte_size + 1; |
| 1556 | size_t n; |
| 1557 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1558 | /* Skip the test case if the target running the test cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1559 | * accommodate large keys due to heap size constraints */ |
Tom Cosgrove | 412a813 | 2023-07-20 16:55:14 +0100 | [diff] [blame] | 1560 | TEST_CALLOC_OR_SKIP(buffer, buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1561 | memset(buffer, 'K', byte_size); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1562 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1563 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1564 | |
| 1565 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1566 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1567 | psa_set_key_type(&attributes, type); |
| 1568 | status = psa_import_key(&attributes, buffer, byte_size, &key); |
| 1569 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 1570 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1571 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1572 | if (status == PSA_SUCCESS) { |
| 1573 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1574 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 1575 | TEST_EQUAL(psa_get_key_bits(&attributes), |
| 1576 | PSA_BYTES_TO_BITS(byte_size)); |
| 1577 | ASSERT_NO_SLOT_NUMBER(&attributes); |
| 1578 | memset(buffer, 0, byte_size + 1); |
| 1579 | PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n)); |
| 1580 | for (n = 0; n < byte_size; n++) { |
| 1581 | TEST_EQUAL(buffer[n], 'K'); |
| 1582 | } |
| 1583 | for (n = byte_size; n < buffer_size; n++) { |
| 1584 | TEST_EQUAL(buffer[n], 0); |
| 1585 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1586 | } |
| 1587 | |
| 1588 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1589 | /* |
| 1590 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1591 | * thus reset them as required. |
| 1592 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1593 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1594 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1595 | psa_destroy_key(key); |
| 1596 | PSA_DONE(); |
| 1597 | mbedtls_free(buffer); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1598 | } |
| 1599 | /* END_CASE */ |
| 1600 | |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 1601 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1602 | /* Import an RSA key with a valid structure (but not valid numbers |
| 1603 | * inside, beyond having sensible size and parity). This is expected to |
| 1604 | * fail for large keys. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1605 | 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] | 1606 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1607 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1608 | size_t bits = bits_arg; |
| 1609 | psa_status_t expected_status = expected_status_arg; |
| 1610 | psa_status_t status; |
| 1611 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1612 | 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] | 1613 | size_t buffer_size = /* Slight overapproximations */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1614 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1615 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1616 | unsigned char *p; |
| 1617 | int ret; |
| 1618 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1619 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1620 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1621 | PSA_ASSERT(psa_crypto_init()); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1622 | TEST_CALLOC(buffer, buffer_size); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1623 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1624 | TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p, |
| 1625 | bits, keypair)) >= 0); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1626 | length = ret; |
| 1627 | |
| 1628 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1629 | psa_set_key_type(&attributes, type); |
| 1630 | status = psa_import_key(&attributes, p, length, &key); |
| 1631 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1632 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1633 | if (status == PSA_SUCCESS) { |
| 1634 | PSA_ASSERT(psa_destroy_key(key)); |
| 1635 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1636 | |
| 1637 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1638 | mbedtls_free(buffer); |
| 1639 | PSA_DONE(); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1640 | } |
| 1641 | /* END_CASE */ |
| 1642 | |
| 1643 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1644 | void import_export(data_t *data, |
| 1645 | int type_arg, |
| 1646 | int usage_arg, int alg_arg, |
| 1647 | int lifetime_arg, |
| 1648 | int expected_bits, |
| 1649 | int export_size_delta, |
| 1650 | int expected_export_status_arg, |
| 1651 | /*whether reexport must give the original input exactly*/ |
| 1652 | int canonical_input) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1653 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1654 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1655 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1656 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1657 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1658 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1659 | psa_key_lifetime_t lifetime = lifetime_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1660 | unsigned char *exported = NULL; |
| 1661 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1662 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1663 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1664 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1665 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1666 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1667 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1668 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1669 | TEST_CALLOC(exported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1670 | if (!canonical_input) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1671 | TEST_CALLOC(reexported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1672 | } |
| 1673 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1674 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1675 | psa_set_key_lifetime(&attributes, lifetime); |
| 1676 | psa_set_key_usage_flags(&attributes, usage_arg); |
| 1677 | psa_set_key_algorithm(&attributes, alg); |
| 1678 | psa_set_key_type(&attributes, type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1679 | |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1680 | if (PSA_KEY_TYPE_IS_DH(type) && |
| 1681 | expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) { |
Przemek Stekiel | 654bef0 | 2022-12-15 13:28:02 +0100 | [diff] [blame] | 1682 | /* Simulate that buffer is too small, by decreasing its size by 1 byte. */ |
| 1683 | export_size -= 1; |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1684 | } |
| 1685 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1686 | /* Import the key */ |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1687 | TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key), |
Przemek Stekiel | 2e7c33d | 2023-04-27 12:29:45 +0200 | [diff] [blame] | 1688 | PSA_SUCCESS); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1689 | |
| 1690 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1691 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1692 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1693 | TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits); |
| 1694 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1695 | |
| 1696 | /* Export the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1697 | status = psa_export_key(key, exported, export_size, &exported_length); |
| 1698 | TEST_EQUAL(status, expected_export_status); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1699 | |
| 1700 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1701 | * and export_size. On errors, the exported length must be 0. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1702 | TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH); |
| 1703 | TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0); |
| 1704 | TEST_LE_U(exported_length, export_size); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1705 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1706 | TEST_ASSERT(mem_is_char(exported + exported_length, 0, |
| 1707 | export_size - exported_length)); |
| 1708 | if (status != PSA_SUCCESS) { |
| 1709 | TEST_EQUAL(exported_length, 0); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1710 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1711 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1712 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1713 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1714 | * this validates the canonical representations. For canonical inputs, |
| 1715 | * this doesn't directly validate the implementation, but it still helps |
| 1716 | * by cross-validating the test data with the sanity check code. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1717 | if (!psa_key_lifetime_is_external(lifetime)) { |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1718 | if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0, 0)) { |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1719 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1720 | } |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1721 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1722 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1723 | if (canonical_input) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1724 | TEST_MEMORY_COMPARE(data->x, data->len, exported, exported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1725 | } else { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1726 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1727 | PSA_ASSERT(psa_import_key(&attributes, exported, exported_length, |
| 1728 | &key2)); |
| 1729 | PSA_ASSERT(psa_export_key(key2, |
| 1730 | reexported, |
| 1731 | export_size, |
| 1732 | &reexported_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1733 | TEST_MEMORY_COMPARE(exported, exported_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1734 | reexported, reexported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1735 | PSA_ASSERT(psa_destroy_key(key2)); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1736 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1737 | TEST_LE_U(exported_length, |
| 1738 | PSA_EXPORT_KEY_OUTPUT_SIZE(type, |
| 1739 | psa_get_key_bits(&got_attributes))); |
Valerio Setti | 1eacae8 | 2023-07-28 16:07:03 +0200 | [diff] [blame] | 1740 | if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 1741 | TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
| 1742 | } else if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) { |
| 1743 | TEST_LE_U(exported_length, PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
| 1744 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1745 | |
| 1746 | destroy: |
| 1747 | /* Destroy the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1748 | PSA_ASSERT(psa_destroy_key(key)); |
| 1749 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1750 | |
| 1751 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1752 | /* |
| 1753 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1754 | * thus reset them as required. |
| 1755 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1756 | psa_reset_key_attributes(&got_attributes); |
| 1757 | psa_destroy_key(key); |
| 1758 | mbedtls_free(exported); |
| 1759 | mbedtls_free(reexported); |
| 1760 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1761 | } |
| 1762 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1763 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1764 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1765 | void import_export_public_key(data_t *data, |
| 1766 | int type_arg, // key pair or public key |
| 1767 | int alg_arg, |
| 1768 | int lifetime_arg, |
| 1769 | int export_size_delta, |
| 1770 | int expected_export_status_arg, |
| 1771 | data_t *expected_public_key) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1772 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1773 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1774 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1775 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1776 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1777 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1778 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1779 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1780 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1781 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1782 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1783 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1784 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1785 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1786 | psa_set_key_lifetime(&attributes, lifetime); |
| 1787 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1788 | psa_set_key_algorithm(&attributes, alg); |
| 1789 | psa_set_key_type(&attributes, type); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1790 | |
| 1791 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1792 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1793 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1794 | /* Export the public key */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1795 | TEST_CALLOC(exported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1796 | status = psa_export_public_key(key, |
| 1797 | exported, export_size, |
| 1798 | &exported_length); |
| 1799 | TEST_EQUAL(status, expected_export_status); |
| 1800 | if (status == PSA_SUCCESS) { |
| 1801 | 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] | 1802 | size_t bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1803 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1804 | bits = psa_get_key_bits(&attributes); |
| 1805 | TEST_LE_U(expected_public_key->len, |
| 1806 | PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1807 | TEST_LE_U(expected_public_key->len, |
| 1808 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1809 | TEST_LE_U(expected_public_key->len, |
| 1810 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1811 | TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1812 | exported, exported_length); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1813 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1814 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1815 | /* |
| 1816 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1817 | * thus reset them as required. |
| 1818 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1819 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1820 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1821 | mbedtls_free(exported); |
| 1822 | psa_destroy_key(key); |
| 1823 | PSA_DONE(); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1824 | } |
| 1825 | /* END_CASE */ |
| 1826 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1827 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1828 | void import_and_exercise_key(data_t *data, |
| 1829 | int type_arg, |
| 1830 | int bits_arg, |
| 1831 | int alg_arg) |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1832 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1833 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1834 | psa_key_type_t type = type_arg; |
| 1835 | size_t bits = bits_arg; |
| 1836 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1837 | 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] | 1838 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1839 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1840 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1841 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1842 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1843 | psa_set_key_usage_flags(&attributes, usage); |
| 1844 | psa_set_key_algorithm(&attributes, alg); |
| 1845 | psa_set_key_type(&attributes, type); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1846 | |
| 1847 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1848 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1849 | |
| 1850 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1851 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1852 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1853 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1854 | |
| 1855 | /* Do something with the key according to its type and permitted usage. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1856 | if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1857 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1858 | } |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1859 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1860 | PSA_ASSERT(psa_destroy_key(key)); |
| 1861 | test_operations_on_invalid_key(key); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1862 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1863 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1864 | /* |
| 1865 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1866 | * thus reset them as required. |
| 1867 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1868 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1869 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1870 | psa_reset_key_attributes(&attributes); |
| 1871 | psa_destroy_key(key); |
| 1872 | PSA_DONE(); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1873 | } |
| 1874 | /* END_CASE */ |
| 1875 | |
| 1876 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1877 | void effective_key_attributes(int type_arg, int expected_type_arg, |
| 1878 | int bits_arg, int expected_bits_arg, |
| 1879 | int usage_arg, int expected_usage_arg, |
| 1880 | int alg_arg, int expected_alg_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1881 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1882 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1883 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1884 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1885 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1886 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1887 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1888 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1889 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1890 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1891 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1892 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1893 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1894 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1895 | psa_set_key_usage_flags(&attributes, usage); |
| 1896 | psa_set_key_algorithm(&attributes, alg); |
| 1897 | psa_set_key_type(&attributes, key_type); |
| 1898 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1899 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1900 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
| 1901 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1902 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1903 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1904 | TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type); |
| 1905 | TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits); |
| 1906 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
| 1907 | TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1908 | |
| 1909 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1910 | /* |
| 1911 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1912 | * thus reset them as required. |
| 1913 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1914 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1915 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1916 | psa_destroy_key(key); |
| 1917 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1918 | } |
| 1919 | /* END_CASE */ |
| 1920 | |
| 1921 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1922 | void check_key_policy(int type_arg, int bits_arg, |
| 1923 | int usage_arg, int alg_arg) |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1924 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1925 | test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg, |
| 1926 | usage_arg, |
| 1927 | mbedtls_test_update_key_usage_flags(usage_arg), |
| 1928 | alg_arg, alg_arg); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1929 | goto exit; |
| 1930 | } |
| 1931 | /* END_CASE */ |
| 1932 | |
| 1933 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1934 | void key_attributes_init() |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1935 | { |
| 1936 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1937 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1938 | * 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] | 1939 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1940 | psa_key_attributes_t func = psa_key_attributes_init(); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1941 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1942 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1943 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1944 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1945 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1946 | TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE); |
| 1947 | TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE); |
| 1948 | TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1949 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1950 | TEST_EQUAL(psa_get_key_type(&func), 0); |
| 1951 | TEST_EQUAL(psa_get_key_type(&init), 0); |
| 1952 | TEST_EQUAL(psa_get_key_type(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1953 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1954 | TEST_EQUAL(psa_get_key_bits(&func), 0); |
| 1955 | TEST_EQUAL(psa_get_key_bits(&init), 0); |
| 1956 | TEST_EQUAL(psa_get_key_bits(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1957 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1958 | TEST_EQUAL(psa_get_key_usage_flags(&func), 0); |
| 1959 | TEST_EQUAL(psa_get_key_usage_flags(&init), 0); |
| 1960 | TEST_EQUAL(psa_get_key_usage_flags(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1961 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1962 | TEST_EQUAL(psa_get_key_algorithm(&func), 0); |
| 1963 | TEST_EQUAL(psa_get_key_algorithm(&init), 0); |
| 1964 | TEST_EQUAL(psa_get_key_algorithm(&zero), 0); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1965 | } |
| 1966 | /* END_CASE */ |
| 1967 | |
| 1968 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1969 | void mac_key_policy(int policy_usage_arg, |
| 1970 | int policy_alg_arg, |
| 1971 | int key_type_arg, |
| 1972 | data_t *key_data, |
| 1973 | int exercise_alg_arg, |
| 1974 | int expected_status_sign_arg, |
| 1975 | int expected_status_verify_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1976 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1977 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1978 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1979 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1980 | psa_key_type_t key_type = key_type_arg; |
| 1981 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 1982 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 1983 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1984 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1985 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 1986 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1987 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1988 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1989 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1990 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1991 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1992 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1993 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1994 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1995 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1996 | &key)); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1997 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1998 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 1999 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2000 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2001 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 2002 | TEST_EQUAL(status, expected_status_sign); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2003 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 2004 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2005 | uint8_t input[128] = { 0 }; |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 2006 | size_t mac_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2007 | TEST_EQUAL(psa_mac_compute(key, exercise_alg, |
| 2008 | input, 128, |
| 2009 | mac, PSA_MAC_MAX_SIZE, &mac_len), |
| 2010 | expected_status_sign); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 2011 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2012 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2013 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 2014 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 2015 | if (status == PSA_SUCCESS) { |
| 2016 | status = psa_mac_update(&operation, input, 128); |
| 2017 | if (status == PSA_SUCCESS) { |
| 2018 | TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE, |
| 2019 | &mac_len), |
| 2020 | expected_status_sign); |
| 2021 | } else { |
| 2022 | TEST_EQUAL(status, expected_status_sign); |
| 2023 | } |
| 2024 | } else { |
| 2025 | TEST_EQUAL(status, expected_status_sign); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2026 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2027 | PSA_ASSERT(psa_mac_abort(&operation)); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2028 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 2029 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2030 | status = psa_mac_verify(key, exercise_alg, input, 128, |
| 2031 | mac, mac_len); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 2032 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2033 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 2034 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2035 | } else { |
| 2036 | TEST_EQUAL(status, expected_status_verify); |
| 2037 | } |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2038 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2039 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2040 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 2041 | if (status == PSA_SUCCESS) { |
| 2042 | status = psa_mac_update(&operation, input, 128); |
| 2043 | if (status == PSA_SUCCESS) { |
| 2044 | status = psa_mac_verify_finish(&operation, mac, mac_len); |
| 2045 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 2046 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2047 | } else { |
| 2048 | TEST_EQUAL(status, expected_status_verify); |
| 2049 | } |
| 2050 | } else { |
| 2051 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2052 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2053 | } else { |
| 2054 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2055 | } |
| 2056 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2057 | psa_mac_abort(&operation); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2058 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2059 | memset(mac, 0, sizeof(mac)); |
| 2060 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 2061 | TEST_EQUAL(status, expected_status_verify); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2062 | |
| 2063 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2064 | psa_mac_abort(&operation); |
| 2065 | psa_destroy_key(key); |
| 2066 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2067 | } |
| 2068 | /* END_CASE */ |
| 2069 | |
| 2070 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2071 | void cipher_key_policy(int policy_usage_arg, |
| 2072 | int policy_alg, |
| 2073 | int key_type, |
| 2074 | data_t *key_data, |
| 2075 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2076 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2077 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2078 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2079 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2080 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2081 | size_t output_buffer_size = 0; |
| 2082 | size_t input_buffer_size = 0; |
| 2083 | size_t output_length = 0; |
| 2084 | uint8_t *output = NULL; |
| 2085 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2086 | psa_status_t status; |
| 2087 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2088 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg); |
| 2089 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg, |
| 2090 | input_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2091 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2092 | TEST_CALLOC(input, input_buffer_size); |
| 2093 | TEST_CALLOC(output, output_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2094 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2095 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2096 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2097 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2098 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2099 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2100 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2101 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2102 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2103 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2104 | /* Check if no key usage flag implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2105 | TEST_EQUAL(policy_usage, |
| 2106 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2107 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2108 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2109 | status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size, |
| 2110 | output, output_buffer_size, |
| 2111 | &output_length); |
| 2112 | if (policy_alg == exercise_alg && |
| 2113 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2114 | PSA_ASSERT(status); |
| 2115 | } else { |
| 2116 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2117 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2118 | |
| 2119 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2120 | status = psa_cipher_encrypt_setup(&operation, key, exercise_alg); |
| 2121 | if (policy_alg == exercise_alg && |
| 2122 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2123 | PSA_ASSERT(status); |
| 2124 | } else { |
| 2125 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2126 | } |
| 2127 | psa_cipher_abort(&operation); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2128 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2129 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2130 | status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size, |
| 2131 | input, input_buffer_size, |
| 2132 | &output_length); |
| 2133 | if (policy_alg == exercise_alg && |
| 2134 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2135 | PSA_ASSERT(status); |
| 2136 | } else { |
| 2137 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2138 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2139 | |
| 2140 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2141 | status = psa_cipher_decrypt_setup(&operation, key, exercise_alg); |
| 2142 | if (policy_alg == exercise_alg && |
| 2143 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2144 | PSA_ASSERT(status); |
| 2145 | } else { |
| 2146 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2147 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2148 | |
| 2149 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2150 | psa_cipher_abort(&operation); |
| 2151 | mbedtls_free(input); |
| 2152 | mbedtls_free(output); |
| 2153 | psa_destroy_key(key); |
| 2154 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2155 | } |
| 2156 | /* END_CASE */ |
| 2157 | |
| 2158 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2159 | void aead_key_policy(int policy_usage_arg, |
| 2160 | int policy_alg, |
| 2161 | int key_type, |
| 2162 | data_t *key_data, |
| 2163 | int nonce_length_arg, |
| 2164 | int tag_length_arg, |
| 2165 | int exercise_alg, |
| 2166 | int expected_status_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2167 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2168 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2169 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2170 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2171 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2172 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2173 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2174 | unsigned char nonce[16] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2175 | size_t nonce_length = nonce_length_arg; |
| 2176 | unsigned char tag[16]; |
| 2177 | size_t tag_length = tag_length_arg; |
| 2178 | size_t output_length; |
| 2179 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2180 | TEST_LE_U(nonce_length, sizeof(nonce)); |
| 2181 | TEST_LE_U(tag_length, sizeof(tag)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2183 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2184 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2185 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2186 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2187 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2188 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2189 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2190 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2191 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2192 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2193 | TEST_EQUAL(policy_usage, |
| 2194 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2195 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2196 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2197 | status = psa_aead_encrypt(key, exercise_alg, |
| 2198 | nonce, nonce_length, |
| 2199 | NULL, 0, |
| 2200 | NULL, 0, |
| 2201 | tag, tag_length, |
| 2202 | &output_length); |
| 2203 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2204 | TEST_EQUAL(status, expected_status); |
| 2205 | } else { |
| 2206 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2207 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2208 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2209 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2210 | status = psa_aead_encrypt_setup(&operation, key, exercise_alg); |
| 2211 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2212 | TEST_EQUAL(status, expected_status); |
| 2213 | } else { |
| 2214 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2215 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2216 | |
| 2217 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2218 | memset(tag, 0, sizeof(tag)); |
| 2219 | status = psa_aead_decrypt(key, exercise_alg, |
| 2220 | nonce, nonce_length, |
| 2221 | NULL, 0, |
| 2222 | tag, tag_length, |
| 2223 | NULL, 0, |
| 2224 | &output_length); |
| 2225 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2226 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2227 | } else if (expected_status == PSA_SUCCESS) { |
| 2228 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2229 | } else { |
| 2230 | TEST_EQUAL(status, expected_status); |
| 2231 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2232 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2233 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2234 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2235 | status = psa_aead_decrypt_setup(&operation, key, exercise_alg); |
| 2236 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2237 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2238 | } else { |
| 2239 | TEST_EQUAL(status, expected_status); |
| 2240 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2241 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2242 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2243 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2244 | psa_destroy_key(key); |
| 2245 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2246 | } |
| 2247 | /* END_CASE */ |
| 2248 | |
| 2249 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2250 | void asymmetric_encryption_key_policy(int policy_usage_arg, |
| 2251 | int policy_alg, |
| 2252 | int key_type, |
| 2253 | data_t *key_data, |
Valerio Setti | f202c29 | 2024-01-15 10:42:37 +0100 | [diff] [blame] | 2254 | int exercise_alg, |
| 2255 | int use_opaque_key) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2256 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2257 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2258 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2259 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2260 | psa_status_t status; |
| 2261 | size_t key_bits; |
| 2262 | size_t buffer_length; |
| 2263 | unsigned char *buffer = NULL; |
| 2264 | size_t output_length; |
| 2265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2266 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2267 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2268 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2269 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2270 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2271 | |
Valerio Setti | f202c29 | 2024-01-15 10:42:37 +0100 | [diff] [blame] | 2272 | if (use_opaque_key) { |
| 2273 | psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION( |
| 2274 | PSA_KEY_PERSISTENCE_VOLATILE, TEST_DRIVER_LOCATION)); |
| 2275 | } |
| 2276 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2277 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2278 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2279 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2280 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2281 | TEST_EQUAL(policy_usage, |
| 2282 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2283 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2284 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 2285 | key_bits = psa_get_key_bits(&attributes); |
| 2286 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, |
| 2287 | exercise_alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2288 | TEST_CALLOC(buffer, buffer_length); |
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 | status = psa_asymmetric_encrypt(key, exercise_alg, |
| 2291 | NULL, 0, |
| 2292 | NULL, 0, |
| 2293 | buffer, buffer_length, |
| 2294 | &output_length); |
| 2295 | if (policy_alg == exercise_alg && |
| 2296 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2297 | PSA_ASSERT(status); |
| 2298 | } else { |
| 2299 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2300 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2301 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2302 | if (buffer_length != 0) { |
| 2303 | memset(buffer, 0, buffer_length); |
| 2304 | } |
| 2305 | status = psa_asymmetric_decrypt(key, exercise_alg, |
| 2306 | buffer, buffer_length, |
| 2307 | NULL, 0, |
| 2308 | buffer, buffer_length, |
| 2309 | &output_length); |
| 2310 | if (policy_alg == exercise_alg && |
| 2311 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2312 | TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING); |
| 2313 | } else { |
| 2314 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2315 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2316 | |
| 2317 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2318 | /* |
| 2319 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2320 | * thus reset them as required. |
| 2321 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2322 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2323 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2324 | psa_destroy_key(key); |
| 2325 | PSA_DONE(); |
| 2326 | mbedtls_free(buffer); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2327 | } |
| 2328 | /* END_CASE */ |
| 2329 | |
| 2330 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2331 | void asymmetric_signature_key_policy(int policy_usage_arg, |
| 2332 | int policy_alg, |
| 2333 | int key_type, |
| 2334 | data_t *key_data, |
| 2335 | int exercise_alg, |
| 2336 | int payload_length_arg, |
| 2337 | int expected_usage_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2338 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2339 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2340 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2341 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 2342 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2343 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2344 | unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 }; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2345 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2346 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2347 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2348 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2349 | int compatible_alg = payload_length_arg > 0; |
| 2350 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2351 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2352 | size_t signature_length; |
| 2353 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2354 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2355 | in the expected usage flags. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2356 | TEST_EQUAL(expected_usage, |
| 2357 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2358 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2359 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2360 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2361 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2362 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2363 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2364 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2365 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2366 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2367 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2368 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2370 | status = psa_sign_hash(key, exercise_alg, |
| 2371 | payload, payload_length, |
| 2372 | signature, sizeof(signature), |
| 2373 | &signature_length); |
| 2374 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) { |
| 2375 | PSA_ASSERT(status); |
| 2376 | } else { |
| 2377 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2378 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2379 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2380 | memset(signature, 0, sizeof(signature)); |
| 2381 | status = psa_verify_hash(key, exercise_alg, |
| 2382 | payload, payload_length, |
| 2383 | signature, sizeof(signature)); |
| 2384 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) { |
| 2385 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2386 | } else { |
| 2387 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2388 | } |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2389 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2390 | if (PSA_ALG_IS_SIGN_HASH(exercise_alg) && |
| 2391 | PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) { |
| 2392 | status = psa_sign_message(key, exercise_alg, |
| 2393 | payload, payload_length, |
| 2394 | signature, sizeof(signature), |
| 2395 | &signature_length); |
| 2396 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) { |
| 2397 | PSA_ASSERT(status); |
| 2398 | } else { |
| 2399 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2400 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2401 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2402 | memset(signature, 0, sizeof(signature)); |
| 2403 | status = psa_verify_message(key, exercise_alg, |
| 2404 | payload, payload_length, |
| 2405 | signature, sizeof(signature)); |
| 2406 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) { |
| 2407 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2408 | } else { |
| 2409 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2410 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2411 | } |
| 2412 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2413 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2414 | psa_destroy_key(key); |
| 2415 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2416 | } |
| 2417 | /* END_CASE */ |
| 2418 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2419 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2420 | void derive_key_policy(int policy_usage, |
| 2421 | int policy_alg, |
| 2422 | int key_type, |
| 2423 | data_t *key_data, |
| 2424 | int exercise_alg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2425 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2426 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2427 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2428 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2429 | psa_status_t status; |
| 2430 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2431 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2433 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2434 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2435 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2436 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2437 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2438 | &key)); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2439 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2440 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2441 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2442 | if (PSA_ALG_IS_TLS12_PRF(exercise_alg) || |
| 2443 | PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) { |
| 2444 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 2445 | &operation, |
| 2446 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2447 | (const uint8_t *) "", 0)); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2448 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2449 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2450 | status = psa_key_derivation_input_key(&operation, |
| 2451 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 2452 | key); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2453 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2454 | if (policy_alg == exercise_alg && |
| 2455 | (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) { |
| 2456 | PSA_ASSERT(status); |
| 2457 | } else { |
| 2458 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2459 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2460 | |
| 2461 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2462 | psa_key_derivation_abort(&operation); |
| 2463 | psa_destroy_key(key); |
| 2464 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2465 | } |
| 2466 | /* END_CASE */ |
| 2467 | |
| 2468 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2469 | void agreement_key_policy(int policy_usage, |
| 2470 | int policy_alg, |
| 2471 | int key_type_arg, |
| 2472 | data_t *key_data, |
| 2473 | int exercise_alg, |
| 2474 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2475 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2476 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2477 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2478 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2479 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2480 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2481 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2482 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2483 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2484 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2485 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2486 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2487 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2488 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2489 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2490 | &key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2491 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2492 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
| 2493 | status = mbedtls_test_psa_key_agreement_with_self(&operation, key); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2494 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2495 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2496 | |
| 2497 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2498 | psa_key_derivation_abort(&operation); |
| 2499 | psa_destroy_key(key); |
| 2500 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2501 | } |
| 2502 | /* END_CASE */ |
| 2503 | |
| 2504 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2505 | void key_policy_alg2(int key_type_arg, data_t *key_data, |
| 2506 | int usage_arg, int alg_arg, int alg2_arg) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2507 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2508 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2509 | psa_key_type_t key_type = key_type_arg; |
| 2510 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2511 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2512 | psa_key_usage_t usage = usage_arg; |
| 2513 | psa_algorithm_t alg = alg_arg; |
| 2514 | psa_algorithm_t alg2 = alg2_arg; |
| 2515 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2516 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2517 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2518 | psa_set_key_usage_flags(&attributes, usage); |
| 2519 | psa_set_key_algorithm(&attributes, alg); |
| 2520 | psa_set_key_enrollment_algorithm(&attributes, alg2); |
| 2521 | psa_set_key_type(&attributes, key_type); |
| 2522 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2523 | &key)); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2524 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2525 | /* Update the usage flags to obtain implicit usage flags */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2526 | usage = mbedtls_test_update_key_usage_flags(usage); |
| 2527 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 2528 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage); |
| 2529 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 2530 | TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2531 | |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 2532 | if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2533 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2534 | } |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 2535 | if (!mbedtls_test_psa_exercise_key(key, usage, alg2, 0)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2536 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2537 | } |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2538 | |
| 2539 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2540 | /* |
| 2541 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2542 | * thus reset them as required. |
| 2543 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2544 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2545 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2546 | psa_destroy_key(key); |
| 2547 | PSA_DONE(); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2548 | } |
| 2549 | /* END_CASE */ |
| 2550 | |
| 2551 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2552 | void raw_agreement_key_policy(int policy_usage, |
| 2553 | int policy_alg, |
| 2554 | int key_type_arg, |
| 2555 | data_t *key_data, |
| 2556 | int exercise_alg, |
| 2557 | int expected_status_arg) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2558 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2559 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2560 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2561 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2562 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2563 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2564 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2565 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2566 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2567 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2568 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2569 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2570 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2571 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2572 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2573 | &key)); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2574 | |
Ryan Everett | 8163028 | 2024-03-12 16:21:12 +0000 | [diff] [blame^] | 2575 | 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] | 2576 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2577 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2578 | |
| 2579 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2580 | psa_key_derivation_abort(&operation); |
| 2581 | psa_destroy_key(key); |
| 2582 | PSA_DONE(); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2583 | } |
| 2584 | /* END_CASE */ |
| 2585 | |
| 2586 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2587 | void copy_success(int source_usage_arg, |
| 2588 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4ea4ad0 | 2022-12-04 15:11:00 +0100 | [diff] [blame] | 2589 | int source_lifetime_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2590 | int type_arg, data_t *material, |
| 2591 | int copy_attributes, |
| 2592 | int target_usage_arg, |
| 2593 | int target_alg_arg, int target_alg2_arg, |
Gilles Peskine | 4ea4ad0 | 2022-12-04 15:11:00 +0100 | [diff] [blame] | 2594 | int target_lifetime_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2595 | int expected_usage_arg, |
| 2596 | int expected_alg_arg, int expected_alg2_arg) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2597 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2598 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2599 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2600 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2601 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2602 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2603 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 2604 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2605 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2606 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2607 | uint8_t *export_buffer = NULL; |
| 2608 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2609 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2610 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2611 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2612 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2613 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2614 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2615 | psa_set_key_type(&source_attributes, type_arg); |
| 2616 | psa_set_key_lifetime(&source_attributes, source_lifetime); |
| 2617 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2618 | material->x, material->len, |
| 2619 | &source_key)); |
| 2620 | PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2621 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2622 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2623 | if (copy_attributes) { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2624 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2625 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2626 | psa_set_key_lifetime(&target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2627 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2628 | if (target_usage_arg != -1) { |
| 2629 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2630 | } |
| 2631 | if (target_alg_arg != -1) { |
| 2632 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2633 | } |
| 2634 | if (target_alg2_arg != -1) { |
| 2635 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
| 2636 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2637 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2638 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2639 | /* Copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2640 | PSA_ASSERT(psa_copy_key(source_key, |
| 2641 | &target_attributes, &target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2642 | |
| 2643 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2644 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2645 | |
| 2646 | /* Test that the target slot has the expected content and policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2647 | PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes)); |
| 2648 | TEST_EQUAL(psa_get_key_type(&source_attributes), |
| 2649 | psa_get_key_type(&target_attributes)); |
| 2650 | TEST_EQUAL(psa_get_key_bits(&source_attributes), |
| 2651 | psa_get_key_bits(&target_attributes)); |
| 2652 | TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes)); |
| 2653 | TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes)); |
| 2654 | TEST_EQUAL(expected_alg2, |
| 2655 | psa_get_key_enrollment_algorithm(&target_attributes)); |
| 2656 | if (expected_usage & PSA_KEY_USAGE_EXPORT) { |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2657 | size_t length; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2658 | TEST_CALLOC(export_buffer, material->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2659 | PSA_ASSERT(psa_export_key(target_key, export_buffer, |
| 2660 | material->len, &length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2661 | TEST_MEMORY_COMPARE(material->x, material->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2662 | export_buffer, length); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2663 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2664 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2665 | if (!psa_key_lifetime_is_external(target_lifetime)) { |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 2666 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg, 0)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2667 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2668 | } |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 2669 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2, 0)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2670 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2671 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2672 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2673 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2674 | PSA_ASSERT(psa_destroy_key(target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2675 | |
| 2676 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2677 | /* |
| 2678 | * Source and target key attributes may have been returned by |
| 2679 | * psa_get_key_attributes() thus reset them as required. |
| 2680 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2681 | psa_reset_key_attributes(&source_attributes); |
| 2682 | psa_reset_key_attributes(&target_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2683 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2684 | PSA_DONE(); |
| 2685 | mbedtls_free(export_buffer); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2686 | } |
| 2687 | /* END_CASE */ |
| 2688 | |
| 2689 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2690 | void copy_fail(int source_usage_arg, |
| 2691 | int source_alg_arg, int source_alg2_arg, |
| 2692 | int source_lifetime_arg, |
| 2693 | int type_arg, data_t *material, |
| 2694 | int target_type_arg, int target_bits_arg, |
| 2695 | int target_usage_arg, |
| 2696 | int target_alg_arg, int target_alg2_arg, |
| 2697 | int target_id_arg, int target_lifetime_arg, |
| 2698 | int expected_status_arg) |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2699 | { |
| 2700 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2701 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2702 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2703 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2704 | 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] | 2705 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2706 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2707 | |
| 2708 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2709 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2710 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2711 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2712 | psa_set_key_type(&source_attributes, type_arg); |
| 2713 | psa_set_key_lifetime(&source_attributes, source_lifetime_arg); |
| 2714 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2715 | material->x, material->len, |
| 2716 | &source_key)); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2717 | |
| 2718 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2719 | psa_set_key_id(&target_attributes, key_id); |
| 2720 | psa_set_key_lifetime(&target_attributes, target_lifetime_arg); |
| 2721 | psa_set_key_type(&target_attributes, target_type_arg); |
| 2722 | psa_set_key_bits(&target_attributes, target_bits_arg); |
| 2723 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2724 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2725 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2726 | |
| 2727 | /* Try to copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2728 | TEST_EQUAL(psa_copy_key(source_key, |
| 2729 | &target_attributes, &target_key), |
| 2730 | expected_status_arg); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2731 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2732 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2733 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2734 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2735 | psa_reset_key_attributes(&source_attributes); |
| 2736 | psa_reset_key_attributes(&target_attributes); |
| 2737 | PSA_DONE(); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2738 | } |
| 2739 | /* END_CASE */ |
| 2740 | |
| 2741 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2742 | void hash_operation_init() |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2743 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2744 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2745 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2746 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2747 | * 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] | 2748 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2749 | psa_hash_operation_t func = psa_hash_operation_init(); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2750 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2751 | psa_hash_operation_t zero; |
| 2752 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2753 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2754 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2755 | /* A freshly-initialized hash operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2756 | TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)), |
| 2757 | PSA_ERROR_BAD_STATE); |
| 2758 | TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)), |
| 2759 | PSA_ERROR_BAD_STATE); |
| 2760 | TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)), |
| 2761 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2762 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2763 | /* A default hash operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2764 | PSA_ASSERT(psa_hash_abort(&func)); |
| 2765 | PSA_ASSERT(psa_hash_abort(&init)); |
| 2766 | PSA_ASSERT(psa_hash_abort(&zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2767 | } |
| 2768 | /* END_CASE */ |
| 2769 | |
| 2770 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2771 | void hash_setup(int alg_arg, |
| 2772 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2773 | { |
| 2774 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2775 | uint8_t *output = NULL; |
| 2776 | size_t output_size = 0; |
| 2777 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2778 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2779 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2780 | psa_status_t status; |
| 2781 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2782 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2783 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2784 | /* Hash Setup, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2785 | output_size = PSA_HASH_LENGTH(alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2786 | TEST_CALLOC(output, output_size); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2787 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2788 | status = psa_hash_compute(alg, NULL, 0, |
| 2789 | output, output_size, &output_length); |
| 2790 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2791 | |
| 2792 | /* Hash Setup, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2793 | status = psa_hash_setup(&operation, alg); |
| 2794 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2795 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2796 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2797 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2798 | |
| 2799 | /* If setup failed, reproduce the failure, so as to |
| 2800 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2801 | if (status != PSA_SUCCESS) { |
| 2802 | TEST_EQUAL(psa_hash_setup(&operation, alg), status); |
| 2803 | } |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2804 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2805 | /* Now the operation object should be reusable. */ |
| 2806 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2807 | PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG)); |
| 2808 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2809 | #endif |
| 2810 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2811 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2812 | mbedtls_free(output); |
| 2813 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2814 | } |
| 2815 | /* END_CASE */ |
| 2816 | |
| 2817 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2818 | void hash_compute_fail(int alg_arg, data_t *input, |
| 2819 | int output_size_arg, int expected_status_arg) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2820 | { |
| 2821 | psa_algorithm_t alg = alg_arg; |
| 2822 | uint8_t *output = NULL; |
| 2823 | size_t output_size = output_size_arg; |
| 2824 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2825 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2826 | psa_status_t expected_status = expected_status_arg; |
| 2827 | psa_status_t status; |
| 2828 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2829 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2830 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2831 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2832 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2833 | /* Hash Compute, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2834 | status = psa_hash_compute(alg, input->x, input->len, |
| 2835 | output, output_size, &output_length); |
| 2836 | TEST_EQUAL(status, expected_status); |
| 2837 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2838 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2839 | /* Hash Compute, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2840 | status = psa_hash_setup(&operation, alg); |
| 2841 | if (status == PSA_SUCCESS) { |
| 2842 | status = psa_hash_update(&operation, input->x, input->len); |
| 2843 | if (status == PSA_SUCCESS) { |
| 2844 | status = psa_hash_finish(&operation, output, output_size, |
| 2845 | &output_length); |
| 2846 | if (status == PSA_SUCCESS) { |
| 2847 | TEST_LE_U(output_length, output_size); |
| 2848 | } else { |
| 2849 | TEST_EQUAL(status, expected_status); |
| 2850 | } |
| 2851 | } else { |
| 2852 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2853 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2854 | } else { |
| 2855 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2856 | } |
| 2857 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2858 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2859 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2860 | mbedtls_free(output); |
| 2861 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2862 | } |
| 2863 | /* END_CASE */ |
| 2864 | |
| 2865 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2866 | void hash_compare_fail(int alg_arg, data_t *input, |
| 2867 | data_t *reference_hash, |
| 2868 | int expected_status_arg) |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2869 | { |
| 2870 | psa_algorithm_t alg = alg_arg; |
| 2871 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2872 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2873 | psa_status_t status; |
| 2874 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2875 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2876 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2877 | /* Hash Compare, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2878 | status = psa_hash_compare(alg, input->x, input->len, |
| 2879 | reference_hash->x, reference_hash->len); |
| 2880 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2881 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2882 | /* Hash Compare, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2883 | status = psa_hash_setup(&operation, alg); |
| 2884 | if (status == PSA_SUCCESS) { |
| 2885 | status = psa_hash_update(&operation, input->x, input->len); |
| 2886 | if (status == PSA_SUCCESS) { |
| 2887 | status = psa_hash_verify(&operation, reference_hash->x, |
| 2888 | reference_hash->len); |
| 2889 | TEST_EQUAL(status, expected_status); |
| 2890 | } else { |
| 2891 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2892 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2893 | } else { |
| 2894 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2895 | } |
| 2896 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2897 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2898 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2899 | PSA_DONE(); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2900 | } |
| 2901 | /* END_CASE */ |
| 2902 | |
| 2903 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2904 | void hash_compute_compare(int alg_arg, data_t *input, |
| 2905 | data_t *expected_output) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2906 | { |
| 2907 | psa_algorithm_t alg = alg_arg; |
| 2908 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2909 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2910 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2911 | size_t i; |
| 2912 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2913 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2914 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2915 | /* Compute with tight buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2916 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 2917 | output, PSA_HASH_LENGTH(alg), |
| 2918 | &output_length)); |
| 2919 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2920 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2921 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2922 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2923 | /* Compute with tight buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2924 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2925 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2926 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 2927 | PSA_HASH_LENGTH(alg), |
| 2928 | &output_length)); |
| 2929 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2930 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2931 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2932 | |
| 2933 | /* Compute with larger buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2934 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 2935 | output, sizeof(output), |
| 2936 | &output_length)); |
| 2937 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2938 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2939 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2940 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2941 | /* Compute with larger buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2942 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2943 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2944 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 2945 | sizeof(output), &output_length)); |
| 2946 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2947 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2948 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2949 | |
| 2950 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2951 | PSA_ASSERT(psa_hash_compare(alg, input->x, input->len, |
| 2952 | output, output_length)); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2953 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2954 | /* Compare with correct hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2955 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2956 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2957 | PSA_ASSERT(psa_hash_verify(&operation, output, |
| 2958 | output_length)); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2959 | |
| 2960 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2961 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2962 | output, output_length + 1), |
| 2963 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2964 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2965 | /* Compare with trailing garbage, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2966 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2967 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2968 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1), |
| 2969 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2970 | |
| 2971 | /* Compare with truncated hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2972 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2973 | output, output_length - 1), |
| 2974 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2975 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2976 | /* Compare with truncated hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2977 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2978 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2979 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1), |
| 2980 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2981 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2982 | /* Compare with corrupted value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2983 | for (i = 0; i < output_length; i++) { |
| 2984 | mbedtls_test_set_step(i); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2985 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2986 | |
| 2987 | /* One-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2988 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2989 | output, output_length), |
| 2990 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2991 | |
| 2992 | /* Multi-Part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2993 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2994 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2995 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length), |
| 2996 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2997 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2998 | output[i] ^= 1; |
| 2999 | } |
| 3000 | |
| 3001 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3002 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3003 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3004 | } |
| 3005 | /* END_CASE */ |
| 3006 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3007 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3008 | void hash_bad_order() |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3009 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3010 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3011 | unsigned char input[] = ""; |
| 3012 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3013 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3014 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 3015 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3016 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 |
| 3017 | }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3018 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3019 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 3020 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3021 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3022 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3023 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3024 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3025 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3026 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3027 | TEST_EQUAL(psa_hash_setup(&operation, alg), |
| 3028 | PSA_ERROR_BAD_STATE); |
| 3029 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3030 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3031 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3032 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3033 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3034 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 3035 | PSA_ERROR_BAD_STATE); |
| 3036 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3037 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3038 | /* Check that update calls abort on error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3039 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 3040 | operation.id = UINT_MAX; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3041 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3042 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 3043 | PSA_ERROR_BAD_STATE); |
| 3044 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3045 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3046 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3047 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3048 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3049 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3050 | PSA_ASSERT(psa_hash_finish(&operation, |
| 3051 | hash, sizeof(hash), &hash_len)); |
| 3052 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 3053 | PSA_ERROR_BAD_STATE); |
| 3054 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3055 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3056 | /* Call verify without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3057 | TEST_EQUAL(psa_hash_verify(&operation, |
| 3058 | valid_hash, sizeof(valid_hash)), |
| 3059 | PSA_ERROR_BAD_STATE); |
| 3060 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3061 | |
| 3062 | /* Call verify after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3063 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3064 | PSA_ASSERT(psa_hash_finish(&operation, |
| 3065 | hash, sizeof(hash), &hash_len)); |
| 3066 | TEST_EQUAL(psa_hash_verify(&operation, |
| 3067 | valid_hash, sizeof(valid_hash)), |
| 3068 | PSA_ERROR_BAD_STATE); |
| 3069 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3070 | |
| 3071 | /* Call verify twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3072 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3073 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3074 | PSA_ASSERT(psa_hash_verify(&operation, |
| 3075 | valid_hash, sizeof(valid_hash))); |
| 3076 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3077 | TEST_EQUAL(psa_hash_verify(&operation, |
| 3078 | valid_hash, sizeof(valid_hash)), |
| 3079 | PSA_ERROR_BAD_STATE); |
| 3080 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3081 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3082 | |
| 3083 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3084 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3085 | hash, sizeof(hash), &hash_len), |
| 3086 | PSA_ERROR_BAD_STATE); |
| 3087 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3088 | |
| 3089 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3090 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3091 | PSA_ASSERT(psa_hash_finish(&operation, |
| 3092 | hash, sizeof(hash), &hash_len)); |
| 3093 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3094 | hash, sizeof(hash), &hash_len), |
| 3095 | PSA_ERROR_BAD_STATE); |
| 3096 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3097 | |
| 3098 | /* Call finish after calling verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3099 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3100 | PSA_ASSERT(psa_hash_verify(&operation, |
| 3101 | valid_hash, sizeof(valid_hash))); |
| 3102 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3103 | hash, sizeof(hash), &hash_len), |
| 3104 | PSA_ERROR_BAD_STATE); |
| 3105 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3106 | |
| 3107 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3108 | PSA_DONE(); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3109 | } |
| 3110 | /* END_CASE */ |
| 3111 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3112 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3113 | void hash_verify_bad_args() |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3114 | { |
| 3115 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3116 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 3117 | * appended to it */ |
| 3118 | unsigned char hash[] = { |
| 3119 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 3120 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3121 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb |
| 3122 | }; |
| 3123 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 3124 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3125 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3126 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3127 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3128 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3129 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3130 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3131 | TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1), |
| 3132 | PSA_ERROR_INVALID_SIGNATURE); |
| 3133 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3134 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3135 | ASSERT_OPERATION_IS_INACTIVE(operation); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3136 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3137 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3138 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3139 | TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size), |
| 3140 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3141 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3142 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3143 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3144 | TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)), |
| 3145 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 3146 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3147 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3148 | PSA_DONE(); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3149 | } |
| 3150 | /* END_CASE */ |
| 3151 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3152 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3153 | void hash_finish_bad_args() |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3154 | { |
| 3155 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 3156 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3157 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 3158 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3159 | size_t hash_len; |
| 3160 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3161 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3162 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3163 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3164 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3165 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3166 | hash, expected_size - 1, &hash_len), |
| 3167 | PSA_ERROR_BUFFER_TOO_SMALL); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3168 | |
| 3169 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3170 | PSA_DONE(); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3171 | } |
| 3172 | /* END_CASE */ |
| 3173 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3174 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3175 | void hash_clone_source_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3176 | { |
| 3177 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3178 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3179 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 3180 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3181 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3182 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3183 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3184 | size_t hash_len; |
| 3185 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3186 | PSA_ASSERT(psa_crypto_init()); |
| 3187 | PSA_ASSERT(psa_hash_setup(&op_source, alg)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3188 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3189 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3190 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3191 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3192 | hash, sizeof(hash), &hash_len)); |
| 3193 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3194 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3195 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3196 | TEST_EQUAL(psa_hash_clone(&op_source, &op_setup), |
| 3197 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3198 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3199 | PSA_ASSERT(psa_hash_clone(&op_source, &op_init)); |
| 3200 | PSA_ASSERT(psa_hash_finish(&op_init, |
| 3201 | hash, sizeof(hash), &hash_len)); |
| 3202 | PSA_ASSERT(psa_hash_clone(&op_source, &op_finished)); |
| 3203 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3204 | hash, sizeof(hash), &hash_len)); |
| 3205 | PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted)); |
| 3206 | PSA_ASSERT(psa_hash_finish(&op_aborted, |
| 3207 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3208 | |
| 3209 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3210 | psa_hash_abort(&op_source); |
| 3211 | psa_hash_abort(&op_init); |
| 3212 | psa_hash_abort(&op_setup); |
| 3213 | psa_hash_abort(&op_finished); |
| 3214 | psa_hash_abort(&op_aborted); |
| 3215 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3216 | } |
| 3217 | /* END_CASE */ |
| 3218 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3219 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3220 | void hash_clone_target_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3221 | { |
| 3222 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3223 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3224 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3225 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3226 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3227 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3228 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 3229 | size_t hash_len; |
| 3230 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3231 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3232 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3233 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3234 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3235 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3236 | hash, sizeof(hash), &hash_len)); |
| 3237 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3238 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3239 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3240 | PSA_ASSERT(psa_hash_clone(&op_setup, &op_target)); |
| 3241 | PSA_ASSERT(psa_hash_finish(&op_target, |
| 3242 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3243 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3244 | TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE); |
| 3245 | TEST_EQUAL(psa_hash_clone(&op_finished, &op_target), |
| 3246 | PSA_ERROR_BAD_STATE); |
| 3247 | TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target), |
| 3248 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3249 | |
| 3250 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3251 | psa_hash_abort(&op_target); |
| 3252 | psa_hash_abort(&op_init); |
| 3253 | psa_hash_abort(&op_setup); |
| 3254 | psa_hash_abort(&op_finished); |
| 3255 | psa_hash_abort(&op_aborted); |
| 3256 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3257 | } |
| 3258 | /* END_CASE */ |
| 3259 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3260 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3261 | void mac_operation_init() |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3262 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3263 | const uint8_t input[1] = { 0 }; |
| 3264 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3265 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3266 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3267 | * 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] | 3268 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3269 | psa_mac_operation_t func = psa_mac_operation_init(); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3270 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 3271 | psa_mac_operation_t zero; |
| 3272 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3273 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3274 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3275 | /* A freshly-initialized MAC operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3276 | TEST_EQUAL(psa_mac_update(&func, |
| 3277 | input, sizeof(input)), |
| 3278 | PSA_ERROR_BAD_STATE); |
| 3279 | TEST_EQUAL(psa_mac_update(&init, |
| 3280 | input, sizeof(input)), |
| 3281 | PSA_ERROR_BAD_STATE); |
| 3282 | TEST_EQUAL(psa_mac_update(&zero, |
| 3283 | input, sizeof(input)), |
| 3284 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3285 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3286 | /* A default MAC operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3287 | PSA_ASSERT(psa_mac_abort(&func)); |
| 3288 | PSA_ASSERT(psa_mac_abort(&init)); |
| 3289 | PSA_ASSERT(psa_mac_abort(&zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3290 | } |
| 3291 | /* END_CASE */ |
| 3292 | |
| 3293 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3294 | void mac_setup(int key_type_arg, |
| 3295 | data_t *key, |
| 3296 | int alg_arg, |
| 3297 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3298 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3299 | psa_key_type_t key_type = key_type_arg; |
| 3300 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3301 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3302 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3303 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3304 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3305 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3306 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3307 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3308 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3309 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3310 | if (!exercise_mac_setup(key_type, key->x, key->len, alg, |
| 3311 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3312 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3313 | } |
| 3314 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3315 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3316 | /* The operation object should be reusable. */ |
| 3317 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3318 | if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 3319 | smoke_test_key_data, |
| 3320 | sizeof(smoke_test_key_data), |
| 3321 | KNOWN_SUPPORTED_MAC_ALG, |
| 3322 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3323 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3324 | } |
| 3325 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3326 | #endif |
| 3327 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3328 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3329 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3330 | } |
| 3331 | /* END_CASE */ |
| 3332 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3333 | /* 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] | 3334 | void mac_bad_order() |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3335 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3336 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3337 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 3338 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3339 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3340 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3341 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3342 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa |
| 3343 | }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3344 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3345 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3346 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 3347 | size_t sign_mac_length = 0; |
| 3348 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3349 | const uint8_t verify_mac[] = { |
| 3350 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 3351 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3352 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 |
| 3353 | }; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3354 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3355 | PSA_ASSERT(psa_crypto_init()); |
| 3356 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 3357 | psa_set_key_algorithm(&attributes, alg); |
| 3358 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3359 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3360 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3361 | &key)); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3362 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3363 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3364 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3365 | PSA_ERROR_BAD_STATE); |
| 3366 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3367 | |
| 3368 | /* Call sign finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3369 | TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac), |
| 3370 | &sign_mac_length), |
| 3371 | PSA_ERROR_BAD_STATE); |
| 3372 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3373 | |
| 3374 | /* Call verify finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3375 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3376 | verify_mac, sizeof(verify_mac)), |
| 3377 | PSA_ERROR_BAD_STATE); |
| 3378 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3379 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3380 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3381 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3382 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3383 | TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg), |
| 3384 | PSA_ERROR_BAD_STATE); |
| 3385 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3386 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3387 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3388 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3389 | /* Call update after sign finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3390 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3391 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3392 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3393 | sign_mac, sizeof(sign_mac), |
| 3394 | &sign_mac_length)); |
| 3395 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3396 | PSA_ERROR_BAD_STATE); |
| 3397 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3398 | |
| 3399 | /* Call update after verify finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3400 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3401 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3402 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3403 | verify_mac, sizeof(verify_mac))); |
| 3404 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3405 | PSA_ERROR_BAD_STATE); |
| 3406 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3407 | |
| 3408 | /* Call sign finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3409 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3410 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3411 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3412 | sign_mac, sizeof(sign_mac), |
| 3413 | &sign_mac_length)); |
| 3414 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3415 | sign_mac, sizeof(sign_mac), |
| 3416 | &sign_mac_length), |
| 3417 | PSA_ERROR_BAD_STATE); |
| 3418 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3419 | |
| 3420 | /* Call verify finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3421 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3422 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3423 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3424 | verify_mac, sizeof(verify_mac))); |
| 3425 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3426 | verify_mac, sizeof(verify_mac)), |
| 3427 | PSA_ERROR_BAD_STATE); |
| 3428 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3429 | |
| 3430 | /* Setup sign but try verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3431 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3432 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3433 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3434 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3435 | verify_mac, sizeof(verify_mac)), |
| 3436 | PSA_ERROR_BAD_STATE); |
| 3437 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3438 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3439 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3440 | |
| 3441 | /* Setup verify but try sign. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3442 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3443 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3444 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3445 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3446 | sign_mac, sizeof(sign_mac), |
| 3447 | &sign_mac_length), |
| 3448 | PSA_ERROR_BAD_STATE); |
| 3449 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3450 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3451 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3452 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3453 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3454 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3455 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3456 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3457 | } |
| 3458 | /* END_CASE */ |
| 3459 | |
| 3460 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3461 | void mac_sign_verify_multi(int key_type_arg, |
| 3462 | data_t *key_data, |
| 3463 | int alg_arg, |
| 3464 | data_t *input, |
| 3465 | int is_verify, |
| 3466 | data_t *expected_mac) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3467 | { |
| 3468 | size_t data_part_len = 0; |
| 3469 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3470 | 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] | 3471 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3472 | mbedtls_test_set_step(2000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3473 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3474 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3475 | alg_arg, |
| 3476 | input, data_part_len, |
| 3477 | expected_mac, |
| 3478 | is_verify, 0) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3479 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3480 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3481 | |
| 3482 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3483 | mbedtls_test_set_step(3000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3484 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3485 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3486 | alg_arg, |
| 3487 | input, data_part_len, |
| 3488 | expected_mac, |
| 3489 | is_verify, 1) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3490 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3491 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3492 | } |
| 3493 | |
| 3494 | /* Goto is required to silence warnings about unused labels, as we |
| 3495 | * don't actually do any test assertions in this function. */ |
| 3496 | goto exit; |
| 3497 | } |
| 3498 | /* END_CASE */ |
| 3499 | |
| 3500 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3501 | void mac_sign(int key_type_arg, |
| 3502 | data_t *key_data, |
| 3503 | int alg_arg, |
| 3504 | data_t *input, |
| 3505 | data_t *expected_mac) |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3506 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3507 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3508 | psa_key_type_t key_type = key_type_arg; |
| 3509 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3510 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3511 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3512 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3513 | size_t mac_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3514 | 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] | 3515 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3516 | const size_t output_sizes_to_test[] = { |
| 3517 | 0, |
| 3518 | 1, |
| 3519 | expected_mac->len - 1, |
| 3520 | expected_mac->len, |
| 3521 | expected_mac->len + 1, |
| 3522 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3523 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3524 | TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3525 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3526 | TEST_ASSERT(expected_mac->len == mac_buffer_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3527 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3528 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3529 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3530 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 3531 | psa_set_key_algorithm(&attributes, alg); |
| 3532 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3533 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3534 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3535 | &key)); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3536 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3537 | 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] | 3538 | const size_t output_size = output_sizes_to_test[i]; |
| 3539 | psa_status_t expected_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3540 | (output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3541 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3542 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3543 | mbedtls_test_set_step(output_size); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3544 | TEST_CALLOC(actual_mac, output_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3545 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3546 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3547 | TEST_EQUAL(psa_mac_compute(key, alg, |
| 3548 | input->x, input->len, |
| 3549 | actual_mac, output_size, &mac_length), |
| 3550 | expected_status); |
| 3551 | if (expected_status == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3552 | TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3553 | actual_mac, mac_length); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3554 | } |
| 3555 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3556 | if (output_size > 0) { |
| 3557 | memset(actual_mac, 0, output_size); |
| 3558 | } |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3559 | |
| 3560 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3561 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3562 | PSA_ASSERT(psa_mac_update(&operation, |
| 3563 | input->x, input->len)); |
| 3564 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3565 | actual_mac, output_size, |
| 3566 | &mac_length), |
| 3567 | expected_status); |
| 3568 | PSA_ASSERT(psa_mac_abort(&operation)); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3569 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3570 | if (expected_status == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3571 | TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3572 | actual_mac, mac_length); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3573 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3574 | mbedtls_free(actual_mac); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3575 | actual_mac = NULL; |
| 3576 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3577 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3578 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3579 | psa_mac_abort(&operation); |
| 3580 | psa_destroy_key(key); |
| 3581 | PSA_DONE(); |
| 3582 | mbedtls_free(actual_mac); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3583 | } |
| 3584 | /* END_CASE */ |
| 3585 | |
| 3586 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3587 | void mac_verify(int key_type_arg, |
| 3588 | data_t *key_data, |
| 3589 | int alg_arg, |
| 3590 | data_t *input, |
| 3591 | data_t *expected_mac) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3592 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3593 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3594 | psa_key_type_t key_type = key_type_arg; |
| 3595 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3596 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3597 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3598 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3599 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3600 | TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3601 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3602 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3603 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3604 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 3605 | psa_set_key_algorithm(&attributes, alg); |
| 3606 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3607 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3608 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3609 | &key)); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3610 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3611 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3612 | PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len, |
| 3613 | expected_mac->x, expected_mac->len)); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3614 | |
| 3615 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3616 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3617 | PSA_ASSERT(psa_mac_update(&operation, |
| 3618 | input->x, input->len)); |
| 3619 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3620 | expected_mac->x, |
| 3621 | expected_mac->len)); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3622 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3623 | /* Test a MAC that's too short, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3624 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3625 | input->x, input->len, |
| 3626 | expected_mac->x, |
| 3627 | expected_mac->len - 1), |
| 3628 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3629 | |
| 3630 | /* Test a MAC that's too short, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3631 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3632 | PSA_ASSERT(psa_mac_update(&operation, |
| 3633 | input->x, input->len)); |
| 3634 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3635 | expected_mac->x, |
| 3636 | expected_mac->len - 1), |
| 3637 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3638 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3639 | /* Test a MAC that's too long, one-shot case. */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3640 | TEST_CALLOC(perturbed_mac, expected_mac->len + 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3641 | memcpy(perturbed_mac, expected_mac->x, expected_mac->len); |
| 3642 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3643 | input->x, input->len, |
| 3644 | perturbed_mac, expected_mac->len + 1), |
| 3645 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3646 | |
| 3647 | /* Test a MAC that's too long, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3648 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3649 | PSA_ASSERT(psa_mac_update(&operation, |
| 3650 | input->x, input->len)); |
| 3651 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3652 | perturbed_mac, |
| 3653 | expected_mac->len + 1), |
| 3654 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3655 | |
| 3656 | /* Test changing one byte. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3657 | for (size_t i = 0; i < expected_mac->len; i++) { |
| 3658 | mbedtls_test_set_step(i); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3659 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3660 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3661 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3662 | input->x, input->len, |
| 3663 | perturbed_mac, expected_mac->len), |
| 3664 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3665 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3666 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3667 | PSA_ASSERT(psa_mac_update(&operation, |
| 3668 | input->x, input->len)); |
| 3669 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3670 | perturbed_mac, |
| 3671 | expected_mac->len), |
| 3672 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3673 | perturbed_mac[i] ^= 1; |
| 3674 | } |
| 3675 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3676 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3677 | psa_mac_abort(&operation); |
| 3678 | psa_destroy_key(key); |
| 3679 | PSA_DONE(); |
| 3680 | mbedtls_free(perturbed_mac); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3681 | } |
| 3682 | /* END_CASE */ |
| 3683 | |
| 3684 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3685 | void cipher_operation_init() |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3686 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3687 | const uint8_t input[1] = { 0 }; |
| 3688 | unsigned char output[1] = { 0 }; |
| 3689 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3690 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3691 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3692 | * 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] | 3693 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3694 | psa_cipher_operation_t func = psa_cipher_operation_init(); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3695 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3696 | psa_cipher_operation_t zero; |
| 3697 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3698 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3699 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3700 | /* A freshly-initialized cipher operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3701 | TEST_EQUAL(psa_cipher_update(&func, |
| 3702 | input, sizeof(input), |
| 3703 | output, sizeof(output), |
| 3704 | &output_length), |
| 3705 | PSA_ERROR_BAD_STATE); |
| 3706 | TEST_EQUAL(psa_cipher_update(&init, |
| 3707 | input, sizeof(input), |
| 3708 | output, sizeof(output), |
| 3709 | &output_length), |
| 3710 | PSA_ERROR_BAD_STATE); |
| 3711 | TEST_EQUAL(psa_cipher_update(&zero, |
| 3712 | input, sizeof(input), |
| 3713 | output, sizeof(output), |
| 3714 | &output_length), |
| 3715 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3716 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3717 | /* A default cipher operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3718 | PSA_ASSERT(psa_cipher_abort(&func)); |
| 3719 | PSA_ASSERT(psa_cipher_abort(&init)); |
| 3720 | PSA_ASSERT(psa_cipher_abort(&zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3721 | } |
| 3722 | /* END_CASE */ |
| 3723 | |
| 3724 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3725 | void cipher_setup(int key_type_arg, |
| 3726 | data_t *key, |
| 3727 | int alg_arg, |
| 3728 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3729 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3730 | psa_key_type_t key_type = key_type_arg; |
| 3731 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3732 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3733 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3734 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3735 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3736 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3737 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3738 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3739 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3740 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3741 | if (!exercise_cipher_setup(key_type, key->x, key->len, alg, |
| 3742 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3743 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3744 | } |
| 3745 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3746 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3747 | /* The operation object should be reusable. */ |
| 3748 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3749 | if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3750 | smoke_test_key_data, |
| 3751 | sizeof(smoke_test_key_data), |
| 3752 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3753 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3754 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3755 | } |
| 3756 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3757 | #endif |
| 3758 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3759 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3760 | psa_cipher_abort(&operation); |
| 3761 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3762 | } |
| 3763 | /* END_CASE */ |
| 3764 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3765 | /* 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] | 3766 | void cipher_bad_order() |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3767 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3768 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3769 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3770 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3771 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3772 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3773 | 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] | 3774 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3775 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3776 | 0xaa, 0xaa, 0xaa, 0xaa |
| 3777 | }; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3778 | const uint8_t text[] = { |
| 3779 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3780 | 0xbb, 0xbb, 0xbb, 0xbb |
| 3781 | }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3782 | 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] | 3783 | size_t length = 0; |
| 3784 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3785 | PSA_ASSERT(psa_crypto_init()); |
| 3786 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3787 | psa_set_key_algorithm(&attributes, alg); |
| 3788 | psa_set_key_type(&attributes, key_type); |
| 3789 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3790 | &key)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3791 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3792 | /* Call encrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3793 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3794 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3795 | TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg), |
| 3796 | PSA_ERROR_BAD_STATE); |
| 3797 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3798 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3799 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3800 | |
| 3801 | /* Call decrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3802 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3803 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3804 | TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg), |
| 3805 | PSA_ERROR_BAD_STATE); |
| 3806 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3807 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3808 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3809 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3810 | /* Generate an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3811 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3812 | buffer, sizeof(buffer), |
| 3813 | &length), |
| 3814 | PSA_ERROR_BAD_STATE); |
| 3815 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3816 | |
| 3817 | /* Generate an IV twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3818 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3819 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3820 | buffer, sizeof(buffer), |
| 3821 | &length)); |
| 3822 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3823 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3824 | buffer, sizeof(buffer), |
| 3825 | &length), |
| 3826 | PSA_ERROR_BAD_STATE); |
| 3827 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3828 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3829 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3830 | |
| 3831 | /* Generate an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3832 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3833 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3834 | iv, sizeof(iv))); |
| 3835 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3836 | buffer, sizeof(buffer), |
| 3837 | &length), |
| 3838 | PSA_ERROR_BAD_STATE); |
| 3839 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3840 | |
| 3841 | /* Set an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3842 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3843 | iv, sizeof(iv)), |
| 3844 | PSA_ERROR_BAD_STATE); |
| 3845 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3846 | |
| 3847 | /* Set an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3848 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3849 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3850 | iv, sizeof(iv))); |
| 3851 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3852 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3853 | iv, sizeof(iv)), |
| 3854 | PSA_ERROR_BAD_STATE); |
| 3855 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3856 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3857 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3858 | |
| 3859 | /* Set an IV after it's already generated. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3860 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3861 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3862 | buffer, sizeof(buffer), |
| 3863 | &length)); |
| 3864 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3865 | iv, sizeof(iv)), |
| 3866 | PSA_ERROR_BAD_STATE); |
| 3867 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3868 | |
| 3869 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3870 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3871 | text, sizeof(text), |
| 3872 | buffer, sizeof(buffer), |
| 3873 | &length), |
| 3874 | PSA_ERROR_BAD_STATE); |
| 3875 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3876 | |
| 3877 | /* Call update without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3878 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3879 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3880 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3881 | text, sizeof(text), |
| 3882 | buffer, sizeof(buffer), |
| 3883 | &length), |
| 3884 | PSA_ERROR_BAD_STATE); |
| 3885 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3886 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3887 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3888 | |
| 3889 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3890 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3891 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3892 | iv, sizeof(iv))); |
| 3893 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3894 | buffer, sizeof(buffer), &length)); |
| 3895 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3896 | text, sizeof(text), |
| 3897 | buffer, sizeof(buffer), |
| 3898 | &length), |
| 3899 | PSA_ERROR_BAD_STATE); |
| 3900 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3901 | |
| 3902 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3903 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3904 | buffer, sizeof(buffer), &length), |
| 3905 | PSA_ERROR_BAD_STATE); |
| 3906 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3907 | |
| 3908 | /* Call finish without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3909 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3910 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3911 | * for cipher modes with padding. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3912 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3913 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3914 | buffer, sizeof(buffer), &length), |
| 3915 | PSA_ERROR_BAD_STATE); |
| 3916 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3917 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3918 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3919 | |
| 3920 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3921 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3922 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3923 | iv, sizeof(iv))); |
| 3924 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3925 | buffer, sizeof(buffer), &length)); |
| 3926 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3927 | buffer, sizeof(buffer), &length), |
| 3928 | PSA_ERROR_BAD_STATE); |
| 3929 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3930 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3931 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3932 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3933 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3934 | psa_cipher_abort(&operation); |
| 3935 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3936 | } |
| 3937 | /* END_CASE */ |
| 3938 | |
| 3939 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3940 | void cipher_encrypt_fail(int alg_arg, |
| 3941 | int key_type_arg, |
| 3942 | data_t *key_data, |
| 3943 | data_t *input, |
| 3944 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3945 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3946 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3947 | psa_status_t status; |
| 3948 | psa_key_type_t key_type = key_type_arg; |
| 3949 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3950 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3951 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 }; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3952 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; |
| 3953 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3954 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3955 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3956 | size_t output_length = 0; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3957 | size_t function_output_length; |
| 3958 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3959 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3960 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3961 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 3962 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3963 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3964 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3965 | psa_set_key_algorithm(&attributes, alg); |
| 3966 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3967 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3968 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 3969 | input->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3970 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3971 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3972 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3973 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3974 | } |
| 3975 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3976 | /* Encrypt, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3977 | status = psa_cipher_encrypt(key, alg, input->x, input->len, output, |
| 3978 | output_buffer_size, &output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3979 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3980 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3981 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3982 | /* Encrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3983 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
| 3984 | if (status == PSA_SUCCESS) { |
| 3985 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 3986 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3987 | iv, iv_size, |
| 3988 | &iv_length)); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3989 | } |
| 3990 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3991 | status = psa_cipher_update(&operation, input->x, input->len, |
| 3992 | output, output_buffer_size, |
| 3993 | &function_output_length); |
| 3994 | if (status == PSA_SUCCESS) { |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3995 | output_length += function_output_length; |
| 3996 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3997 | status = psa_cipher_finish(&operation, output + output_length, |
| 3998 | output_buffer_size - output_length, |
| 3999 | &function_output_length); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4000 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4001 | TEST_EQUAL(status, expected_status); |
| 4002 | } else { |
| 4003 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4004 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4005 | } else { |
| 4006 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4007 | } |
| 4008 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4009 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4010 | psa_cipher_abort(&operation); |
| 4011 | mbedtls_free(output); |
| 4012 | psa_destroy_key(key); |
| 4013 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4014 | } |
| 4015 | /* END_CASE */ |
| 4016 | |
| 4017 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4018 | void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data, |
| 4019 | data_t *input, int iv_length, |
| 4020 | int expected_result) |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4021 | { |
| 4022 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4023 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4024 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4025 | size_t output_buffer_size = 0; |
| 4026 | unsigned char *output = NULL; |
| 4027 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4028 | 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] | 4029 | TEST_CALLOC(output, output_buffer_size); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4030 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4031 | PSA_ASSERT(psa_crypto_init()); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4032 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4033 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4034 | psa_set_key_algorithm(&attributes, alg); |
| 4035 | psa_set_key_type(&attributes, key_type); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4036 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4037 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4038 | &key)); |
| 4039 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4040 | TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output, |
| 4041 | iv_length)); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4042 | |
| 4043 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4044 | psa_cipher_abort(&operation); |
| 4045 | mbedtls_free(output); |
| 4046 | psa_destroy_key(key); |
| 4047 | PSA_DONE(); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4048 | } |
| 4049 | /* END_CASE */ |
| 4050 | |
| 4051 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4052 | void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data, |
| 4053 | data_t *plaintext, data_t *ciphertext) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4054 | { |
| 4055 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4056 | psa_key_type_t key_type = key_type_arg; |
| 4057 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 4058 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4059 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4060 | unsigned char *output = NULL; |
| 4061 | size_t output_buffer_size = 0; |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4062 | size_t output_length, length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4063 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4064 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4065 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4066 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4067 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4068 | TEST_LE_U(ciphertext->len, |
| 4069 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len)); |
| 4070 | TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len), |
| 4071 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len)); |
| 4072 | TEST_LE_U(plaintext->len, |
| 4073 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len)); |
| 4074 | TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len), |
| 4075 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len)); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4076 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4077 | |
| 4078 | /* Set up key and output buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4079 | psa_set_key_usage_flags(&attributes, |
| 4080 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4081 | psa_set_key_algorithm(&attributes, alg); |
| 4082 | psa_set_key_type(&attributes, key_type); |
| 4083 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4084 | &key)); |
| 4085 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 4086 | plaintext->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4087 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4088 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4089 | /* set_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4090 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4091 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 4092 | PSA_ERROR_BAD_STATE); |
| 4093 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 4094 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 4095 | PSA_ERROR_BAD_STATE); |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 4096 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4097 | /* generate_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4098 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4099 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 4100 | &length), |
| 4101 | PSA_ERROR_BAD_STATE); |
| 4102 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 4103 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 4104 | &length), |
| 4105 | PSA_ERROR_BAD_STATE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4106 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4107 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4108 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4109 | output_length = 0; |
| 4110 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4111 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4112 | plaintext->x, plaintext->len, |
| 4113 | output, output_buffer_size, |
| 4114 | &length)); |
| 4115 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4116 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4117 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4118 | mbedtls_buffer_offset(output, output_length), |
| 4119 | output_buffer_size - output_length, |
| 4120 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4121 | output_length += length; |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4122 | TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4123 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4124 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4125 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4126 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4127 | output_length = 0; |
| 4128 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4129 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4130 | ciphertext->x, ciphertext->len, |
| 4131 | output, output_buffer_size, |
| 4132 | &length)); |
| 4133 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4134 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4135 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4136 | mbedtls_buffer_offset(output, output_length), |
| 4137 | output_buffer_size - output_length, |
| 4138 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4139 | output_length += length; |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4140 | TEST_MEMORY_COMPARE(plaintext->x, plaintext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4141 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4142 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4143 | /* One-shot encryption */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4144 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4145 | PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len, |
| 4146 | output, output_buffer_size, |
| 4147 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4148 | TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4149 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4150 | |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4151 | /* One-shot decryption */ |
| 4152 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4153 | PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len, |
| 4154 | output, output_buffer_size, |
| 4155 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4156 | TEST_MEMORY_COMPARE(plaintext->x, plaintext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4157 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4158 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4159 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4160 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4161 | mbedtls_free(output); |
| 4162 | psa_cipher_abort(&operation); |
| 4163 | psa_destroy_key(key); |
| 4164 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4165 | } |
| 4166 | /* END_CASE */ |
| 4167 | |
| 4168 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4169 | 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] | 4170 | { |
| 4171 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4172 | psa_algorithm_t alg = alg_arg; |
| 4173 | psa_key_type_t key_type = key_type_arg; |
| 4174 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4175 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4176 | psa_status_t status; |
| 4177 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4178 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4179 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4180 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4181 | psa_set_key_algorithm(&attributes, alg); |
| 4182 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4183 | |
| 4184 | /* Usage of either of these two size macros would cause divide by zero |
| 4185 | * with incorrect key types previously. Input length should be irrelevant |
| 4186 | * here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4187 | TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16), |
| 4188 | 0); |
| 4189 | TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4190 | |
| 4191 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4192 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4193 | &key)); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4194 | |
| 4195 | /* Should fail due to invalid alg type (to support invalid key type). |
| 4196 | * Encrypt or decrypt will end up in the same place. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4197 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4198 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4199 | TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4200 | |
| 4201 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4202 | psa_cipher_abort(&operation); |
| 4203 | psa_destroy_key(key); |
| 4204 | PSA_DONE(); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4205 | } |
| 4206 | /* END_CASE */ |
| 4207 | |
| 4208 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4209 | void cipher_encrypt_validation(int alg_arg, |
| 4210 | int key_type_arg, |
| 4211 | data_t *key_data, |
| 4212 | data_t *input) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4213 | { |
| 4214 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4215 | psa_key_type_t key_type = key_type_arg; |
| 4216 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4217 | size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4218 | unsigned char *output1 = NULL; |
| 4219 | size_t output1_buffer_size = 0; |
| 4220 | size_t output1_length = 0; |
| 4221 | unsigned char *output2 = NULL; |
| 4222 | size_t output2_buffer_size = 0; |
| 4223 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4224 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4225 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4226 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4227 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4228 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4230 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4231 | psa_set_key_algorithm(&attributes, alg); |
| 4232 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4233 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4234 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4235 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4236 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4237 | TEST_CALLOC(output1, output1_buffer_size); |
| 4238 | TEST_CALLOC(output2, output2_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4239 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4240 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4241 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4242 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 4243 | /* The one-shot cipher encryption uses generated iv so validating |
| 4244 | the output is not possible. Validating with multipart encryption. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4245 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1, |
| 4246 | output1_buffer_size, &output1_length)); |
| 4247 | TEST_LE_U(output1_length, |
| 4248 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4249 | TEST_LE_U(output1_length, |
| 4250 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4252 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4253 | PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4254 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4255 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4256 | input->x, input->len, |
| 4257 | output2, output2_buffer_size, |
| 4258 | &function_output_length)); |
| 4259 | TEST_LE_U(function_output_length, |
| 4260 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4261 | TEST_LE_U(function_output_length, |
| 4262 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4263 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4264 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4265 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4266 | output2 + output2_length, |
| 4267 | output2_buffer_size - output2_length, |
| 4268 | &function_output_length)); |
| 4269 | TEST_LE_U(function_output_length, |
| 4270 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4271 | TEST_LE_U(function_output_length, |
| 4272 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4273 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4274 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4275 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4276 | TEST_MEMORY_COMPARE(output1 + iv_size, output1_length - iv_size, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4277 | output2, output2_length); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4278 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4279 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4280 | psa_cipher_abort(&operation); |
| 4281 | mbedtls_free(output1); |
| 4282 | mbedtls_free(output2); |
| 4283 | psa_destroy_key(key); |
| 4284 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4285 | } |
| 4286 | /* END_CASE */ |
| 4287 | |
| 4288 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4289 | void cipher_encrypt_multipart(int alg_arg, int key_type_arg, |
| 4290 | data_t *key_data, data_t *iv, |
| 4291 | data_t *input, |
| 4292 | int first_part_size_arg, |
| 4293 | int output1_length_arg, int output2_length_arg, |
| 4294 | data_t *expected_output, |
| 4295 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4296 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4297 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4298 | psa_key_type_t key_type = key_type_arg; |
| 4299 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4300 | psa_status_t status; |
| 4301 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4302 | size_t first_part_size = first_part_size_arg; |
| 4303 | size_t output1_length = output1_length_arg; |
| 4304 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4305 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4306 | size_t output_buffer_size = 0; |
| 4307 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4308 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4309 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4310 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4311 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4312 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4314 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4315 | psa_set_key_algorithm(&attributes, alg); |
| 4316 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4317 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4318 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4319 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4320 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4321 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4322 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4323 | if (iv->len > 0) { |
| 4324 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4325 | } |
| 4326 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4327 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4328 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4329 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4330 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4331 | TEST_LE_U(first_part_size, input->len); |
| 4332 | PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size, |
| 4333 | output, output_buffer_size, |
| 4334 | &function_output_length)); |
| 4335 | TEST_ASSERT(function_output_length == output1_length); |
| 4336 | TEST_LE_U(function_output_length, |
| 4337 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4338 | TEST_LE_U(function_output_length, |
| 4339 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4340 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4341 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4342 | if (first_part_size < input->len) { |
| 4343 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4344 | input->x + first_part_size, |
| 4345 | input->len - first_part_size, |
| 4346 | (output_buffer_size == 0 ? NULL : |
| 4347 | output + total_output_length), |
| 4348 | output_buffer_size - total_output_length, |
| 4349 | &function_output_length)); |
| 4350 | TEST_ASSERT(function_output_length == output2_length); |
| 4351 | TEST_LE_U(function_output_length, |
| 4352 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4353 | alg, |
| 4354 | input->len - first_part_size)); |
| 4355 | TEST_LE_U(function_output_length, |
| 4356 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4357 | total_output_length += function_output_length; |
| 4358 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4359 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4360 | status = psa_cipher_finish(&operation, |
| 4361 | (output_buffer_size == 0 ? NULL : |
| 4362 | output + total_output_length), |
| 4363 | output_buffer_size - total_output_length, |
| 4364 | &function_output_length); |
| 4365 | TEST_LE_U(function_output_length, |
| 4366 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4367 | TEST_LE_U(function_output_length, |
| 4368 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4369 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4370 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4371 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4372 | if (expected_status == PSA_SUCCESS) { |
| 4373 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4374 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4375 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4376 | output, total_output_length); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4377 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4378 | |
| 4379 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4380 | psa_cipher_abort(&operation); |
| 4381 | mbedtls_free(output); |
| 4382 | psa_destroy_key(key); |
| 4383 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4384 | } |
| 4385 | /* END_CASE */ |
| 4386 | |
| 4387 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4388 | void cipher_decrypt_multipart(int alg_arg, int key_type_arg, |
| 4389 | data_t *key_data, data_t *iv, |
| 4390 | data_t *input, |
| 4391 | int first_part_size_arg, |
| 4392 | int output1_length_arg, int output2_length_arg, |
| 4393 | data_t *expected_output, |
| 4394 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4395 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4396 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4397 | psa_key_type_t key_type = key_type_arg; |
| 4398 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4399 | psa_status_t status; |
| 4400 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4401 | size_t first_part_size = first_part_size_arg; |
| 4402 | size_t output1_length = output1_length_arg; |
| 4403 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4404 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4405 | size_t output_buffer_size = 0; |
| 4406 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4407 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4408 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4409 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4410 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4411 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4412 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4413 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4414 | psa_set_key_algorithm(&attributes, alg); |
| 4415 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4416 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4417 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4418 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4419 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4420 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4421 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4422 | if (iv->len > 0) { |
| 4423 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4424 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4425 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4426 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4427 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4428 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4429 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4430 | TEST_LE_U(first_part_size, input->len); |
| 4431 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4432 | input->x, first_part_size, |
| 4433 | output, output_buffer_size, |
| 4434 | &function_output_length)); |
| 4435 | TEST_ASSERT(function_output_length == output1_length); |
| 4436 | TEST_LE_U(function_output_length, |
| 4437 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4438 | TEST_LE_U(function_output_length, |
| 4439 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4440 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4441 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4442 | if (first_part_size < input->len) { |
| 4443 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4444 | input->x + first_part_size, |
| 4445 | input->len - first_part_size, |
| 4446 | (output_buffer_size == 0 ? NULL : |
| 4447 | output + total_output_length), |
| 4448 | output_buffer_size - total_output_length, |
| 4449 | &function_output_length)); |
| 4450 | TEST_ASSERT(function_output_length == output2_length); |
| 4451 | TEST_LE_U(function_output_length, |
| 4452 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4453 | alg, |
| 4454 | input->len - first_part_size)); |
| 4455 | TEST_LE_U(function_output_length, |
| 4456 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4457 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4458 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4459 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4460 | status = psa_cipher_finish(&operation, |
| 4461 | (output_buffer_size == 0 ? NULL : |
| 4462 | output + total_output_length), |
| 4463 | output_buffer_size - total_output_length, |
| 4464 | &function_output_length); |
| 4465 | TEST_LE_U(function_output_length, |
| 4466 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4467 | TEST_LE_U(function_output_length, |
| 4468 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4469 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4470 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4471 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4472 | if (expected_status == PSA_SUCCESS) { |
| 4473 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4474 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4475 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4476 | output, total_output_length); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4477 | } |
| 4478 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4479 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4480 | psa_cipher_abort(&operation); |
| 4481 | mbedtls_free(output); |
| 4482 | psa_destroy_key(key); |
| 4483 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4484 | } |
| 4485 | /* END_CASE */ |
| 4486 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4487 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4488 | void cipher_decrypt_fail(int alg_arg, |
| 4489 | int key_type_arg, |
| 4490 | data_t *key_data, |
| 4491 | data_t *iv, |
| 4492 | data_t *input_arg, |
| 4493 | int expected_status_arg) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4494 | { |
| 4495 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4496 | psa_status_t status; |
| 4497 | psa_key_type_t key_type = key_type_arg; |
| 4498 | psa_algorithm_t alg = alg_arg; |
| 4499 | psa_status_t expected_status = expected_status_arg; |
| 4500 | unsigned char *input = NULL; |
| 4501 | size_t input_buffer_size = 0; |
| 4502 | unsigned char *output = NULL; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4503 | unsigned char *output_multi = NULL; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4504 | size_t output_buffer_size = 0; |
| 4505 | size_t output_length = 0; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4506 | size_t function_output_length; |
| 4507 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4508 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4509 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4510 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 4511 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4512 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4513 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4514 | psa_set_key_algorithm(&attributes, alg); |
| 4515 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4516 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4517 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4518 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4519 | } |
| 4520 | |
| 4521 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4522 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4523 | if (input_buffer_size > 0) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4524 | TEST_CALLOC(input, input_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4525 | memcpy(input, iv->x, iv->len); |
| 4526 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4527 | } |
| 4528 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4529 | 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] | 4530 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4531 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4532 | /* Decrypt, one-short */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4533 | status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4534 | output_buffer_size, &output_length); |
| 4535 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4536 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4537 | /* Decrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4538 | status = psa_cipher_decrypt_setup(&operation, key, alg); |
| 4539 | if (status == PSA_SUCCESS) { |
| 4540 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 4541 | input_arg->len) + |
| 4542 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4543 | TEST_CALLOC(output_multi, output_buffer_size); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4544 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4545 | if (iv->len > 0) { |
| 4546 | status = psa_cipher_set_iv(&operation, iv->x, iv->len); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4547 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4548 | if (status != PSA_SUCCESS) { |
| 4549 | TEST_EQUAL(status, expected_status); |
| 4550 | } |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4551 | } |
| 4552 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4553 | if (status == PSA_SUCCESS) { |
| 4554 | status = psa_cipher_update(&operation, |
| 4555 | input_arg->x, input_arg->len, |
| 4556 | output_multi, output_buffer_size, |
| 4557 | &function_output_length); |
| 4558 | if (status == PSA_SUCCESS) { |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4559 | output_length = function_output_length; |
| 4560 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4561 | status = psa_cipher_finish(&operation, |
| 4562 | output_multi + output_length, |
| 4563 | output_buffer_size - output_length, |
| 4564 | &function_output_length); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4565 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4566 | TEST_EQUAL(status, expected_status); |
| 4567 | } else { |
| 4568 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4569 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4570 | } else { |
| 4571 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4572 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4573 | } else { |
| 4574 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4575 | } |
| 4576 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4577 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4578 | psa_cipher_abort(&operation); |
| 4579 | mbedtls_free(input); |
| 4580 | mbedtls_free(output); |
| 4581 | mbedtls_free(output_multi); |
| 4582 | psa_destroy_key(key); |
| 4583 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4584 | } |
| 4585 | /* END_CASE */ |
| 4586 | |
| 4587 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4588 | void cipher_decrypt(int alg_arg, |
| 4589 | int key_type_arg, |
| 4590 | data_t *key_data, |
| 4591 | data_t *iv, |
| 4592 | data_t *input_arg, |
| 4593 | data_t *expected_output) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4594 | { |
| 4595 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4596 | psa_key_type_t key_type = key_type_arg; |
| 4597 | psa_algorithm_t alg = alg_arg; |
| 4598 | unsigned char *input = NULL; |
| 4599 | size_t input_buffer_size = 0; |
| 4600 | unsigned char *output = NULL; |
| 4601 | size_t output_buffer_size = 0; |
| 4602 | size_t output_length = 0; |
| 4603 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4604 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4605 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4606 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4607 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4608 | psa_set_key_algorithm(&attributes, alg); |
| 4609 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4610 | |
| 4611 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4612 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4613 | if (input_buffer_size > 0) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4614 | TEST_CALLOC(input, input_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4615 | memcpy(input, iv->x, iv->len); |
| 4616 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4617 | } |
| 4618 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4619 | 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] | 4620 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4621 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4622 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4623 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4624 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4625 | PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4626 | output_buffer_size, &output_length)); |
| 4627 | TEST_LE_U(output_length, |
| 4628 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size)); |
| 4629 | TEST_LE_U(output_length, |
| 4630 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4631 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4632 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4633 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4634 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4635 | mbedtls_free(input); |
| 4636 | mbedtls_free(output); |
| 4637 | psa_destroy_key(key); |
| 4638 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4639 | } |
| 4640 | /* END_CASE */ |
| 4641 | |
| 4642 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4643 | void cipher_verify_output(int alg_arg, |
| 4644 | int key_type_arg, |
| 4645 | data_t *key_data, |
| 4646 | data_t *input) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4647 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4648 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4649 | psa_key_type_t key_type = key_type_arg; |
| 4650 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4651 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4652 | size_t output1_size = 0; |
| 4653 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4654 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4655 | size_t output2_size = 0; |
| 4656 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4657 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4658 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4659 | PSA_ASSERT(psa_crypto_init()); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4660 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4661 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4662 | psa_set_key_algorithm(&attributes, alg); |
| 4663 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4664 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4665 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4666 | &key)); |
| 4667 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4668 | TEST_CALLOC(output1, output1_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4669 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4670 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, |
| 4671 | output1, output1_size, |
| 4672 | &output1_length)); |
| 4673 | TEST_LE_U(output1_length, |
| 4674 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4675 | TEST_LE_U(output1_length, |
| 4676 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4677 | |
| 4678 | output2_size = output1_length; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4679 | TEST_CALLOC(output2, output2_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4680 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4681 | PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length, |
| 4682 | output2, output2_size, |
| 4683 | &output2_length)); |
| 4684 | TEST_LE_U(output2_length, |
| 4685 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4686 | TEST_LE_U(output2_length, |
| 4687 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4688 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4689 | TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4690 | |
| 4691 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4692 | mbedtls_free(output1); |
| 4693 | mbedtls_free(output2); |
| 4694 | psa_destroy_key(key); |
| 4695 | PSA_DONE(); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4696 | } |
| 4697 | /* END_CASE */ |
| 4698 | |
| 4699 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4700 | void cipher_verify_output_multipart(int alg_arg, |
| 4701 | int key_type_arg, |
| 4702 | data_t *key_data, |
| 4703 | data_t *input, |
| 4704 | int first_part_size_arg) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4705 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4706 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4707 | psa_key_type_t key_type = key_type_arg; |
| 4708 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4709 | size_t first_part_size = first_part_size_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4710 | unsigned char iv[16] = { 0 }; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4711 | size_t iv_size = 16; |
| 4712 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4713 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4714 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4715 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4716 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4717 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4718 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4719 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4720 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 4721 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4722 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4723 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4724 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4725 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4726 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4727 | psa_set_key_algorithm(&attributes, alg); |
| 4728 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4729 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4730 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4731 | &key)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4732 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4733 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg)); |
| 4734 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4735 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4736 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 4737 | PSA_ASSERT(psa_cipher_generate_iv(&operation1, |
| 4738 | iv, iv_size, |
| 4739 | &iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4740 | } |
| 4741 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4742 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4743 | TEST_LE_U(output1_buffer_size, |
| 4744 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4745 | TEST_CALLOC(output1, output1_buffer_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4746 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4747 | TEST_LE_U(first_part_size, input->len); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4748 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4749 | PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size, |
| 4750 | output1, output1_buffer_size, |
| 4751 | &function_output_length)); |
| 4752 | TEST_LE_U(function_output_length, |
| 4753 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4754 | TEST_LE_U(function_output_length, |
| 4755 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4756 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4757 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4758 | PSA_ASSERT(psa_cipher_update(&operation1, |
| 4759 | input->x + first_part_size, |
| 4760 | input->len - first_part_size, |
| 4761 | output1, output1_buffer_size, |
| 4762 | &function_output_length)); |
| 4763 | TEST_LE_U(function_output_length, |
| 4764 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4765 | alg, |
| 4766 | input->len - first_part_size)); |
| 4767 | TEST_LE_U(function_output_length, |
| 4768 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4769 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4771 | PSA_ASSERT(psa_cipher_finish(&operation1, |
| 4772 | output1 + output1_length, |
| 4773 | output1_buffer_size - output1_length, |
| 4774 | &function_output_length)); |
| 4775 | TEST_LE_U(function_output_length, |
| 4776 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4777 | TEST_LE_U(function_output_length, |
| 4778 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4779 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4780 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4781 | PSA_ASSERT(psa_cipher_abort(&operation1)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4782 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4783 | output2_buffer_size = output1_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4784 | TEST_LE_U(output2_buffer_size, |
| 4785 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4786 | TEST_LE_U(output2_buffer_size, |
| 4787 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4788 | TEST_CALLOC(output2, output2_buffer_size); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4789 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4790 | if (iv_length > 0) { |
| 4791 | PSA_ASSERT(psa_cipher_set_iv(&operation2, |
| 4792 | iv, iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4793 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4794 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4795 | PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size, |
| 4796 | output2, output2_buffer_size, |
| 4797 | &function_output_length)); |
| 4798 | TEST_LE_U(function_output_length, |
| 4799 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4800 | TEST_LE_U(function_output_length, |
| 4801 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4802 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4803 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4804 | PSA_ASSERT(psa_cipher_update(&operation2, |
| 4805 | output1 + first_part_size, |
| 4806 | output1_length - first_part_size, |
| 4807 | output2, output2_buffer_size, |
| 4808 | &function_output_length)); |
| 4809 | TEST_LE_U(function_output_length, |
| 4810 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4811 | alg, |
| 4812 | output1_length - first_part_size)); |
| 4813 | TEST_LE_U(function_output_length, |
| 4814 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4815 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4816 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4817 | PSA_ASSERT(psa_cipher_finish(&operation2, |
| 4818 | output2 + output2_length, |
| 4819 | output2_buffer_size - output2_length, |
| 4820 | &function_output_length)); |
| 4821 | TEST_LE_U(function_output_length, |
| 4822 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4823 | TEST_LE_U(function_output_length, |
| 4824 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4825 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4826 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4827 | PSA_ASSERT(psa_cipher_abort(&operation2)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4828 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4829 | TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4830 | |
| 4831 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4832 | psa_cipher_abort(&operation1); |
| 4833 | psa_cipher_abort(&operation2); |
| 4834 | mbedtls_free(output1); |
| 4835 | mbedtls_free(output2); |
| 4836 | psa_destroy_key(key); |
| 4837 | PSA_DONE(); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4838 | } |
| 4839 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 4840 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4841 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4842 | void aead_encrypt_decrypt(int key_type_arg, data_t *key_data, |
| 4843 | int alg_arg, |
| 4844 | data_t *nonce, |
| 4845 | data_t *additional_data, |
| 4846 | data_t *input_data, |
| 4847 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4848 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4849 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4850 | psa_key_type_t key_type = key_type_arg; |
| 4851 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4852 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4853 | unsigned char *output_data = NULL; |
| 4854 | size_t output_size = 0; |
| 4855 | size_t output_length = 0; |
| 4856 | unsigned char *output_data2 = NULL; |
| 4857 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4858 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4859 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4860 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4861 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4862 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4863 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4864 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4865 | psa_set_key_algorithm(&attributes, alg); |
| 4866 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4867 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4868 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4869 | &key)); |
| 4870 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4871 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4872 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4873 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4874 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4875 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4876 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4877 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4878 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
| 4879 | TEST_EQUAL(output_size, |
| 4880 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4881 | TEST_LE_U(output_size, |
| 4882 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4883 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4884 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4885 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4886 | status = psa_aead_encrypt(key, alg, |
| 4887 | nonce->x, nonce->len, |
| 4888 | additional_data->x, |
| 4889 | additional_data->len, |
| 4890 | input_data->x, input_data->len, |
| 4891 | output_data, output_size, |
| 4892 | &output_length); |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4893 | |
| 4894 | /* If the operation is not supported, just skip and not fail in case the |
| 4895 | * encryption involves a common limitation of cryptography hardwares and |
| 4896 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4897 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4898 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4899 | 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] | 4900 | } |
| 4901 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4902 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4903 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4904 | if (PSA_SUCCESS == expected_result) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4905 | TEST_CALLOC(output_data2, output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4906 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4907 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4908 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4909 | TEST_EQUAL(input_data->len, |
| 4910 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length)); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4911 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4912 | TEST_LE_U(input_data->len, |
| 4913 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length)); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4914 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4915 | TEST_EQUAL(psa_aead_decrypt(key, alg, |
| 4916 | nonce->x, nonce->len, |
| 4917 | additional_data->x, |
| 4918 | additional_data->len, |
| 4919 | output_data, output_length, |
| 4920 | output_data2, output_length, |
| 4921 | &output_length2), |
| 4922 | expected_result); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4923 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4924 | TEST_MEMORY_COMPARE(input_data->x, input_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4925 | output_data2, output_length2); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4926 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4927 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4928 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4929 | psa_destroy_key(key); |
| 4930 | mbedtls_free(output_data); |
| 4931 | mbedtls_free(output_data2); |
| 4932 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4933 | } |
| 4934 | /* END_CASE */ |
| 4935 | |
| 4936 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4937 | void aead_encrypt(int key_type_arg, data_t *key_data, |
| 4938 | int alg_arg, |
| 4939 | data_t *nonce, |
| 4940 | data_t *additional_data, |
| 4941 | data_t *input_data, |
| 4942 | data_t *expected_result) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4943 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4944 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4945 | psa_key_type_t key_type = key_type_arg; |
| 4946 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4947 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4948 | unsigned char *output_data = NULL; |
| 4949 | size_t output_size = 0; |
| 4950 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4951 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4952 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4953 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4954 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4955 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4956 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4957 | psa_set_key_algorithm(&attributes, alg); |
| 4958 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4959 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4960 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4961 | &key)); |
| 4962 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4963 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4964 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4965 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4966 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4967 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4968 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4969 | TEST_EQUAL(output_size, |
| 4970 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4971 | TEST_LE_U(output_size, |
| 4972 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4973 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4974 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4975 | status = psa_aead_encrypt(key, alg, |
| 4976 | nonce->x, nonce->len, |
| 4977 | additional_data->x, additional_data->len, |
| 4978 | input_data->x, input_data->len, |
| 4979 | output_data, output_size, |
| 4980 | &output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4981 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4982 | /* If the operation is not supported, just skip and not fail in case the |
| 4983 | * encryption involves a common limitation of cryptography hardwares and |
| 4984 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4985 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4986 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4987 | 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] | 4988 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4989 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4990 | PSA_ASSERT(status); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4991 | TEST_MEMORY_COMPARE(expected_result->x, expected_result->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4992 | output_data, output_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4993 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4994 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4995 | psa_destroy_key(key); |
| 4996 | mbedtls_free(output_data); |
| 4997 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4998 | } |
| 4999 | /* END_CASE */ |
| 5000 | |
| 5001 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5002 | void aead_decrypt(int key_type_arg, data_t *key_data, |
| 5003 | int alg_arg, |
| 5004 | data_t *nonce, |
| 5005 | data_t *additional_data, |
| 5006 | data_t *input_data, |
| 5007 | data_t *expected_data, |
| 5008 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5009 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5010 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5011 | psa_key_type_t key_type = key_type_arg; |
| 5012 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5013 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5014 | unsigned char *output_data = NULL; |
| 5015 | size_t output_size = 0; |
| 5016 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5017 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 5018 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 5019 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5020 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5021 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5022 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5023 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 5024 | psa_set_key_algorithm(&attributes, alg); |
| 5025 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5026 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5027 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5028 | &key)); |
| 5029 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5030 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5031 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5032 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 5033 | alg); |
| 5034 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 5035 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5036 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 5037 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5038 | TEST_EQUAL(output_size, |
| 5039 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 5040 | TEST_LE_U(output_size, |
| 5041 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5042 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5043 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5044 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5045 | status = psa_aead_decrypt(key, alg, |
| 5046 | nonce->x, nonce->len, |
| 5047 | additional_data->x, |
| 5048 | additional_data->len, |
| 5049 | input_data->x, input_data->len, |
| 5050 | output_data, output_size, |
| 5051 | &output_length); |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 5052 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 5053 | /* If the operation is not supported, just skip and not fail in case the |
| 5054 | * decryption involves a common limitation of cryptography hardwares and |
| 5055 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5056 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5057 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5058 | 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] | 5059 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 5060 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5061 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5062 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5063 | if (expected_result == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 5064 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 5065 | output_data, output_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5066 | } |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5067 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5068 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5069 | psa_destroy_key(key); |
| 5070 | mbedtls_free(output_data); |
| 5071 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5072 | } |
| 5073 | /* END_CASE */ |
| 5074 | |
| 5075 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5076 | void aead_multipart_encrypt(int key_type_arg, data_t *key_data, |
| 5077 | int alg_arg, |
| 5078 | data_t *nonce, |
| 5079 | data_t *additional_data, |
| 5080 | data_t *input_data, |
| 5081 | int do_set_lengths, |
| 5082 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5083 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5084 | size_t ad_part_len = 0; |
| 5085 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5086 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5087 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5088 | for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) { |
| 5089 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5090 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5091 | if (do_set_lengths) { |
| 5092 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5093 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5094 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5095 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5096 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5097 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5098 | |
| 5099 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5100 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5101 | alg_arg, nonce, |
| 5102 | additional_data, |
| 5103 | ad_part_len, |
| 5104 | input_data, -1, |
| 5105 | set_lengths_method, |
| 5106 | expected_output, |
| 5107 | 1, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5108 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5109 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5110 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5111 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 5112 | mbedtls_test_set_step(1000 + ad_part_len); |
| 5113 | |
| 5114 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5115 | alg_arg, nonce, |
| 5116 | additional_data, |
| 5117 | ad_part_len, |
| 5118 | input_data, -1, |
| 5119 | set_lengths_method, |
| 5120 | expected_output, |
| 5121 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5122 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5123 | } |
| 5124 | } |
| 5125 | |
| 5126 | for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) { |
| 5127 | /* Split data into length(data_part_len) parts. */ |
| 5128 | mbedtls_test_set_step(2000 + data_part_len); |
| 5129 | |
| 5130 | if (do_set_lengths) { |
| 5131 | if (data_part_len & 0x01) { |
| 5132 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 5133 | } else { |
| 5134 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
| 5135 | } |
| 5136 | } |
| 5137 | |
| 5138 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5139 | alg_arg, nonce, |
| 5140 | additional_data, -1, |
| 5141 | input_data, data_part_len, |
| 5142 | set_lengths_method, |
| 5143 | expected_output, |
| 5144 | 1, 0)) { |
| 5145 | break; |
| 5146 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5147 | |
| 5148 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5149 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5151 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5152 | alg_arg, nonce, |
| 5153 | additional_data, -1, |
| 5154 | input_data, data_part_len, |
| 5155 | set_lengths_method, |
| 5156 | expected_output, |
| 5157 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5158 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5159 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5160 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5161 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5162 | /* Goto is required to silence warnings about unused labels, as we |
| 5163 | * don't actually do any test assertions in this function. */ |
| 5164 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5165 | } |
| 5166 | /* END_CASE */ |
| 5167 | |
| 5168 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5169 | void aead_multipart_decrypt(int key_type_arg, data_t *key_data, |
| 5170 | int alg_arg, |
| 5171 | data_t *nonce, |
| 5172 | data_t *additional_data, |
| 5173 | data_t *input_data, |
| 5174 | int do_set_lengths, |
| 5175 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5176 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5177 | size_t ad_part_len = 0; |
| 5178 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5179 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5181 | 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] | 5182 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5183 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5184 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5185 | if (do_set_lengths) { |
| 5186 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5187 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5188 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5189 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5190 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5191 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5192 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5193 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5194 | alg_arg, nonce, |
| 5195 | additional_data, |
| 5196 | ad_part_len, |
| 5197 | input_data, -1, |
| 5198 | set_lengths_method, |
| 5199 | expected_output, |
| 5200 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5201 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5202 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5203 | |
| 5204 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5205 | mbedtls_test_set_step(1000 + ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5206 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5207 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5208 | alg_arg, nonce, |
| 5209 | additional_data, |
| 5210 | ad_part_len, |
| 5211 | input_data, -1, |
| 5212 | set_lengths_method, |
| 5213 | expected_output, |
| 5214 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5215 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5216 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5217 | } |
| 5218 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5219 | 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] | 5220 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5221 | mbedtls_test_set_step(2000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5222 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5223 | if (do_set_lengths) { |
| 5224 | if (data_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5225 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5226 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5227 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5228 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5229 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5230 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5231 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5232 | alg_arg, nonce, |
| 5233 | additional_data, -1, |
| 5234 | input_data, data_part_len, |
| 5235 | set_lengths_method, |
| 5236 | expected_output, |
| 5237 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5238 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5239 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5240 | |
| 5241 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5242 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5243 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5244 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5245 | alg_arg, nonce, |
| 5246 | additional_data, -1, |
| 5247 | input_data, data_part_len, |
| 5248 | set_lengths_method, |
| 5249 | expected_output, |
| 5250 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5251 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5252 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5253 | } |
| 5254 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5255 | /* Goto is required to silence warnings about unused labels, as we |
| 5256 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5257 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5258 | } |
| 5259 | /* END_CASE */ |
| 5260 | |
| 5261 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5262 | void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data, |
| 5263 | int alg_arg, |
| 5264 | int nonce_length, |
| 5265 | int expected_nonce_length_arg, |
| 5266 | data_t *additional_data, |
| 5267 | data_t *input_data, |
| 5268 | int expected_status_arg) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5269 | { |
| 5270 | |
| 5271 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5272 | psa_key_type_t key_type = key_type_arg; |
| 5273 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5274 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5275 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5276 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5277 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5278 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5279 | size_t actual_nonce_length = 0; |
| 5280 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 5281 | unsigned char *output = NULL; |
| 5282 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5283 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5284 | size_t ciphertext_size = 0; |
| 5285 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5286 | size_t tag_length = 0; |
| 5287 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5288 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5289 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5290 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5291 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5292 | psa_set_key_algorithm(&attributes, alg); |
| 5293 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5294 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5295 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5296 | &key)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5297 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5298 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5299 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5300 | 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] | 5301 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5302 | TEST_CALLOC(output, output_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5303 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5304 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5306 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5307 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5308 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5309 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5310 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5311 | |
| 5312 | /* If the operation is not supported, just skip and not fail in case the |
| 5313 | * encryption involves a common limitation of cryptography hardwares and |
| 5314 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5315 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5316 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5317 | 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] | 5318 | } |
| 5319 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5320 | PSA_ASSERT(status); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5321 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5322 | status = psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5323 | nonce_length, |
| 5324 | &actual_nonce_length); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5325 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5326 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5327 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5328 | TEST_EQUAL(actual_nonce_length, expected_nonce_length); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 5329 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5330 | if (expected_status == PSA_SUCCESS) { |
| 5331 | TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type, |
| 5332 | alg)); |
| 5333 | } |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 5334 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5335 | TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 5336 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5337 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5338 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5339 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5340 | input_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5341 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5342 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5343 | additional_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5344 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5345 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5346 | output, output_size, |
| 5347 | &ciphertext_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5349 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5350 | &ciphertext_length, tag_buffer, |
| 5351 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5352 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5353 | |
| 5354 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5355 | psa_destroy_key(key); |
| 5356 | mbedtls_free(output); |
| 5357 | mbedtls_free(ciphertext); |
| 5358 | psa_aead_abort(&operation); |
| 5359 | PSA_DONE(); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5360 | } |
| 5361 | /* END_CASE */ |
| 5362 | |
| 5363 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5364 | void aead_multipart_set_nonce(int key_type_arg, data_t *key_data, |
| 5365 | int alg_arg, |
| 5366 | int nonce_length_arg, |
| 5367 | int set_lengths_method_arg, |
| 5368 | data_t *additional_data, |
| 5369 | data_t *input_data, |
| 5370 | int expected_status_arg) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5371 | { |
| 5372 | |
| 5373 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5374 | psa_key_type_t key_type = key_type_arg; |
| 5375 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5376 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5377 | uint8_t *nonce_buffer = NULL; |
| 5378 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5379 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5380 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5381 | unsigned char *output = NULL; |
| 5382 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5383 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5384 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5385 | size_t ciphertext_size = 0; |
| 5386 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5387 | size_t tag_length = 0; |
| 5388 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5389 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5390 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5392 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5393 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5394 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5395 | psa_set_key_algorithm(&attributes, alg); |
| 5396 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5397 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5398 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5399 | &key)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5400 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5401 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5402 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5403 | 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] | 5404 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5405 | TEST_CALLOC(output, output_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5406 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5407 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5408 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5409 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5410 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5411 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5412 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5413 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5414 | |
| 5415 | /* If the operation is not supported, just skip and not fail in case the |
| 5416 | * encryption involves a common limitation of cryptography hardwares and |
| 5417 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5418 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5419 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5420 | 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] | 5421 | } |
| 5422 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5423 | PSA_ASSERT(status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5424 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5425 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5426 | if (nonce_length_arg == -1) { |
| 5427 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5428 | TEST_CALLOC(nonce_buffer, 4); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5429 | nonce_length = 0; |
| 5430 | } else { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5431 | /* If length is zero, then this will return NULL. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5432 | nonce_length = (size_t) nonce_length_arg; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5433 | TEST_CALLOC(nonce_buffer, nonce_length); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5434 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5435 | if (nonce_buffer) { |
| 5436 | for (index = 0; index < nonce_length - 1; ++index) { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5437 | nonce_buffer[index] = 'a' + index; |
| 5438 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5439 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5440 | } |
| 5441 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5442 | if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { |
| 5443 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5444 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5445 | } |
| 5446 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5447 | status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5449 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5450 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5451 | if (expected_status == PSA_SUCCESS) { |
| 5452 | if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) { |
| 5453 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5454 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5455 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5456 | 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] | 5457 | expected_status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5458 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5459 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5460 | /* 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] | 5461 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5462 | additional_data->len), |
| 5463 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5464 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5465 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5466 | output, output_size, |
| 5467 | &ciphertext_length), |
| 5468 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5469 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5470 | TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5471 | &ciphertext_length, tag_buffer, |
| 5472 | PSA_AEAD_TAG_MAX_SIZE, &tag_length), |
| 5473 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5474 | } |
| 5475 | |
| 5476 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5477 | psa_destroy_key(key); |
| 5478 | mbedtls_free(output); |
| 5479 | mbedtls_free(ciphertext); |
| 5480 | mbedtls_free(nonce_buffer); |
| 5481 | psa_aead_abort(&operation); |
| 5482 | PSA_DONE(); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5483 | } |
| 5484 | /* END_CASE */ |
| 5485 | |
| 5486 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5487 | 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] | 5488 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5489 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5490 | data_t *nonce, |
| 5491 | data_t *additional_data, |
| 5492 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5493 | int expected_status_arg) |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5494 | { |
| 5495 | |
| 5496 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5497 | psa_key_type_t key_type = key_type_arg; |
| 5498 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5499 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5500 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5501 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5502 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5503 | unsigned char *output = NULL; |
| 5504 | unsigned char *ciphertext = NULL; |
| 5505 | size_t output_size = output_size_arg; |
| 5506 | size_t ciphertext_size = 0; |
| 5507 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5508 | size_t tag_length = 0; |
| 5509 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5510 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5511 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5512 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5513 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5514 | psa_set_key_algorithm(&attributes, alg); |
| 5515 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5516 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5517 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5518 | &key)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5519 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5520 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5521 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5522 | TEST_CALLOC(output, output_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5523 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5524 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5525 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5526 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5527 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5528 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5529 | |
| 5530 | /* If the operation is not supported, just skip and not fail in case the |
| 5531 | * encryption involves a common limitation of cryptography hardwares and |
| 5532 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5533 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5534 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5535 | 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] | 5536 | } |
| 5537 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5538 | PSA_ASSERT(status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5539 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5540 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5541 | input_data->len)); |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5542 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5543 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5544 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5545 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5546 | additional_data->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5547 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5548 | status = psa_aead_update(&operation, input_data->x, input_data->len, |
| 5549 | output, output_size, &ciphertext_length); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5550 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5551 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5552 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5553 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5554 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5555 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5556 | &ciphertext_length, tag_buffer, |
| 5557 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5558 | } |
| 5559 | |
| 5560 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5561 | psa_destroy_key(key); |
| 5562 | mbedtls_free(output); |
| 5563 | mbedtls_free(ciphertext); |
| 5564 | psa_aead_abort(&operation); |
| 5565 | PSA_DONE(); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5566 | } |
| 5567 | /* END_CASE */ |
| 5568 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5569 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5570 | void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data, |
| 5571 | int alg_arg, |
| 5572 | int finish_ciphertext_size_arg, |
| 5573 | int tag_size_arg, |
| 5574 | data_t *nonce, |
| 5575 | data_t *additional_data, |
| 5576 | data_t *input_data, |
| 5577 | int expected_status_arg) |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5578 | { |
| 5579 | |
| 5580 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5581 | psa_key_type_t key_type = key_type_arg; |
| 5582 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5583 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5584 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5585 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5586 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5587 | unsigned char *ciphertext = NULL; |
| 5588 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5589 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5590 | size_t ciphertext_size = 0; |
| 5591 | size_t ciphertext_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5592 | size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg; |
| 5593 | size_t tag_size = (size_t) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5594 | size_t tag_length = 0; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5595 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5596 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5597 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5598 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5599 | psa_set_key_algorithm(&attributes, alg); |
| 5600 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5601 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5602 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5603 | &key)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5604 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5605 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5606 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5607 | 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] | 5608 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5609 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5610 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5611 | TEST_CALLOC(finish_ciphertext, finish_ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5612 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5613 | TEST_CALLOC(tag_buffer, tag_size); |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5614 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5615 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5616 | |
| 5617 | /* If the operation is not supported, just skip and not fail in case the |
| 5618 | * encryption involves a common limitation of cryptography hardwares and |
| 5619 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5620 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5621 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5622 | 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] | 5623 | } |
| 5624 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5625 | PSA_ASSERT(status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5626 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5627 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5628 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5629 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5630 | input_data->len)); |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5631 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5632 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5633 | additional_data->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5634 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5635 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5636 | ciphertext, ciphertext_size, &ciphertext_length)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5637 | |
| 5638 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5639 | status = psa_aead_finish(&operation, finish_ciphertext, |
| 5640 | finish_ciphertext_size, |
| 5641 | &ciphertext_length, tag_buffer, |
| 5642 | tag_size, &tag_length); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5643 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5644 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5645 | |
| 5646 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5647 | psa_destroy_key(key); |
| 5648 | mbedtls_free(ciphertext); |
| 5649 | mbedtls_free(finish_ciphertext); |
| 5650 | mbedtls_free(tag_buffer); |
| 5651 | psa_aead_abort(&operation); |
| 5652 | PSA_DONE(); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5653 | } |
| 5654 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5655 | |
| 5656 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5657 | void aead_multipart_verify(int key_type_arg, data_t *key_data, |
| 5658 | int alg_arg, |
| 5659 | data_t *nonce, |
| 5660 | data_t *additional_data, |
| 5661 | data_t *input_data, |
| 5662 | data_t *tag, |
| 5663 | int tag_usage_arg, |
| 5664 | int expected_setup_status_arg, |
| 5665 | int expected_status_arg) |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5666 | { |
| 5667 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5668 | psa_key_type_t key_type = key_type_arg; |
| 5669 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5670 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5671 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5672 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5673 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5674 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5675 | unsigned char *plaintext = NULL; |
| 5676 | unsigned char *finish_plaintext = NULL; |
| 5677 | size_t plaintext_size = 0; |
| 5678 | size_t plaintext_length = 0; |
| 5679 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5680 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5681 | unsigned char *tag_buffer = NULL; |
| 5682 | size_t tag_size = 0; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5683 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5684 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5685 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5686 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 5687 | psa_set_key_algorithm(&attributes, alg); |
| 5688 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5689 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5690 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5691 | &key)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5692 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5693 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5694 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5695 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 5696 | input_data->len); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5697 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5698 | TEST_CALLOC(plaintext, plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5699 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5700 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5701 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5702 | TEST_CALLOC(finish_plaintext, verify_plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5703 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5704 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5705 | |
| 5706 | /* If the operation is not supported, just skip and not fail in case the |
| 5707 | * encryption involves a common limitation of cryptography hardwares and |
| 5708 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5709 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5710 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5711 | 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] | 5712 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5713 | TEST_EQUAL(status, expected_setup_status); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5714 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5715 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5716 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5717 | } |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5718 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5719 | PSA_ASSERT(status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5720 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5721 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5722 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5723 | status = psa_aead_set_lengths(&operation, additional_data->len, |
| 5724 | input_data->len); |
| 5725 | PSA_ASSERT(status); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5726 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5727 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5728 | additional_data->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5729 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5730 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 5731 | input_data->len, |
| 5732 | plaintext, plaintext_size, |
| 5733 | &plaintext_length)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5734 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5735 | if (tag_usage == USE_GIVEN_TAG) { |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5736 | tag_buffer = tag->x; |
| 5737 | tag_size = tag->len; |
| 5738 | } |
| 5739 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5740 | status = psa_aead_verify(&operation, finish_plaintext, |
| 5741 | verify_plaintext_size, |
| 5742 | &plaintext_length, |
| 5743 | tag_buffer, tag_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5744 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5745 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5746 | |
| 5747 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5748 | psa_destroy_key(key); |
| 5749 | mbedtls_free(plaintext); |
| 5750 | mbedtls_free(finish_plaintext); |
| 5751 | psa_aead_abort(&operation); |
| 5752 | PSA_DONE(); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5753 | } |
| 5754 | /* END_CASE */ |
| 5755 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5756 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5757 | void aead_multipart_setup(int key_type_arg, data_t *key_data, |
| 5758 | int alg_arg, int expected_status_arg) |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5759 | { |
| 5760 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5761 | psa_key_type_t key_type = key_type_arg; |
| 5762 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5763 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5764 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5765 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5766 | psa_status_t expected_status = expected_status_arg; |
| 5767 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5768 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5769 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5770 | psa_set_key_usage_flags(&attributes, |
| 5771 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5772 | psa_set_key_algorithm(&attributes, alg); |
| 5773 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5774 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5775 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5776 | &key)); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5777 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5778 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5779 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5780 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5781 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5782 | psa_aead_abort(&operation); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5783 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5784 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5785 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5786 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5787 | |
| 5788 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5789 | psa_destroy_key(key); |
| 5790 | psa_aead_abort(&operation); |
| 5791 | PSA_DONE(); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5792 | } |
| 5793 | /* END_CASE */ |
| 5794 | |
| 5795 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5796 | void aead_multipart_state_test(int key_type_arg, data_t *key_data, |
| 5797 | int alg_arg, |
| 5798 | data_t *nonce, |
| 5799 | data_t *additional_data, |
| 5800 | data_t *input_data) |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5801 | { |
| 5802 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5803 | psa_key_type_t key_type = key_type_arg; |
| 5804 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5805 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5806 | unsigned char *output_data = NULL; |
| 5807 | unsigned char *final_data = NULL; |
| 5808 | size_t output_size = 0; |
| 5809 | size_t finish_output_size = 0; |
| 5810 | size_t output_length = 0; |
| 5811 | size_t key_bits = 0; |
| 5812 | size_t tag_length = 0; |
| 5813 | size_t tag_size = 0; |
| 5814 | size_t nonce_length = 0; |
| 5815 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5816 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5817 | size_t output_part_length = 0; |
| 5818 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5819 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5820 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5821 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5822 | psa_set_key_usage_flags(&attributes, |
| 5823 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5824 | psa_set_key_algorithm(&attributes, alg); |
| 5825 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5826 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5827 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5828 | &key)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5829 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5830 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5831 | key_bits = psa_get_key_bits(&attributes); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5832 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5833 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5834 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5835 | TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5836 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5837 | 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] | 5838 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5839 | TEST_CALLOC(output_data, output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5840 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5841 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5842 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5843 | TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5844 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5845 | TEST_CALLOC(final_data, finish_output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5846 | |
| 5847 | /* Test all operations error without calling setup first. */ |
| 5848 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5849 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5850 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5851 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5852 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5853 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5854 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5855 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5856 | &nonce_length), |
| 5857 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5858 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5859 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5860 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5861 | /* ------------------------------------------------------- */ |
| 5862 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5863 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 5864 | input_data->len), |
| 5865 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5866 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5867 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5868 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5869 | /* ------------------------------------------------------- */ |
| 5870 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5871 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5872 | additional_data->len), |
| 5873 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5874 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5875 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5876 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5877 | /* ------------------------------------------------------- */ |
| 5878 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5879 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 5880 | input_data->len, output_data, |
| 5881 | output_size, &output_length), |
| 5882 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5883 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5884 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5885 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5886 | /* ------------------------------------------------------- */ |
| 5887 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5888 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 5889 | finish_output_size, |
| 5890 | &output_part_length, |
| 5891 | tag_buffer, tag_length, |
| 5892 | &tag_size), |
| 5893 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5894 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5895 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5896 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5897 | /* ------------------------------------------------------- */ |
| 5898 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5899 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 5900 | finish_output_size, |
| 5901 | &output_part_length, |
| 5902 | tag_buffer, |
| 5903 | tag_length), |
| 5904 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5905 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5906 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5907 | |
| 5908 | /* Test for double setups. */ |
| 5909 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5910 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5911 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5912 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 5913 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5914 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5915 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5916 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5917 | /* ------------------------------------------------------- */ |
| 5918 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5919 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5920 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5921 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 5922 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5923 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5924 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5925 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5926 | /* ------------------------------------------------------- */ |
| 5927 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5928 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5929 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5930 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 5931 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5932 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5933 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5934 | |
| 5935 | /* ------------------------------------------------------- */ |
| 5936 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5937 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5938 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5939 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 5940 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5941 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5942 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5943 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5944 | /* Test for not setting a nonce. */ |
| 5945 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5946 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5947 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5948 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5949 | additional_data->len), |
| 5950 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5951 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5952 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5953 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5954 | /* ------------------------------------------------------- */ |
| 5955 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5956 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5957 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5958 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 5959 | input_data->len, output_data, |
| 5960 | output_size, &output_length), |
| 5961 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5962 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5963 | psa_aead_abort(&operation); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5964 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5965 | /* ------------------------------------------------------- */ |
| 5966 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5967 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5968 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5969 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 5970 | finish_output_size, |
| 5971 | &output_part_length, |
| 5972 | tag_buffer, tag_length, |
| 5973 | &tag_size), |
| 5974 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5975 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5976 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5977 | |
| 5978 | /* ------------------------------------------------------- */ |
| 5979 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5980 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5981 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5982 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 5983 | finish_output_size, |
| 5984 | &output_part_length, |
| 5985 | tag_buffer, |
| 5986 | tag_length), |
| 5987 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5988 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5989 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5990 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5991 | /* Test for double setting nonce. */ |
| 5992 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5993 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5994 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5995 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5996 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5997 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5998 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5999 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6000 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6001 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6002 | /* Test for double generating nonce. */ |
| 6003 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6004 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6005 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6006 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6007 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6008 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6009 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6010 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6011 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6012 | &nonce_length), |
| 6013 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6014 | |
| 6015 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6016 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6017 | |
| 6018 | /* Test for generate nonce then set and vice versa */ |
| 6019 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6020 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6021 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6022 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6023 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6024 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6025 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6026 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6027 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6028 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6029 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6030 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6031 | /* Test for generating nonce after calling set lengths */ |
| 6032 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6033 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6035 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6036 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6037 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6038 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6039 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6040 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6041 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6042 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6043 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6044 | /* 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] | 6045 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6046 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6047 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6048 | if (operation.alg == PSA_ALG_CCM) { |
| 6049 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6050 | input_data->len), |
| 6051 | PSA_ERROR_INVALID_ARGUMENT); |
| 6052 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6053 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6054 | &nonce_length), |
| 6055 | PSA_ERROR_BAD_STATE); |
| 6056 | } else { |
| 6057 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6058 | input_data->len)); |
| 6059 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6060 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6061 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6062 | } |
| 6063 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6064 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6065 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6066 | /* 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] | 6067 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6068 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6069 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6070 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 6071 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 6072 | input_data->len), |
| 6073 | PSA_ERROR_INVALID_ARGUMENT); |
| 6074 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6075 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6076 | &nonce_length), |
| 6077 | PSA_ERROR_BAD_STATE); |
| 6078 | } else { |
| 6079 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 6080 | input_data->len)); |
| 6081 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6082 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6083 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6084 | } |
| 6085 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6086 | psa_aead_abort(&operation); |
Dave Rodgman | d26d744 | 2023-02-11 17:14:54 +0000 | [diff] [blame] | 6087 | #endif |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6088 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6089 | /* 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] | 6090 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6091 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6092 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6093 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6094 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6095 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6096 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6097 | if (operation.alg == PSA_ALG_CCM) { |
| 6098 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6099 | input_data->len), |
| 6100 | PSA_ERROR_INVALID_ARGUMENT); |
| 6101 | } else { |
| 6102 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6103 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6104 | } |
| 6105 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6106 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6107 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6108 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6109 | /* Test for setting nonce after calling set lengths */ |
| 6110 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6111 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6112 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6113 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6114 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6115 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6116 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6117 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6118 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6119 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6120 | /* 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] | 6121 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6122 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6123 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6124 | if (operation.alg == PSA_ALG_CCM) { |
| 6125 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6126 | input_data->len), |
| 6127 | PSA_ERROR_INVALID_ARGUMENT); |
| 6128 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6129 | PSA_ERROR_BAD_STATE); |
| 6130 | } else { |
| 6131 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6132 | input_data->len)); |
| 6133 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6134 | } |
| 6135 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6136 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6137 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6138 | /* 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] | 6139 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6140 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6141 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6142 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 6143 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 6144 | input_data->len), |
| 6145 | PSA_ERROR_INVALID_ARGUMENT); |
| 6146 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6147 | PSA_ERROR_BAD_STATE); |
| 6148 | } else { |
| 6149 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 6150 | input_data->len)); |
| 6151 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6152 | } |
| 6153 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6154 | psa_aead_abort(&operation); |
Dave Rodgman | a476363 | 2023-02-11 18:36:23 +0000 | [diff] [blame] | 6155 | #endif |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6156 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6157 | /* 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] | 6158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6159 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6160 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6161 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6162 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6163 | if (operation.alg == PSA_ALG_CCM) { |
| 6164 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6165 | input_data->len), |
| 6166 | PSA_ERROR_INVALID_ARGUMENT); |
| 6167 | } else { |
| 6168 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6169 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6170 | } |
| 6171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6172 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6173 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6174 | /* 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] | 6175 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6176 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6177 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6178 | if (operation.alg == PSA_ALG_GCM) { |
| 6179 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6180 | SIZE_MAX), |
| 6181 | PSA_ERROR_INVALID_ARGUMENT); |
| 6182 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6183 | PSA_ERROR_BAD_STATE); |
| 6184 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6185 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6186 | SIZE_MAX)); |
| 6187 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6188 | } |
| 6189 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6190 | psa_aead_abort(&operation); |
Dave Rodgman | 91e8321 | 2023-02-11 20:07:43 +0000 | [diff] [blame] | 6191 | #endif |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6192 | |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 6193 | /* 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] | 6194 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6195 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6196 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6197 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6198 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6199 | if (operation.alg == PSA_ALG_GCM) { |
| 6200 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6201 | SIZE_MAX), |
| 6202 | PSA_ERROR_INVALID_ARGUMENT); |
| 6203 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6204 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6205 | SIZE_MAX)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6206 | } |
| 6207 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6208 | psa_aead_abort(&operation); |
Dave Rodgman | 641288b | 2023-02-11 22:02:04 +0000 | [diff] [blame] | 6209 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6210 | |
| 6211 | /* ------------------------------------------------------- */ |
| 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_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6216 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6217 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6218 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6219 | &nonce_length), |
| 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 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6224 | /* Test for generating nonce in decrypt setup. */ |
| 6225 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6226 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6227 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6228 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6229 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6230 | &nonce_length), |
| 6231 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6232 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6233 | psa_aead_abort(&operation); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6234 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6235 | /* Test for setting lengths twice. */ |
| 6236 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6237 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6238 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6239 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6240 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6241 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6242 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6243 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6244 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6245 | input_data->len), |
| 6246 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6247 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6248 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6249 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6250 | /* Test for setting lengths after setting nonce + already starting data. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6252 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6253 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6254 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6256 | if (operation.alg == PSA_ALG_CCM) { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6257 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6258 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6259 | additional_data->len), |
| 6260 | PSA_ERROR_BAD_STATE); |
| 6261 | } else { |
| 6262 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6263 | additional_data->len)); |
| 6264 | |
| 6265 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6266 | input_data->len), |
| 6267 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6268 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6269 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6270 | |
| 6271 | /* ------------------------------------------------------- */ |
| 6272 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6273 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6274 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6275 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6276 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6277 | if (operation.alg == PSA_ALG_CCM) { |
| 6278 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6279 | input_data->len, output_data, |
| 6280 | output_size, &output_length), |
| 6281 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6282 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6283 | } else { |
| 6284 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6285 | input_data->len, output_data, |
| 6286 | output_size, &output_length)); |
| 6287 | |
| 6288 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6289 | input_data->len), |
| 6290 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6291 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6292 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6293 | |
| 6294 | /* ------------------------------------------------------- */ |
| 6295 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6296 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6297 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6298 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6299 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6300 | if (operation.alg == PSA_ALG_CCM) { |
| 6301 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6302 | finish_output_size, |
| 6303 | &output_part_length, |
| 6304 | tag_buffer, tag_length, |
| 6305 | &tag_size)); |
| 6306 | } else { |
| 6307 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6308 | finish_output_size, |
| 6309 | &output_part_length, |
| 6310 | tag_buffer, tag_length, |
| 6311 | &tag_size)); |
| 6312 | |
| 6313 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6314 | input_data->len), |
| 6315 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6316 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6317 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6318 | |
| 6319 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 6320 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6321 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6322 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6323 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6324 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6325 | &nonce_length)); |
| 6326 | if (operation.alg == PSA_ALG_CCM) { |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6327 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6328 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6329 | additional_data->len), |
| 6330 | PSA_ERROR_BAD_STATE); |
| 6331 | } else { |
| 6332 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6333 | additional_data->len)); |
| 6334 | |
| 6335 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6336 | input_data->len), |
| 6337 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6338 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6339 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6340 | |
| 6341 | /* ------------------------------------------------------- */ |
| 6342 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6343 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6344 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6345 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6346 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6347 | &nonce_length)); |
| 6348 | if (operation.alg == PSA_ALG_CCM) { |
| 6349 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6350 | input_data->len, output_data, |
| 6351 | output_size, &output_length), |
| 6352 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6354 | } else { |
| 6355 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6356 | input_data->len, output_data, |
| 6357 | output_size, &output_length)); |
| 6358 | |
| 6359 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6360 | input_data->len), |
| 6361 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6362 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6363 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6364 | |
| 6365 | /* ------------------------------------------------------- */ |
| 6366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6367 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6368 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6369 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6370 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6371 | &nonce_length)); |
| 6372 | if (operation.alg == PSA_ALG_CCM) { |
| 6373 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6374 | finish_output_size, |
| 6375 | &output_part_length, |
| 6376 | tag_buffer, tag_length, |
| 6377 | &tag_size)); |
| 6378 | } else { |
| 6379 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6380 | finish_output_size, |
| 6381 | &output_part_length, |
| 6382 | tag_buffer, tag_length, |
| 6383 | &tag_size)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6385 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6386 | input_data->len), |
| 6387 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6388 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6389 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6390 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6391 | /* Test for not sending any additional data or data after setting non zero |
| 6392 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6393 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6394 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6395 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6396 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6397 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6398 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6399 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6400 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6401 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6402 | finish_output_size, |
| 6403 | &output_part_length, |
| 6404 | tag_buffer, tag_length, |
| 6405 | &tag_size), |
| 6406 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6407 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6408 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6409 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6410 | /* Test for not sending any additional data or data after setting non-zero |
| 6411 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6412 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6413 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6414 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6415 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6416 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6417 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6418 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6419 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6420 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6421 | finish_output_size, |
| 6422 | &output_part_length, |
| 6423 | tag_buffer, |
| 6424 | tag_length), |
| 6425 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6426 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6427 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6428 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6429 | /* Test for not sending any additional data after setting a non-zero length |
| 6430 | * for it. */ |
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_encrypt_setup(&operation, key, alg)); |
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_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6435 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6436 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6437 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6438 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6439 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6440 | input_data->len, output_data, |
| 6441 | output_size, &output_length), |
| 6442 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6444 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6445 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6446 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 6447 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6448 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6449 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6450 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6451 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6452 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6453 | input_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6454 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6455 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6456 | additional_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6457 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6458 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6459 | finish_output_size, |
| 6460 | &output_part_length, |
| 6461 | tag_buffer, tag_length, |
| 6462 | &tag_size), |
| 6463 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6464 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6465 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6466 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6467 | /* Test for sending too much additional data after setting lengths. */ |
| 6468 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6469 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6470 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6471 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6472 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6473 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6474 | |
| 6475 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6476 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6477 | additional_data->len), |
| 6478 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6479 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6480 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6481 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6482 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6483 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6484 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6485 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6486 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6487 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6488 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6489 | input_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6490 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6491 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6492 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6493 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6494 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6495 | 1), |
| 6496 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6497 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6498 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6499 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6500 | /* Test for sending too much data after setting lengths. */ |
| 6501 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6502 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6503 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6504 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6505 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6506 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6507 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6508 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6509 | input_data->len, output_data, |
| 6510 | output_size, &output_length), |
| 6511 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6512 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6513 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6514 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6515 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6516 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6517 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6518 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6519 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6520 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6521 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6522 | input_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6523 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6524 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6525 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6526 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6527 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6528 | input_data->len, output_data, |
| 6529 | output_size, &output_length)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6530 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6531 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6532 | 1, output_data, |
| 6533 | output_size, &output_length), |
| 6534 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6535 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6536 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6537 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6538 | /* Test sending additional data after data. */ |
| 6539 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6540 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6541 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6542 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6543 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6544 | if (operation.alg != PSA_ALG_CCM) { |
| 6545 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6546 | input_data->len, output_data, |
| 6547 | output_size, &output_length)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6548 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6549 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6550 | additional_data->len), |
| 6551 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6552 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6553 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6554 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6555 | /* Test calling finish on decryption. */ |
| 6556 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6557 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6558 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6559 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6560 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6561 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6562 | finish_output_size, |
| 6563 | &output_part_length, |
| 6564 | tag_buffer, tag_length, |
| 6565 | &tag_size), |
| 6566 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6567 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6568 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6569 | |
| 6570 | /* Test calling verify on encryption. */ |
| 6571 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6572 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6573 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6574 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6575 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6576 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6577 | finish_output_size, |
| 6578 | &output_part_length, |
| 6579 | tag_buffer, |
| 6580 | tag_length), |
| 6581 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6582 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6583 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6584 | |
| 6585 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6586 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6587 | psa_destroy_key(key); |
| 6588 | psa_aead_abort(&operation); |
| 6589 | mbedtls_free(output_data); |
| 6590 | mbedtls_free(final_data); |
| 6591 | PSA_DONE(); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6592 | } |
| 6593 | /* END_CASE */ |
| 6594 | |
| 6595 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6596 | void signature_size(int type_arg, |
| 6597 | int bits, |
| 6598 | int alg_arg, |
| 6599 | int expected_size_arg) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6600 | { |
| 6601 | psa_key_type_t type = type_arg; |
| 6602 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6603 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6604 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6605 | TEST_EQUAL(actual_size, (size_t) expected_size_arg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6606 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6607 | exit: |
| 6608 | ; |
| 6609 | } |
| 6610 | /* END_CASE */ |
| 6611 | |
| 6612 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6613 | void sign_hash_deterministic(int key_type_arg, data_t *key_data, |
| 6614 | int alg_arg, data_t *input_data, |
| 6615 | data_t *output_data) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6616 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6617 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6618 | psa_key_type_t key_type = key_type_arg; |
| 6619 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6620 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6621 | unsigned char *signature = NULL; |
| 6622 | size_t signature_size; |
| 6623 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6624 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6625 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6626 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6627 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6628 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6629 | psa_set_key_algorithm(&attributes, alg); |
| 6630 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6631 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6632 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6633 | &key)); |
| 6634 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6635 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6636 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6637 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6638 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6639 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6640 | key_bits, alg); |
| 6641 | TEST_ASSERT(signature_size != 0); |
| 6642 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6643 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6644 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6645 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6646 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6647 | input_data->x, input_data->len, |
| 6648 | signature, signature_size, |
| 6649 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6650 | /* Verify that the signature is what is expected. */ |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 6651 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 6652 | signature, signature_length); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6653 | |
| 6654 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6655 | /* |
| 6656 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6657 | * thus reset them as required. |
| 6658 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6659 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6660 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6661 | psa_destroy_key(key); |
| 6662 | mbedtls_free(signature); |
| 6663 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6664 | } |
| 6665 | /* END_CASE */ |
| 6666 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6667 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6668 | /** |
| 6669 | * sign_hash_interruptible() test intentions: |
| 6670 | * |
| 6671 | * Note: This test can currently only handle ECDSA. |
| 6672 | * |
| 6673 | * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 6674 | * and private keys / keypairs only). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6675 | * |
| 6676 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 6677 | * expected for different max_ops values. |
| 6678 | * |
| 6679 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 6680 | * and that each successful stage completes some ops (this is not mandated by |
| 6681 | * the PSA specification, but is currently the case). |
| 6682 | * |
| 6683 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 6684 | * complete() calls does not alter the number of ops returned. |
| 6685 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6686 | void sign_hash_interruptible(int key_type_arg, data_t *key_data, |
| 6687 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6688 | data_t *output_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6689 | { |
| 6690 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6691 | psa_key_type_t key_type = key_type_arg; |
| 6692 | psa_algorithm_t alg = alg_arg; |
| 6693 | size_t key_bits; |
| 6694 | unsigned char *signature = NULL; |
| 6695 | size_t signature_size; |
| 6696 | size_t signature_length = 0xdeadbeef; |
| 6697 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6698 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6699 | uint32_t num_ops = 0; |
| 6700 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6701 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6702 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6703 | size_t min_completes = 0; |
| 6704 | size_t max_completes = 0; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6705 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6706 | psa_sign_hash_interruptible_operation_t operation = |
| 6707 | psa_sign_hash_interruptible_operation_init(); |
| 6708 | |
| 6709 | PSA_ASSERT(psa_crypto_init()); |
| 6710 | |
| 6711 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6712 | psa_set_key_algorithm(&attributes, alg); |
| 6713 | psa_set_key_type(&attributes, key_type); |
| 6714 | |
| 6715 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6716 | &key)); |
| 6717 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6718 | key_bits = psa_get_key_bits(&attributes); |
| 6719 | |
| 6720 | /* Allocate a buffer which has the size advertised by the |
| 6721 | * library. */ |
| 6722 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6723 | key_bits, alg); |
| 6724 | TEST_ASSERT(signature_size != 0); |
| 6725 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6726 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6727 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6728 | psa_interruptible_set_max_ops(max_ops); |
| 6729 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6730 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6731 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6732 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6733 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6734 | TEST_ASSERT(num_ops_prior == 0); |
| 6735 | |
| 6736 | /* Start performing the signature. */ |
| 6737 | PSA_ASSERT(psa_sign_hash_start(&operation, key, alg, |
| 6738 | input_data->x, input_data->len)); |
| 6739 | |
| 6740 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6741 | TEST_ASSERT(num_ops_prior == 0); |
| 6742 | |
| 6743 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6744 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6745 | status = psa_sign_hash_complete(&operation, signature, signature_size, |
| 6746 | &signature_length); |
| 6747 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6748 | num_completes++; |
| 6749 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6750 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 6751 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6752 | /* We are asserting here that every complete makes progress |
| 6753 | * (completes some ops), which is true of the internal |
| 6754 | * implementation and probably any implementation, however this is |
| 6755 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6756 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6757 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6758 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 6759 | |
| 6760 | /* Ensure calling get_num_ops() twice still returns the same |
| 6761 | * number of ops as previously reported. */ |
| 6762 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6763 | |
| 6764 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6765 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6766 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6767 | |
| 6768 | TEST_ASSERT(status == PSA_SUCCESS); |
| 6769 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6770 | TEST_LE_U(min_completes, num_completes); |
| 6771 | TEST_LE_U(num_completes, max_completes); |
| 6772 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6773 | /* Verify that the signature is what is expected. */ |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 6774 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 6775 | signature, signature_length); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6776 | |
| 6777 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6778 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6779 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6780 | TEST_ASSERT(num_ops == 0); |
| 6781 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6782 | exit: |
| 6783 | |
| 6784 | /* |
| 6785 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6786 | * thus reset them as required. |
| 6787 | */ |
| 6788 | psa_reset_key_attributes(&attributes); |
| 6789 | |
| 6790 | psa_destroy_key(key); |
| 6791 | mbedtls_free(signature); |
| 6792 | PSA_DONE(); |
| 6793 | } |
| 6794 | /* END_CASE */ |
| 6795 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6796 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6797 | void sign_hash_fail(int key_type_arg, data_t *key_data, |
| 6798 | int alg_arg, data_t *input_data, |
| 6799 | int signature_size_arg, int expected_status_arg) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6800 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6801 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6802 | psa_key_type_t key_type = key_type_arg; |
| 6803 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6804 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6805 | psa_status_t actual_status; |
| 6806 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 6807 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 6808 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6809 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6810 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6811 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6812 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6813 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6814 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6815 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6816 | psa_set_key_algorithm(&attributes, alg); |
| 6817 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6818 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6819 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6820 | &key)); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6821 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6822 | actual_status = psa_sign_hash(key, alg, |
| 6823 | input_data->x, input_data->len, |
| 6824 | signature, signature_size, |
| 6825 | &signature_length); |
| 6826 | TEST_EQUAL(actual_status, expected_status); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6827 | /* The value of *signature_length is unspecified on error, but |
| 6828 | * whatever it is, it should be less than signature_size, so that |
| 6829 | * if the caller tries to read *signature_length bytes without |
| 6830 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6831 | TEST_LE_U(signature_length, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6832 | |
| 6833 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6834 | psa_reset_key_attributes(&attributes); |
| 6835 | psa_destroy_key(key); |
| 6836 | mbedtls_free(signature); |
| 6837 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6838 | } |
| 6839 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6840 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6841 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6842 | /** |
| 6843 | * sign_hash_fail_interruptible() test intentions: |
| 6844 | * |
| 6845 | * Note: This test can currently only handle ECDSA. |
| 6846 | * |
| 6847 | * 1. Test that various failure cases for interruptible sign hash fail with the |
| 6848 | * correct error codes, and at the correct point (at start or during |
| 6849 | * complete). |
| 6850 | * |
| 6851 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 6852 | * expected for different max_ops values. |
| 6853 | * |
| 6854 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 6855 | * and that each successful stage completes some ops (this is not mandated by |
| 6856 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 6857 | * |
| 6858 | * 4. Check that calling complete() when start() fails and complete() |
| 6859 | * after completion results in a BAD_STATE error. |
| 6860 | * |
| 6861 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 6862 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6863 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6864 | void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 6865 | int alg_arg, data_t *input_data, |
| 6866 | int signature_size_arg, |
| 6867 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6868 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6869 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6870 | { |
| 6871 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6872 | psa_key_type_t key_type = key_type_arg; |
| 6873 | psa_algorithm_t alg = alg_arg; |
| 6874 | size_t signature_size = signature_size_arg; |
| 6875 | psa_status_t actual_status; |
| 6876 | psa_status_t expected_start_status = expected_start_status_arg; |
| 6877 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 6878 | unsigned char *signature = NULL; |
| 6879 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6880 | uint32_t num_ops = 0; |
| 6881 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6882 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6883 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6884 | size_t min_completes = 0; |
| 6885 | size_t max_completes = 0; |
| 6886 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6887 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6888 | psa_sign_hash_interruptible_operation_t operation = |
| 6889 | psa_sign_hash_interruptible_operation_init(); |
| 6890 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6891 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6892 | |
| 6893 | PSA_ASSERT(psa_crypto_init()); |
| 6894 | |
| 6895 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6896 | psa_set_key_algorithm(&attributes, alg); |
| 6897 | psa_set_key_type(&attributes, key_type); |
| 6898 | |
| 6899 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6900 | &key)); |
| 6901 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6902 | psa_interruptible_set_max_ops(max_ops); |
| 6903 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6904 | interruptible_signverify_get_minmax_completes(max_ops, |
| 6905 | expected_complete_status, |
| 6906 | &min_completes, |
| 6907 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6908 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6909 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6910 | TEST_ASSERT(num_ops_prior == 0); |
| 6911 | |
| 6912 | /* Start performing the signature. */ |
| 6913 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 6914 | input_data->x, input_data->len); |
| 6915 | |
| 6916 | TEST_EQUAL(actual_status, expected_start_status); |
| 6917 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6918 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 6919 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 6920 | * start failed. */ |
| 6921 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6922 | signature_size, |
| 6923 | &signature_length); |
| 6924 | |
| 6925 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 6926 | |
| 6927 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6928 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 6929 | input_data->x, input_data->len); |
| 6930 | |
| 6931 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 6932 | } |
| 6933 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6934 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6935 | TEST_ASSERT(num_ops_prior == 0); |
| 6936 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6937 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6938 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6939 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6940 | signature_size, |
| 6941 | &signature_length); |
| 6942 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6943 | num_completes++; |
| 6944 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6945 | if (actual_status == PSA_SUCCESS || |
| 6946 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6947 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6948 | /* We are asserting here that every complete makes progress |
| 6949 | * (completes some ops), which is true of the internal |
| 6950 | * implementation and probably any implementation, however this is |
| 6951 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6952 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6953 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6954 | num_ops_prior = num_ops; |
| 6955 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6956 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6957 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6958 | TEST_EQUAL(actual_status, expected_complete_status); |
| 6959 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 6960 | /* Check that another complete returns BAD_STATE. */ |
| 6961 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6962 | signature_size, |
| 6963 | &signature_length); |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6964 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 6965 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6966 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6967 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6968 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6969 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6970 | TEST_ASSERT(num_ops == 0); |
| 6971 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6972 | /* The value of *signature_length is unspecified on error, but |
| 6973 | * whatever it is, it should be less than signature_size, so that |
| 6974 | * if the caller tries to read *signature_length bytes without |
| 6975 | * checking the error code then they don't overflow a buffer. */ |
| 6976 | TEST_LE_U(signature_length, signature_size); |
| 6977 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6978 | TEST_LE_U(min_completes, num_completes); |
| 6979 | TEST_LE_U(num_completes, max_completes); |
| 6980 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6981 | exit: |
| 6982 | psa_reset_key_attributes(&attributes); |
| 6983 | psa_destroy_key(key); |
| 6984 | mbedtls_free(signature); |
| 6985 | PSA_DONE(); |
| 6986 | } |
| 6987 | /* END_CASE */ |
| 6988 | |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6989 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6990 | void sign_verify_hash(int key_type_arg, data_t *key_data, |
| 6991 | int alg_arg, data_t *input_data) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6992 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6993 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6994 | psa_key_type_t key_type = key_type_arg; |
| 6995 | psa_algorithm_t alg = alg_arg; |
| 6996 | size_t key_bits; |
| 6997 | unsigned char *signature = NULL; |
| 6998 | size_t signature_size; |
| 6999 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7000 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7001 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7002 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7003 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7004 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 7005 | psa_set_key_algorithm(&attributes, alg); |
| 7006 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7007 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7008 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7009 | &key)); |
| 7010 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7011 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7012 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 7013 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7014 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7015 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7016 | key_bits, alg); |
| 7017 | TEST_ASSERT(signature_size != 0); |
| 7018 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7019 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7020 | |
| 7021 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7022 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 7023 | input_data->x, input_data->len, |
| 7024 | signature, signature_size, |
| 7025 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7026 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7027 | TEST_LE_U(signature_length, signature_size); |
| 7028 | TEST_ASSERT(signature_length > 0); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7029 | |
| 7030 | /* Use the library to verify that the signature is correct. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7031 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 7032 | input_data->x, input_data->len, |
| 7033 | signature, signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7035 | if (input_data->len != 0) { |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7036 | /* Flip a bit in the input and verify that the signature is now |
| 7037 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 7038 | * because ECDSA may ignore the last few bits of the input. */ |
| 7039 | input_data->x[0] ^= 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7040 | TEST_EQUAL(psa_verify_hash(key, alg, |
| 7041 | input_data->x, input_data->len, |
| 7042 | signature, signature_length), |
| 7043 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7044 | } |
| 7045 | |
| 7046 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7047 | /* |
| 7048 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7049 | * thus reset them as required. |
| 7050 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7051 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7052 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7053 | psa_destroy_key(key); |
| 7054 | mbedtls_free(signature); |
| 7055 | PSA_DONE(); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7056 | } |
| 7057 | /* END_CASE */ |
| 7058 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7059 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7060 | /** |
| 7061 | * sign_verify_hash_interruptible() test intentions: |
| 7062 | * |
| 7063 | * Note: This test can currently only handle ECDSA. |
| 7064 | * |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 7065 | * 1. Test that we can sign an input hash with the given keypair and then |
| 7066 | * afterwards verify that signature. This is currently the only way to test |
| 7067 | * non deterministic ECDSA, but this test can also handle deterministic. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7068 | * |
| 7069 | * 2. Test that after corrupting the hash, the verification detects an invalid |
| 7070 | * signature. |
| 7071 | * |
| 7072 | * 3. Test the number of calls to psa_sign_hash_complete() required are as |
| 7073 | * expected for different max_ops values. |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7074 | * |
| 7075 | * 4. Test that the number of ops done prior to starting signing and after abort |
| 7076 | * is zero and that each successful signing stage completes some ops (this is |
| 7077 | * not mandated by the PSA specification, but is currently the case). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7078 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7079 | 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] | 7080 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7081 | int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7082 | { |
| 7083 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7084 | psa_key_type_t key_type = key_type_arg; |
| 7085 | psa_algorithm_t alg = alg_arg; |
| 7086 | size_t key_bits; |
| 7087 | unsigned char *signature = NULL; |
| 7088 | size_t signature_size; |
| 7089 | size_t signature_length = 0xdeadbeef; |
| 7090 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7091 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7092 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7093 | uint32_t num_ops = 0; |
| 7094 | uint32_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7095 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7096 | size_t min_completes = 0; |
| 7097 | size_t max_completes = 0; |
| 7098 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7099 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7100 | psa_sign_hash_interruptible_operation_init(); |
| 7101 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7102 | psa_verify_hash_interruptible_operation_init(); |
| 7103 | |
| 7104 | PSA_ASSERT(psa_crypto_init()); |
| 7105 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7106 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7107 | PSA_KEY_USAGE_VERIFY_HASH); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7108 | psa_set_key_algorithm(&attributes, alg); |
| 7109 | psa_set_key_type(&attributes, key_type); |
| 7110 | |
| 7111 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7112 | &key)); |
| 7113 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7114 | key_bits = psa_get_key_bits(&attributes); |
| 7115 | |
| 7116 | /* Allocate a buffer which has the size advertised by the |
| 7117 | * library. */ |
| 7118 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7119 | key_bits, alg); |
| 7120 | TEST_ASSERT(signature_size != 0); |
| 7121 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7122 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7123 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7124 | psa_interruptible_set_max_ops(max_ops); |
| 7125 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7126 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 7127 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7128 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7129 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); |
| 7130 | TEST_ASSERT(num_ops_prior == 0); |
| 7131 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7132 | /* Start performing the signature. */ |
| 7133 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7134 | input_data->x, input_data->len)); |
| 7135 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7136 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); |
| 7137 | TEST_ASSERT(num_ops_prior == 0); |
| 7138 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7139 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7140 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7141 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7142 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7143 | signature_size, |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7144 | &signature_length); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7145 | |
| 7146 | num_completes++; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7147 | |
| 7148 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 7149 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7150 | /* We are asserting here that every complete makes progress |
| 7151 | * (completes some ops), which is true of the internal |
| 7152 | * implementation and probably any implementation, however this is |
| 7153 | * not mandated by the PSA specification. */ |
| 7154 | TEST_ASSERT(num_ops > num_ops_prior); |
| 7155 | |
| 7156 | num_ops_prior = num_ops; |
| 7157 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7158 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7159 | |
| 7160 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7161 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7162 | TEST_LE_U(min_completes, num_completes); |
| 7163 | TEST_LE_U(num_completes, max_completes); |
| 7164 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7165 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7166 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7167 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7168 | TEST_ASSERT(num_ops == 0); |
| 7169 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7170 | /* Check that the signature length looks sensible. */ |
| 7171 | TEST_LE_U(signature_length, signature_size); |
| 7172 | TEST_ASSERT(signature_length > 0); |
| 7173 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7174 | num_completes = 0; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7175 | |
| 7176 | /* Start verification. */ |
| 7177 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7178 | input_data->x, input_data->len, |
| 7179 | signature, signature_length)); |
| 7180 | |
| 7181 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7182 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7183 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7184 | |
| 7185 | num_completes++; |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7186 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7187 | |
| 7188 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7189 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7190 | TEST_LE_U(min_completes, num_completes); |
| 7191 | TEST_LE_U(num_completes, max_completes); |
| 7192 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7193 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7194 | |
| 7195 | verify_operation = psa_verify_hash_interruptible_operation_init(); |
| 7196 | |
| 7197 | if (input_data->len != 0) { |
| 7198 | /* Flip a bit in the input and verify that the signature is now |
| 7199 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 7200 | * because ECDSA may ignore the last few bits of the input. */ |
| 7201 | input_data->x[0] ^= 1; |
| 7202 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7203 | /* Start verification. */ |
| 7204 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7205 | input_data->x, input_data->len, |
| 7206 | signature, signature_length)); |
| 7207 | |
| 7208 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7209 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7210 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7211 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7212 | |
| 7213 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7214 | } |
| 7215 | |
| 7216 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7217 | |
| 7218 | exit: |
| 7219 | /* |
| 7220 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7221 | * thus reset them as required. |
| 7222 | */ |
| 7223 | psa_reset_key_attributes(&attributes); |
| 7224 | |
| 7225 | psa_destroy_key(key); |
| 7226 | mbedtls_free(signature); |
| 7227 | PSA_DONE(); |
| 7228 | } |
| 7229 | /* END_CASE */ |
| 7230 | |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7231 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7232 | void verify_hash(int key_type_arg, data_t *key_data, |
| 7233 | int alg_arg, data_t *hash_data, |
| 7234 | data_t *signature_data) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7235 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7236 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7237 | psa_key_type_t key_type = key_type_arg; |
| 7238 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7239 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7240 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7241 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 7242 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7243 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7244 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7245 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7246 | psa_set_key_algorithm(&attributes, alg); |
| 7247 | psa_set_key_type(&attributes, key_type); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7248 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7249 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7250 | &key)); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7252 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 7253 | hash_data->x, hash_data->len, |
| 7254 | signature_data->x, signature_data->len)); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 7255 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7256 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7257 | psa_reset_key_attributes(&attributes); |
| 7258 | psa_destroy_key(key); |
| 7259 | PSA_DONE(); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7260 | } |
| 7261 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7262 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7263 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7264 | /** |
| 7265 | * verify_hash_interruptible() test intentions: |
| 7266 | * |
| 7267 | * Note: This test can currently only handle ECDSA. |
| 7268 | * |
| 7269 | * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 7270 | * only). Given this test only does verification it can accept public keys as |
| 7271 | * well as private keys / keypairs. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7272 | * |
| 7273 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7274 | * expected for different max_ops values. |
| 7275 | * |
| 7276 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7277 | * and that each successful stage completes some ops (this is not mandated by |
| 7278 | * the PSA specification, but is currently the case). |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7279 | * |
| 7280 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 7281 | * complete() calls does not alter the number of ops returned. |
| 7282 | * |
| 7283 | * 5. Test that after corrupting the hash, the verification detects an invalid |
| 7284 | * signature. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7285 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7286 | void verify_hash_interruptible(int key_type_arg, data_t *key_data, |
| 7287 | int alg_arg, data_t *hash_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7288 | data_t *signature_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7289 | { |
| 7290 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7291 | psa_key_type_t key_type = key_type_arg; |
| 7292 | psa_algorithm_t alg = alg_arg; |
| 7293 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7294 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7295 | uint32_t num_ops = 0; |
| 7296 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7297 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7298 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7299 | size_t min_completes = 0; |
| 7300 | size_t max_completes = 0; |
| 7301 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7302 | psa_verify_hash_interruptible_operation_t operation = |
| 7303 | psa_verify_hash_interruptible_operation_init(); |
| 7304 | |
| 7305 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 7306 | |
| 7307 | PSA_ASSERT(psa_crypto_init()); |
| 7308 | |
| 7309 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7310 | psa_set_key_algorithm(&attributes, alg); |
| 7311 | psa_set_key_type(&attributes, key_type); |
| 7312 | |
| 7313 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7314 | &key)); |
| 7315 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7316 | psa_interruptible_set_max_ops(max_ops); |
| 7317 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7318 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 7319 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7320 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7321 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7322 | |
| 7323 | TEST_ASSERT(num_ops_prior == 0); |
| 7324 | |
| 7325 | /* Start verification. */ |
| 7326 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7327 | hash_data->x, hash_data->len, |
| 7328 | signature_data->x, signature_data->len) |
| 7329 | ); |
| 7330 | |
| 7331 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7332 | |
| 7333 | TEST_ASSERT(num_ops_prior == 0); |
| 7334 | |
| 7335 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7336 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7337 | status = psa_verify_hash_complete(&operation); |
| 7338 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7339 | num_completes++; |
| 7340 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7341 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 7342 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 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. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7347 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7348 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7349 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 7350 | |
| 7351 | /* Ensure calling get_num_ops() twice still returns the same |
| 7352 | * number of ops as previously reported. */ |
| 7353 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7354 | |
| 7355 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7356 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7357 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7358 | |
| 7359 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7360 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7361 | TEST_LE_U(min_completes, num_completes); |
| 7362 | TEST_LE_U(num_completes, max_completes); |
| 7363 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7364 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7365 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7366 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7367 | TEST_ASSERT(num_ops == 0); |
| 7368 | |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7369 | if (hash_data->len != 0) { |
| 7370 | /* Flip a bit in the hash and verify that the signature is now detected |
| 7371 | * as invalid. Flip a bit at the beginning, not at the end, because |
| 7372 | * ECDSA may ignore the last few bits of the input. */ |
| 7373 | hash_data->x[0] ^= 1; |
| 7374 | |
| 7375 | /* Start verification. */ |
| 7376 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7377 | hash_data->x, hash_data->len, |
| 7378 | signature_data->x, signature_data->len)); |
| 7379 | |
| 7380 | /* Continue performing the signature until complete. */ |
| 7381 | do { |
| 7382 | status = psa_verify_hash_complete(&operation); |
| 7383 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7384 | |
| 7385 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7386 | } |
| 7387 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7388 | exit: |
| 7389 | psa_reset_key_attributes(&attributes); |
| 7390 | psa_destroy_key(key); |
| 7391 | PSA_DONE(); |
| 7392 | } |
| 7393 | /* END_CASE */ |
| 7394 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7395 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7396 | void verify_hash_fail(int key_type_arg, data_t *key_data, |
| 7397 | int alg_arg, data_t *hash_data, |
| 7398 | data_t *signature_data, |
| 7399 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7400 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7401 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7402 | psa_key_type_t key_type = key_type_arg; |
| 7403 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7404 | psa_status_t actual_status; |
| 7405 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7406 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7407 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7408 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7409 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7410 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7411 | psa_set_key_algorithm(&attributes, alg); |
| 7412 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7413 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7414 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7415 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7416 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7417 | actual_status = psa_verify_hash(key, alg, |
| 7418 | hash_data->x, hash_data->len, |
| 7419 | signature_data->x, signature_data->len); |
| 7420 | TEST_EQUAL(actual_status, expected_status); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7421 | |
| 7422 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7423 | psa_reset_key_attributes(&attributes); |
| 7424 | psa_destroy_key(key); |
| 7425 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7426 | } |
| 7427 | /* END_CASE */ |
| 7428 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7429 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7430 | /** |
| 7431 | * verify_hash_fail_interruptible() test intentions: |
| 7432 | * |
| 7433 | * Note: This test can currently only handle ECDSA. |
| 7434 | * |
| 7435 | * 1. Test that various failure cases for interruptible verify hash fail with |
| 7436 | * the correct error codes, and at the correct point (at start or during |
| 7437 | * complete). |
| 7438 | * |
| 7439 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7440 | * expected for different max_ops values. |
| 7441 | * |
| 7442 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7443 | * and that each successful stage completes some ops (this is not mandated by |
| 7444 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7445 | * |
| 7446 | * 4. Check that calling complete() when start() fails and complete() |
| 7447 | * after completion results in a BAD_STATE error. |
| 7448 | * |
| 7449 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 7450 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7451 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7452 | void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 7453 | int alg_arg, data_t *hash_data, |
| 7454 | data_t *signature_data, |
| 7455 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7456 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7457 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7458 | { |
| 7459 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7460 | psa_key_type_t key_type = key_type_arg; |
| 7461 | psa_algorithm_t alg = alg_arg; |
| 7462 | psa_status_t actual_status; |
| 7463 | psa_status_t expected_start_status = expected_start_status_arg; |
| 7464 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 7465 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7466 | uint32_t num_ops = 0; |
| 7467 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7468 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7469 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7470 | size_t min_completes = 0; |
| 7471 | size_t max_completes = 0; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7472 | psa_verify_hash_interruptible_operation_t operation = |
| 7473 | psa_verify_hash_interruptible_operation_init(); |
| 7474 | |
| 7475 | PSA_ASSERT(psa_crypto_init()); |
| 7476 | |
| 7477 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7478 | psa_set_key_algorithm(&attributes, alg); |
| 7479 | psa_set_key_type(&attributes, key_type); |
| 7480 | |
| 7481 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7482 | &key)); |
| 7483 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7484 | psa_interruptible_set_max_ops(max_ops); |
| 7485 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7486 | interruptible_signverify_get_minmax_completes(max_ops, |
| 7487 | expected_complete_status, |
| 7488 | &min_completes, |
| 7489 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7490 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7491 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7492 | TEST_ASSERT(num_ops_prior == 0); |
| 7493 | |
| 7494 | /* Start verification. */ |
| 7495 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7496 | hash_data->x, hash_data->len, |
| 7497 | signature_data->x, |
| 7498 | signature_data->len); |
| 7499 | |
| 7500 | TEST_EQUAL(actual_status, expected_start_status); |
| 7501 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7502 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 7503 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7504 | * start failed. */ |
| 7505 | actual_status = psa_verify_hash_complete(&operation); |
| 7506 | |
| 7507 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7508 | |
| 7509 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7510 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7511 | hash_data->x, hash_data->len, |
| 7512 | signature_data->x, |
| 7513 | signature_data->len); |
| 7514 | |
| 7515 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7516 | } |
| 7517 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7518 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7519 | TEST_ASSERT(num_ops_prior == 0); |
| 7520 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7521 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7522 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7523 | actual_status = psa_verify_hash_complete(&operation); |
| 7524 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7525 | num_completes++; |
| 7526 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7527 | if (actual_status == PSA_SUCCESS || |
| 7528 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7529 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7530 | /* We are asserting here that every complete makes progress |
| 7531 | * (completes some ops), which is true of the internal |
| 7532 | * implementation and probably any implementation, however this is |
| 7533 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7534 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7535 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7536 | num_ops_prior = num_ops; |
| 7537 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7538 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7539 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7540 | TEST_EQUAL(actual_status, expected_complete_status); |
| 7541 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 7542 | /* Check that another complete returns BAD_STATE. */ |
| 7543 | actual_status = psa_verify_hash_complete(&operation); |
| 7544 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7545 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7546 | TEST_LE_U(min_completes, num_completes); |
| 7547 | TEST_LE_U(num_completes, max_completes); |
| 7548 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7549 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7550 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7551 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7552 | TEST_ASSERT(num_ops == 0); |
| 7553 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7554 | exit: |
| 7555 | psa_reset_key_attributes(&attributes); |
| 7556 | psa_destroy_key(key); |
| 7557 | PSA_DONE(); |
| 7558 | } |
| 7559 | /* END_CASE */ |
| 7560 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7561 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7562 | /** |
| 7563 | * interruptible_signverify_hash_state_test() test intentions: |
| 7564 | * |
| 7565 | * Note: This test can currently only handle ECDSA. |
| 7566 | * |
| 7567 | * 1. Test that calling the various interruptible sign and verify hash functions |
| 7568 | * in incorrect orders returns BAD_STATE errors. |
| 7569 | */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7570 | void interruptible_signverify_hash_state_test(int key_type_arg, |
| 7571 | data_t *key_data, int alg_arg, data_t *input_data) |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7572 | { |
| 7573 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7574 | psa_key_type_t key_type = key_type_arg; |
| 7575 | psa_algorithm_t alg = alg_arg; |
| 7576 | size_t key_bits; |
| 7577 | unsigned char *signature = NULL; |
| 7578 | size_t signature_size; |
| 7579 | size_t signature_length = 0xdeadbeef; |
| 7580 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7581 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7582 | psa_sign_hash_interruptible_operation_init(); |
| 7583 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7584 | psa_verify_hash_interruptible_operation_init(); |
| 7585 | |
| 7586 | PSA_ASSERT(psa_crypto_init()); |
| 7587 | |
| 7588 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7589 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7590 | psa_set_key_algorithm(&attributes, alg); |
| 7591 | psa_set_key_type(&attributes, key_type); |
| 7592 | |
| 7593 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7594 | &key)); |
| 7595 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7596 | key_bits = psa_get_key_bits(&attributes); |
| 7597 | |
| 7598 | /* Allocate a buffer which has the size advertised by the |
| 7599 | * library. */ |
| 7600 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7601 | key_bits, alg); |
| 7602 | TEST_ASSERT(signature_size != 0); |
| 7603 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7604 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7605 | |
| 7606 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7607 | |
| 7608 | /* --- Attempt completes prior to starts --- */ |
| 7609 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7610 | signature_size, |
| 7611 | &signature_length), |
| 7612 | PSA_ERROR_BAD_STATE); |
| 7613 | |
| 7614 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7615 | |
| 7616 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7617 | PSA_ERROR_BAD_STATE); |
| 7618 | |
| 7619 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7620 | |
| 7621 | /* --- Aborts in all other places. --- */ |
| 7622 | psa_sign_hash_abort(&sign_operation); |
| 7623 | |
| 7624 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7625 | input_data->x, input_data->len)); |
| 7626 | |
| 7627 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7628 | |
| 7629 | psa_interruptible_set_max_ops(1); |
| 7630 | |
| 7631 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7632 | input_data->x, input_data->len)); |
| 7633 | |
| 7634 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7635 | signature_size, |
| 7636 | &signature_length), |
| 7637 | PSA_OPERATION_INCOMPLETE); |
| 7638 | |
| 7639 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7640 | |
| 7641 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7642 | |
| 7643 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7644 | input_data->x, input_data->len)); |
| 7645 | |
| 7646 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7647 | signature_size, |
| 7648 | &signature_length)); |
| 7649 | |
| 7650 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7651 | |
| 7652 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7653 | |
| 7654 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7655 | input_data->x, input_data->len, |
| 7656 | signature, signature_length)); |
| 7657 | |
| 7658 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7659 | |
| 7660 | psa_interruptible_set_max_ops(1); |
| 7661 | |
| 7662 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7663 | input_data->x, input_data->len, |
| 7664 | signature, signature_length)); |
| 7665 | |
| 7666 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7667 | PSA_OPERATION_INCOMPLETE); |
| 7668 | |
| 7669 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7670 | |
| 7671 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7672 | |
| 7673 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7674 | input_data->x, input_data->len, |
| 7675 | signature, signature_length)); |
| 7676 | |
| 7677 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7678 | |
| 7679 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7680 | |
| 7681 | /* --- Attempt double starts. --- */ |
| 7682 | |
| 7683 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7684 | input_data->x, input_data->len)); |
| 7685 | |
| 7686 | TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg, |
| 7687 | input_data->x, input_data->len), |
| 7688 | PSA_ERROR_BAD_STATE); |
| 7689 | |
| 7690 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7691 | |
| 7692 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7693 | input_data->x, input_data->len, |
| 7694 | signature, signature_length)); |
| 7695 | |
| 7696 | TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg, |
| 7697 | input_data->x, input_data->len, |
| 7698 | signature, signature_length), |
| 7699 | PSA_ERROR_BAD_STATE); |
| 7700 | |
| 7701 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7702 | |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7703 | exit: |
| 7704 | /* |
| 7705 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7706 | * thus reset them as required. |
| 7707 | */ |
| 7708 | psa_reset_key_attributes(&attributes); |
| 7709 | |
| 7710 | psa_destroy_key(key); |
| 7711 | mbedtls_free(signature); |
| 7712 | PSA_DONE(); |
| 7713 | } |
| 7714 | /* END_CASE */ |
| 7715 | |
| 7716 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7717 | /** |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7718 | * interruptible_signverify_hash_edgecase_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7719 | * |
| 7720 | * Note: This test can currently only handle ECDSA. |
| 7721 | * |
| 7722 | * 1. Test various edge cases in the interruptible sign and verify hash |
| 7723 | * interfaces. |
| 7724 | */ |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7725 | void interruptible_signverify_hash_edgecase_tests(int key_type_arg, |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7726 | data_t *key_data, int alg_arg, data_t *input_data) |
| 7727 | { |
| 7728 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7729 | psa_key_type_t key_type = key_type_arg; |
| 7730 | psa_algorithm_t alg = alg_arg; |
| 7731 | size_t key_bits; |
| 7732 | unsigned char *signature = NULL; |
| 7733 | size_t signature_size; |
| 7734 | size_t signature_length = 0xdeadbeef; |
| 7735 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7736 | uint8_t *input_buffer = NULL; |
| 7737 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7738 | psa_sign_hash_interruptible_operation_init(); |
| 7739 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7740 | psa_verify_hash_interruptible_operation_init(); |
| 7741 | |
| 7742 | PSA_ASSERT(psa_crypto_init()); |
| 7743 | |
| 7744 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7745 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7746 | psa_set_key_algorithm(&attributes, alg); |
| 7747 | psa_set_key_type(&attributes, key_type); |
| 7748 | |
| 7749 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7750 | &key)); |
| 7751 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7752 | key_bits = psa_get_key_bits(&attributes); |
| 7753 | |
| 7754 | /* Allocate a buffer which has the size advertised by the |
| 7755 | * library. */ |
| 7756 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7757 | key_bits, alg); |
| 7758 | TEST_ASSERT(signature_size != 0); |
| 7759 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7760 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7761 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7762 | /* --- Change function inputs mid run, to cause an error (sign only, |
| 7763 | * verify passes all inputs to start. --- */ |
| 7764 | |
| 7765 | psa_interruptible_set_max_ops(1); |
| 7766 | |
| 7767 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7768 | input_data->x, input_data->len)); |
| 7769 | |
| 7770 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7771 | signature_size, |
| 7772 | &signature_length), |
| 7773 | PSA_OPERATION_INCOMPLETE); |
| 7774 | |
| 7775 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7776 | 0, |
| 7777 | &signature_length), |
| 7778 | PSA_ERROR_BUFFER_TOO_SMALL); |
| 7779 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7780 | /* And test that this invalidates the operation. */ |
| 7781 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7782 | 0, |
| 7783 | &signature_length), |
| 7784 | PSA_ERROR_BAD_STATE); |
| 7785 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7786 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7787 | |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7788 | /* Trash the hash buffer in between start and complete, to ensure |
| 7789 | * no reliance on external buffers. */ |
| 7790 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7791 | |
Paul Elliott | 6c68df4 | 2023-10-23 15:33:37 +0100 | [diff] [blame] | 7792 | TEST_CALLOC(input_buffer, input_data->len); |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7793 | |
| 7794 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7795 | |
| 7796 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7797 | input_buffer, input_data->len)); |
| 7798 | |
| 7799 | memset(input_buffer, '!', input_data->len); |
| 7800 | mbedtls_free(input_buffer); |
| 7801 | input_buffer = NULL; |
| 7802 | |
| 7803 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7804 | signature_size, |
| 7805 | &signature_length)); |
| 7806 | |
| 7807 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7808 | |
Paul Elliott | 6c68df4 | 2023-10-23 15:33:37 +0100 | [diff] [blame] | 7809 | TEST_CALLOC(input_buffer, input_data->len); |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7810 | |
| 7811 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7812 | |
| 7813 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7814 | input_buffer, input_data->len, |
| 7815 | signature, signature_length)); |
| 7816 | |
| 7817 | memset(input_buffer, '!', input_data->len); |
| 7818 | mbedtls_free(input_buffer); |
| 7819 | input_buffer = NULL; |
| 7820 | |
| 7821 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7822 | |
| 7823 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7824 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7825 | exit: |
| 7826 | /* |
| 7827 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7828 | * thus reset them as required. |
| 7829 | */ |
| 7830 | psa_reset_key_attributes(&attributes); |
| 7831 | |
| 7832 | psa_destroy_key(key); |
| 7833 | mbedtls_free(signature); |
Paul Elliott | 6c68df4 | 2023-10-23 15:33:37 +0100 | [diff] [blame] | 7834 | mbedtls_free(input_buffer); |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7835 | PSA_DONE(); |
| 7836 | } |
| 7837 | /* END_CASE */ |
| 7838 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7839 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7840 | /** |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 7841 | * interruptible_signverify_hash_ops_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7842 | * |
| 7843 | * Note: This test can currently only handle ECDSA. |
| 7844 | * |
| 7845 | * 1. Test that setting max ops is reflected in both interruptible sign and |
| 7846 | * verify hash |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7847 | * 2. Test that changing the value of max_ops to unlimited during an operation |
| 7848 | * causes that operation to complete in the next call. |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7849 | * |
| 7850 | * 3. Test that calling get_num_ops() between complete calls gives the same |
| 7851 | * 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] | 7852 | */ |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 7853 | void interruptible_signverify_hash_ops_tests(int key_type_arg, |
| 7854 | data_t *key_data, int alg_arg, |
| 7855 | data_t *input_data) |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7856 | { |
| 7857 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7858 | psa_key_type_t key_type = key_type_arg; |
| 7859 | psa_algorithm_t alg = alg_arg; |
| 7860 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7861 | size_t key_bits; |
| 7862 | unsigned char *signature = NULL; |
| 7863 | size_t signature_size; |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7864 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7865 | uint32_t num_ops = 0; |
| 7866 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 7867 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7868 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7869 | psa_sign_hash_interruptible_operation_init(); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7870 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7871 | psa_verify_hash_interruptible_operation_init(); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7872 | |
| 7873 | PSA_ASSERT(psa_crypto_init()); |
| 7874 | |
| 7875 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7876 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7877 | psa_set_key_algorithm(&attributes, alg); |
| 7878 | psa_set_key_type(&attributes, key_type); |
| 7879 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7880 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key)); |
| 7881 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7882 | key_bits = psa_get_key_bits(&attributes); |
| 7883 | |
| 7884 | /* Allocate a buffer which has the size advertised by the |
| 7885 | * library. */ |
| 7886 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7887 | |
| 7888 | TEST_ASSERT(signature_size != 0); |
| 7889 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7890 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7891 | |
| 7892 | /* Check that default max ops gets set if we don't set it. */ |
| 7893 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7894 | input_data->x, input_data->len)); |
| 7895 | |
| 7896 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 7897 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7898 | |
| 7899 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7900 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7901 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7902 | input_data->x, input_data->len, |
| 7903 | signature, signature_size)); |
| 7904 | |
| 7905 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 7906 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7907 | |
| 7908 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7909 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7910 | /* Check that max ops gets set properly. */ |
| 7911 | |
| 7912 | psa_interruptible_set_max_ops(0xbeef); |
| 7913 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7914 | TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7915 | |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7916 | /* --- Ensure changing the max ops mid operation works (operation should |
| 7917 | * complete successfully after setting max ops to unlimited --- */ |
| 7918 | psa_interruptible_set_max_ops(1); |
| 7919 | |
| 7920 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7921 | input_data->x, input_data->len)); |
| 7922 | |
| 7923 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7924 | signature_size, |
| 7925 | &signature_length), |
| 7926 | PSA_OPERATION_INCOMPLETE); |
| 7927 | |
| 7928 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7929 | |
| 7930 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7931 | signature_size, |
| 7932 | &signature_length)); |
| 7933 | |
| 7934 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7935 | |
| 7936 | psa_interruptible_set_max_ops(1); |
| 7937 | |
| 7938 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7939 | input_data->x, input_data->len, |
| 7940 | signature, signature_length)); |
| 7941 | |
| 7942 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7943 | PSA_OPERATION_INCOMPLETE); |
| 7944 | |
| 7945 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7946 | |
| 7947 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7948 | |
| 7949 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7950 | |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7951 | /* --- Test that not calling get_num_ops inbetween complete calls does not |
| 7952 | * result in lost ops. ---*/ |
| 7953 | |
| 7954 | psa_interruptible_set_max_ops(1); |
| 7955 | |
| 7956 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7957 | input_data->x, input_data->len)); |
| 7958 | |
| 7959 | /* Continue performing the signature until complete. */ |
| 7960 | do { |
| 7961 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7962 | signature_size, |
| 7963 | &signature_length); |
| 7964 | |
| 7965 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7966 | |
| 7967 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7968 | |
| 7969 | PSA_ASSERT(status); |
| 7970 | |
| 7971 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7972 | |
| 7973 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7974 | input_data->x, input_data->len)); |
| 7975 | |
| 7976 | /* Continue performing the signature until complete. */ |
| 7977 | do { |
| 7978 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7979 | signature_size, |
| 7980 | &signature_length); |
| 7981 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7982 | |
| 7983 | PSA_ASSERT(status); |
| 7984 | |
| 7985 | TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation)); |
| 7986 | |
| 7987 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7988 | |
| 7989 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7990 | input_data->x, input_data->len, |
| 7991 | signature, signature_length)); |
| 7992 | |
| 7993 | /* Continue performing the verification until complete. */ |
| 7994 | do { |
| 7995 | status = psa_verify_hash_complete(&verify_operation); |
| 7996 | |
| 7997 | num_ops = psa_verify_hash_get_num_ops(&verify_operation); |
| 7998 | |
| 7999 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 8000 | |
| 8001 | PSA_ASSERT(status); |
| 8002 | |
| 8003 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 8004 | |
| 8005 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 8006 | input_data->x, input_data->len, |
| 8007 | signature, signature_length)); |
| 8008 | |
| 8009 | /* Continue performing the verification until complete. */ |
| 8010 | do { |
| 8011 | status = psa_verify_hash_complete(&verify_operation); |
| 8012 | |
| 8013 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 8014 | |
| 8015 | PSA_ASSERT(status); |
| 8016 | |
| 8017 | TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation)); |
| 8018 | |
| 8019 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 8020 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8021 | exit: |
| 8022 | /* |
| 8023 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8024 | * thus reset them as required. |
| 8025 | */ |
| 8026 | psa_reset_key_attributes(&attributes); |
| 8027 | |
| 8028 | psa_destroy_key(key); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 8029 | mbedtls_free(signature); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8030 | PSA_DONE(); |
| 8031 | } |
| 8032 | /* END_CASE */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 8033 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8034 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8035 | void sign_message_deterministic(int key_type_arg, |
| 8036 | data_t *key_data, |
| 8037 | int alg_arg, |
| 8038 | data_t *input_data, |
| 8039 | data_t *output_data) |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8040 | { |
| 8041 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8042 | psa_key_type_t key_type = key_type_arg; |
| 8043 | psa_algorithm_t alg = alg_arg; |
| 8044 | size_t key_bits; |
| 8045 | unsigned char *signature = NULL; |
| 8046 | size_t signature_size; |
| 8047 | size_t signature_length = 0xdeadbeef; |
| 8048 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8049 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8050 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8051 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8052 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 8053 | psa_set_key_algorithm(&attributes, alg); |
| 8054 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8055 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8056 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8057 | &key)); |
| 8058 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8059 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8060 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8061 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8062 | TEST_ASSERT(signature_size != 0); |
| 8063 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8064 | TEST_CALLOC(signature, signature_size); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8065 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8066 | PSA_ASSERT(psa_sign_message(key, alg, |
| 8067 | input_data->x, input_data->len, |
| 8068 | signature, signature_size, |
| 8069 | &signature_length)); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8070 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8071 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8072 | signature, signature_length); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8073 | |
| 8074 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8075 | psa_reset_key_attributes(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8076 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8077 | psa_destroy_key(key); |
| 8078 | mbedtls_free(signature); |
| 8079 | PSA_DONE(); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8080 | |
| 8081 | } |
| 8082 | /* END_CASE */ |
| 8083 | |
| 8084 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8085 | void sign_message_fail(int key_type_arg, |
| 8086 | data_t *key_data, |
| 8087 | int alg_arg, |
| 8088 | data_t *input_data, |
| 8089 | int signature_size_arg, |
| 8090 | int expected_status_arg) |
| 8091 | { |
| 8092 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8093 | psa_key_type_t key_type = key_type_arg; |
| 8094 | psa_algorithm_t alg = alg_arg; |
| 8095 | size_t signature_size = signature_size_arg; |
| 8096 | psa_status_t actual_status; |
| 8097 | psa_status_t expected_status = expected_status_arg; |
| 8098 | unsigned char *signature = NULL; |
| 8099 | size_t signature_length = 0xdeadbeef; |
| 8100 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8101 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8102 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8103 | |
| 8104 | PSA_ASSERT(psa_crypto_init()); |
| 8105 | |
| 8106 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 8107 | psa_set_key_algorithm(&attributes, alg); |
| 8108 | psa_set_key_type(&attributes, key_type); |
| 8109 | |
| 8110 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8111 | &key)); |
| 8112 | |
| 8113 | actual_status = psa_sign_message(key, alg, |
| 8114 | input_data->x, input_data->len, |
| 8115 | signature, signature_size, |
| 8116 | &signature_length); |
| 8117 | TEST_EQUAL(actual_status, expected_status); |
| 8118 | /* The value of *signature_length is unspecified on error, but |
| 8119 | * whatever it is, it should be less than signature_size, so that |
| 8120 | * if the caller tries to read *signature_length bytes without |
| 8121 | * checking the error code then they don't overflow a buffer. */ |
| 8122 | TEST_LE_U(signature_length, signature_size); |
| 8123 | |
| 8124 | exit: |
| 8125 | psa_reset_key_attributes(&attributes); |
| 8126 | psa_destroy_key(key); |
| 8127 | mbedtls_free(signature); |
| 8128 | PSA_DONE(); |
| 8129 | } |
| 8130 | /* END_CASE */ |
| 8131 | |
| 8132 | /* BEGIN_CASE */ |
| 8133 | void sign_verify_message(int key_type_arg, |
| 8134 | data_t *key_data, |
| 8135 | int alg_arg, |
| 8136 | data_t *input_data) |
| 8137 | { |
| 8138 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8139 | psa_key_type_t key_type = key_type_arg; |
| 8140 | psa_algorithm_t alg = alg_arg; |
| 8141 | size_t key_bits; |
| 8142 | unsigned char *signature = NULL; |
| 8143 | size_t signature_size; |
| 8144 | size_t signature_length = 0xdeadbeef; |
| 8145 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8146 | |
| 8147 | PSA_ASSERT(psa_crypto_init()); |
| 8148 | |
| 8149 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 8150 | PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8151 | psa_set_key_algorithm(&attributes, alg); |
| 8152 | psa_set_key_type(&attributes, key_type); |
| 8153 | |
| 8154 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8155 | &key)); |
| 8156 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8157 | key_bits = psa_get_key_bits(&attributes); |
| 8158 | |
| 8159 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8160 | TEST_ASSERT(signature_size != 0); |
| 8161 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8162 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8163 | |
| 8164 | PSA_ASSERT(psa_sign_message(key, alg, |
| 8165 | input_data->x, input_data->len, |
| 8166 | signature, signature_size, |
| 8167 | &signature_length)); |
| 8168 | TEST_LE_U(signature_length, signature_size); |
| 8169 | TEST_ASSERT(signature_length > 0); |
| 8170 | |
| 8171 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8172 | input_data->x, input_data->len, |
| 8173 | signature, signature_length)); |
| 8174 | |
| 8175 | if (input_data->len != 0) { |
| 8176 | /* Flip a bit in the input and verify that the signature is now |
| 8177 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 8178 | * because ECDSA may ignore the last few bits of the input. */ |
| 8179 | input_data->x[0] ^= 1; |
| 8180 | TEST_EQUAL(psa_verify_message(key, alg, |
| 8181 | input_data->x, input_data->len, |
| 8182 | signature, signature_length), |
| 8183 | PSA_ERROR_INVALID_SIGNATURE); |
| 8184 | } |
| 8185 | |
| 8186 | exit: |
| 8187 | psa_reset_key_attributes(&attributes); |
| 8188 | |
| 8189 | psa_destroy_key(key); |
| 8190 | mbedtls_free(signature); |
| 8191 | PSA_DONE(); |
| 8192 | } |
| 8193 | /* END_CASE */ |
| 8194 | |
| 8195 | /* BEGIN_CASE */ |
| 8196 | void verify_message(int key_type_arg, |
| 8197 | data_t *key_data, |
| 8198 | int alg_arg, |
| 8199 | data_t *input_data, |
| 8200 | data_t *signature_data) |
| 8201 | { |
| 8202 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8203 | psa_key_type_t key_type = key_type_arg; |
| 8204 | psa_algorithm_t alg = alg_arg; |
| 8205 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8206 | |
| 8207 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 8208 | |
| 8209 | PSA_ASSERT(psa_crypto_init()); |
| 8210 | |
| 8211 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8212 | psa_set_key_algorithm(&attributes, alg); |
| 8213 | psa_set_key_type(&attributes, key_type); |
| 8214 | |
| 8215 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8216 | &key)); |
| 8217 | |
| 8218 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8219 | input_data->x, input_data->len, |
| 8220 | signature_data->x, signature_data->len)); |
| 8221 | |
| 8222 | exit: |
| 8223 | psa_reset_key_attributes(&attributes); |
| 8224 | psa_destroy_key(key); |
| 8225 | PSA_DONE(); |
| 8226 | } |
| 8227 | /* END_CASE */ |
| 8228 | |
| 8229 | /* BEGIN_CASE */ |
| 8230 | void verify_message_fail(int key_type_arg, |
| 8231 | data_t *key_data, |
| 8232 | int alg_arg, |
| 8233 | data_t *hash_data, |
| 8234 | data_t *signature_data, |
| 8235 | int expected_status_arg) |
| 8236 | { |
| 8237 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8238 | psa_key_type_t key_type = key_type_arg; |
| 8239 | psa_algorithm_t alg = alg_arg; |
| 8240 | psa_status_t actual_status; |
| 8241 | psa_status_t expected_status = expected_status_arg; |
| 8242 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8243 | |
| 8244 | PSA_ASSERT(psa_crypto_init()); |
| 8245 | |
| 8246 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8247 | psa_set_key_algorithm(&attributes, alg); |
| 8248 | psa_set_key_type(&attributes, key_type); |
| 8249 | |
| 8250 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8251 | &key)); |
| 8252 | |
| 8253 | actual_status = psa_verify_message(key, alg, |
| 8254 | hash_data->x, hash_data->len, |
| 8255 | signature_data->x, |
| 8256 | signature_data->len); |
| 8257 | TEST_EQUAL(actual_status, expected_status); |
| 8258 | |
| 8259 | exit: |
| 8260 | psa_reset_key_attributes(&attributes); |
| 8261 | psa_destroy_key(key); |
| 8262 | PSA_DONE(); |
| 8263 | } |
| 8264 | /* END_CASE */ |
| 8265 | |
| 8266 | /* BEGIN_CASE */ |
| 8267 | void asymmetric_encrypt(int key_type_arg, |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8268 | data_t *key_data, |
| 8269 | int alg_arg, |
| 8270 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8271 | data_t *label, |
| 8272 | int expected_output_length_arg, |
| 8273 | int expected_status_arg) |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8274 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8275 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8276 | psa_key_type_t key_type = key_type_arg; |
| 8277 | psa_algorithm_t alg = alg_arg; |
| 8278 | size_t expected_output_length = expected_output_length_arg; |
| 8279 | size_t key_bits; |
| 8280 | unsigned char *output = NULL; |
| 8281 | size_t output_size; |
| 8282 | size_t output_length = ~0; |
| 8283 | psa_status_t actual_status; |
| 8284 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8285 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8286 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8287 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 8288 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8289 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8290 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 8291 | psa_set_key_algorithm(&attributes, alg); |
| 8292 | psa_set_key_type(&attributes, key_type); |
| 8293 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8294 | &key)); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8295 | |
| 8296 | /* Determine the maximum output length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8297 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8298 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8299 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8300 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8301 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8302 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8303 | |
| 8304 | /* Encrypt the input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8305 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8306 | input_data->x, input_data->len, |
| 8307 | label->x, label->len, |
| 8308 | output, output_size, |
| 8309 | &output_length); |
| 8310 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8311 | if (actual_status == PSA_SUCCESS) { |
| 8312 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8313 | } else { |
| 8314 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8315 | } |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8316 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8317 | /* If the label is empty, the test framework puts a non-null pointer |
| 8318 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8319 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8320 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8321 | if (output_size != 0) { |
| 8322 | memset(output, 0, output_size); |
| 8323 | } |
| 8324 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8325 | input_data->x, input_data->len, |
| 8326 | NULL, label->len, |
| 8327 | output, output_size, |
| 8328 | &output_length); |
| 8329 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8330 | if (actual_status == PSA_SUCCESS) { |
| 8331 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8332 | } else { |
| 8333 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8334 | } |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8335 | } |
| 8336 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8337 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8338 | /* |
| 8339 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8340 | * thus reset them as required. |
| 8341 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8342 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8343 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8344 | psa_destroy_key(key); |
| 8345 | mbedtls_free(output); |
| 8346 | PSA_DONE(); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8347 | } |
| 8348 | /* END_CASE */ |
| 8349 | |
| 8350 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8351 | void asymmetric_encrypt_decrypt(int key_type_arg, |
| 8352 | data_t *key_data, |
| 8353 | int alg_arg, |
| 8354 | data_t *input_data, |
| 8355 | data_t *label) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8356 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8357 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8358 | psa_key_type_t key_type = key_type_arg; |
| 8359 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8360 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8361 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8362 | size_t output_size; |
| 8363 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8364 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8365 | size_t output2_size; |
| 8366 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8367 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8368 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8369 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8371 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 8372 | psa_set_key_algorithm(&attributes, alg); |
| 8373 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8374 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8375 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8376 | &key)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8377 | |
| 8378 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8379 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8380 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8381 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8382 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8383 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8384 | TEST_CALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8385 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8386 | output2_size = input_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8387 | TEST_LE_U(output2_size, |
| 8388 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)); |
| 8389 | TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8390 | TEST_CALLOC(output2, output2_size); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8391 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 8392 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 8393 | * the original plaintext because of the non-optional random |
| 8394 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8395 | PSA_ASSERT(psa_asymmetric_encrypt(key, alg, |
| 8396 | input_data->x, input_data->len, |
| 8397 | label->x, label->len, |
| 8398 | output, output_size, |
| 8399 | &output_length)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8400 | /* We don't know what ciphertext length to expect, but check that |
| 8401 | * it looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8402 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8403 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8404 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8405 | output, output_length, |
| 8406 | label->x, label->len, |
| 8407 | output2, output2_size, |
| 8408 | &output2_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8409 | TEST_MEMORY_COMPARE(input_data->x, input_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8410 | output2, output2_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8411 | |
| 8412 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8413 | /* |
| 8414 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8415 | * thus reset them as required. |
| 8416 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8417 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8418 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8419 | psa_destroy_key(key); |
| 8420 | mbedtls_free(output); |
| 8421 | mbedtls_free(output2); |
| 8422 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8423 | } |
| 8424 | /* END_CASE */ |
| 8425 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8426 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8427 | void asymmetric_decrypt(int key_type_arg, |
| 8428 | data_t *key_data, |
| 8429 | int alg_arg, |
| 8430 | data_t *input_data, |
| 8431 | data_t *label, |
| 8432 | data_t *expected_data) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8433 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8434 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8435 | psa_key_type_t key_type = key_type_arg; |
| 8436 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8437 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8438 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 8439 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8440 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8441 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8442 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8443 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8444 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8445 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8446 | psa_set_key_algorithm(&attributes, alg); |
| 8447 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8449 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8450 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8451 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8452 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8453 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8454 | |
| 8455 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8456 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8457 | TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8458 | TEST_CALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8459 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8460 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8461 | input_data->x, input_data->len, |
| 8462 | label->x, label->len, |
| 8463 | output, |
| 8464 | output_size, |
| 8465 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8466 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8467 | output, output_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8468 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8469 | /* If the label is empty, the test framework puts a non-null pointer |
| 8470 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8471 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8472 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8473 | if (output_size != 0) { |
| 8474 | memset(output, 0, output_size); |
| 8475 | } |
| 8476 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8477 | input_data->x, input_data->len, |
| 8478 | NULL, label->len, |
| 8479 | output, |
| 8480 | output_size, |
| 8481 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8482 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8483 | output, output_length); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8484 | } |
| 8485 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8486 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8487 | psa_reset_key_attributes(&attributes); |
| 8488 | psa_destroy_key(key); |
| 8489 | mbedtls_free(output); |
| 8490 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8491 | } |
| 8492 | /* END_CASE */ |
| 8493 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8494 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8495 | void asymmetric_decrypt_fail(int key_type_arg, |
| 8496 | data_t *key_data, |
| 8497 | int alg_arg, |
| 8498 | data_t *input_data, |
| 8499 | data_t *label, |
| 8500 | int output_size_arg, |
| 8501 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8502 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8503 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8504 | psa_key_type_t key_type = key_type_arg; |
| 8505 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8506 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 8507 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8508 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8509 | psa_status_t actual_status; |
| 8510 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8511 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8512 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8513 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8514 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8515 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8516 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8517 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8518 | psa_set_key_algorithm(&attributes, alg); |
| 8519 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8520 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8521 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8522 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8523 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8524 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8525 | input_data->x, input_data->len, |
| 8526 | label->x, label->len, |
| 8527 | output, output_size, |
| 8528 | &output_length); |
| 8529 | TEST_EQUAL(actual_status, expected_status); |
| 8530 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8531 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8532 | /* If the label is empty, the test framework puts a non-null pointer |
| 8533 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8534 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8535 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8536 | if (output_size != 0) { |
| 8537 | memset(output, 0, output_size); |
| 8538 | } |
| 8539 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8540 | input_data->x, input_data->len, |
| 8541 | NULL, label->len, |
| 8542 | output, output_size, |
| 8543 | &output_length); |
| 8544 | TEST_EQUAL(actual_status, expected_status); |
| 8545 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8546 | } |
| 8547 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8548 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8549 | psa_reset_key_attributes(&attributes); |
| 8550 | psa_destroy_key(key); |
| 8551 | mbedtls_free(output); |
| 8552 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8553 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8554 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8555 | |
| 8556 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8557 | void key_derivation_init() |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8558 | { |
| 8559 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 8560 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 8561 | * 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] | 8562 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8563 | size_t capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8564 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init(); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 8565 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8566 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8567 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8568 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8569 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8570 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8571 | TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity), |
| 8572 | PSA_ERROR_BAD_STATE); |
| 8573 | TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity), |
| 8574 | PSA_ERROR_BAD_STATE); |
| 8575 | TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity), |
| 8576 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8577 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8578 | /* A default operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8579 | PSA_ASSERT(psa_key_derivation_abort(&func)); |
| 8580 | PSA_ASSERT(psa_key_derivation_abort(&init)); |
| 8581 | PSA_ASSERT(psa_key_derivation_abort(&zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8582 | } |
| 8583 | /* END_CASE */ |
| 8584 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 8585 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8586 | void derive_setup(int alg_arg, int expected_status_arg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8587 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8588 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8589 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8590 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8591 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8592 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8593 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8594 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8595 | expected_status); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8596 | |
| 8597 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8598 | psa_key_derivation_abort(&operation); |
| 8599 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8600 | } |
| 8601 | /* END_CASE */ |
| 8602 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8603 | /* BEGIN_CASE */ |
Kusumit Ghoderao | 9ffd397 | 2023-12-01 16:40:13 +0530 | [diff] [blame] | 8604 | void derive_set_capacity(int alg_arg, int64_t capacity_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8605 | int expected_status_arg) |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8606 | { |
| 8607 | psa_algorithm_t alg = alg_arg; |
| 8608 | size_t capacity = capacity_arg; |
| 8609 | psa_status_t expected_status = expected_status_arg; |
| 8610 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8611 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8612 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8613 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8614 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8615 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8616 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 8617 | expected_status); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8618 | |
| 8619 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8620 | psa_key_derivation_abort(&operation); |
| 8621 | PSA_DONE(); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8622 | } |
| 8623 | /* END_CASE */ |
| 8624 | |
| 8625 | /* BEGIN_CASE */ |
Kusumit Ghoderao | d60dfc0 | 2023-05-01 17:39:27 +0530 | [diff] [blame] | 8626 | void parse_binary_string_test(data_t *input, int output) |
| 8627 | { |
| 8628 | uint64_t value; |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 8629 | value = mbedtls_test_parse_binary_string(input); |
Kusumit Ghoderao | d60dfc0 | 2023-05-01 17:39:27 +0530 | [diff] [blame] | 8630 | TEST_EQUAL(value, output); |
| 8631 | } |
| 8632 | /* END_CASE */ |
| 8633 | |
| 8634 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8635 | void derive_input(int alg_arg, |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8636 | int step_arg1, int key_type_arg1, data_t *input1, |
| 8637 | int expected_status_arg1, |
| 8638 | int step_arg2, int key_type_arg2, data_t *input2, |
| 8639 | int expected_status_arg2, |
| 8640 | int step_arg3, int key_type_arg3, data_t *input3, |
| 8641 | int expected_status_arg3, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8642 | int output_key_type_arg, int expected_output_status_arg) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8643 | { |
| 8644 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8645 | psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 }; |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8646 | 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] | 8647 | psa_status_t expected_statuses[] = { expected_status_arg1, |
| 8648 | expected_status_arg2, |
| 8649 | expected_status_arg3 }; |
| 8650 | data_t *inputs[] = { input1, input2, input3 }; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8651 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8652 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8653 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8654 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8655 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8656 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8657 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8658 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8659 | psa_status_t expected_output_status = expected_output_status_arg; |
| 8660 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8661 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8662 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8663 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8664 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8665 | psa_set_key_algorithm(&attributes, alg); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8666 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8667 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8668 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8669 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8670 | mbedtls_test_set_step(i); |
| 8671 | if (steps[i] == 0) { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 8672 | /* Skip this step */ |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8673 | } else if (((psa_key_type_t) key_types[i]) != PSA_KEY_TYPE_NONE && |
| 8674 | key_types[i] != INPUT_INTEGER) { |
| 8675 | psa_set_key_type(&attributes, ((psa_key_type_t) key_types[i])); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8676 | PSA_ASSERT(psa_import_key(&attributes, |
| 8677 | inputs[i]->x, inputs[i]->len, |
| 8678 | &keys[i])); |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8679 | 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] | 8680 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8681 | // When taking a private key as secret input, use key agreement |
| 8682 | // to add the shared secret to the derivation |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8683 | TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self( |
| 8684 | &operation, keys[i]), |
| 8685 | expected_statuses[i]); |
| 8686 | } else { |
| 8687 | TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i], |
| 8688 | keys[i]), |
| 8689 | expected_statuses[i]); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8690 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8691 | } else { |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8692 | if (key_types[i] == INPUT_INTEGER) { |
| 8693 | TEST_EQUAL(psa_key_derivation_input_integer( |
| 8694 | &operation, steps[i], |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 8695 | mbedtls_test_parse_binary_string(inputs[i])), |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8696 | expected_statuses[i]); |
| 8697 | } else { |
Kusumit Ghoderao | 02326d5 | 2023-04-06 17:47:59 +0530 | [diff] [blame] | 8698 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8699 | &operation, steps[i], |
| 8700 | inputs[i]->x, inputs[i]->len), |
| 8701 | expected_statuses[i]); |
Kusumit Ghoderao | 02326d5 | 2023-04-06 17:47:59 +0530 | [diff] [blame] | 8702 | } |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8703 | } |
| 8704 | } |
| 8705 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8706 | if (output_key_type != PSA_KEY_TYPE_NONE) { |
| 8707 | psa_reset_key_attributes(&attributes); |
| 8708 | psa_set_key_type(&attributes, output_key_type); |
| 8709 | psa_set_key_bits(&attributes, 8); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8710 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8711 | psa_key_derivation_output_key(&attributes, &operation, |
| 8712 | &output_key); |
| 8713 | } else { |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8714 | uint8_t buffer[1]; |
| 8715 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8716 | psa_key_derivation_output_bytes(&operation, |
| 8717 | buffer, sizeof(buffer)); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8718 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8719 | TEST_EQUAL(actual_output_status, expected_output_status); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8720 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8721 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8722 | psa_key_derivation_abort(&operation); |
| 8723 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8724 | psa_destroy_key(keys[i]); |
| 8725 | } |
| 8726 | psa_destroy_key(output_key); |
| 8727 | PSA_DONE(); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8728 | } |
| 8729 | /* END_CASE */ |
| 8730 | |
Kusumit Ghoderao | 42b02b9 | 2023-06-06 16:48:46 +0530 | [diff] [blame] | 8731 | /* BEGIN_CASE*/ |
| 8732 | void derive_input_invalid_cost(int alg_arg, int64_t cost) |
| 8733 | { |
| 8734 | psa_algorithm_t alg = alg_arg; |
| 8735 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8736 | |
| 8737 | PSA_ASSERT(psa_crypto_init()); |
| 8738 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8739 | |
| 8740 | TEST_EQUAL(psa_key_derivation_input_integer(&operation, |
| 8741 | PSA_KEY_DERIVATION_INPUT_COST, |
| 8742 | cost), |
| 8743 | PSA_ERROR_NOT_SUPPORTED); |
| 8744 | |
| 8745 | exit: |
| 8746 | psa_key_derivation_abort(&operation); |
| 8747 | PSA_DONE(); |
| 8748 | } |
| 8749 | /* END_CASE*/ |
| 8750 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8751 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8752 | void derive_over_capacity(int alg_arg) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8753 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8754 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8755 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 8756 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8757 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8758 | unsigned char input1[] = "Input 1"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8759 | size_t input1_length = sizeof(input1); |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8760 | unsigned char input2[] = "Input 2"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8761 | size_t input2_length = sizeof(input2); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8762 | uint8_t buffer[42]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8763 | size_t capacity = sizeof(buffer); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 8764 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 8765 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8766 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8767 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8768 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8769 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8771 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8772 | psa_set_key_algorithm(&attributes, alg); |
| 8773 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8774 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8775 | PSA_ASSERT(psa_import_key(&attributes, |
| 8776 | key_data, sizeof(key_data), |
| 8777 | &key)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8778 | |
| 8779 | /* valid key derivation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8780 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8781 | input1, input1_length, |
| 8782 | input2, input2_length, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 8783 | capacity, 0)) { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8784 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8785 | } |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8786 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8787 | /* state of operation shouldn't allow additional generation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8788 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8789 | PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8790 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8791 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8792 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8793 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity), |
| 8794 | PSA_ERROR_INSUFFICIENT_DATA); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8795 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8796 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8797 | psa_key_derivation_abort(&operation); |
| 8798 | psa_destroy_key(key); |
| 8799 | PSA_DONE(); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8800 | } |
| 8801 | /* END_CASE */ |
| 8802 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8803 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8804 | void derive_actions_without_setup() |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8805 | { |
| 8806 | uint8_t output_buffer[16]; |
| 8807 | size_t buffer_size = 16; |
| 8808 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8809 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8810 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8811 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8812 | output_buffer, buffer_size) |
| 8813 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8814 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8815 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8816 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8817 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8818 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8819 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8820 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8821 | output_buffer, buffer_size) |
| 8822 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8823 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8824 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8825 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8826 | |
| 8827 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8828 | psa_key_derivation_abort(&operation); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8829 | } |
| 8830 | /* END_CASE */ |
| 8831 | |
| 8832 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8833 | void derive_output(int alg_arg, |
| 8834 | int step1_arg, data_t *input1, int expected_status_arg1, |
| 8835 | int step2_arg, data_t *input2, int expected_status_arg2, |
| 8836 | int step3_arg, data_t *input3, int expected_status_arg3, |
| 8837 | int step4_arg, data_t *input4, int expected_status_arg4, |
| 8838 | data_t *key_agreement_peer_key, |
| 8839 | int requested_capacity_arg, |
| 8840 | data_t *expected_output1, |
| 8841 | data_t *expected_output2, |
| 8842 | int other_key_input_type, |
| 8843 | int key_input_type, |
| 8844 | int derive_type) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8845 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8846 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8847 | psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg }; |
| 8848 | data_t *inputs[] = { input1, input2, input3, input4 }; |
| 8849 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8850 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8851 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8852 | MBEDTLS_SVC_KEY_ID_INIT }; |
| 8853 | psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2, |
| 8854 | expected_status_arg3, expected_status_arg4 }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8855 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8856 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8857 | uint8_t *expected_outputs[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8858 | { expected_output1->x, expected_output2->x }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8859 | size_t output_sizes[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8860 | { expected_output1->len, expected_output2->len }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8861 | size_t output_buffer_size = 0; |
| 8862 | uint8_t *output_buffer = NULL; |
| 8863 | size_t expected_capacity; |
| 8864 | size_t current_capacity; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8865 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; |
| 8866 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; |
| 8867 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; |
| 8868 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8869 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8870 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8871 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8872 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8873 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
| 8874 | if (output_sizes[i] > output_buffer_size) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8875 | output_buffer_size = output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8876 | } |
| 8877 | if (output_sizes[i] == 0) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8878 | expected_outputs[i] = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8879 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8880 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8881 | TEST_CALLOC(output_buffer, output_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8882 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8883 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8884 | /* Extraction phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8885 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8886 | PSA_ASSERT(psa_key_derivation_set_capacity(&operation, |
| 8887 | requested_capacity)); |
| 8888 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8889 | switch (steps[i]) { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8890 | case 0: |
| 8891 | break; |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 8892 | case PSA_KEY_DERIVATION_INPUT_COST: |
| 8893 | TEST_EQUAL(psa_key_derivation_input_integer( |
Kusumit Ghoderao | f28e0f5 | 2023-06-06 15:03:22 +0530 | [diff] [blame] | 8894 | &operation, steps[i], |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 8895 | mbedtls_test_parse_binary_string(inputs[i])), |
Kusumit Ghoderao | f28e0f5 | 2023-06-06 15:03:22 +0530 | [diff] [blame] | 8896 | statuses[i]); |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 8897 | if (statuses[i] != PSA_SUCCESS) { |
| 8898 | goto exit; |
| 8899 | } |
| 8900 | break; |
| 8901 | case PSA_KEY_DERIVATION_INPUT_PASSWORD: |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8902 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8903 | switch (key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8904 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8905 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8906 | &operation, steps[i], |
| 8907 | inputs[i]->x, inputs[i]->len), |
| 8908 | statuses[i]); |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8909 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8910 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8911 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8912 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8913 | break; |
| 8914 | case 1: // input key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8915 | psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE); |
| 8916 | psa_set_key_algorithm(&attributes1, alg); |
| 8917 | psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8918 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8919 | PSA_ASSERT(psa_import_key(&attributes1, |
| 8920 | inputs[i]->x, inputs[i]->len, |
| 8921 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8922 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8923 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 8924 | PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1)); |
| 8925 | TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)), |
| 8926 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8927 | } |
| 8928 | |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 8929 | TEST_EQUAL(psa_key_derivation_input_key(&operation, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8930 | steps[i], |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 8931 | keys[i]), |
| 8932 | statuses[i]); |
| 8933 | |
| 8934 | if (statuses[i] != PSA_SUCCESS) { |
| 8935 | goto exit; |
| 8936 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8937 | break; |
| 8938 | default: |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 8939 | TEST_FAIL("default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8940 | break; |
| 8941 | } |
| 8942 | break; |
| 8943 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8944 | switch (other_key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8945 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8946 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 8947 | steps[i], |
| 8948 | inputs[i]->x, |
| 8949 | inputs[i]->len), |
| 8950 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8951 | break; |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 8952 | case 1: // input key, type DERIVE |
| 8953 | case 11: // input key, type RAW |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8954 | psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE); |
| 8955 | psa_set_key_algorithm(&attributes2, alg); |
| 8956 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8957 | |
| 8958 | // other secret of type RAW_DATA passed with input_key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8959 | if (other_key_input_type == 11) { |
| 8960 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA); |
| 8961 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8962 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8963 | PSA_ASSERT(psa_import_key(&attributes2, |
| 8964 | inputs[i]->x, inputs[i]->len, |
| 8965 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8966 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8967 | TEST_EQUAL(psa_key_derivation_input_key(&operation, |
| 8968 | steps[i], |
| 8969 | keys[i]), |
| 8970 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8971 | break; |
| 8972 | case 2: // key agreement |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8973 | psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE); |
| 8974 | psa_set_key_algorithm(&attributes3, alg); |
| 8975 | psa_set_key_type(&attributes3, |
| 8976 | PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8977 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8978 | PSA_ASSERT(psa_import_key(&attributes3, |
| 8979 | inputs[i]->x, inputs[i]->len, |
| 8980 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8981 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8982 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 8983 | &operation, |
| 8984 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 8985 | keys[i], key_agreement_peer_key->x, |
| 8986 | key_agreement_peer_key->len), statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8987 | break; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8988 | default: |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 8989 | TEST_FAIL("default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8990 | break; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8991 | } |
| 8992 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8993 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8994 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8995 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8996 | break; |
| 8997 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8998 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8999 | &operation, steps[i], |
| 9000 | inputs[i]->x, inputs[i]->len), statuses[i]); |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 9001 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9002 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 9003 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9004 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 9005 | break; |
| 9006 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 9007 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 9008 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9009 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 9010 | ¤t_capacity)); |
| 9011 | TEST_EQUAL(current_capacity, requested_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9012 | expected_capacity = requested_capacity; |
| 9013 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9014 | if (derive_type == 1) { // output key |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 9015 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; |
| 9016 | |
| 9017 | /* For output key derivation secret must be provided using |
| 9018 | input key, otherwise operation is not permitted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9019 | if (key_input_type == 1) { |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 9020 | expected_status = PSA_SUCCESS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9021 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9022 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9023 | psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT); |
| 9024 | psa_set_key_algorithm(&attributes4, alg); |
| 9025 | psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE); |
| 9026 | psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9027 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9028 | TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation, |
| 9029 | &derived_key), expected_status); |
| 9030 | } else { // output bytes |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9031 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9032 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9033 | /* Read some bytes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9034 | status = psa_key_derivation_output_bytes(&operation, |
| 9035 | output_buffer, output_sizes[i]); |
| 9036 | if (expected_capacity == 0 && output_sizes[i] == 0) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9037 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9038 | TEST_ASSERT(status == PSA_SUCCESS || |
| 9039 | status == PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9040 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9041 | } else if (expected_capacity == 0 || |
| 9042 | output_sizes[i] > expected_capacity) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9043 | /* Capacity exceeded. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9044 | TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9045 | expected_capacity = 0; |
| 9046 | continue; |
| 9047 | } |
| 9048 | /* Success. Check the read data. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9049 | PSA_ASSERT(status); |
| 9050 | if (output_sizes[i] != 0) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9051 | TEST_MEMORY_COMPARE(output_buffer, output_sizes[i], |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9052 | expected_outputs[i], output_sizes[i]); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9053 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9054 | /* Check the operation status. */ |
| 9055 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9056 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 9057 | ¤t_capacity)); |
| 9058 | TEST_EQUAL(expected_capacity, current_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9059 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9060 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9061 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9062 | |
| 9063 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9064 | mbedtls_free(output_buffer); |
| 9065 | psa_key_derivation_abort(&operation); |
| 9066 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 9067 | psa_destroy_key(keys[i]); |
| 9068 | } |
| 9069 | psa_destroy_key(derived_key); |
| 9070 | PSA_DONE(); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9071 | } |
| 9072 | /* END_CASE */ |
| 9073 | |
| 9074 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9075 | void derive_full(int alg_arg, |
| 9076 | data_t *key_data, |
| 9077 | data_t *input1, |
| 9078 | data_t *input2, |
| 9079 | int requested_capacity_arg) |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9080 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9081 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9082 | psa_algorithm_t alg = alg_arg; |
| 9083 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9084 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Kusumit Ghoderao | 9ffd397 | 2023-12-01 16:40:13 +0530 | [diff] [blame] | 9085 | unsigned char output_buffer[32]; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9086 | size_t expected_capacity = requested_capacity; |
| 9087 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9088 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9089 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9090 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9091 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9092 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9093 | psa_set_key_algorithm(&attributes, alg); |
| 9094 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9095 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9096 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 9097 | &key)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9098 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9099 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 9100 | input1->x, input1->len, |
| 9101 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9102 | requested_capacity, 0)) { |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 9103 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9104 | } |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 9105 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9106 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 9107 | ¤t_capacity)); |
| 9108 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9109 | |
| 9110 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9111 | while (current_capacity > 0) { |
| 9112 | size_t read_size = sizeof(output_buffer); |
| 9113 | if (read_size > current_capacity) { |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9114 | read_size = current_capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9115 | } |
| 9116 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9117 | output_buffer, |
| 9118 | read_size)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9119 | expected_capacity -= read_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9120 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 9121 | ¤t_capacity)); |
| 9122 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9123 | } |
| 9124 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9125 | /* Check that the operation refuses to go over capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9126 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1), |
| 9127 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9128 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9129 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9130 | |
| 9131 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9132 | psa_key_derivation_abort(&operation); |
| 9133 | psa_destroy_key(key); |
| 9134 | PSA_DONE(); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9135 | } |
| 9136 | /* END_CASE */ |
| 9137 | |
Stephan Koch | 78109f5 | 2023-04-12 14:19:36 +0200 | [diff] [blame] | 9138 | /* 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] | 9139 | void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg, |
| 9140 | int derivation_step, |
| 9141 | int capacity, int expected_capacity_status_arg, |
| 9142 | data_t *expected_output, |
| 9143 | int expected_output_status_arg) |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9144 | { |
| 9145 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 9146 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | d378504 | 2022-09-16 06:45:44 -0400 | [diff] [blame] | 9147 | 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] | 9148 | uint8_t *output_buffer = NULL; |
| 9149 | psa_status_t status; |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 9150 | psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg; |
| 9151 | psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg; |
| 9152 | 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] | 9153 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9154 | TEST_CALLOC(output_buffer, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9155 | PSA_ASSERT(psa_crypto_init()); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9156 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9157 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9158 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 9159 | expected_capacity_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9160 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9161 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 9162 | step, input->x, input->len), |
| 9163 | expected_input_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9164 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9165 | if (((psa_status_t) expected_input_status) != PSA_SUCCESS) { |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9166 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9167 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9168 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9169 | status = psa_key_derivation_output_bytes(&operation, output_buffer, |
| 9170 | expected_output->len); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9172 | TEST_EQUAL(status, expected_output_status); |
| 9173 | if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9174 | TEST_MEMORY_COMPARE(output_buffer, expected_output->len, expected_output->x, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9175 | expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9176 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9177 | |
| 9178 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9179 | mbedtls_free(output_buffer); |
| 9180 | psa_key_derivation_abort(&operation); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9181 | PSA_DONE(); |
| 9182 | } |
| 9183 | /* END_CASE */ |
| 9184 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9185 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9186 | void derive_key_exercise(int alg_arg, |
| 9187 | data_t *key_data, |
| 9188 | data_t *input1, |
| 9189 | data_t *input2, |
| 9190 | int derived_type_arg, |
| 9191 | int derived_bits_arg, |
| 9192 | int derived_usage_arg, |
| 9193 | int derived_alg_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9194 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9195 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9196 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9197 | psa_algorithm_t alg = alg_arg; |
| 9198 | psa_key_type_t derived_type = derived_type_arg; |
| 9199 | size_t derived_bits = derived_bits_arg; |
| 9200 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 9201 | psa_algorithm_t derived_alg = derived_alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9202 | size_t capacity = PSA_BITS_TO_BYTES(derived_bits); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9203 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9204 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 9205 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9206 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9207 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9208 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9209 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9210 | psa_set_key_algorithm(&attributes, alg); |
| 9211 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
| 9212 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 9213 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9214 | |
| 9215 | /* Derive a key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9216 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9217 | input1->x, input1->len, |
| 9218 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9219 | capacity, 0)) { |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9220 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9221 | } |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9222 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9223 | psa_set_key_usage_flags(&attributes, derived_usage); |
| 9224 | psa_set_key_algorithm(&attributes, derived_alg); |
| 9225 | psa_set_key_type(&attributes, derived_type); |
| 9226 | psa_set_key_bits(&attributes, derived_bits); |
| 9227 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation, |
| 9228 | &derived_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9229 | |
| 9230 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9231 | PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes)); |
| 9232 | TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type); |
| 9233 | TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9234 | |
| 9235 | /* Exercise the derived key. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 9236 | 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] | 9237 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9238 | } |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9239 | |
| 9240 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9241 | /* |
| 9242 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9243 | * thus reset them as required. |
| 9244 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9245 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9246 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9247 | psa_key_derivation_abort(&operation); |
| 9248 | psa_destroy_key(base_key); |
| 9249 | psa_destroy_key(derived_key); |
| 9250 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9251 | } |
| 9252 | /* END_CASE */ |
| 9253 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9254 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9255 | void derive_key_export(int alg_arg, |
| 9256 | data_t *key_data, |
| 9257 | data_t *input1, |
| 9258 | data_t *input2, |
| 9259 | int bytes1_arg, |
| 9260 | int bytes2_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9261 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9262 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9263 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9264 | psa_algorithm_t alg = alg_arg; |
| 9265 | size_t bytes1 = bytes1_arg; |
| 9266 | size_t bytes2 = bytes2_arg; |
| 9267 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9268 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9269 | uint8_t *output_buffer = NULL; |
| 9270 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9271 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9272 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9273 | size_t length; |
| 9274 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9275 | TEST_CALLOC(output_buffer, capacity); |
| 9276 | TEST_CALLOC(export_buffer, capacity); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9277 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9278 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9279 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9280 | psa_set_key_algorithm(&base_attributes, alg); |
| 9281 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9282 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9283 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9284 | |
| 9285 | /* Derive some material and output it. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9286 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9287 | input1->x, input1->len, |
| 9288 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9289 | capacity, 0)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9290 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9291 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9292 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9293 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9294 | output_buffer, |
| 9295 | capacity)); |
| 9296 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9297 | |
| 9298 | /* 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] | 9299 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9300 | input1->x, input1->len, |
| 9301 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9302 | capacity, 0)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9303 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9304 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9306 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9307 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9308 | psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA); |
| 9309 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1)); |
| 9310 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9311 | &derived_key)); |
| 9312 | PSA_ASSERT(psa_export_key(derived_key, |
| 9313 | export_buffer, bytes1, |
| 9314 | &length)); |
| 9315 | TEST_EQUAL(length, bytes1); |
| 9316 | PSA_ASSERT(psa_destroy_key(derived_key)); |
| 9317 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2)); |
| 9318 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9319 | &derived_key)); |
| 9320 | PSA_ASSERT(psa_export_key(derived_key, |
| 9321 | export_buffer + bytes1, bytes2, |
| 9322 | &length)); |
| 9323 | TEST_EQUAL(length, bytes2); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9324 | |
| 9325 | /* Compare the outputs from the two runs. */ |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9326 | TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9327 | export_buffer, capacity); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9328 | |
| 9329 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9330 | mbedtls_free(output_buffer); |
| 9331 | mbedtls_free(export_buffer); |
| 9332 | psa_key_derivation_abort(&operation); |
| 9333 | psa_destroy_key(base_key); |
| 9334 | psa_destroy_key(derived_key); |
| 9335 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9336 | } |
| 9337 | /* END_CASE */ |
| 9338 | |
| 9339 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9340 | void derive_key_type(int alg_arg, |
| 9341 | data_t *key_data, |
| 9342 | data_t *input1, |
| 9343 | data_t *input2, |
| 9344 | int key_type_arg, int bits_arg, |
| 9345 | data_t *expected_export) |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9346 | { |
| 9347 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9348 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9349 | const psa_algorithm_t alg = alg_arg; |
| 9350 | const psa_key_type_t key_type = key_type_arg; |
| 9351 | const size_t bits = bits_arg; |
| 9352 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9353 | const size_t export_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9354 | PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9355 | uint8_t *export_buffer = NULL; |
| 9356 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9357 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9358 | size_t export_length; |
| 9359 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9360 | TEST_CALLOC(export_buffer, export_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9361 | PSA_ASSERT(psa_crypto_init()); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9362 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9363 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9364 | psa_set_key_algorithm(&base_attributes, alg); |
| 9365 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9366 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9367 | &base_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9368 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9369 | if (mbedtls_test_psa_setup_key_derivation_wrap( |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9370 | &operation, base_key, alg, |
| 9371 | input1->x, input1->len, |
| 9372 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9373 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY, 0) == 0) { |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9374 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9375 | } |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9377 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9378 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9379 | psa_set_key_type(&derived_attributes, key_type); |
| 9380 | psa_set_key_bits(&derived_attributes, bits); |
| 9381 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9382 | &derived_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9383 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9384 | PSA_ASSERT(psa_export_key(derived_key, |
| 9385 | export_buffer, export_buffer_size, |
| 9386 | &export_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9387 | TEST_MEMORY_COMPARE(export_buffer, export_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9388 | expected_export->x, expected_export->len); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9389 | |
| 9390 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9391 | mbedtls_free(export_buffer); |
| 9392 | psa_key_derivation_abort(&operation); |
| 9393 | psa_destroy_key(base_key); |
| 9394 | psa_destroy_key(derived_key); |
| 9395 | PSA_DONE(); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9396 | } |
| 9397 | /* END_CASE */ |
| 9398 | |
| 9399 | /* BEGIN_CASE */ |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9400 | void derive_key_ext(int alg_arg, |
| 9401 | data_t *key_data, |
| 9402 | data_t *input1, |
| 9403 | data_t *input2, |
| 9404 | int key_type_arg, int bits_arg, |
Gilles Peskine | c81393b | 2024-02-14 20:51:28 +0100 | [diff] [blame] | 9405 | int flags_arg, |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 9406 | data_t *params_data, |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9407 | psa_status_t expected_status, |
| 9408 | data_t *expected_export) |
| 9409 | { |
| 9410 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9411 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9412 | const psa_algorithm_t alg = alg_arg; |
| 9413 | const psa_key_type_t key_type = key_type_arg; |
| 9414 | const size_t bits = bits_arg; |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 9415 | psa_key_production_parameters_t *params = NULL; |
| 9416 | size_t params_data_length = 0; |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9417 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9418 | const size_t export_buffer_size = |
| 9419 | PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits); |
| 9420 | uint8_t *export_buffer = NULL; |
| 9421 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9422 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9423 | size_t export_length; |
| 9424 | |
| 9425 | TEST_CALLOC(export_buffer, export_buffer_size); |
| 9426 | PSA_ASSERT(psa_crypto_init()); |
| 9427 | |
| 9428 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9429 | psa_set_key_algorithm(&base_attributes, alg); |
| 9430 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9431 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9432 | &base_key)); |
| 9433 | |
| 9434 | if (mbedtls_test_psa_setup_key_derivation_wrap( |
| 9435 | &operation, base_key, alg, |
| 9436 | input1->x, input1->len, |
| 9437 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9438 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY, 0) == 0) { |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9439 | goto exit; |
| 9440 | } |
| 9441 | |
| 9442 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9443 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9444 | psa_set_key_type(&derived_attributes, key_type); |
| 9445 | psa_set_key_bits(&derived_attributes, bits); |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 9446 | if (!setup_key_production_parameters(¶ms, ¶ms_data_length, |
| 9447 | flags_arg, params_data)) { |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9448 | goto exit; |
| 9449 | } |
| 9450 | |
| 9451 | TEST_EQUAL(psa_key_derivation_output_key_ext(&derived_attributes, &operation, |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 9452 | params, params_data_length, |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9453 | &derived_key), |
| 9454 | expected_status); |
| 9455 | |
| 9456 | if (expected_status == PSA_SUCCESS) { |
| 9457 | PSA_ASSERT(psa_export_key(derived_key, |
| 9458 | export_buffer, export_buffer_size, |
| 9459 | &export_length)); |
| 9460 | TEST_MEMORY_COMPARE(export_buffer, export_length, |
| 9461 | expected_export->x, expected_export->len); |
| 9462 | } |
| 9463 | |
| 9464 | exit: |
| 9465 | mbedtls_free(export_buffer); |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 9466 | mbedtls_free(params); |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9467 | psa_key_derivation_abort(&operation); |
| 9468 | psa_destroy_key(base_key); |
| 9469 | psa_destroy_key(derived_key); |
| 9470 | PSA_DONE(); |
| 9471 | } |
| 9472 | /* END_CASE */ |
| 9473 | |
| 9474 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9475 | void derive_key(int alg_arg, |
| 9476 | data_t *key_data, data_t *input1, data_t *input2, |
| 9477 | int type_arg, int bits_arg, |
| 9478 | int expected_status_arg, |
| 9479 | int is_large_output) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9480 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9481 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9482 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9483 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 9484 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9485 | size_t bits = bits_arg; |
| 9486 | psa_status_t expected_status = expected_status_arg; |
| 9487 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9488 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9489 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9490 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9491 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9492 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9493 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9494 | psa_set_key_algorithm(&base_attributes, alg); |
| 9495 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9496 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9497 | &base_key)); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9498 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9499 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9500 | input1->x, input1->len, |
| 9501 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9502 | SIZE_MAX, 0)) { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9503 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9504 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9505 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9506 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9507 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9508 | psa_set_key_type(&derived_attributes, type); |
| 9509 | psa_set_key_bits(&derived_attributes, bits); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9510 | |
| 9511 | psa_status_t status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9512 | psa_key_derivation_output_key(&derived_attributes, |
| 9513 | &operation, |
| 9514 | &derived_key); |
| 9515 | if (is_large_output > 0) { |
| 9516 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9517 | } |
| 9518 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9519 | |
| 9520 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9521 | psa_key_derivation_abort(&operation); |
| 9522 | psa_destroy_key(base_key); |
| 9523 | psa_destroy_key(derived_key); |
| 9524 | PSA_DONE(); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9525 | } |
| 9526 | /* END_CASE */ |
| 9527 | |
| 9528 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9529 | void key_agreement_setup(int alg_arg, |
| 9530 | int our_key_type_arg, int our_key_alg_arg, |
| 9531 | data_t *our_key_data, data_t *peer_key_data, |
| 9532 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9533 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9534 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9535 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 9536 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9537 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9538 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9539 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9540 | psa_status_t expected_status = expected_status_arg; |
| 9541 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9542 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9543 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9544 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9545 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9546 | psa_set_key_algorithm(&attributes, our_key_alg); |
| 9547 | psa_set_key_type(&attributes, our_key_type); |
| 9548 | PSA_ASSERT(psa_import_key(&attributes, |
| 9549 | our_key_data->x, our_key_data->len, |
| 9550 | &our_key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9551 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9552 | /* The tests currently include inputs that should fail at either step. |
| 9553 | * Test cases that fail at the setup step should be changed to call |
| 9554 | * key_derivation_setup instead, and this function should be renamed |
| 9555 | * to key_agreement_fail. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9556 | status = psa_key_derivation_setup(&operation, alg); |
| 9557 | if (status == PSA_SUCCESS) { |
| 9558 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 9559 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 9560 | our_key, |
| 9561 | peer_key_data->x, peer_key_data->len), |
| 9562 | expected_status); |
| 9563 | } else { |
| 9564 | TEST_ASSERT(status == expected_status); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9565 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9566 | |
| 9567 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9568 | psa_key_derivation_abort(&operation); |
| 9569 | psa_destroy_key(our_key); |
| 9570 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9571 | } |
| 9572 | /* END_CASE */ |
| 9573 | |
| 9574 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9575 | void raw_key_agreement(int alg_arg, |
| 9576 | int our_key_type_arg, data_t *our_key_data, |
| 9577 | data_t *peer_key_data, |
| 9578 | data_t *expected_output) |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9579 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9580 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9581 | psa_algorithm_t alg = alg_arg; |
| 9582 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9583 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9584 | unsigned char *output = NULL; |
| 9585 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9586 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9587 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9588 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9589 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9590 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9591 | psa_set_key_algorithm(&attributes, alg); |
| 9592 | psa_set_key_type(&attributes, our_key_type); |
| 9593 | PSA_ASSERT(psa_import_key(&attributes, |
| 9594 | our_key_data->x, our_key_data->len, |
| 9595 | &our_key)); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9596 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9597 | PSA_ASSERT(psa_get_key_attributes(our_key, &attributes)); |
| 9598 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9599 | |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9600 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9601 | TEST_LE_U(expected_output->len, |
| 9602 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits)); |
| 9603 | TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits), |
| 9604 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9605 | |
| 9606 | /* Good case with exact output size */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9607 | TEST_CALLOC(output, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9608 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9609 | peer_key_data->x, peer_key_data->len, |
| 9610 | output, expected_output->len, |
| 9611 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9612 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9613 | expected_output->x, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9614 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9615 | output = NULL; |
| 9616 | output_length = ~0; |
| 9617 | |
| 9618 | /* Larger buffer */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9619 | TEST_CALLOC(output, expected_output->len + 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9620 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9621 | peer_key_data->x, peer_key_data->len, |
| 9622 | output, expected_output->len + 1, |
| 9623 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9624 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9625 | expected_output->x, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9626 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9627 | output = NULL; |
| 9628 | output_length = ~0; |
| 9629 | |
| 9630 | /* Buffer too small */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9631 | TEST_CALLOC(output, expected_output->len - 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9632 | TEST_EQUAL(psa_raw_key_agreement(alg, our_key, |
| 9633 | peer_key_data->x, peer_key_data->len, |
| 9634 | output, expected_output->len - 1, |
| 9635 | &output_length), |
| 9636 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9637 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9638 | TEST_LE_U(output_length, expected_output->len - 1); |
| 9639 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9640 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9641 | |
| 9642 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9643 | mbedtls_free(output); |
| 9644 | psa_destroy_key(our_key); |
| 9645 | PSA_DONE(); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9646 | } |
| 9647 | /* END_CASE */ |
| 9648 | |
| 9649 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9650 | void key_agreement_capacity(int alg_arg, |
| 9651 | int our_key_type_arg, data_t *our_key_data, |
| 9652 | data_t *peer_key_data, |
| 9653 | int expected_capacity_arg) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9654 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9655 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9656 | psa_algorithm_t alg = alg_arg; |
| 9657 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9658 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9659 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9660 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9661 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9662 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9663 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9664 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9665 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9666 | psa_set_key_algorithm(&attributes, alg); |
| 9667 | psa_set_key_type(&attributes, our_key_type); |
| 9668 | PSA_ASSERT(psa_import_key(&attributes, |
| 9669 | our_key_data->x, our_key_data->len, |
| 9670 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9671 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9672 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9673 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9674 | &operation, |
| 9675 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9676 | peer_key_data->x, peer_key_data->len)); |
| 9677 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9678 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9679 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9680 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9681 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9682 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9683 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 9684 | /* Test the advertised capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9685 | PSA_ASSERT(psa_key_derivation_get_capacity( |
| 9686 | &operation, &actual_capacity)); |
| 9687 | TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9688 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9689 | /* Test the actual capacity by reading the output. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9690 | while (actual_capacity > sizeof(output)) { |
| 9691 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9692 | output, sizeof(output))); |
| 9693 | actual_capacity -= sizeof(output); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9694 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9695 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9696 | output, actual_capacity)); |
| 9697 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1), |
| 9698 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9699 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9700 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9701 | psa_key_derivation_abort(&operation); |
| 9702 | psa_destroy_key(our_key); |
| 9703 | PSA_DONE(); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9704 | } |
| 9705 | /* END_CASE */ |
| 9706 | |
Valerio Setti | ad81967 | 2023-12-29 12:14:41 +0100 | [diff] [blame] | 9707 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */ |
| 9708 | 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] | 9709 | { |
| 9710 | mbedtls_ecp_group_id grp_id = grp_id_arg; |
Valerio Setti | ad81967 | 2023-12-29 12:14:41 +0100 | [diff] [blame] | 9711 | psa_ecc_family_t ecc_family = psa_family_arg; |
| 9712 | size_t bits = bits_arg; |
| 9713 | size_t bits_tmp; |
Valerio Setti | bf999cb | 2023-12-28 17:48:13 +0100 | [diff] [blame] | 9714 | |
Valerio Setti | ad81967 | 2023-12-29 12:14:41 +0100 | [diff] [blame] | 9715 | TEST_EQUAL(ecc_family, mbedtls_ecc_group_to_psa(grp_id, &bits_tmp)); |
| 9716 | TEST_EQUAL(bits, bits_tmp); |
Valerio Setti | ac73952 | 2024-01-04 10:22:01 +0100 | [diff] [blame] | 9717 | TEST_EQUAL(grp_id, mbedtls_ecc_group_from_psa(ecc_family, bits)); |
Valerio Setti | bf999cb | 2023-12-28 17:48:13 +0100 | [diff] [blame] | 9718 | } |
| 9719 | /* END_CASE */ |
| 9720 | |
Valerio Setti | ac73952 | 2024-01-04 10:22:01 +0100 | [diff] [blame] | 9721 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */ |
| 9722 | void ecc_conversion_functions_fail() |
| 9723 | { |
| 9724 | size_t bits; |
| 9725 | |
Valerio Setti | db6e029 | 2024-01-05 10:15:45 +0100 | [diff] [blame] | 9726 | /* Invalid legacy curve identifiers. */ |
| 9727 | TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_MAX, &bits)); |
| 9728 | TEST_EQUAL(0, bits); |
Valerio Setti | ac73952 | 2024-01-04 10:22:01 +0100 | [diff] [blame] | 9729 | TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_NONE, &bits)); |
| 9730 | TEST_EQUAL(0, bits); |
| 9731 | |
| 9732 | /* Invalid PSA EC family. */ |
| 9733 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(0, 192)); |
| 9734 | /* Invalid bit-size for a valid EC family. */ |
| 9735 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_SECP_R1, 512)); |
| 9736 | |
| 9737 | /* Twisted-Edward curves are not supported yet. */ |
| 9738 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, |
| 9739 | mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 255)); |
| 9740 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, |
| 9741 | mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 448)); |
| 9742 | } |
| 9743 | /* END_CASE */ |
| 9744 | |
| 9745 | |
Valerio Setti | bf999cb | 2023-12-28 17:48:13 +0100 | [diff] [blame] | 9746 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9747 | void key_agreement_output(int alg_arg, |
| 9748 | int our_key_type_arg, data_t *our_key_data, |
| 9749 | data_t *peer_key_data, |
| 9750 | data_t *expected_output1, data_t *expected_output2) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9751 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9752 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9753 | psa_algorithm_t alg = alg_arg; |
| 9754 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9755 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9756 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9757 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9758 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9759 | TEST_CALLOC(actual_output, MAX(expected_output1->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9760 | expected_output2->len)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9761 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9762 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9763 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9764 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9765 | psa_set_key_algorithm(&attributes, alg); |
| 9766 | psa_set_key_type(&attributes, our_key_type); |
| 9767 | PSA_ASSERT(psa_import_key(&attributes, |
| 9768 | our_key_data->x, our_key_data->len, |
| 9769 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9771 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9772 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9773 | &operation, |
| 9774 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9775 | peer_key_data->x, peer_key_data->len)); |
| 9776 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9777 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9778 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9779 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9780 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9781 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9782 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9783 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9784 | actual_output, |
| 9785 | expected_output1->len)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9786 | TEST_MEMORY_COMPARE(actual_output, expected_output1->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9787 | expected_output1->x, expected_output1->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9788 | if (expected_output2->len != 0) { |
| 9789 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9790 | actual_output, |
| 9791 | expected_output2->len)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9792 | TEST_MEMORY_COMPARE(actual_output, expected_output2->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9793 | expected_output2->x, expected_output2->len); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9794 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9795 | |
| 9796 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9797 | psa_key_derivation_abort(&operation); |
| 9798 | psa_destroy_key(our_key); |
| 9799 | PSA_DONE(); |
| 9800 | mbedtls_free(actual_output); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9801 | } |
| 9802 | /* END_CASE */ |
| 9803 | |
| 9804 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9805 | void generate_random(int bytes_arg) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9806 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9807 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9808 | unsigned char *output = NULL; |
| 9809 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9810 | size_t i; |
| 9811 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9812 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9813 | TEST_ASSERT(bytes_arg >= 0); |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 9814 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9815 | TEST_CALLOC(output, bytes); |
| 9816 | TEST_CALLOC(changed, bytes); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9817 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9818 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9819 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9820 | /* Run several times, to ensure that every output byte will be |
| 9821 | * nonzero at least once with overwhelming probability |
| 9822 | * (2^(-8*number_of_runs)). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9823 | for (run = 0; run < 10; run++) { |
| 9824 | if (bytes != 0) { |
| 9825 | memset(output, 0, bytes); |
| 9826 | } |
| 9827 | PSA_ASSERT(psa_generate_random(output, bytes)); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9828 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9829 | for (i = 0; i < bytes; i++) { |
| 9830 | if (output[i] != 0) { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9831 | ++changed[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9832 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9833 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9834 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9835 | |
| 9836 | /* Check that every byte was changed to nonzero at least once. This |
| 9837 | * validates that psa_generate_random is overwriting every byte of |
| 9838 | * the output buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9839 | for (i = 0; i < bytes; i++) { |
| 9840 | TEST_ASSERT(changed[i] != 0); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9841 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9842 | |
| 9843 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9844 | PSA_DONE(); |
| 9845 | mbedtls_free(output); |
| 9846 | mbedtls_free(changed); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9847 | } |
| 9848 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9849 | |
Ryan | 3a1b786 | 2024-03-01 17:24:04 +0000 | [diff] [blame] | 9850 | #if defined MBEDTLS_THREADING_PTHREAD |
| 9851 | |
| 9852 | /* BEGIN_CASE depends_on:MBEDTLS_THREADING_PTHREAD */ |
| 9853 | void concurrently_generate_keys(int type_arg, |
| 9854 | int bits_arg, |
| 9855 | int usage_arg, |
| 9856 | int alg_arg, |
| 9857 | int expected_status_arg, |
| 9858 | int is_large_key_arg, |
| 9859 | int arg_thread_count, |
| 9860 | int reps_arg) |
| 9861 | { |
| 9862 | size_t thread_count = (size_t) arg_thread_count; |
| 9863 | mbedtls_test_thread_t *threads = NULL; |
| 9864 | generate_key_context gkc; |
| 9865 | gkc.type = type_arg; |
| 9866 | gkc.usage = usage_arg; |
| 9867 | gkc.bits = bits_arg; |
| 9868 | gkc.alg = alg_arg; |
| 9869 | gkc.expected_status = expected_status_arg; |
| 9870 | gkc.is_large_key = is_large_key_arg; |
| 9871 | gkc.reps = reps_arg; |
| 9872 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9873 | |
| 9874 | PSA_ASSERT(psa_crypto_init()); |
| 9875 | |
| 9876 | psa_set_key_usage_flags(&attributes, usage_arg); |
| 9877 | psa_set_key_algorithm(&attributes, alg_arg); |
| 9878 | psa_set_key_type(&attributes, type_arg); |
| 9879 | psa_set_key_bits(&attributes, bits_arg); |
| 9880 | gkc.attributes = &attributes; |
| 9881 | |
| 9882 | TEST_CALLOC(threads, sizeof(mbedtls_test_thread_t) * thread_count); |
| 9883 | |
| 9884 | /* Split threads to generate key then destroy key. */ |
| 9885 | for (size_t i = 0; i < thread_count; i++) { |
| 9886 | TEST_EQUAL( |
| 9887 | mbedtls_test_thread_create(&threads[i], thread_generate_key, |
| 9888 | (void *) &gkc), 0); |
| 9889 | } |
| 9890 | |
| 9891 | /* Join threads. */ |
| 9892 | for (size_t i = 0; i < thread_count; i++) { |
| 9893 | TEST_EQUAL(mbedtls_test_thread_join(&threads[i]), 0); |
| 9894 | } |
| 9895 | |
| 9896 | exit: |
| 9897 | mbedtls_free(threads); |
| 9898 | PSA_DONE(); |
| 9899 | } |
| 9900 | /* END_CASE */ |
| 9901 | #endif |
| 9902 | |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9903 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9904 | void generate_key(int type_arg, |
| 9905 | int bits_arg, |
| 9906 | int usage_arg, |
| 9907 | int alg_arg, |
| 9908 | int expected_status_arg, |
| 9909 | int is_large_key) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9910 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9911 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9912 | psa_key_type_t type = type_arg; |
| 9913 | psa_key_usage_t usage = usage_arg; |
| 9914 | size_t bits = bits_arg; |
| 9915 | psa_algorithm_t alg = alg_arg; |
| 9916 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9917 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 9918 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9919 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9920 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9921 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9922 | psa_set_key_usage_flags(&attributes, usage); |
| 9923 | psa_set_key_algorithm(&attributes, alg); |
| 9924 | psa_set_key_type(&attributes, type); |
| 9925 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9926 | |
| 9927 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9928 | psa_status_t status = psa_generate_key(&attributes, &key); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9929 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9930 | if (is_large_key > 0) { |
| 9931 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9932 | } |
| 9933 | TEST_EQUAL(status, expected_status); |
| 9934 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9935 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9936 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9937 | |
| 9938 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9939 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 9940 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 9941 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9942 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 9943 | /* Do something with the key according to its type and permitted usage. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 9944 | if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 9945 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9946 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9947 | |
| 9948 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9949 | /* |
| 9950 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9951 | * thus reset them as required. |
| 9952 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9953 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9954 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9955 | psa_destroy_key(key); |
| 9956 | PSA_DONE(); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9957 | } |
| 9958 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 9959 | |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9960 | /* BEGIN_CASE */ |
| 9961 | void generate_key_ext(int type_arg, |
| 9962 | int bits_arg, |
| 9963 | int usage_arg, |
| 9964 | int alg_arg, |
Gilles Peskine | c81393b | 2024-02-14 20:51:28 +0100 | [diff] [blame] | 9965 | int flags_arg, |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 9966 | data_t *params_data, |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9967 | int expected_status_arg) |
| 9968 | { |
| 9969 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9970 | psa_key_type_t type = type_arg; |
| 9971 | psa_key_usage_t usage = usage_arg; |
| 9972 | size_t bits = bits_arg; |
| 9973 | psa_algorithm_t alg = alg_arg; |
| 9974 | psa_status_t expected_status = expected_status_arg; |
| 9975 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 9976 | psa_key_production_parameters_t *params = NULL; |
| 9977 | size_t params_data_length = 0; |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9978 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9979 | |
| 9980 | PSA_ASSERT(psa_crypto_init()); |
| 9981 | |
| 9982 | psa_set_key_usage_flags(&attributes, usage); |
| 9983 | psa_set_key_algorithm(&attributes, alg); |
| 9984 | psa_set_key_type(&attributes, type); |
| 9985 | psa_set_key_bits(&attributes, bits); |
| 9986 | |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 9987 | if (!setup_key_production_parameters(¶ms, ¶ms_data_length, |
| 9988 | flags_arg, params_data)) { |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9989 | goto exit; |
| 9990 | } |
| 9991 | |
| 9992 | /* Generate a key */ |
| 9993 | psa_status_t status = psa_generate_key_ext(&attributes, |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 9994 | params, params_data_length, |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9995 | &key); |
| 9996 | |
| 9997 | TEST_EQUAL(status, expected_status); |
| 9998 | if (expected_status != PSA_SUCCESS) { |
| 9999 | goto exit; |
| 10000 | } |
| 10001 | |
| 10002 | /* Test the key information */ |
| 10003 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 10004 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 10005 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
| 10006 | |
Gilles Peskine | 7a18f96 | 2024-02-12 16:48:11 +0100 | [diff] [blame] | 10007 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) |
| 10008 | if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10009 | TEST_ASSERT(rsa_test_e(key, bits, params_data)); |
Gilles Peskine | 7a18f96 | 2024-02-12 16:48:11 +0100 | [diff] [blame] | 10010 | } |
| 10011 | #endif |
| 10012 | |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10013 | /* Do something with the key according to its type and permitted usage. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 10014 | if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) { |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10015 | goto exit; |
| 10016 | } |
| 10017 | |
| 10018 | exit: |
| 10019 | /* |
| 10020 | * Key attributes may have been returned by psa_get_key_attributes() |
| 10021 | * thus reset them as required. |
| 10022 | */ |
| 10023 | psa_reset_key_attributes(&got_attributes); |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10024 | mbedtls_free(params); |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10025 | psa_destroy_key(key); |
| 10026 | PSA_DONE(); |
| 10027 | } |
| 10028 | /* END_CASE */ |
| 10029 | |
| 10030 | /* BEGIN_CASE */ |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10031 | void key_production_parameters_init() |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10032 | { |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10033 | psa_key_production_parameters_t init = PSA_KEY_PRODUCTION_PARAMETERS_INIT; |
| 10034 | psa_key_production_parameters_t zero; |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10035 | memset(&zero, 0, sizeof(zero)); |
| 10036 | |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10037 | TEST_EQUAL(init.flags, 0); |
| 10038 | TEST_EQUAL(zero.flags, 0); |
| 10039 | } |
| 10040 | /* END_CASE */ |
| 10041 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10042 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10043 | void persistent_key_load_key_from_storage(data_t *data, |
| 10044 | int type_arg, int bits_arg, |
| 10045 | int usage_flags_arg, int alg_arg, |
| 10046 | int generation_method) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10047 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10048 | 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] | 10049 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 10050 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10051 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10052 | psa_key_type_t type = type_arg; |
| 10053 | size_t bits = bits_arg; |
| 10054 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 10055 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 10056 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10057 | unsigned char *first_export = NULL; |
| 10058 | unsigned char *second_export = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10059 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits); |
Sergey | bef1f63 | 2023-03-06 15:25:06 -0700 | [diff] [blame] | 10060 | size_t first_exported_length = 0; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10061 | size_t second_exported_length; |
| 10062 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10063 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 10064 | TEST_CALLOC(first_export, export_size); |
| 10065 | TEST_CALLOC(second_export, export_size); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10066 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10067 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10068 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10069 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10070 | psa_set_key_id(&attributes, key_id); |
| 10071 | psa_set_key_usage_flags(&attributes, usage_flags); |
| 10072 | psa_set_key_algorithm(&attributes, alg); |
| 10073 | psa_set_key_type(&attributes, type); |
| 10074 | psa_set_key_bits(&attributes, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10075 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10076 | switch (generation_method) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10077 | case IMPORT_KEY: |
| 10078 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10079 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, |
| 10080 | &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10081 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10082 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10083 | case GENERATE_KEY: |
| 10084 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10085 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10086 | break; |
| 10087 | |
| 10088 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 10089 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10090 | { |
| 10091 | /* Create base key */ |
| 10092 | psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256); |
| 10093 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10094 | psa_set_key_usage_flags(&base_attributes, |
| 10095 | PSA_KEY_USAGE_DERIVE); |
| 10096 | psa_set_key_algorithm(&base_attributes, derive_alg); |
| 10097 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 10098 | PSA_ASSERT(psa_import_key(&base_attributes, |
| 10099 | data->x, data->len, |
| 10100 | &base_key)); |
| 10101 | /* Derive a key. */ |
| 10102 | PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg)); |
| 10103 | PSA_ASSERT(psa_key_derivation_input_key( |
| 10104 | &operation, |
| 10105 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key)); |
| 10106 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 10107 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
| 10108 | NULL, 0)); |
| 10109 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, |
| 10110 | &operation, |
| 10111 | &key)); |
| 10112 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
| 10113 | PSA_ASSERT(psa_destroy_key(base_key)); |
| 10114 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10115 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 10116 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10117 | TEST_ASSUME(!"KDF not supported in this configuration"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 10118 | #endif |
| 10119 | break; |
| 10120 | |
| 10121 | default: |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 10122 | TEST_FAIL("generation_method not implemented in test"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 10123 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10124 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10125 | psa_reset_key_attributes(&attributes); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10126 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10127 | /* Export the key if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10128 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 10129 | PSA_ASSERT(psa_export_key(key, |
| 10130 | first_export, export_size, |
| 10131 | &first_exported_length)); |
| 10132 | if (generation_method == IMPORT_KEY) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 10133 | TEST_MEMORY_COMPARE(data->x, data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 10134 | first_export, first_exported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10135 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10136 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10137 | |
| 10138 | /* Shutdown and restart */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10139 | PSA_ASSERT(psa_purge_key(key)); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 10140 | PSA_DONE(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10141 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10142 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10143 | /* Check key slot still contains key data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10144 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 10145 | TEST_ASSERT(mbedtls_svc_key_id_equal( |
| 10146 | psa_get_key_id(&attributes), key_id)); |
| 10147 | TEST_EQUAL(psa_get_key_lifetime(&attributes), |
| 10148 | PSA_KEY_LIFETIME_PERSISTENT); |
| 10149 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 10150 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 10151 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 10152 | mbedtls_test_update_key_usage_flags(usage_flags)); |
| 10153 | TEST_EQUAL(psa_get_key_algorithm(&attributes), alg); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10154 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10155 | /* Export the key again if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10156 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 10157 | PSA_ASSERT(psa_export_key(key, |
| 10158 | second_export, export_size, |
| 10159 | &second_exported_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 10160 | TEST_MEMORY_COMPARE(first_export, first_exported_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 10161 | second_export, second_exported_length); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10162 | } |
| 10163 | |
| 10164 | /* Do something with the key according to its type and permitted usage. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 10165 | if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg, 0)) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10166 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10167 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10168 | |
| 10169 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 10170 | /* |
| 10171 | * Key attributes may have been returned by psa_get_key_attributes() |
| 10172 | * thus reset them as required. |
| 10173 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10174 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 10175 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10176 | mbedtls_free(first_export); |
| 10177 | mbedtls_free(second_export); |
| 10178 | psa_key_derivation_abort(&operation); |
| 10179 | psa_destroy_key(base_key); |
| 10180 | psa_destroy_key(key); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 10181 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10182 | } |
| 10183 | /* END_CASE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10184 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 10185 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10186 | void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg, |
| 10187 | int primitive_arg, int hash_arg, int role_arg, |
| 10188 | int test_input, data_t *pw_data, |
| 10189 | int inj_err_type_arg, |
| 10190 | int expected_error_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10191 | { |
| 10192 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10193 | psa_pake_operation_t operation = psa_pake_operation_init(); |
| 10194 | psa_algorithm_t alg = alg_arg; |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 10195 | psa_pake_primitive_t primitive = primitive_arg; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 10196 | psa_key_type_t key_type_pw = key_type_pw_arg; |
| 10197 | psa_key_usage_t key_usage_pw = key_usage_pw_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10198 | psa_algorithm_t hash_alg = hash_arg; |
| 10199 | psa_pake_role_t role = role_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10200 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10201 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10202 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
| 10203 | psa_status_t expected_error = expected_error_arg; |
| 10204 | psa_status_t status; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10205 | unsigned char *output_buffer = NULL; |
| 10206 | size_t output_len = 0; |
| 10207 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10208 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10209 | |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 10210 | size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10211 | PSA_PAKE_STEP_KEY_SHARE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 10212 | TEST_CALLOC(output_buffer, buf_size); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10213 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10214 | if (pw_data->len > 0) { |
| 10215 | psa_set_key_usage_flags(&attributes, key_usage_pw); |
| 10216 | psa_set_key_algorithm(&attributes, alg); |
| 10217 | psa_set_key_type(&attributes, key_type_pw); |
| 10218 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10219 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10220 | } |
| 10221 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10222 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10223 | psa_pake_cs_set_primitive(&cipher_suite, primitive); |
| 10224 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10225 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10226 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 10227 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10228 | if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) { |
| 10229 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 10230 | expected_error); |
| 10231 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10232 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 10233 | expected_error); |
| 10234 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10235 | TEST_EQUAL(psa_pake_set_password_key(&operation, key), |
| 10236 | expected_error); |
| 10237 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10238 | TEST_EQUAL(psa_pake_set_role(&operation, role), |
| 10239 | expected_error); |
| 10240 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10241 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10242 | NULL, 0, NULL), |
| 10243 | expected_error); |
| 10244 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10245 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0), |
| 10246 | expected_error); |
| 10247 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10248 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10249 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10250 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10251 | status = psa_pake_setup(&operation, &cipher_suite); |
| 10252 | if (status != PSA_SUCCESS) { |
| 10253 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10254 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10255 | } |
| 10256 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10257 | if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) { |
| 10258 | TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite), |
| 10259 | expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10260 | goto exit; |
| 10261 | } |
| 10262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10263 | status = psa_pake_set_role(&operation, role); |
| 10264 | if (status != PSA_SUCCESS) { |
| 10265 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10266 | goto exit; |
| 10267 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10268 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10269 | if (pw_data->len > 0) { |
| 10270 | status = psa_pake_set_password_key(&operation, key); |
| 10271 | if (status != PSA_SUCCESS) { |
| 10272 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10273 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10274 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10275 | } |
| 10276 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10277 | if (inj_err_type == INJECT_ERR_INVALID_USER) { |
| 10278 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 10279 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10280 | goto exit; |
| 10281 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 10282 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10283 | if (inj_err_type == INJECT_ERR_INVALID_PEER) { |
| 10284 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 10285 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10286 | goto exit; |
| 10287 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 10288 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10289 | if (inj_err_type == INJECT_ERR_SET_USER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10290 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10291 | TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4), |
| 10292 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10293 | goto exit; |
| 10294 | } |
| 10295 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10296 | if (inj_err_type == INJECT_ERR_SET_PEER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10297 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10298 | TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4), |
| 10299 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10300 | goto exit; |
| 10301 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 10302 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10303 | const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10304 | PSA_PAKE_STEP_KEY_SHARE); |
| 10305 | const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10306 | PSA_PAKE_STEP_ZK_PUBLIC); |
| 10307 | const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10308 | PSA_PAKE_STEP_ZK_PROOF); |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 10309 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10310 | if (test_input) { |
| 10311 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 10312 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0), |
| 10313 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10314 | goto exit; |
| 10315 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10316 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10317 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 10318 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 10319 | output_buffer, size_zk_proof), |
| 10320 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10321 | goto exit; |
| 10322 | } |
| 10323 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10324 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 10325 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10326 | output_buffer, size_zk_proof), |
| 10327 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10328 | goto exit; |
| 10329 | } |
| 10330 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10331 | status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10332 | output_buffer, size_key_share); |
| 10333 | if (status != PSA_SUCCESS) { |
| 10334 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10335 | goto exit; |
| 10336 | } |
| 10337 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10338 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 10339 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10340 | output_buffer, size_zk_public + 1), |
| 10341 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10342 | goto exit; |
| 10343 | } |
| 10344 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10345 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10346 | // 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] | 10347 | psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10348 | output_buffer, size_zk_public + 1); |
| 10349 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10350 | output_buffer, size_zk_public), |
| 10351 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10352 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10353 | } |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10354 | } else { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10355 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 10356 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10357 | NULL, 0, NULL), |
| 10358 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10359 | goto exit; |
| 10360 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10361 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10362 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 10363 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 10364 | output_buffer, buf_size, &output_len), |
| 10365 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10366 | goto exit; |
| 10367 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10368 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10369 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 10370 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10371 | output_buffer, buf_size, &output_len), |
| 10372 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10373 | goto exit; |
| 10374 | } |
| 10375 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10376 | status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10377 | output_buffer, buf_size, &output_len); |
| 10378 | if (status != PSA_SUCCESS) { |
| 10379 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10380 | goto exit; |
| 10381 | } |
| 10382 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10383 | TEST_ASSERT(output_len > 0); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10385 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 10386 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10387 | output_buffer, size_zk_public - 1, &output_len), |
| 10388 | PSA_ERROR_BUFFER_TOO_SMALL); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10389 | goto exit; |
| 10390 | } |
| 10391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10392 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10393 | // 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] | 10394 | psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10395 | output_buffer, size_zk_public - 1, &output_len); |
| 10396 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10397 | output_buffer, buf_size, &output_len), |
| 10398 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10399 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10400 | } |
| 10401 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10402 | |
| 10403 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10404 | PSA_ASSERT(psa_destroy_key(key)); |
| 10405 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10406 | mbedtls_free(output_buffer); |
| 10407 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10408 | } |
| 10409 | /* END_CASE */ |
| 10410 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 10411 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10412 | void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg, |
| 10413 | int client_input_first, int inject_error, |
| 10414 | data_t *pw_data) |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10415 | { |
| 10416 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10417 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10418 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10419 | psa_algorithm_t alg = alg_arg; |
| 10420 | psa_algorithm_t hash_alg = hash_arg; |
| 10421 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10422 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10423 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10424 | PSA_INIT(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10425 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10426 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10427 | psa_set_key_algorithm(&attributes, alg); |
| 10428 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10429 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10430 | &key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10431 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10432 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10433 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10434 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10435 | |
| 10436 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10437 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10438 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10439 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10440 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10441 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10442 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10443 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10444 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10445 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10446 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10447 | client_input_first, 1, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10449 | if (inject_error == 1 || inject_error == 2) { |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10450 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10451 | } |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10452 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10453 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10454 | client_input_first, 2, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10455 | |
| 10456 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10457 | psa_destroy_key(key); |
| 10458 | psa_pake_abort(&server); |
| 10459 | psa_pake_abort(&client); |
| 10460 | PSA_DONE(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10461 | } |
| 10462 | /* END_CASE */ |
| 10463 | |
| 10464 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10465 | void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg, |
| 10466 | int derive_alg_arg, data_t *pw_data, |
| 10467 | int client_input_first, int inj_err_type_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10468 | { |
| 10469 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10470 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10471 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10472 | psa_algorithm_t alg = alg_arg; |
| 10473 | psa_algorithm_t hash_alg = hash_arg; |
| 10474 | psa_algorithm_t derive_alg = derive_alg_arg; |
| 10475 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10476 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10477 | psa_key_derivation_operation_t server_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10478 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10479 | psa_key_derivation_operation_t client_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10480 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10481 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10482 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10483 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10484 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10485 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10486 | psa_set_key_algorithm(&attributes, alg); |
| 10487 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10488 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10489 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10490 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10491 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10492 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10493 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10494 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10495 | /* Get shared key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10496 | PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg)); |
| 10497 | PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10498 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10499 | if (PSA_ALG_IS_TLS12_PRF(derive_alg) || |
| 10500 | PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) { |
| 10501 | PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive, |
| 10502 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10503 | (const uint8_t *) "", 0)); |
| 10504 | PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive, |
| 10505 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10506 | (const uint8_t *) "", 0)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10507 | } |
| 10508 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10509 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10510 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10511 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10512 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10513 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10514 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10515 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10516 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10517 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10518 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) { |
| 10519 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10520 | PSA_ERROR_BAD_STATE); |
| 10521 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10522 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10523 | goto exit; |
| 10524 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10525 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10526 | /* First round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10527 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10528 | client_input_first, 1, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10529 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10530 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) { |
| 10531 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10532 | PSA_ERROR_BAD_STATE); |
| 10533 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10534 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10535 | goto exit; |
| 10536 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10537 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10538 | /* Second round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10539 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10540 | client_input_first, 2, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10541 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10542 | PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive)); |
| 10543 | PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10544 | |
| 10545 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10546 | psa_key_derivation_abort(&server_derive); |
| 10547 | psa_key_derivation_abort(&client_derive); |
| 10548 | psa_destroy_key(key); |
| 10549 | psa_pake_abort(&server); |
| 10550 | psa_pake_abort(&client); |
| 10551 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10552 | } |
| 10553 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10554 | |
| 10555 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10556 | void ecjpake_size_macros() |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10557 | { |
| 10558 | const psa_algorithm_t alg = PSA_ALG_JPAKE; |
| 10559 | const size_t bits = 256; |
| 10560 | const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10561 | 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] | 10562 | 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] | 10563 | PSA_ECC_FAMILY_SECP_R1); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10564 | |
| 10565 | // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types |
| 10566 | /* 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] | 10567 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10568 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
| 10569 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10570 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10571 | /* The output for ZK_PROOF is the same bitsize as the curve */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10572 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10573 | PSA_BITS_TO_BYTES(bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10574 | |
| 10575 | /* Input sizes are the same as output sizes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10576 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10577 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE)); |
| 10578 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10579 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC)); |
| 10580 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10581 | 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] | 10582 | |
| 10583 | /* These inequalities will always hold even when other PAKEs are added */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10584 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10585 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10586 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10587 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10588 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10589 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10590 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10591 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10592 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10593 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10594 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10595 | PSA_PAKE_INPUT_MAX_SIZE); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10596 | } |
| 10597 | /* END_CASE */ |