Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2 | #include <stdint.h> |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 3 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 4 | #include "mbedtls/asn1.h" |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 5 | #include "mbedtls/asn1write.h" |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 6 | #include "mbedtls/oid.h" |
Gilles Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 7 | #include "common.h" |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 8 | |
Valerio Setti | bf999cb | 2023-12-28 17:48:13 +0100 | [diff] [blame] | 9 | #include "mbedtls/psa_util.h" |
| 10 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 11 | /* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random() |
| 12 | * uses mbedtls_ctr_drbg internally. */ |
| 13 | #include "mbedtls/ctr_drbg.h" |
| 14 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 15 | #include "psa/crypto.h" |
Ronald Cron | 4184107 | 2020-09-17 15:28:26 +0200 | [diff] [blame] | 16 | #include "psa_crypto_slot_management.h" |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 17 | |
Manuel Pégourié-Gonnard | 7abdf7e | 2023-03-09 11:17:43 +0100 | [diff] [blame] | 18 | #include "psa_crypto_core.h" |
| 19 | |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 20 | #include "test/asn1_helpers.h" |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 21 | #include "test/psa_crypto_helpers.h" |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 22 | #include "test/psa_exercise_key.h" |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 23 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 24 | #include "test/drivers/test_driver.h" |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 25 | #define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION |
| 26 | #else |
| 27 | #define TEST_DRIVER_LOCATION 0x7fffff |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 28 | #endif |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 29 | |
Ryan | 3a1b786 | 2024-03-01 17:24:04 +0000 | [diff] [blame] | 30 | #if defined(MBEDTLS_THREADING_PTHREAD) |
| 31 | #include "mbedtls/threading.h" |
| 32 | #endif |
| 33 | |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 34 | /* If this comes up, it's a bug in the test code or in the test data. */ |
| 35 | #define UNUSED 0xdeadbeef |
| 36 | |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 37 | /* Assert that an operation is (not) active. |
| 38 | * This serves as a proxy for checking if the operation is aborted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 39 | #define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0) |
| 40 | #define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 41 | |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 42 | #if defined(PSA_WANT_ALG_JPAKE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 43 | int ecjpake_operation_setup(psa_pake_operation_t *operation, |
| 44 | psa_pake_cipher_suite_t *cipher_suite, |
| 45 | psa_pake_role_t role, |
| 46 | mbedtls_svc_key_id_t key, |
| 47 | size_t key_available) |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 48 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 49 | PSA_ASSERT(psa_pake_abort(operation)); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 50 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 51 | PSA_ASSERT(psa_pake_setup(operation, cipher_suite)); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 52 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 53 | PSA_ASSERT(psa_pake_set_role(operation, role)); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 54 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 55 | if (key_available) { |
| 56 | PSA_ASSERT(psa_pake_set_password_key(operation, key)); |
| 57 | } |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 58 | return 0; |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 59 | exit: |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 60 | return 1; |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 61 | } |
| 62 | #endif |
| 63 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 64 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 65 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 66 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 67 | /** Test if a buffer contains a constant byte value. |
| 68 | * |
| 69 | * `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] | 70 | * |
| 71 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 72 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 73 | * \param size Size of the buffer in bytes. |
| 74 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 75 | * \return 1 if the buffer is all-bits-zero. |
| 76 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 77 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 78 | 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] | 79 | { |
| 80 | size_t i; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 81 | for (i = 0; i < size; i++) { |
| 82 | if (((unsigned char *) buffer)[i] != c) { |
| 83 | return 0; |
| 84 | } |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 85 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 86 | return 1; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 87 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 88 | #if defined(MBEDTLS_ASN1_WRITE_C) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 89 | /* 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] | 90 | static int asn1_write_10x(unsigned char **p, |
| 91 | unsigned char *start, |
| 92 | size_t bits, |
| 93 | unsigned char x) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 94 | { |
| 95 | int ret; |
| 96 | int len = bits / 8 + 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 97 | if (bits == 0) { |
| 98 | return MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 99 | } |
| 100 | if (bits <= 8 && x >= 1 << (bits - 1)) { |
| 101 | return MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 102 | } |
| 103 | if (*p < start || *p - start < (ptrdiff_t) len) { |
| 104 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 105 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 106 | *p -= len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 107 | (*p)[len-1] = x; |
| 108 | if (bits % 8 == 0) { |
| 109 | (*p)[1] |= 1; |
| 110 | } else { |
| 111 | (*p)[0] |= 1 << (bits % 8); |
| 112 | } |
| 113 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 114 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, |
| 115 | MBEDTLS_ASN1_INTEGER)); |
| 116 | return len; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 117 | } |
| 118 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 119 | static int construct_fake_rsa_key(unsigned char *buffer, |
| 120 | size_t buffer_size, |
| 121 | unsigned char **p, |
| 122 | size_t bits, |
| 123 | int keypair) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 124 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 125 | size_t half_bits = (bits + 1) / 2; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 126 | int ret; |
| 127 | int len = 0; |
| 128 | /* Construct something that looks like a DER encoding of |
| 129 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 130 | * RSAPrivateKey ::= SEQUENCE { |
| 131 | * version Version, |
| 132 | * modulus INTEGER, -- n |
| 133 | * publicExponent INTEGER, -- e |
| 134 | * privateExponent INTEGER, -- d |
| 135 | * prime1 INTEGER, -- p |
| 136 | * prime2 INTEGER, -- q |
| 137 | * exponent1 INTEGER, -- d mod (p-1) |
| 138 | * exponent2 INTEGER, -- d mod (q-1) |
| 139 | * coefficient INTEGER, -- (inverse of q) mod p |
| 140 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 141 | * } |
| 142 | * Or, for a public key, the same structure with only |
| 143 | * version, modulus and publicExponent. |
| 144 | */ |
| 145 | *p = buffer + buffer_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 146 | if (keypair) { |
| 147 | MBEDTLS_ASN1_CHK_ADD(len, /* pq */ |
| 148 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 149 | MBEDTLS_ASN1_CHK_ADD(len, /* dq */ |
| 150 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 151 | MBEDTLS_ASN1_CHK_ADD(len, /* dp */ |
| 152 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 153 | MBEDTLS_ASN1_CHK_ADD(len, /* q */ |
| 154 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 155 | MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */ |
| 156 | asn1_write_10x(p, buffer, half_bits, 3)); |
| 157 | MBEDTLS_ASN1_CHK_ADD(len, /* d */ |
| 158 | asn1_write_10x(p, buffer, bits, 1)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 159 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 160 | MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */ |
| 161 | asn1_write_10x(p, buffer, 17, 1)); |
| 162 | MBEDTLS_ASN1_CHK_ADD(len, /* n */ |
| 163 | asn1_write_10x(p, buffer, bits, 1)); |
| 164 | if (keypair) { |
| 165 | MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */ |
| 166 | mbedtls_asn1_write_int(p, buffer, 0)); |
| 167 | } |
| 168 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 169 | { |
| 170 | const unsigned char tag = |
| 171 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 172 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 173 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 174 | return len; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 175 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 176 | #endif /* MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 177 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 178 | int exercise_mac_setup(psa_key_type_t key_type, |
| 179 | const unsigned char *key_bytes, |
| 180 | size_t key_length, |
| 181 | psa_algorithm_t alg, |
| 182 | psa_mac_operation_t *operation, |
| 183 | psa_status_t *status) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 184 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 185 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 186 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 187 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 188 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 189 | psa_set_key_algorithm(&attributes, alg); |
| 190 | psa_set_key_type(&attributes, key_type); |
| 191 | PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 192 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 193 | *status = psa_mac_sign_setup(operation, key, alg); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 194 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 195 | PSA_ASSERT(psa_mac_abort(operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 196 | /* If setup failed, reproduce the failure, so that the caller can |
| 197 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 198 | if (*status != PSA_SUCCESS) { |
| 199 | TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 200 | } |
| 201 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 202 | psa_destroy_key(key); |
| 203 | return 1; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 204 | |
| 205 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 206 | psa_destroy_key(key); |
| 207 | return 0; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 208 | } |
| 209 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 210 | int exercise_cipher_setup(psa_key_type_t key_type, |
| 211 | const unsigned char *key_bytes, |
| 212 | size_t key_length, |
| 213 | psa_algorithm_t alg, |
| 214 | psa_cipher_operation_t *operation, |
| 215 | psa_status_t *status) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 216 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 217 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 218 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 219 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 220 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 221 | psa_set_key_algorithm(&attributes, alg); |
| 222 | psa_set_key_type(&attributes, key_type); |
| 223 | PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 224 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 225 | *status = psa_cipher_encrypt_setup(operation, key, alg); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 226 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 227 | PSA_ASSERT(psa_cipher_abort(operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 228 | /* If setup failed, reproduce the failure, so that the caller can |
| 229 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 230 | if (*status != PSA_SUCCESS) { |
| 231 | TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg), |
| 232 | *status); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 233 | } |
| 234 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 235 | psa_destroy_key(key); |
| 236 | return 1; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 237 | |
| 238 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 239 | psa_destroy_key(key); |
| 240 | return 0; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 241 | } |
| 242 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 243 | 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] | 244 | { |
| 245 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 246 | 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] | 247 | uint8_t buffer[1]; |
| 248 | size_t length; |
| 249 | int ok = 0; |
| 250 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 251 | psa_set_key_id(&attributes, key_id); |
| 252 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 253 | psa_set_key_algorithm(&attributes, PSA_ALG_CTR); |
| 254 | psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); |
| 255 | TEST_EQUAL(psa_get_key_attributes(key, &attributes), |
| 256 | PSA_ERROR_INVALID_HANDLE); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 257 | TEST_EQUAL( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 258 | 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] | 259 | TEST_EQUAL( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 260 | MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0); |
| 261 | TEST_EQUAL(psa_get_key_lifetime(&attributes), 0); |
| 262 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0); |
| 263 | TEST_EQUAL(psa_get_key_algorithm(&attributes), 0); |
| 264 | TEST_EQUAL(psa_get_key_type(&attributes), 0); |
| 265 | TEST_EQUAL(psa_get_key_bits(&attributes), 0); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 266 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 267 | TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length), |
| 268 | PSA_ERROR_INVALID_HANDLE); |
| 269 | TEST_EQUAL(psa_export_public_key(key, |
| 270 | buffer, sizeof(buffer), &length), |
| 271 | PSA_ERROR_INVALID_HANDLE); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 272 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 273 | ok = 1; |
| 274 | |
| 275 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 276 | /* |
| 277 | * Key attributes may have been returned by psa_get_key_attributes() |
| 278 | * thus reset them as required. |
| 279 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 280 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 282 | return ok; |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 283 | } |
| 284 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 285 | /* Assert that a key isn't reported as having a slot number. */ |
| 286 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 287 | #define ASSERT_NO_SLOT_NUMBER(attributes) \ |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 288 | do \ |
| 289 | { \ |
| 290 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 291 | TEST_EQUAL(psa_get_key_slot_number( \ |
| 292 | attributes, \ |
| 293 | &ASSERT_NO_SLOT_NUMBER_slot_number), \ |
| 294 | PSA_ERROR_INVALID_ARGUMENT); \ |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 295 | } \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 296 | while (0) |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 297 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 298 | #define ASSERT_NO_SLOT_NUMBER(attributes) \ |
| 299 | ((void) 0) |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 300 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 301 | |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 302 | #define INPUT_INTEGER 0x10000 /* Out of range of psa_key_type_t */ |
| 303 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 304 | /* An overapproximation of the amount of storage needed for a key of the |
| 305 | * given type and with the given content. The API doesn't make it easy |
| 306 | * to find a good value for the size. The current implementation doesn't |
| 307 | * care about the value anyway. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 308 | #define KEY_BITS_FROM_DATA(type, data) \ |
| 309 | (data)->len |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 310 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 311 | typedef enum { |
| 312 | IMPORT_KEY = 0, |
| 313 | GENERATE_KEY = 1, |
| 314 | DERIVE_KEY = 2 |
| 315 | } generate_method; |
| 316 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 317 | typedef enum { |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 318 | DO_NOT_SET_LENGTHS = 0, |
| 319 | SET_LENGTHS_BEFORE_NONCE = 1, |
| 320 | SET_LENGTHS_AFTER_NONCE = 2 |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 321 | } set_lengths_method_t; |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 322 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 323 | typedef enum { |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 324 | USE_NULL_TAG = 0, |
| 325 | USE_GIVEN_TAG = 1, |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 326 | } tag_usage_method_t; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 327 | |
Kusumit Ghoderao | a14ae5a | 2023-04-19 14:16:26 +0530 | [diff] [blame] | 328 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 329 | /*! |
| 330 | * \brief Internal Function for AEAD multipart tests. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 331 | * \param key_type_arg Type of key passed in |
| 332 | * \param key_data The encryption / decryption key data |
| 333 | * \param alg_arg The type of algorithm used |
| 334 | * \param nonce Nonce data |
| 335 | * \param additional_data Additional data |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 336 | * \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] | 337 | * feed additional data in to be encrypted / |
| 338 | * decrypted. If -1, no chunking. |
| 339 | * \param input_data Data to encrypt / decrypt |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 340 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 341 | * the data in to be encrypted / decrypted. If |
| 342 | * -1, no chunking |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 343 | * \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] | 344 | * expected here, this controls whether or not |
| 345 | * to set lengths, and in what order with |
| 346 | * respect to set nonce. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 347 | * \param expected_output Expected output |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 348 | * \param is_encrypt If non-zero this is an encryption operation. |
Paul Elliott | 41ffae1 | 2021-07-22 21:52:01 +0100 | [diff] [blame] | 349 | * \param do_zero_parts If non-zero, interleave zero length chunks |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 350 | * with normal length chunks. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 351 | * \return int Zero on failure, non-zero on success. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 352 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 353 | static int aead_multipart_internal_func(int key_type_arg, data_t *key_data, |
| 354 | int alg_arg, |
| 355 | data_t *nonce, |
| 356 | data_t *additional_data, |
| 357 | int ad_part_len_arg, |
| 358 | data_t *input_data, |
| 359 | int data_part_len_arg, |
| 360 | set_lengths_method_t set_lengths_method, |
| 361 | data_t *expected_output, |
| 362 | int is_encrypt, |
| 363 | int do_zero_parts) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 364 | { |
| 365 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 366 | psa_key_type_t key_type = key_type_arg; |
| 367 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 368 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 369 | unsigned char *output_data = NULL; |
| 370 | unsigned char *part_data = NULL; |
| 371 | unsigned char *final_data = NULL; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 372 | size_t data_true_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 373 | size_t part_data_size = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 374 | size_t output_size = 0; |
| 375 | size_t final_output_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 376 | size_t output_length = 0; |
| 377 | size_t key_bits = 0; |
| 378 | size_t tag_length = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 379 | size_t part_offset = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 380 | size_t part_length = 0; |
| 381 | size_t output_part_length = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 382 | size_t tag_size = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 383 | size_t ad_part_len = 0; |
| 384 | size_t data_part_len = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 385 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 386 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 387 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 388 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 389 | int test_ok = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 390 | size_t part_count = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 392 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 393 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 394 | if (is_encrypt) { |
| 395 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 396 | } else { |
| 397 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 398 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 399 | |
| 400 | psa_set_key_algorithm(&attributes, alg); |
| 401 | psa_set_key_type(&attributes, key_type); |
| 402 | |
| 403 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 404 | &key)); |
| 405 | |
| 406 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 407 | key_bits = psa_get_key_bits(&attributes); |
| 408 | |
| 409 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); |
| 410 | |
| 411 | if (is_encrypt) { |
| 412 | /* Tag gets written at end of buffer. */ |
| 413 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 414 | (input_data->len + |
| 415 | tag_length)); |
| 416 | data_true_size = input_data->len; |
| 417 | } else { |
| 418 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 419 | (input_data->len - |
| 420 | tag_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 421 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 422 | /* Do not want to attempt to decrypt tag. */ |
| 423 | data_true_size = input_data->len - tag_length; |
| 424 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 425 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 426 | TEST_CALLOC(output_data, output_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 427 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 428 | if (is_encrypt) { |
| 429 | final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
| 430 | TEST_LE_U(final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
| 431 | } else { |
| 432 | final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); |
| 433 | TEST_LE_U(final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 434 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 435 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 436 | TEST_CALLOC(final_data, final_output_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 437 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 438 | if (is_encrypt) { |
| 439 | status = psa_aead_encrypt_setup(&operation, key, alg); |
| 440 | } else { |
| 441 | status = psa_aead_decrypt_setup(&operation, key, alg); |
| 442 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 443 | |
| 444 | /* If the operation is not supported, just skip and not fail in case the |
| 445 | * encryption involves a common limitation of cryptography hardwares and |
| 446 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 447 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 448 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 449 | 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] | 450 | } |
| 451 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 452 | PSA_ASSERT(status); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 453 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 454 | if (set_lengths_method == DO_NOT_SET_LENGTHS) { |
| 455 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
| 456 | } else if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { |
| 457 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 458 | data_true_size)); |
| 459 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
| 460 | } else if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) { |
| 461 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 462 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 463 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 464 | data_true_size)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 465 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 466 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 467 | if (ad_part_len_arg != -1) { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 468 | /* Pass additional data in parts */ |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 469 | ad_part_len = (size_t) ad_part_len_arg; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 470 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 471 | for (part_offset = 0, part_count = 0; |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 472 | part_offset < additional_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 473 | part_offset += part_length, part_count++) { |
| 474 | if (do_zero_parts && (part_count & 0x01)) { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 475 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 476 | } else if (additional_data->len - part_offset < ad_part_len) { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 477 | part_length = additional_data->len - part_offset; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 478 | } else { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 479 | part_length = ad_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 480 | } |
| 481 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 482 | PSA_ASSERT(psa_aead_update_ad(&operation, |
| 483 | additional_data->x + part_offset, |
| 484 | part_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 485 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 486 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 487 | } else { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 488 | /* Pass additional data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 489 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 490 | additional_data->len)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 491 | } |
| 492 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 493 | if (data_part_len_arg != -1) { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 494 | /* Pass data in parts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 495 | data_part_len = (size_t) data_part_len_arg; |
| 496 | part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 497 | (size_t) data_part_len); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 498 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 499 | TEST_CALLOC(part_data, part_data_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 500 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 501 | for (part_offset = 0, part_count = 0; |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 502 | part_offset < data_true_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 503 | part_offset += part_length, part_count++) { |
| 504 | if (do_zero_parts && (part_count & 0x01)) { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 505 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 506 | } else if ((data_true_size - part_offset) < data_part_len) { |
| 507 | part_length = (data_true_size - part_offset); |
| 508 | } else { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 509 | part_length = data_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 510 | } |
| 511 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 512 | PSA_ASSERT(psa_aead_update(&operation, |
| 513 | (input_data->x + part_offset), |
| 514 | part_length, part_data, |
| 515 | part_data_size, |
| 516 | &output_part_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 517 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 518 | if (output_data && output_part_length) { |
| 519 | memcpy((output_data + output_length), part_data, |
| 520 | output_part_length); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 521 | } |
| 522 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 523 | output_length += output_part_length; |
| 524 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 525 | } else { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 526 | /* Pass all data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 527 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 528 | data_true_size, output_data, |
| 529 | output_size, &output_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 530 | } |
| 531 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 532 | if (is_encrypt) { |
| 533 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 534 | final_output_size, |
| 535 | &output_part_length, |
| 536 | tag_buffer, tag_length, |
| 537 | &tag_size)); |
| 538 | } else { |
| 539 | PSA_ASSERT(psa_aead_verify(&operation, final_data, |
| 540 | final_output_size, |
| 541 | &output_part_length, |
| 542 | (input_data->x + data_true_size), |
| 543 | tag_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 544 | } |
| 545 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 546 | if (output_data && output_part_length) { |
| 547 | memcpy((output_data + output_length), final_data, |
| 548 | output_part_length); |
| 549 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 550 | |
| 551 | output_length += output_part_length; |
| 552 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 553 | |
| 554 | /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE |
| 555 | * should be exact.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 556 | if (is_encrypt) { |
| 557 | TEST_EQUAL(tag_length, tag_size); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 558 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 559 | if (output_data && tag_length) { |
| 560 | memcpy((output_data + output_length), tag_buffer, |
| 561 | tag_length); |
| 562 | } |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 563 | |
| 564 | output_length += tag_length; |
| 565 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 566 | TEST_EQUAL(output_length, |
| 567 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 568 | input_data->len)); |
| 569 | TEST_LE_U(output_length, |
| 570 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
| 571 | } else { |
| 572 | TEST_EQUAL(output_length, |
| 573 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, |
| 574 | input_data->len)); |
| 575 | TEST_LE_U(output_length, |
| 576 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 577 | } |
| 578 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 579 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 580 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 581 | output_data, output_length); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 582 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 583 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 584 | test_ok = 1; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 585 | |
| 586 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 587 | psa_destroy_key(key); |
| 588 | psa_aead_abort(&operation); |
| 589 | mbedtls_free(output_data); |
| 590 | mbedtls_free(part_data); |
| 591 | mbedtls_free(final_data); |
| 592 | PSA_DONE(); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 593 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 594 | return test_ok; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 595 | } |
| 596 | |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 597 | /*! |
| 598 | * \brief Internal Function for MAC multipart tests. |
| 599 | * \param key_type_arg Type of key passed in |
| 600 | * \param key_data The encryption / decryption key data |
| 601 | * \param alg_arg The type of algorithm used |
| 602 | * \param input_data Data to encrypt / decrypt |
| 603 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 604 | * the data in to be encrypted / decrypted. If |
| 605 | * -1, no chunking |
| 606 | * \param expected_output Expected output |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 607 | * \param is_verify If non-zero this is a verify operation. |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 608 | * \param do_zero_parts If non-zero, interleave zero length chunks |
| 609 | * with normal length chunks. |
| 610 | * \return int Zero on failure, non-zero on success. |
| 611 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 612 | static int mac_multipart_internal_func(int key_type_arg, data_t *key_data, |
| 613 | int alg_arg, |
| 614 | data_t *input_data, |
| 615 | int data_part_len_arg, |
| 616 | data_t *expected_output, |
| 617 | int is_verify, |
| 618 | int do_zero_parts) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 619 | { |
| 620 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 621 | psa_key_type_t key_type = key_type_arg; |
| 622 | psa_algorithm_t alg = alg_arg; |
| 623 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 624 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
| 625 | size_t part_offset = 0; |
| 626 | size_t part_length = 0; |
| 627 | size_t data_part_len = 0; |
| 628 | size_t mac_len = 0; |
| 629 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 630 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 631 | |
| 632 | int test_ok = 0; |
| 633 | size_t part_count = 0; |
| 634 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 635 | PSA_INIT(); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 636 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 637 | if (is_verify) { |
| 638 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 639 | } else { |
| 640 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 641 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 642 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 643 | psa_set_key_algorithm(&attributes, alg); |
| 644 | psa_set_key_type(&attributes, key_type); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 645 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 646 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 647 | &key)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 648 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 649 | if (is_verify) { |
| 650 | status = psa_mac_verify_setup(&operation, key, alg); |
| 651 | } else { |
| 652 | status = psa_mac_sign_setup(&operation, key, alg); |
| 653 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 654 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 655 | PSA_ASSERT(status); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 656 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 657 | if (data_part_len_arg != -1) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 658 | /* Pass data in parts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 659 | data_part_len = (size_t) data_part_len_arg; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 660 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 661 | for (part_offset = 0, part_count = 0; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 662 | part_offset < input_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 663 | part_offset += part_length, part_count++) { |
| 664 | if (do_zero_parts && (part_count & 0x01)) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 665 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 666 | } else if ((input_data->len - part_offset) < data_part_len) { |
| 667 | part_length = (input_data->len - part_offset); |
| 668 | } else { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 669 | part_length = data_part_len; |
| 670 | } |
| 671 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 672 | PSA_ASSERT(psa_mac_update(&operation, |
| 673 | (input_data->x + part_offset), |
| 674 | part_length)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 675 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 676 | } else { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 677 | /* Pass all data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 678 | PSA_ASSERT(psa_mac_update(&operation, input_data->x, |
| 679 | input_data->len)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 680 | } |
| 681 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 682 | if (is_verify) { |
| 683 | PSA_ASSERT(psa_mac_verify_finish(&operation, expected_output->x, |
| 684 | expected_output->len)); |
| 685 | } else { |
| 686 | PSA_ASSERT(psa_mac_sign_finish(&operation, mac, |
| 687 | PSA_MAC_MAX_SIZE, &mac_len)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 688 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 689 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 690 | mac, mac_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | test_ok = 1; |
| 694 | |
| 695 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 696 | psa_destroy_key(key); |
| 697 | psa_mac_abort(&operation); |
| 698 | PSA_DONE(); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 699 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 700 | return test_ok; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 701 | } |
| 702 | |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 703 | #if defined(PSA_WANT_ALG_JPAKE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 704 | static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive, |
| 705 | psa_pake_operation_t *server, |
| 706 | psa_pake_operation_t *client, |
| 707 | int client_input_first, |
| 708 | int round, int inject_error) |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 709 | { |
| 710 | unsigned char *buffer0 = NULL, *buffer1 = NULL; |
| 711 | size_t buffer_length = ( |
| 712 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) + |
| 713 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) + |
| 714 | 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] | 715 | /* The output should be exactly this size according to the spec */ |
| 716 | const size_t expected_size_key_share = |
| 717 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE); |
| 718 | /* The output should be exactly this size according to the spec */ |
| 719 | const size_t expected_size_zk_public = |
| 720 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC); |
| 721 | /* The output can be smaller: the spec allows stripping leading zeroes */ |
| 722 | const size_t max_expected_size_zk_proof = |
| 723 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 724 | size_t buffer0_off = 0; |
| 725 | size_t buffer1_off = 0; |
| 726 | size_t s_g1_len, s_g2_len, s_a_len; |
| 727 | size_t s_g1_off, s_g2_off, s_a_off; |
| 728 | size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len; |
| 729 | size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off; |
| 730 | size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len; |
| 731 | size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off; |
| 732 | size_t c_g1_len, c_g2_len, c_a_len; |
| 733 | size_t c_g1_off, c_g2_off, c_a_off; |
| 734 | size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len; |
| 735 | size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off; |
| 736 | size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len; |
| 737 | size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off; |
| 738 | psa_status_t expected_status = PSA_SUCCESS; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 739 | psa_status_t status; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 740 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 741 | TEST_CALLOC(buffer0, buffer_length); |
| 742 | TEST_CALLOC(buffer1, buffer_length); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 743 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 744 | switch (round) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 745 | case 1: |
| 746 | /* Server first round Output */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 747 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 748 | buffer0 + buffer0_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 749 | buffer_length - buffer0_off, &s_g1_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 750 | TEST_EQUAL(s_g1_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 751 | s_g1_off = buffer0_off; |
| 752 | buffer0_off += s_g1_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 753 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 754 | buffer0 + buffer0_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 755 | buffer_length - buffer0_off, &s_x1_pk_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 756 | TEST_EQUAL(s_x1_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 757 | s_x1_pk_off = buffer0_off; |
| 758 | buffer0_off += s_x1_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 759 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 760 | buffer0 + buffer0_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 761 | buffer_length - buffer0_off, &s_x1_pr_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 762 | TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 763 | s_x1_pr_off = buffer0_off; |
| 764 | buffer0_off += s_x1_pr_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 765 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 766 | buffer0 + buffer0_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 767 | buffer_length - buffer0_off, &s_g2_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 768 | TEST_EQUAL(s_g2_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 769 | s_g2_off = buffer0_off; |
| 770 | buffer0_off += s_g2_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 771 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 772 | buffer0 + buffer0_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 773 | buffer_length - buffer0_off, &s_x2_pk_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 774 | TEST_EQUAL(s_x2_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 775 | s_x2_pk_off = buffer0_off; |
| 776 | buffer0_off += s_x2_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 777 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 778 | buffer0 + buffer0_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 779 | buffer_length - buffer0_off, &s_x2_pr_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 780 | TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 781 | s_x2_pr_off = buffer0_off; |
| 782 | buffer0_off += s_x2_pr_len; |
| 783 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 784 | if (inject_error == 1) { |
Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 785 | buffer0[s_x1_pr_off + 8] ^= 1; |
| 786 | buffer0[s_x2_pr_off + 7] ^= 1; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 787 | expected_status = PSA_ERROR_DATA_INVALID; |
| 788 | } |
| 789 | |
Neil Armstrong | 51009d7 | 2022-09-05 17:59:54 +0200 | [diff] [blame] | 790 | /* |
| 791 | * When injecting errors in inputs, the implementation is |
| 792 | * free to detect it right away of with a delay. |
| 793 | * This permits delaying the error until the end of the input |
| 794 | * sequence, if no error appears then, this will be treated |
| 795 | * as an error. |
| 796 | */ |
| 797 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 798 | if (client_input_first == 1) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 799 | /* Client first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 800 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 801 | buffer0 + s_g1_off, s_g1_len); |
| 802 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 803 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 804 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 805 | } else { |
| 806 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 807 | } |
| 808 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 809 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 810 | buffer0 + s_x1_pk_off, |
| 811 | s_x1_pk_len); |
| 812 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 813 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 814 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 815 | } else { |
| 816 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 817 | } |
| 818 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 819 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 820 | buffer0 + s_x1_pr_off, |
| 821 | s_x1_pr_len); |
| 822 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 823 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 824 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 825 | } else { |
| 826 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 827 | } |
| 828 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 829 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 830 | buffer0 + s_g2_off, |
| 831 | s_g2_len); |
| 832 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 833 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 834 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 835 | } else { |
| 836 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 837 | } |
| 838 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 839 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 840 | buffer0 + s_x2_pk_off, |
| 841 | s_x2_pk_len); |
| 842 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 843 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 844 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 845 | } else { |
| 846 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 847 | } |
| 848 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 849 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 850 | buffer0 + s_x2_pr_off, |
| 851 | s_x2_pr_len); |
| 852 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 853 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 854 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 855 | } else { |
| 856 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 857 | } |
| 858 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 859 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 860 | if (inject_error == 1) { |
| 861 | TEST_ASSERT( |
| 862 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 863 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | /* Client first round Output */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 867 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 868 | buffer1 + buffer1_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 869 | buffer_length - buffer1_off, &c_g1_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 870 | TEST_EQUAL(c_g1_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 871 | c_g1_off = buffer1_off; |
| 872 | buffer1_off += c_g1_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 873 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 874 | buffer1 + buffer1_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 875 | buffer_length - buffer1_off, &c_x1_pk_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 876 | TEST_EQUAL(c_x1_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 877 | c_x1_pk_off = buffer1_off; |
| 878 | buffer1_off += c_x1_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 879 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 880 | buffer1 + buffer1_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 881 | buffer_length - buffer1_off, &c_x1_pr_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 882 | TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 883 | c_x1_pr_off = buffer1_off; |
| 884 | buffer1_off += c_x1_pr_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 885 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 886 | buffer1 + buffer1_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 887 | buffer_length - buffer1_off, &c_g2_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 888 | TEST_EQUAL(c_g2_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 889 | c_g2_off = buffer1_off; |
| 890 | buffer1_off += c_g2_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 891 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 892 | buffer1 + buffer1_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 893 | buffer_length - buffer1_off, &c_x2_pk_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 894 | TEST_EQUAL(c_x2_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 895 | c_x2_pk_off = buffer1_off; |
| 896 | buffer1_off += c_x2_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 897 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 898 | buffer1 + buffer1_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 899 | buffer_length - buffer1_off, &c_x2_pr_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 900 | TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 901 | c_x2_pr_off = buffer1_off; |
| 902 | buffer1_off += c_x2_pr_len; |
| 903 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 904 | if (client_input_first == 0) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 905 | /* Client first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 906 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 907 | buffer0 + s_g1_off, s_g1_len); |
| 908 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 909 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 910 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 911 | } else { |
| 912 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 913 | } |
| 914 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 915 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 916 | buffer0 + s_x1_pk_off, |
| 917 | s_x1_pk_len); |
| 918 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 919 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 920 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 921 | } else { |
| 922 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 923 | } |
| 924 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 925 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 926 | buffer0 + s_x1_pr_off, |
| 927 | s_x1_pr_len); |
| 928 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 929 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 930 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 931 | } else { |
| 932 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 933 | } |
| 934 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 935 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 936 | buffer0 + s_g2_off, |
| 937 | s_g2_len); |
| 938 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 939 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 940 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 941 | } else { |
| 942 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 943 | } |
| 944 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 945 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 946 | buffer0 + s_x2_pk_off, |
| 947 | s_x2_pk_len); |
| 948 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 949 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 950 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 951 | } else { |
| 952 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 953 | } |
| 954 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 955 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 956 | buffer0 + s_x2_pr_off, |
| 957 | s_x2_pr_len); |
| 958 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 959 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 960 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 961 | } else { |
| 962 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 963 | } |
| 964 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 965 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 966 | if (inject_error == 1) { |
| 967 | TEST_ASSERT( |
| 968 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 969 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 970 | } |
| 971 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 972 | if (inject_error == 2) { |
Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 973 | buffer1[c_x1_pr_off + 12] ^= 1; |
| 974 | buffer1[c_x2_pr_off + 7] ^= 1; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 975 | expected_status = PSA_ERROR_DATA_INVALID; |
| 976 | } |
| 977 | |
| 978 | /* Server first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 979 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 980 | buffer1 + c_g1_off, c_g1_len); |
| 981 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 982 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 983 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 984 | } else { |
| 985 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 986 | } |
| 987 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 988 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 989 | buffer1 + c_x1_pk_off, c_x1_pk_len); |
| 990 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 991 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 992 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 993 | } else { |
| 994 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 995 | } |
| 996 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 997 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 998 | buffer1 + c_x1_pr_off, c_x1_pr_len); |
| 999 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1000 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1001 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1002 | } else { |
| 1003 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1004 | } |
| 1005 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1006 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 1007 | buffer1 + c_g2_off, c_g2_len); |
| 1008 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1009 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1010 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1011 | } else { |
| 1012 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1013 | } |
| 1014 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1015 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1016 | buffer1 + c_x2_pk_off, c_x2_pk_len); |
| 1017 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1018 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1019 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1020 | } else { |
| 1021 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1022 | } |
| 1023 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1024 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1025 | buffer1 + c_x2_pr_off, c_x2_pr_len); |
| 1026 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1027 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1028 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1029 | } else { |
| 1030 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1031 | } |
| 1032 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1033 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1034 | if (inject_error == 2) { |
| 1035 | TEST_ASSERT( |
| 1036 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1037 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1038 | |
| 1039 | break; |
| 1040 | |
| 1041 | case 2: |
| 1042 | /* Server second round Output */ |
| 1043 | buffer0_off = 0; |
| 1044 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1045 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 1046 | buffer0 + buffer0_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 1047 | buffer_length - buffer0_off, &s_a_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1048 | TEST_EQUAL(s_a_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1049 | s_a_off = buffer0_off; |
| 1050 | buffer0_off += s_a_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1051 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1052 | buffer0 + buffer0_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 1053 | buffer_length - buffer0_off, &s_x2s_pk_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1054 | TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1055 | s_x2s_pk_off = buffer0_off; |
| 1056 | buffer0_off += s_x2s_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1057 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1058 | buffer0 + buffer0_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 1059 | buffer_length - buffer0_off, &s_x2s_pr_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1060 | TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1061 | s_x2s_pr_off = buffer0_off; |
| 1062 | buffer0_off += s_x2s_pr_len; |
| 1063 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1064 | if (inject_error == 3) { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1065 | buffer0[s_x2s_pk_off + 12] += 0x33; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1066 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1067 | } |
| 1068 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1069 | if (client_input_first == 1) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1070 | /* Client second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1071 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1072 | buffer0 + s_a_off, s_a_len); |
| 1073 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1074 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1075 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1076 | } else { |
| 1077 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1078 | } |
| 1079 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1080 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1081 | buffer0 + s_x2s_pk_off, |
| 1082 | s_x2s_pk_len); |
| 1083 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1084 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1085 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1086 | } else { |
| 1087 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1088 | } |
| 1089 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1090 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1091 | buffer0 + s_x2s_pr_off, |
| 1092 | s_x2s_pr_len); |
| 1093 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1094 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1095 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1096 | } else { |
| 1097 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1098 | } |
| 1099 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1100 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1101 | if (inject_error == 3) { |
| 1102 | TEST_ASSERT( |
| 1103 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1104 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | /* Client second round Output */ |
| 1108 | buffer1_off = 0; |
| 1109 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1110 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1111 | buffer1 + buffer1_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 1112 | buffer_length - buffer1_off, &c_a_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1113 | TEST_EQUAL(c_a_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1114 | c_a_off = buffer1_off; |
| 1115 | buffer1_off += c_a_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1116 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1117 | buffer1 + buffer1_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 1118 | buffer_length - buffer1_off, &c_x2s_pk_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1119 | TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1120 | c_x2s_pk_off = buffer1_off; |
| 1121 | buffer1_off += c_x2s_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1122 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1123 | buffer1 + buffer1_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 1124 | buffer_length - buffer1_off, &c_x2s_pr_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1125 | TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1126 | c_x2s_pr_off = buffer1_off; |
| 1127 | buffer1_off += c_x2s_pr_len; |
| 1128 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1129 | if (client_input_first == 0) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1130 | /* Client second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1131 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1132 | buffer0 + s_a_off, s_a_len); |
| 1133 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1134 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1135 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1136 | } else { |
| 1137 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1138 | } |
| 1139 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1140 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1141 | buffer0 + s_x2s_pk_off, |
| 1142 | s_x2s_pk_len); |
| 1143 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1144 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1145 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1146 | } else { |
| 1147 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1148 | } |
| 1149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1150 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1151 | buffer0 + s_x2s_pr_off, |
| 1152 | s_x2s_pr_len); |
| 1153 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1154 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1155 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1156 | } else { |
| 1157 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1158 | } |
| 1159 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1160 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1161 | if (inject_error == 3) { |
| 1162 | TEST_ASSERT( |
| 1163 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1164 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1165 | } |
| 1166 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1167 | if (inject_error == 4) { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1168 | buffer1[c_x2s_pk_off + 7] += 0x28; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1169 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1170 | } |
| 1171 | |
| 1172 | /* Server second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1173 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 1174 | buffer1 + c_a_off, c_a_len); |
| 1175 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1176 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1177 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1178 | } else { |
| 1179 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1180 | } |
| 1181 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1182 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1183 | buffer1 + c_x2s_pk_off, c_x2s_pk_len); |
| 1184 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1185 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1186 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1187 | } else { |
| 1188 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1189 | } |
| 1190 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1191 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1192 | buffer1 + c_x2s_pr_off, c_x2s_pr_len); |
| 1193 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1194 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1195 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1196 | } else { |
| 1197 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1198 | } |
| 1199 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1200 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1201 | if (inject_error == 4) { |
| 1202 | TEST_ASSERT( |
| 1203 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1204 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1205 | |
| 1206 | break; |
| 1207 | |
| 1208 | } |
| 1209 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1210 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1211 | mbedtls_free(buffer0); |
| 1212 | mbedtls_free(buffer1); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1213 | } |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 1214 | #endif /* PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1215 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1216 | typedef enum { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 1217 | INJECT_ERR_NONE = 0, |
| 1218 | INJECT_ERR_UNINITIALIZED_ACCESS, |
| 1219 | INJECT_ERR_DUPLICATE_SETUP, |
| 1220 | INJECT_ERR_INVALID_USER, |
| 1221 | INJECT_ERR_INVALID_PEER, |
| 1222 | INJECT_ERR_SET_USER, |
| 1223 | INJECT_ERR_SET_PEER, |
| 1224 | INJECT_EMPTY_IO_BUFFER, |
| 1225 | INJECT_UNKNOWN_STEP, |
| 1226 | INJECT_INVALID_FIRST_STEP, |
| 1227 | INJECT_WRONG_BUFFER_SIZE, |
| 1228 | INJECT_VALID_OPERATION_AFTER_FAILURE, |
| 1229 | INJECT_ANTICIPATE_KEY_DERIVATION_1, |
| 1230 | INJECT_ANTICIPATE_KEY_DERIVATION_2, |
| 1231 | } ecjpake_injected_failure_t; |
| 1232 | |
Paul Elliott | 01885fa | 2023-02-09 12:07:30 +0000 | [diff] [blame] | 1233 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Paul Elliott | 1243f93 | 2023-02-07 11:21:10 +0000 | [diff] [blame] | 1234 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1235 | static void interruptible_signverify_get_minmax_completes(uint32_t max_ops, |
| 1236 | psa_status_t expected_status, |
| 1237 | size_t *min_completes, |
| 1238 | size_t *max_completes) |
| 1239 | { |
| 1240 | |
| 1241 | /* This is slightly contrived, but we only really know that with a minimum |
| 1242 | value of max_ops that a successful operation should take more than one op |
| 1243 | to complete, and likewise that with a max_ops of |
| 1244 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */ |
| 1245 | if (max_ops == 0 || max_ops == 1) { |
Paul Elliott | c86d45e | 2023-02-15 17:38:05 +0000 | [diff] [blame] | 1246 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1247 | if (expected_status == PSA_SUCCESS) { |
| 1248 | *min_completes = 2; |
| 1249 | } else { |
| 1250 | *min_completes = 1; |
| 1251 | } |
| 1252 | |
| 1253 | *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED; |
| 1254 | } else { |
| 1255 | *min_completes = 1; |
| 1256 | *max_completes = 1; |
| 1257 | } |
| 1258 | } |
Paul Elliott | 01885fa | 2023-02-09 12:07:30 +0000 | [diff] [blame] | 1259 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1260 | |
Gilles Peskine | 1d25a0a | 2024-02-12 16:40:04 +0100 | [diff] [blame] | 1261 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) |
| 1262 | static int rsa_test_e(mbedtls_svc_key_id_t key, |
| 1263 | size_t bits, |
| 1264 | const data_t *e_arg) |
| 1265 | { |
| 1266 | uint8_t *exported = NULL; |
| 1267 | size_t exported_size = |
| 1268 | PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits); |
| 1269 | size_t exported_length = SIZE_MAX; |
| 1270 | int ok = 0; |
| 1271 | |
| 1272 | TEST_CALLOC(exported, exported_size); |
| 1273 | PSA_ASSERT(psa_export_public_key(key, |
| 1274 | exported, exported_size, |
| 1275 | &exported_length)); |
| 1276 | uint8_t *p = exported; |
| 1277 | uint8_t *end = exported + exported_length; |
| 1278 | size_t len; |
| 1279 | /* RSAPublicKey ::= SEQUENCE { |
| 1280 | * modulus INTEGER, -- n |
| 1281 | * publicExponent INTEGER } -- e |
| 1282 | */ |
| 1283 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 1284 | MBEDTLS_ASN1_SEQUENCE | |
| 1285 | MBEDTLS_ASN1_CONSTRUCTED)); |
| 1286 | TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1)); |
| 1287 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 1288 | MBEDTLS_ASN1_INTEGER)); |
| 1289 | if (len >= 1 && p[0] == 0) { |
| 1290 | ++p; |
| 1291 | --len; |
| 1292 | } |
| 1293 | if (e_arg->len == 0) { |
| 1294 | TEST_EQUAL(len, 3); |
| 1295 | TEST_EQUAL(p[0], 1); |
| 1296 | TEST_EQUAL(p[1], 0); |
| 1297 | TEST_EQUAL(p[2], 1); |
| 1298 | } else { |
Gilles Peskine | 7a18f96 | 2024-02-12 16:48:11 +0100 | [diff] [blame] | 1299 | const uint8_t *expected = e_arg->x; |
| 1300 | size_t expected_len = e_arg->len; |
| 1301 | while (expected_len > 0 && *expected == 0) { |
| 1302 | ++expected; |
| 1303 | --expected_len; |
| 1304 | } |
| 1305 | TEST_MEMORY_COMPARE(p, len, expected, expected_len); |
Gilles Peskine | 1d25a0a | 2024-02-12 16:40:04 +0100 | [diff] [blame] | 1306 | } |
| 1307 | ok = 1; |
| 1308 | |
| 1309 | exit: |
| 1310 | mbedtls_free(exported); |
| 1311 | return ok; |
| 1312 | } |
| 1313 | #endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE */ |
| 1314 | |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 1315 | static int setup_key_production_parameters( |
| 1316 | psa_key_production_parameters_t **params, size_t *params_data_length, |
| 1317 | int flags_arg, const data_t *params_data) |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 1318 | { |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 1319 | *params_data_length = params_data->len; |
Gilles Peskine | c81393b | 2024-02-14 20:51:28 +0100 | [diff] [blame] | 1320 | /* If there are N bytes of padding at the end of |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 1321 | * psa_key_production_parameters_t, then it's enough to allocate |
| 1322 | * MIN(sizeof(psa_key_production_parameters_t), |
| 1323 | * offsetof(psa_key_production_parameters_t, data) + params_data_length). |
Gilles Peskine | c81393b | 2024-02-14 20:51:28 +0100 | [diff] [blame] | 1324 | * |
| 1325 | * For simplicity, here, we allocate up to N more bytes than necessary. |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 1326 | * In practice, the current layout of psa_key_production_parameters_t |
Gilles Peskine | c81393b | 2024-02-14 20:51:28 +0100 | [diff] [blame] | 1327 | * makes padding extremely unlikely, so we don't worry about testing |
| 1328 | * that the library code doesn't try to access these extra N bytes. |
| 1329 | */ |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 1330 | *params = mbedtls_calloc(1, sizeof(**params) + *params_data_length); |
| 1331 | TEST_ASSERT(*params != NULL); |
| 1332 | (*params)->flags = (uint32_t) flags_arg; |
| 1333 | memcpy((*params)->data, params_data->x, params_data->len); |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 1334 | return 1; |
| 1335 | exit: |
| 1336 | return 0; |
| 1337 | } |
| 1338 | |
Ryan | 3a1b786 | 2024-03-01 17:24:04 +0000 | [diff] [blame] | 1339 | #if defined(MBEDTLS_THREADING_PTHREAD) |
Ryan Everett | 5061999 | 2024-03-12 16:55:14 +0000 | [diff] [blame] | 1340 | |
| 1341 | typedef struct same_key_context { |
| 1342 | data_t *data; |
| 1343 | mbedtls_svc_key_id_t key; |
| 1344 | psa_key_attributes_t *attributes; |
| 1345 | int type; |
| 1346 | int bits; |
| 1347 | /* The following two parameters are used to ensure that when multiple |
| 1348 | * threads attempt to load/destroy the key, exactly one thread succeeds. */ |
| 1349 | int key_loaded; |
| 1350 | mbedtls_threading_mutex_t MBEDTLS_PRIVATE(key_loaded_mutex); |
| 1351 | } |
| 1352 | same_key_context; |
| 1353 | |
| 1354 | /* Attempt to import the key in ctx. This handles any valid error codes |
| 1355 | * and reports an error for any invalid codes. This function also insures |
| 1356 | * that once imported by some thread, all threads can use the key. */ |
| 1357 | void *thread_import_key(void *ctx) |
| 1358 | { |
| 1359 | mbedtls_svc_key_id_t returned_key_id; |
| 1360 | same_key_context *skc = (struct same_key_context *) ctx; |
| 1361 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1362 | |
Ryan Everett | 6c48870 | 2024-03-14 17:49:44 +0000 | [diff] [blame] | 1363 | /* Import the key, exactly one thread must succeed. */ |
Ryan Everett | 5061999 | 2024-03-12 16:55:14 +0000 | [diff] [blame] | 1364 | psa_status_t status = psa_import_key(skc->attributes, skc->data->x, |
| 1365 | skc->data->len, &returned_key_id); |
| 1366 | switch (status) { |
| 1367 | case PSA_SUCCESS: |
| 1368 | if (mbedtls_mutex_lock(&skc->key_loaded_mutex) == 0) { |
| 1369 | if (skc->key_loaded) { |
| 1370 | mbedtls_mutex_unlock(&skc->key_loaded_mutex); |
| 1371 | /* More than one thread has succeeded, report a failure. */ |
Ryan Everett | 3de040f | 2024-03-14 17:50:06 +0000 | [diff] [blame] | 1372 | TEST_FAIL("The same key has been loaded into the key store multiple times."); |
Ryan Everett | 5061999 | 2024-03-12 16:55:14 +0000 | [diff] [blame] | 1373 | } |
| 1374 | skc->key_loaded = 1; |
| 1375 | mbedtls_mutex_unlock(&skc->key_loaded_mutex); |
| 1376 | } |
| 1377 | break; |
| 1378 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
| 1379 | /* If all of the key slots are reserved when a thread |
| 1380 | * locks the mutex to reserve a new slot, it will return |
| 1381 | * PSA_ERROR_INSUFFICIENT_MEMORY; this is correct behaviour. |
| 1382 | * There is a chance for this to occur here when the number of |
| 1383 | * threads running this function is larger than the number of |
| 1384 | * free key slots. Each thread reserves an empty key slot, |
| 1385 | * unlocks the mutex, then relocks it to finalize key creation. |
| 1386 | * It is at that point where the thread sees that the key |
| 1387 | * already exists, releases the reserved slot, |
| 1388 | * and returns PSA_ERROR_ALREADY_EXISTS. |
| 1389 | * There is no guarantee that the key is loaded upon this return |
| 1390 | * code, so we can't test the key information. Just stop this |
| 1391 | * thread from executing, note that this is not an error. */ |
| 1392 | goto exit; |
| 1393 | break; |
| 1394 | case PSA_ERROR_ALREADY_EXISTS: |
| 1395 | /* The key has been loaded by a different thread. */ |
| 1396 | break; |
| 1397 | default: |
| 1398 | PSA_ASSERT(status); |
| 1399 | } |
| 1400 | /* At this point the key must exist, test the key information. */ |
| 1401 | status = psa_get_key_attributes(skc->key, &got_attributes); |
| 1402 | if (status == PSA_ERROR_INSUFFICIENT_MEMORY) { |
| 1403 | /* This is not a test failure. The following sequence of events |
| 1404 | * causes this to occur: |
| 1405 | * 1: This thread successfuly imports a persistent key skc->key. |
| 1406 | * 2: N threads reserve an empty key slot in psa_import_key, |
| 1407 | * where N is equal to the number of free key slots. |
| 1408 | * 3: A final thread attempts to reserve an empty key slot, kicking |
| 1409 | * skc->key (which has no registered readers) out of its slot. |
| 1410 | * 4: This thread calls psa_get_key_attributes(skc->key,...): |
| 1411 | * it sees that skc->key is not in a slot, attempts to load it and |
| 1412 | * finds that there are no free slots. |
| 1413 | * This thread returns PSA_ERROR_INSUFFICIENT_MEMORY. |
| 1414 | * |
| 1415 | * The PSA spec allows this behaviour, it is an unavoidable consequence |
| 1416 | * of allowing persistent keys to be kicked out of the key store while |
| 1417 | * they are still valid. */ |
| 1418 | goto exit; |
| 1419 | } |
| 1420 | PSA_ASSERT(status); |
| 1421 | TEST_EQUAL(psa_get_key_type(&got_attributes), skc->type); |
| 1422 | TEST_EQUAL(psa_get_key_bits(&got_attributes), skc->bits); |
| 1423 | |
| 1424 | exit: |
| 1425 | /* Key attributes may have been returned by psa_get_key_attributes(), |
| 1426 | * reset them as required. */ |
| 1427 | psa_reset_key_attributes(&got_attributes); |
| 1428 | return NULL; |
| 1429 | } |
| 1430 | |
| 1431 | void *thread_use_and_destroy_key(void *ctx) |
| 1432 | { |
| 1433 | same_key_context *skc = (struct same_key_context *) ctx; |
| 1434 | |
| 1435 | /* Do something with the key according |
| 1436 | * to its type and permitted usage. */ |
| 1437 | TEST_ASSERT(mbedtls_test_psa_exercise_key(skc->key, |
| 1438 | skc->attributes->policy.usage, |
| 1439 | skc->attributes->policy.alg, 1)); |
| 1440 | |
| 1441 | psa_status_t status = psa_destroy_key(skc->key); |
| 1442 | if (status == PSA_SUCCESS) { |
| 1443 | if (mbedtls_mutex_lock(&skc->key_loaded_mutex) == 0) { |
| 1444 | /* Ensure that we are the only thread to succeed. */ |
| 1445 | if (skc->key_loaded != 1) { |
| 1446 | mbedtls_mutex_unlock(&skc->key_loaded_mutex); |
Ryan Everett | 3de040f | 2024-03-14 17:50:06 +0000 | [diff] [blame] | 1447 | TEST_FAIL("The same key has been destroyed multiple times."); |
Ryan Everett | 5061999 | 2024-03-12 16:55:14 +0000 | [diff] [blame] | 1448 | } |
| 1449 | skc->key_loaded = 0; |
| 1450 | mbedtls_mutex_unlock(&skc->key_loaded_mutex); |
| 1451 | } |
| 1452 | } else { |
| 1453 | TEST_EQUAL(status, PSA_ERROR_INVALID_HANDLE); |
| 1454 | } |
| 1455 | |
| 1456 | exit: |
| 1457 | return NULL; |
| 1458 | } |
| 1459 | |
Ryan | 3a1b786 | 2024-03-01 17:24:04 +0000 | [diff] [blame] | 1460 | typedef struct generate_key_context { |
| 1461 | psa_key_type_t type; |
| 1462 | psa_key_usage_t usage; |
| 1463 | size_t bits; |
| 1464 | psa_algorithm_t alg; |
| 1465 | psa_status_t expected_status; |
| 1466 | psa_key_attributes_t *attributes; |
| 1467 | int is_large_key; |
| 1468 | int reps; |
| 1469 | } |
| 1470 | generate_key_context; |
| 1471 | void *thread_generate_key(void *ctx) |
| 1472 | { |
| 1473 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1474 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1475 | generate_key_context *gkc = (struct generate_key_context *) ctx; |
| 1476 | |
| 1477 | /* If there are race conditions, it is likely the case that they do not |
| 1478 | * arise every time the code runs. We repeat the code to increase the |
| 1479 | * chance that any race conditions will be hit. */ |
| 1480 | for (int n = 0; n < gkc->reps; n++) { |
| 1481 | /* Generate a key */ |
| 1482 | psa_status_t status = psa_generate_key(gkc->attributes, &key); |
| 1483 | |
| 1484 | if (gkc->is_large_key > 0) { |
| 1485 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 1486 | } |
| 1487 | |
| 1488 | TEST_EQUAL(status, gkc->expected_status); |
| 1489 | if (gkc->expected_status != PSA_SUCCESS) { |
| 1490 | PSA_ASSERT(psa_destroy_key(key)); |
| 1491 | goto exit; |
| 1492 | } |
| 1493 | |
| 1494 | /* Test the key information */ |
| 1495 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1496 | TEST_EQUAL(psa_get_key_type(&got_attributes), gkc->type); |
| 1497 | TEST_EQUAL(psa_get_key_bits(&got_attributes), gkc->bits); |
| 1498 | |
| 1499 | /* Do something with the key according |
| 1500 | * to its type and permitted usage. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1501 | if (!mbedtls_test_psa_exercise_key(key, gkc->usage, gkc->alg, 0)) { |
Ryan | 3a1b786 | 2024-03-01 17:24:04 +0000 | [diff] [blame] | 1502 | psa_destroy_key(key); |
| 1503 | goto exit; |
| 1504 | } |
| 1505 | psa_reset_key_attributes(&got_attributes); |
| 1506 | |
| 1507 | PSA_ASSERT(psa_destroy_key(key)); |
| 1508 | } |
| 1509 | exit: |
| 1510 | /* |
| 1511 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1512 | * thus reset them as required. |
| 1513 | */ |
| 1514 | psa_reset_key_attributes(&got_attributes); |
| 1515 | return NULL; |
| 1516 | } |
| 1517 | #endif /* MBEDTLS_THREADING_PTHREAD */ |
| 1518 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1519 | /* END_HEADER */ |
| 1520 | |
| 1521 | /* BEGIN_DEPENDENCIES |
| 1522 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1523 | * END_DEPENDENCIES |
| 1524 | */ |
| 1525 | |
| 1526 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 7abdf7e | 2023-03-09 11:17:43 +0100 | [diff] [blame] | 1527 | void psa_can_do_hash() |
| 1528 | { |
| 1529 | /* We can't test that this is specific to drivers until partial init has |
| 1530 | * been implemented, but we can at least test before/after full init. */ |
| 1531 | TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE)); |
| 1532 | PSA_INIT(); |
| 1533 | TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE)); |
| 1534 | PSA_DONE(); |
| 1535 | } |
| 1536 | /* END_CASE */ |
| 1537 | |
| 1538 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1539 | void static_checks() |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1540 | { |
| 1541 | size_t max_truncated_mac_size = |
| 1542 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1543 | |
| 1544 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1545 | * encoding. The shifted mask is the maximum truncated value. The |
| 1546 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1547 | TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size); |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1548 | } |
| 1549 | /* END_CASE */ |
| 1550 | |
| 1551 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1552 | void import_with_policy(int type_arg, |
| 1553 | int usage_arg, int alg_arg, |
| 1554 | int expected_status_arg) |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1555 | { |
| 1556 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1557 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1558 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1559 | psa_key_type_t type = type_arg; |
| 1560 | psa_key_usage_t usage = usage_arg; |
| 1561 | psa_algorithm_t alg = alg_arg; |
| 1562 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1563 | const uint8_t key_material[16] = { 0 }; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1564 | psa_status_t status; |
| 1565 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1566 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1567 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1568 | psa_set_key_type(&attributes, type); |
| 1569 | psa_set_key_usage_flags(&attributes, usage); |
| 1570 | psa_set_key_algorithm(&attributes, alg); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1571 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1572 | status = psa_import_key(&attributes, |
| 1573 | key_material, sizeof(key_material), |
| 1574 | &key); |
| 1575 | TEST_EQUAL(status, expected_status); |
| 1576 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1577 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1578 | } |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1579 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1580 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1581 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1582 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), |
| 1583 | mbedtls_test_update_key_usage_flags(usage)); |
| 1584 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 1585 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1586 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1587 | PSA_ASSERT(psa_destroy_key(key)); |
| 1588 | test_operations_on_invalid_key(key); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1589 | |
| 1590 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1591 | /* |
| 1592 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1593 | * thus reset them as required. |
| 1594 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1595 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1596 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1597 | psa_destroy_key(key); |
| 1598 | PSA_DONE(); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1599 | } |
| 1600 | /* END_CASE */ |
| 1601 | |
| 1602 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1603 | void import_with_data(data_t *data, int type_arg, |
| 1604 | int attr_bits_arg, |
| 1605 | int expected_status_arg) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1606 | { |
| 1607 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1608 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1609 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1610 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1611 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1612 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1613 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1614 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1615 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1616 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1617 | psa_set_key_type(&attributes, type); |
| 1618 | psa_set_key_bits(&attributes, attr_bits); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1619 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1620 | status = psa_import_key(&attributes, data->x, data->len, &key); |
Manuel Pégourié-Gonnard | 0509b58 | 2023-08-08 12:47:56 +0200 | [diff] [blame] | 1621 | /* When expecting INVALID_ARGUMENT, also accept NOT_SUPPORTED. |
| 1622 | * |
| 1623 | * This can happen with a type supported only by a driver: |
| 1624 | * - the driver sees the invalid data (for example wrong size) and thinks |
| 1625 | * "well perhaps this is a key size I don't support" so it returns |
| 1626 | * NOT_SUPPORTED which is correct at this point; |
| 1627 | * - we fallback to built-ins, which don't support this type, so return |
| 1628 | * NOT_SUPPORTED which again is correct at this point. |
| 1629 | */ |
| 1630 | if (expected_status == PSA_ERROR_INVALID_ARGUMENT && |
| 1631 | status == PSA_ERROR_NOT_SUPPORTED) { |
| 1632 | ; // OK |
| 1633 | } else { |
| 1634 | TEST_EQUAL(status, expected_status); |
| 1635 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1636 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1637 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1638 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1639 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1640 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1641 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1642 | if (attr_bits != 0) { |
| 1643 | TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes)); |
| 1644 | } |
| 1645 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1646 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1647 | PSA_ASSERT(psa_destroy_key(key)); |
| 1648 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1649 | |
| 1650 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1651 | /* |
| 1652 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1653 | * thus reset them as required. |
| 1654 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1655 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1656 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1657 | psa_destroy_key(key); |
| 1658 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1659 | } |
| 1660 | /* END_CASE */ |
| 1661 | |
| 1662 | /* BEGIN_CASE */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1663 | /* Construct and attempt to import a large unstructured key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1664 | void import_large_key(int type_arg, int byte_size_arg, |
| 1665 | int expected_status_arg) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1666 | { |
| 1667 | psa_key_type_t type = type_arg; |
| 1668 | size_t byte_size = byte_size_arg; |
| 1669 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1670 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1671 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1672 | psa_status_t status; |
| 1673 | uint8_t *buffer = NULL; |
| 1674 | size_t buffer_size = byte_size + 1; |
| 1675 | size_t n; |
| 1676 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1677 | /* Skip the test case if the target running the test cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1678 | * accommodate large keys due to heap size constraints */ |
Tom Cosgrove | 412a813 | 2023-07-20 16:55:14 +0100 | [diff] [blame] | 1679 | TEST_CALLOC_OR_SKIP(buffer, buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1680 | memset(buffer, 'K', byte_size); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1681 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1682 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1683 | |
| 1684 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1685 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1686 | psa_set_key_type(&attributes, type); |
| 1687 | status = psa_import_key(&attributes, buffer, byte_size, &key); |
| 1688 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 1689 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1690 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1691 | if (status == PSA_SUCCESS) { |
| 1692 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1693 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 1694 | TEST_EQUAL(psa_get_key_bits(&attributes), |
| 1695 | PSA_BYTES_TO_BITS(byte_size)); |
| 1696 | ASSERT_NO_SLOT_NUMBER(&attributes); |
| 1697 | memset(buffer, 0, byte_size + 1); |
| 1698 | PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n)); |
| 1699 | for (n = 0; n < byte_size; n++) { |
| 1700 | TEST_EQUAL(buffer[n], 'K'); |
| 1701 | } |
| 1702 | for (n = byte_size; n < buffer_size; n++) { |
| 1703 | TEST_EQUAL(buffer[n], 0); |
| 1704 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1705 | } |
| 1706 | |
| 1707 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1708 | /* |
| 1709 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1710 | * thus reset them as required. |
| 1711 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1712 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1713 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1714 | psa_destroy_key(key); |
| 1715 | PSA_DONE(); |
| 1716 | mbedtls_free(buffer); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1717 | } |
| 1718 | /* END_CASE */ |
| 1719 | |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 1720 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1721 | /* Import an RSA key with a valid structure (but not valid numbers |
| 1722 | * inside, beyond having sensible size and parity). This is expected to |
| 1723 | * fail for large keys. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1724 | 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] | 1725 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1726 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1727 | size_t bits = bits_arg; |
| 1728 | psa_status_t expected_status = expected_status_arg; |
| 1729 | psa_status_t status; |
| 1730 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1731 | 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] | 1732 | size_t buffer_size = /* Slight overapproximations */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1733 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1734 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1735 | unsigned char *p; |
| 1736 | int ret; |
| 1737 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1738 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1739 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1740 | PSA_ASSERT(psa_crypto_init()); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1741 | TEST_CALLOC(buffer, buffer_size); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1742 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1743 | TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p, |
| 1744 | bits, keypair)) >= 0); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1745 | length = ret; |
| 1746 | |
| 1747 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1748 | psa_set_key_type(&attributes, type); |
| 1749 | status = psa_import_key(&attributes, p, length, &key); |
| 1750 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1751 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1752 | if (status == PSA_SUCCESS) { |
| 1753 | PSA_ASSERT(psa_destroy_key(key)); |
| 1754 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1755 | |
| 1756 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1757 | mbedtls_free(buffer); |
| 1758 | PSA_DONE(); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1759 | } |
| 1760 | /* END_CASE */ |
| 1761 | |
| 1762 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1763 | void import_export(data_t *data, |
| 1764 | int type_arg, |
| 1765 | int usage_arg, int alg_arg, |
| 1766 | int lifetime_arg, |
| 1767 | int expected_bits, |
| 1768 | int export_size_delta, |
| 1769 | int expected_export_status_arg, |
| 1770 | /*whether reexport must give the original input exactly*/ |
| 1771 | int canonical_input) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [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; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [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; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [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; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1779 | unsigned char *exported = NULL; |
| 1780 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1781 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1782 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1783 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1784 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1785 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1786 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1787 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1788 | TEST_CALLOC(exported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1789 | if (!canonical_input) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1790 | TEST_CALLOC(reexported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1791 | } |
| 1792 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1793 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1794 | psa_set_key_lifetime(&attributes, lifetime); |
| 1795 | psa_set_key_usage_flags(&attributes, usage_arg); |
| 1796 | psa_set_key_algorithm(&attributes, alg); |
| 1797 | psa_set_key_type(&attributes, type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1798 | |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1799 | if (PSA_KEY_TYPE_IS_DH(type) && |
| 1800 | expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) { |
Przemek Stekiel | 654bef0 | 2022-12-15 13:28:02 +0100 | [diff] [blame] | 1801 | /* Simulate that buffer is too small, by decreasing its size by 1 byte. */ |
| 1802 | export_size -= 1; |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1803 | } |
| 1804 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1805 | /* Import the key */ |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1806 | TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key), |
Przemek Stekiel | 2e7c33d | 2023-04-27 12:29:45 +0200 | [diff] [blame] | 1807 | PSA_SUCCESS); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1808 | |
| 1809 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1810 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1811 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1812 | TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits); |
| 1813 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1814 | |
| 1815 | /* Export the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1816 | status = psa_export_key(key, exported, export_size, &exported_length); |
| 1817 | TEST_EQUAL(status, expected_export_status); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1818 | |
| 1819 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1820 | * and export_size. On errors, the exported length must be 0. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1821 | TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH); |
| 1822 | TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0); |
| 1823 | TEST_LE_U(exported_length, export_size); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1824 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1825 | TEST_ASSERT(mem_is_char(exported + exported_length, 0, |
| 1826 | export_size - exported_length)); |
| 1827 | if (status != PSA_SUCCESS) { |
| 1828 | TEST_EQUAL(exported_length, 0); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1829 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1830 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1831 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1832 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1833 | * this validates the canonical representations. For canonical inputs, |
| 1834 | * this doesn't directly validate the implementation, but it still helps |
| 1835 | * by cross-validating the test data with the sanity check code. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1836 | if (!psa_key_lifetime_is_external(lifetime)) { |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1837 | if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0, 0)) { |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1838 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1839 | } |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1840 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1841 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1842 | if (canonical_input) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1843 | TEST_MEMORY_COMPARE(data->x, data->len, exported, exported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1844 | } else { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1845 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1846 | PSA_ASSERT(psa_import_key(&attributes, exported, exported_length, |
| 1847 | &key2)); |
| 1848 | PSA_ASSERT(psa_export_key(key2, |
| 1849 | reexported, |
| 1850 | export_size, |
| 1851 | &reexported_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1852 | TEST_MEMORY_COMPARE(exported, exported_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1853 | reexported, reexported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1854 | PSA_ASSERT(psa_destroy_key(key2)); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1855 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1856 | TEST_LE_U(exported_length, |
| 1857 | PSA_EXPORT_KEY_OUTPUT_SIZE(type, |
| 1858 | psa_get_key_bits(&got_attributes))); |
Valerio Setti | 1eacae8 | 2023-07-28 16:07:03 +0200 | [diff] [blame] | 1859 | if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 1860 | TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
| 1861 | } else if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) { |
| 1862 | TEST_LE_U(exported_length, PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
| 1863 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1864 | |
| 1865 | destroy: |
| 1866 | /* Destroy the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1867 | PSA_ASSERT(psa_destroy_key(key)); |
| 1868 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1869 | |
| 1870 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1871 | /* |
| 1872 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1873 | * thus reset them as required. |
| 1874 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1875 | psa_reset_key_attributes(&got_attributes); |
| 1876 | psa_destroy_key(key); |
| 1877 | mbedtls_free(exported); |
| 1878 | mbedtls_free(reexported); |
| 1879 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1880 | } |
| 1881 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1882 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1883 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1884 | void import_export_public_key(data_t *data, |
| 1885 | int type_arg, // key pair or public key |
| 1886 | int alg_arg, |
| 1887 | int lifetime_arg, |
| 1888 | int export_size_delta, |
| 1889 | int expected_export_status_arg, |
| 1890 | data_t *expected_public_key) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1891 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1892 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1893 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1894 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1895 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1896 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1897 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1898 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1899 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1900 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1901 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1902 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1903 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1904 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1905 | psa_set_key_lifetime(&attributes, lifetime); |
| 1906 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1907 | psa_set_key_algorithm(&attributes, alg); |
| 1908 | psa_set_key_type(&attributes, type); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1909 | |
| 1910 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1911 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1912 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1913 | /* Export the public key */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1914 | TEST_CALLOC(exported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1915 | status = psa_export_public_key(key, |
| 1916 | exported, export_size, |
| 1917 | &exported_length); |
| 1918 | TEST_EQUAL(status, expected_export_status); |
| 1919 | if (status == PSA_SUCCESS) { |
| 1920 | 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] | 1921 | size_t bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1922 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1923 | bits = psa_get_key_bits(&attributes); |
| 1924 | TEST_LE_U(expected_public_key->len, |
| 1925 | PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1926 | TEST_LE_U(expected_public_key->len, |
| 1927 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1928 | TEST_LE_U(expected_public_key->len, |
| 1929 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1930 | TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1931 | exported, exported_length); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1932 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1933 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1934 | /* |
| 1935 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1936 | * thus reset them as required. |
| 1937 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1938 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1939 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1940 | mbedtls_free(exported); |
| 1941 | psa_destroy_key(key); |
| 1942 | PSA_DONE(); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1943 | } |
| 1944 | /* END_CASE */ |
| 1945 | |
Ryan Everett | 5061999 | 2024-03-12 16:55:14 +0000 | [diff] [blame] | 1946 | |
| 1947 | #if defined(MBEDTLS_THREADING_PTHREAD) |
| 1948 | /* BEGIN_CASE depends_on:MBEDTLS_THREADING_PTHREAD:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
| 1949 | void concurrently_use_same_persistent_key(data_t *data, |
| 1950 | int type_arg, |
| 1951 | int bits_arg, |
| 1952 | int alg_arg, |
| 1953 | int thread_count_arg) |
| 1954 | { |
| 1955 | size_t thread_count = (size_t) thread_count_arg; |
| 1956 | mbedtls_test_thread_t *threads = NULL; |
| 1957 | mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1); |
| 1958 | same_key_context skc; |
| 1959 | skc.data = data; |
| 1960 | skc.key = key_id; |
| 1961 | skc.type = type_arg; |
| 1962 | skc.bits = bits_arg; |
| 1963 | skc.key_loaded = 0; |
| 1964 | mbedtls_mutex_init(&skc.key_loaded_mutex); |
| 1965 | psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(skc.type, alg_arg); |
| 1966 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1967 | |
| 1968 | PSA_ASSERT(psa_crypto_init()); |
| 1969 | |
| 1970 | psa_set_key_id(&attributes, key_id); |
| 1971 | psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_PERSISTENT); |
| 1972 | psa_set_key_usage_flags(&attributes, usage); |
| 1973 | psa_set_key_algorithm(&attributes, alg_arg); |
| 1974 | psa_set_key_type(&attributes, type_arg); |
| 1975 | psa_set_key_bits(&attributes, bits_arg); |
| 1976 | skc.attributes = &attributes; |
| 1977 | |
| 1978 | TEST_CALLOC(threads, sizeof(mbedtls_test_thread_t) * thread_count); |
| 1979 | |
| 1980 | /* Test that when multiple threads import the same key, |
| 1981 | * exactly one thread succeeds and the rest fail with valid errors. |
| 1982 | * Also test that all threads can use the key as soon as it has been |
| 1983 | * imported. */ |
| 1984 | for (size_t i = 0; i < thread_count; i++) { |
| 1985 | TEST_EQUAL( |
| 1986 | mbedtls_test_thread_create(&threads[i], thread_import_key, |
| 1987 | (void *) &skc), 0); |
| 1988 | } |
| 1989 | |
| 1990 | /* Join threads. */ |
| 1991 | for (size_t i = 0; i < thread_count; i++) { |
| 1992 | TEST_EQUAL(mbedtls_test_thread_join(&threads[i]), 0); |
| 1993 | } |
| 1994 | |
| 1995 | /* Test that when multiple threads use and destroy a key no corruption |
| 1996 | * occurs, and exactly one thread succeeds when destroying the key. */ |
| 1997 | for (size_t i = 0; i < thread_count; i++) { |
| 1998 | TEST_EQUAL( |
| 1999 | mbedtls_test_thread_create(&threads[i], thread_use_and_destroy_key, |
| 2000 | (void *) &skc), 0); |
| 2001 | } |
| 2002 | |
| 2003 | /* Join threads. */ |
| 2004 | for (size_t i = 0; i < thread_count; i++) { |
| 2005 | TEST_EQUAL(mbedtls_test_thread_join(&threads[i]), 0); |
| 2006 | } |
| 2007 | /* Ensure that one thread succeeded in destroying the key. */ |
| 2008 | TEST_ASSERT(!skc.key_loaded); |
| 2009 | exit: |
| 2010 | psa_reset_key_attributes(&attributes); |
| 2011 | mbedtls_mutex_free(&skc.key_loaded_mutex); |
| 2012 | mbedtls_free(threads); |
| 2013 | PSA_DONE(); |
| 2014 | } |
| 2015 | /* END_CASE */ |
| 2016 | #endif |
| 2017 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2018 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2019 | void import_and_exercise_key(data_t *data, |
| 2020 | int type_arg, |
| 2021 | int bits_arg, |
| 2022 | int alg_arg) |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2023 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2024 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2025 | psa_key_type_t type = type_arg; |
| 2026 | size_t bits = bits_arg; |
| 2027 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2028 | 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] | 2029 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2030 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2031 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2032 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2033 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2034 | psa_set_key_usage_flags(&attributes, usage); |
| 2035 | psa_set_key_algorithm(&attributes, alg); |
| 2036 | psa_set_key_type(&attributes, type); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2037 | |
| 2038 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2039 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2040 | |
| 2041 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2042 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 2043 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 2044 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2045 | |
| 2046 | /* Do something with the key according to its type and permitted usage. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 2047 | if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 2048 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2049 | } |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2050 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2051 | PSA_ASSERT(psa_destroy_key(key)); |
| 2052 | test_operations_on_invalid_key(key); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 2053 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2054 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2055 | /* |
| 2056 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2057 | * thus reset them as required. |
| 2058 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2059 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2060 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2061 | psa_reset_key_attributes(&attributes); |
| 2062 | psa_destroy_key(key); |
| 2063 | PSA_DONE(); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2064 | } |
| 2065 | /* END_CASE */ |
| 2066 | |
| 2067 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2068 | void effective_key_attributes(int type_arg, int expected_type_arg, |
| 2069 | int bits_arg, int expected_bits_arg, |
| 2070 | int usage_arg, int expected_usage_arg, |
| 2071 | int alg_arg, int expected_alg_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2072 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2073 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 2074 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 2075 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 2076 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 2077 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2078 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 2079 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2080 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 2081 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2082 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2083 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2084 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2085 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2086 | psa_set_key_usage_flags(&attributes, usage); |
| 2087 | psa_set_key_algorithm(&attributes, alg); |
| 2088 | psa_set_key_type(&attributes, key_type); |
| 2089 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2090 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2091 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
| 2092 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2093 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2094 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 2095 | TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type); |
| 2096 | TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits); |
| 2097 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
| 2098 | TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2099 | |
| 2100 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2101 | /* |
| 2102 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2103 | * thus reset them as required. |
| 2104 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2105 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2106 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2107 | psa_destroy_key(key); |
| 2108 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2109 | } |
| 2110 | /* END_CASE */ |
| 2111 | |
| 2112 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2113 | void check_key_policy(int type_arg, int bits_arg, |
| 2114 | int usage_arg, int alg_arg) |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 2115 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2116 | test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg, |
| 2117 | usage_arg, |
| 2118 | mbedtls_test_update_key_usage_flags(usage_arg), |
| 2119 | alg_arg, alg_arg); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 2120 | goto exit; |
| 2121 | } |
| 2122 | /* END_CASE */ |
| 2123 | |
| 2124 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2125 | void key_attributes_init() |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 2126 | { |
| 2127 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2128 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2129 | * 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] | 2130 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2131 | psa_key_attributes_t func = psa_key_attributes_init(); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2132 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 2133 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 2134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2135 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 2136 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2137 | TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE); |
| 2138 | TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE); |
| 2139 | TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2140 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2141 | TEST_EQUAL(psa_get_key_type(&func), 0); |
| 2142 | TEST_EQUAL(psa_get_key_type(&init), 0); |
| 2143 | TEST_EQUAL(psa_get_key_type(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2144 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2145 | TEST_EQUAL(psa_get_key_bits(&func), 0); |
| 2146 | TEST_EQUAL(psa_get_key_bits(&init), 0); |
| 2147 | TEST_EQUAL(psa_get_key_bits(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2149 | TEST_EQUAL(psa_get_key_usage_flags(&func), 0); |
| 2150 | TEST_EQUAL(psa_get_key_usage_flags(&init), 0); |
| 2151 | TEST_EQUAL(psa_get_key_usage_flags(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2153 | TEST_EQUAL(psa_get_key_algorithm(&func), 0); |
| 2154 | TEST_EQUAL(psa_get_key_algorithm(&init), 0); |
| 2155 | TEST_EQUAL(psa_get_key_algorithm(&zero), 0); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 2156 | } |
| 2157 | /* END_CASE */ |
| 2158 | |
| 2159 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2160 | void mac_key_policy(int policy_usage_arg, |
| 2161 | int policy_alg_arg, |
| 2162 | int key_type_arg, |
| 2163 | data_t *key_data, |
| 2164 | int exercise_alg_arg, |
| 2165 | int expected_status_sign_arg, |
| 2166 | int expected_status_verify_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +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; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2170 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2171 | psa_key_type_t key_type = key_type_arg; |
| 2172 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 2173 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 2174 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2175 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 2176 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 2177 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2178 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2179 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2180 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2181 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2182 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2183 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2184 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2185 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2186 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2187 | &key)); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2188 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2189 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 2190 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2191 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2192 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 2193 | TEST_EQUAL(status, expected_status_sign); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2194 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 2195 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2196 | uint8_t input[128] = { 0 }; |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 2197 | size_t mac_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2198 | TEST_EQUAL(psa_mac_compute(key, exercise_alg, |
| 2199 | input, 128, |
| 2200 | mac, PSA_MAC_MAX_SIZE, &mac_len), |
| 2201 | expected_status_sign); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 2202 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2203 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2204 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 2205 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 2206 | if (status == PSA_SUCCESS) { |
| 2207 | status = psa_mac_update(&operation, input, 128); |
| 2208 | if (status == PSA_SUCCESS) { |
| 2209 | TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE, |
| 2210 | &mac_len), |
| 2211 | expected_status_sign); |
| 2212 | } else { |
| 2213 | TEST_EQUAL(status, expected_status_sign); |
| 2214 | } |
| 2215 | } else { |
| 2216 | TEST_EQUAL(status, expected_status_sign); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2217 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2218 | PSA_ASSERT(psa_mac_abort(&operation)); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2219 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 2220 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2221 | status = psa_mac_verify(key, exercise_alg, input, 128, |
| 2222 | mac, mac_len); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 2223 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2224 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 2225 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2226 | } else { |
| 2227 | TEST_EQUAL(status, expected_status_verify); |
| 2228 | } |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2229 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2230 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2231 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 2232 | if (status == PSA_SUCCESS) { |
| 2233 | status = psa_mac_update(&operation, input, 128); |
| 2234 | if (status == PSA_SUCCESS) { |
| 2235 | status = psa_mac_verify_finish(&operation, mac, mac_len); |
| 2236 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 2237 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2238 | } else { |
| 2239 | TEST_EQUAL(status, expected_status_verify); |
| 2240 | } |
| 2241 | } else { |
| 2242 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2243 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2244 | } else { |
| 2245 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2246 | } |
| 2247 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2248 | psa_mac_abort(&operation); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2249 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2250 | memset(mac, 0, sizeof(mac)); |
| 2251 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 2252 | TEST_EQUAL(status, expected_status_verify); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2253 | |
| 2254 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2255 | psa_mac_abort(&operation); |
| 2256 | psa_destroy_key(key); |
| 2257 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2258 | } |
| 2259 | /* END_CASE */ |
| 2260 | |
| 2261 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2262 | void cipher_key_policy(int policy_usage_arg, |
| 2263 | int policy_alg, |
| 2264 | int key_type, |
| 2265 | data_t *key_data, |
| 2266 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2267 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2268 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2269 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2270 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2271 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2272 | size_t output_buffer_size = 0; |
| 2273 | size_t input_buffer_size = 0; |
| 2274 | size_t output_length = 0; |
| 2275 | uint8_t *output = NULL; |
| 2276 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2277 | psa_status_t status; |
| 2278 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2279 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg); |
| 2280 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg, |
| 2281 | input_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2282 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2283 | TEST_CALLOC(input, input_buffer_size); |
| 2284 | TEST_CALLOC(output, output_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2285 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2286 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2287 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2288 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2289 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2290 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2291 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2292 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2293 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2294 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2295 | /* Check if no key usage flag implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2296 | TEST_EQUAL(policy_usage, |
| 2297 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2298 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2299 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2300 | status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size, |
| 2301 | output, output_buffer_size, |
| 2302 | &output_length); |
| 2303 | if (policy_alg == exercise_alg && |
| 2304 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2305 | PSA_ASSERT(status); |
| 2306 | } else { |
| 2307 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2308 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2309 | |
| 2310 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2311 | status = psa_cipher_encrypt_setup(&operation, key, exercise_alg); |
| 2312 | if (policy_alg == exercise_alg && |
| 2313 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2314 | PSA_ASSERT(status); |
| 2315 | } else { |
| 2316 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2317 | } |
| 2318 | psa_cipher_abort(&operation); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2319 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2320 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2321 | status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size, |
| 2322 | input, input_buffer_size, |
| 2323 | &output_length); |
| 2324 | if (policy_alg == exercise_alg && |
| 2325 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2326 | PSA_ASSERT(status); |
| 2327 | } else { |
| 2328 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2329 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2330 | |
| 2331 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2332 | status = psa_cipher_decrypt_setup(&operation, key, exercise_alg); |
| 2333 | if (policy_alg == exercise_alg && |
| 2334 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2335 | PSA_ASSERT(status); |
| 2336 | } else { |
| 2337 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2338 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2339 | |
| 2340 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2341 | psa_cipher_abort(&operation); |
| 2342 | mbedtls_free(input); |
| 2343 | mbedtls_free(output); |
| 2344 | psa_destroy_key(key); |
| 2345 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2346 | } |
| 2347 | /* END_CASE */ |
| 2348 | |
| 2349 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2350 | void aead_key_policy(int policy_usage_arg, |
| 2351 | int policy_alg, |
| 2352 | int key_type, |
| 2353 | data_t *key_data, |
| 2354 | int nonce_length_arg, |
| 2355 | int tag_length_arg, |
| 2356 | int exercise_alg, |
| 2357 | int expected_status_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2358 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2359 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2360 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2361 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2362 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2363 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2364 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2365 | unsigned char nonce[16] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2366 | size_t nonce_length = nonce_length_arg; |
| 2367 | unsigned char tag[16]; |
| 2368 | size_t tag_length = tag_length_arg; |
| 2369 | size_t output_length; |
| 2370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2371 | TEST_LE_U(nonce_length, sizeof(nonce)); |
| 2372 | TEST_LE_U(tag_length, sizeof(tag)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2374 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2375 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2376 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2377 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2378 | psa_set_key_type(&attributes, key_type); |
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 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2381 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2382 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2383 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2384 | TEST_EQUAL(policy_usage, |
| 2385 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2386 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2387 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2388 | status = psa_aead_encrypt(key, exercise_alg, |
| 2389 | nonce, nonce_length, |
| 2390 | NULL, 0, |
| 2391 | NULL, 0, |
| 2392 | tag, tag_length, |
| 2393 | &output_length); |
| 2394 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2395 | TEST_EQUAL(status, expected_status); |
| 2396 | } else { |
| 2397 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2398 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2399 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2400 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2401 | status = psa_aead_encrypt_setup(&operation, key, exercise_alg); |
| 2402 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2403 | TEST_EQUAL(status, expected_status); |
| 2404 | } else { |
| 2405 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2406 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2407 | |
| 2408 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2409 | memset(tag, 0, sizeof(tag)); |
| 2410 | status = psa_aead_decrypt(key, exercise_alg, |
| 2411 | nonce, nonce_length, |
| 2412 | NULL, 0, |
| 2413 | tag, tag_length, |
| 2414 | NULL, 0, |
| 2415 | &output_length); |
| 2416 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2417 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2418 | } else if (expected_status == PSA_SUCCESS) { |
| 2419 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2420 | } else { |
| 2421 | TEST_EQUAL(status, expected_status); |
| 2422 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2423 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2424 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2425 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2426 | status = psa_aead_decrypt_setup(&operation, key, exercise_alg); |
| 2427 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2428 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2429 | } else { |
| 2430 | TEST_EQUAL(status, expected_status); |
| 2431 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2432 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2433 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2434 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2435 | psa_destroy_key(key); |
| 2436 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2437 | } |
| 2438 | /* END_CASE */ |
| 2439 | |
| 2440 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2441 | void asymmetric_encryption_key_policy(int policy_usage_arg, |
| 2442 | int policy_alg, |
| 2443 | int key_type, |
| 2444 | data_t *key_data, |
Valerio Setti | f202c29 | 2024-01-15 10:42:37 +0100 | [diff] [blame] | 2445 | int exercise_alg, |
| 2446 | int use_opaque_key) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2447 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2448 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2449 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2450 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2451 | psa_status_t status; |
| 2452 | size_t key_bits; |
| 2453 | size_t buffer_length; |
| 2454 | unsigned char *buffer = NULL; |
| 2455 | size_t output_length; |
| 2456 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2457 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2458 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2459 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2460 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2461 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2462 | |
Valerio Setti | f202c29 | 2024-01-15 10:42:37 +0100 | [diff] [blame] | 2463 | if (use_opaque_key) { |
| 2464 | psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION( |
| 2465 | PSA_KEY_PERSISTENCE_VOLATILE, TEST_DRIVER_LOCATION)); |
| 2466 | } |
| 2467 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2468 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2469 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2470 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2471 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2472 | TEST_EQUAL(policy_usage, |
| 2473 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2474 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2475 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 2476 | key_bits = psa_get_key_bits(&attributes); |
| 2477 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, |
| 2478 | exercise_alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2479 | TEST_CALLOC(buffer, buffer_length); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2480 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2481 | status = psa_asymmetric_encrypt(key, exercise_alg, |
| 2482 | NULL, 0, |
| 2483 | NULL, 0, |
| 2484 | buffer, buffer_length, |
| 2485 | &output_length); |
| 2486 | if (policy_alg == exercise_alg && |
| 2487 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2488 | PSA_ASSERT(status); |
| 2489 | } else { |
| 2490 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2491 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2492 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2493 | if (buffer_length != 0) { |
| 2494 | memset(buffer, 0, buffer_length); |
| 2495 | } |
| 2496 | status = psa_asymmetric_decrypt(key, exercise_alg, |
| 2497 | buffer, buffer_length, |
| 2498 | NULL, 0, |
| 2499 | buffer, buffer_length, |
| 2500 | &output_length); |
| 2501 | if (policy_alg == exercise_alg && |
| 2502 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2503 | TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING); |
| 2504 | } else { |
| 2505 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2506 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2507 | |
| 2508 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2509 | /* |
| 2510 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2511 | * thus reset them as required. |
| 2512 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2513 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2514 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2515 | psa_destroy_key(key); |
| 2516 | PSA_DONE(); |
| 2517 | mbedtls_free(buffer); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2518 | } |
| 2519 | /* END_CASE */ |
| 2520 | |
| 2521 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2522 | void asymmetric_signature_key_policy(int policy_usage_arg, |
| 2523 | int policy_alg, |
| 2524 | int key_type, |
| 2525 | data_t *key_data, |
| 2526 | int exercise_alg, |
| 2527 | int payload_length_arg, |
| 2528 | int expected_usage_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2529 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2530 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2531 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2532 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 2533 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2534 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2535 | unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 }; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2536 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2537 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2538 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2539 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2540 | int compatible_alg = payload_length_arg > 0; |
| 2541 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2542 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2543 | size_t signature_length; |
| 2544 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2545 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2546 | in the expected usage flags. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2547 | TEST_EQUAL(expected_usage, |
| 2548 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2549 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2550 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2551 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2552 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2553 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2554 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2555 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2556 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2557 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2558 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2559 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2560 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2561 | status = psa_sign_hash(key, exercise_alg, |
| 2562 | payload, payload_length, |
| 2563 | signature, sizeof(signature), |
| 2564 | &signature_length); |
| 2565 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) { |
| 2566 | PSA_ASSERT(status); |
| 2567 | } else { |
| 2568 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2569 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2570 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2571 | memset(signature, 0, sizeof(signature)); |
| 2572 | status = psa_verify_hash(key, exercise_alg, |
| 2573 | payload, payload_length, |
| 2574 | signature, sizeof(signature)); |
| 2575 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) { |
| 2576 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2577 | } else { |
| 2578 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2579 | } |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2580 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2581 | if (PSA_ALG_IS_SIGN_HASH(exercise_alg) && |
| 2582 | PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) { |
| 2583 | status = psa_sign_message(key, exercise_alg, |
| 2584 | payload, payload_length, |
| 2585 | signature, sizeof(signature), |
| 2586 | &signature_length); |
| 2587 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) { |
| 2588 | PSA_ASSERT(status); |
| 2589 | } else { |
| 2590 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2591 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2592 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2593 | memset(signature, 0, sizeof(signature)); |
| 2594 | status = psa_verify_message(key, exercise_alg, |
| 2595 | payload, payload_length, |
| 2596 | signature, sizeof(signature)); |
| 2597 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) { |
| 2598 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2599 | } else { |
| 2600 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2601 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2602 | } |
| 2603 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2604 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2605 | psa_destroy_key(key); |
| 2606 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2607 | } |
| 2608 | /* END_CASE */ |
| 2609 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2610 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2611 | void derive_key_policy(int policy_usage, |
| 2612 | int policy_alg, |
| 2613 | int key_type, |
| 2614 | data_t *key_data, |
| 2615 | int exercise_alg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2616 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2617 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2618 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2619 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2620 | psa_status_t status; |
| 2621 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2622 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2623 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2624 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2625 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2626 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2627 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2628 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2629 | &key)); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2630 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2631 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2632 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2633 | if (PSA_ALG_IS_TLS12_PRF(exercise_alg) || |
| 2634 | PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) { |
| 2635 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 2636 | &operation, |
| 2637 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2638 | (const uint8_t *) "", 0)); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2639 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2640 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2641 | status = psa_key_derivation_input_key(&operation, |
| 2642 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 2643 | key); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2644 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2645 | if (policy_alg == exercise_alg && |
| 2646 | (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) { |
| 2647 | PSA_ASSERT(status); |
| 2648 | } else { |
| 2649 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2650 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2651 | |
| 2652 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2653 | psa_key_derivation_abort(&operation); |
| 2654 | psa_destroy_key(key); |
| 2655 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2656 | } |
| 2657 | /* END_CASE */ |
| 2658 | |
| 2659 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2660 | void agreement_key_policy(int policy_usage, |
| 2661 | int policy_alg, |
| 2662 | int key_type_arg, |
| 2663 | data_t *key_data, |
| 2664 | int exercise_alg, |
| 2665 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2666 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2667 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2668 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2669 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2670 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2671 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2672 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2673 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2674 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2676 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2677 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2678 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2679 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2680 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2681 | &key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2682 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2683 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
Ryan Everett | 73e4ea3 | 2024-03-12 16:29:55 +0000 | [diff] [blame] | 2684 | status = mbedtls_test_psa_key_agreement_with_self(&operation, key, 0); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2685 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2686 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2687 | |
| 2688 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2689 | psa_key_derivation_abort(&operation); |
| 2690 | psa_destroy_key(key); |
| 2691 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2692 | } |
| 2693 | /* END_CASE */ |
| 2694 | |
| 2695 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2696 | void key_policy_alg2(int key_type_arg, data_t *key_data, |
| 2697 | int usage_arg, int alg_arg, int alg2_arg) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2698 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2699 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2700 | psa_key_type_t key_type = key_type_arg; |
| 2701 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2702 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2703 | psa_key_usage_t usage = usage_arg; |
| 2704 | psa_algorithm_t alg = alg_arg; |
| 2705 | psa_algorithm_t alg2 = alg2_arg; |
| 2706 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2707 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2708 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2709 | psa_set_key_usage_flags(&attributes, usage); |
| 2710 | psa_set_key_algorithm(&attributes, alg); |
| 2711 | psa_set_key_enrollment_algorithm(&attributes, alg2); |
| 2712 | psa_set_key_type(&attributes, key_type); |
| 2713 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2714 | &key)); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2715 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2716 | /* Update the usage flags to obtain implicit usage flags */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2717 | usage = mbedtls_test_update_key_usage_flags(usage); |
| 2718 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 2719 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage); |
| 2720 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 2721 | TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2722 | |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 2723 | if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2724 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2725 | } |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 2726 | if (!mbedtls_test_psa_exercise_key(key, usage, alg2, 0)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2727 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2728 | } |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2729 | |
| 2730 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2731 | /* |
| 2732 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2733 | * thus reset them as required. |
| 2734 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2735 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2736 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2737 | psa_destroy_key(key); |
| 2738 | PSA_DONE(); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2739 | } |
| 2740 | /* END_CASE */ |
| 2741 | |
| 2742 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2743 | void raw_agreement_key_policy(int policy_usage, |
| 2744 | int policy_alg, |
| 2745 | int key_type_arg, |
| 2746 | data_t *key_data, |
| 2747 | int exercise_alg, |
| 2748 | int expected_status_arg) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2749 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2750 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2751 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2752 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2753 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2754 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2755 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2756 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2757 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2758 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2759 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2760 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2761 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2762 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2763 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2764 | &key)); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2765 | |
Ryan Everett | 8163028 | 2024-03-12 16:21:12 +0000 | [diff] [blame] | 2766 | 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] | 2767 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2768 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2769 | |
| 2770 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2771 | psa_key_derivation_abort(&operation); |
| 2772 | psa_destroy_key(key); |
| 2773 | PSA_DONE(); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2774 | } |
| 2775 | /* END_CASE */ |
| 2776 | |
| 2777 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2778 | void copy_success(int source_usage_arg, |
| 2779 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4ea4ad0 | 2022-12-04 15:11:00 +0100 | [diff] [blame] | 2780 | int source_lifetime_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2781 | int type_arg, data_t *material, |
| 2782 | int copy_attributes, |
| 2783 | int target_usage_arg, |
| 2784 | int target_alg_arg, int target_alg2_arg, |
Gilles Peskine | 4ea4ad0 | 2022-12-04 15:11:00 +0100 | [diff] [blame] | 2785 | int target_lifetime_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2786 | int expected_usage_arg, |
| 2787 | int expected_alg_arg, int expected_alg2_arg) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2788 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2789 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2790 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2791 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2792 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2793 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2794 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 2795 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2796 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2797 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2798 | uint8_t *export_buffer = NULL; |
| 2799 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2800 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2801 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2802 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2803 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2804 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2805 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2806 | psa_set_key_type(&source_attributes, type_arg); |
| 2807 | psa_set_key_lifetime(&source_attributes, source_lifetime); |
| 2808 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2809 | material->x, material->len, |
| 2810 | &source_key)); |
| 2811 | PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2812 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2813 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2814 | if (copy_attributes) { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2815 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2816 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2817 | psa_set_key_lifetime(&target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2818 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2819 | if (target_usage_arg != -1) { |
| 2820 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2821 | } |
| 2822 | if (target_alg_arg != -1) { |
| 2823 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2824 | } |
| 2825 | if (target_alg2_arg != -1) { |
| 2826 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
| 2827 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2828 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2829 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2830 | /* Copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2831 | PSA_ASSERT(psa_copy_key(source_key, |
| 2832 | &target_attributes, &target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2833 | |
| 2834 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2835 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2836 | |
| 2837 | /* Test that the target slot has the expected content and policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2838 | PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes)); |
| 2839 | TEST_EQUAL(psa_get_key_type(&source_attributes), |
| 2840 | psa_get_key_type(&target_attributes)); |
| 2841 | TEST_EQUAL(psa_get_key_bits(&source_attributes), |
| 2842 | psa_get_key_bits(&target_attributes)); |
| 2843 | TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes)); |
| 2844 | TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes)); |
| 2845 | TEST_EQUAL(expected_alg2, |
| 2846 | psa_get_key_enrollment_algorithm(&target_attributes)); |
| 2847 | if (expected_usage & PSA_KEY_USAGE_EXPORT) { |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2848 | size_t length; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2849 | TEST_CALLOC(export_buffer, material->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2850 | PSA_ASSERT(psa_export_key(target_key, export_buffer, |
| 2851 | material->len, &length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2852 | TEST_MEMORY_COMPARE(material->x, material->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2853 | export_buffer, length); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2854 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2855 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2856 | if (!psa_key_lifetime_is_external(target_lifetime)) { |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 2857 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg, 0)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2858 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2859 | } |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 2860 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2, 0)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2861 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2862 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2863 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2864 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2865 | PSA_ASSERT(psa_destroy_key(target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2866 | |
| 2867 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2868 | /* |
| 2869 | * Source and target key attributes may have been returned by |
| 2870 | * psa_get_key_attributes() thus reset them as required. |
| 2871 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2872 | psa_reset_key_attributes(&source_attributes); |
| 2873 | psa_reset_key_attributes(&target_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2874 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2875 | PSA_DONE(); |
| 2876 | mbedtls_free(export_buffer); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2877 | } |
| 2878 | /* END_CASE */ |
| 2879 | |
| 2880 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2881 | void copy_fail(int source_usage_arg, |
| 2882 | int source_alg_arg, int source_alg2_arg, |
| 2883 | int source_lifetime_arg, |
| 2884 | int type_arg, data_t *material, |
| 2885 | int target_type_arg, int target_bits_arg, |
| 2886 | int target_usage_arg, |
| 2887 | int target_alg_arg, int target_alg2_arg, |
| 2888 | int target_id_arg, int target_lifetime_arg, |
| 2889 | int expected_status_arg) |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2890 | { |
| 2891 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2892 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2893 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2894 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2895 | 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] | 2896 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2897 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2898 | |
| 2899 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2900 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2901 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2902 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2903 | psa_set_key_type(&source_attributes, type_arg); |
| 2904 | psa_set_key_lifetime(&source_attributes, source_lifetime_arg); |
| 2905 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2906 | material->x, material->len, |
| 2907 | &source_key)); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2908 | |
| 2909 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2910 | psa_set_key_id(&target_attributes, key_id); |
| 2911 | psa_set_key_lifetime(&target_attributes, target_lifetime_arg); |
| 2912 | psa_set_key_type(&target_attributes, target_type_arg); |
| 2913 | psa_set_key_bits(&target_attributes, target_bits_arg); |
| 2914 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2915 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2916 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2917 | |
| 2918 | /* Try to copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2919 | TEST_EQUAL(psa_copy_key(source_key, |
| 2920 | &target_attributes, &target_key), |
| 2921 | expected_status_arg); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2922 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2923 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2924 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2925 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2926 | psa_reset_key_attributes(&source_attributes); |
| 2927 | psa_reset_key_attributes(&target_attributes); |
| 2928 | PSA_DONE(); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2929 | } |
| 2930 | /* END_CASE */ |
| 2931 | |
| 2932 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2933 | void hash_operation_init() |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2934 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2935 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2936 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2937 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2938 | * 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] | 2939 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2940 | psa_hash_operation_t func = psa_hash_operation_init(); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2941 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2942 | psa_hash_operation_t zero; |
| 2943 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2944 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2945 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2946 | /* A freshly-initialized hash operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2947 | TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)), |
| 2948 | PSA_ERROR_BAD_STATE); |
| 2949 | TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)), |
| 2950 | PSA_ERROR_BAD_STATE); |
| 2951 | TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)), |
| 2952 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2953 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2954 | /* A default hash operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2955 | PSA_ASSERT(psa_hash_abort(&func)); |
| 2956 | PSA_ASSERT(psa_hash_abort(&init)); |
| 2957 | PSA_ASSERT(psa_hash_abort(&zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2958 | } |
| 2959 | /* END_CASE */ |
| 2960 | |
| 2961 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2962 | void hash_setup(int alg_arg, |
| 2963 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2964 | { |
| 2965 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2966 | uint8_t *output = NULL; |
| 2967 | size_t output_size = 0; |
| 2968 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2969 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2970 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2971 | psa_status_t status; |
| 2972 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2973 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2974 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2975 | /* Hash Setup, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2976 | output_size = PSA_HASH_LENGTH(alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2977 | TEST_CALLOC(output, output_size); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2978 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2979 | status = psa_hash_compute(alg, NULL, 0, |
| 2980 | output, output_size, &output_length); |
| 2981 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2982 | |
| 2983 | /* Hash Setup, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2984 | status = psa_hash_setup(&operation, alg); |
| 2985 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2986 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2987 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2988 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2989 | |
| 2990 | /* If setup failed, reproduce the failure, so as to |
| 2991 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2992 | if (status != PSA_SUCCESS) { |
| 2993 | TEST_EQUAL(psa_hash_setup(&operation, alg), status); |
| 2994 | } |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2995 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2996 | /* Now the operation object should be reusable. */ |
| 2997 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2998 | PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG)); |
| 2999 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3000 | #endif |
| 3001 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3002 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3003 | mbedtls_free(output); |
| 3004 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3005 | } |
| 3006 | /* END_CASE */ |
| 3007 | |
| 3008 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3009 | void hash_compute_fail(int alg_arg, data_t *input, |
| 3010 | int output_size_arg, int expected_status_arg) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3011 | { |
| 3012 | psa_algorithm_t alg = alg_arg; |
| 3013 | uint8_t *output = NULL; |
| 3014 | size_t output_size = output_size_arg; |
| 3015 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 3016 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3017 | psa_status_t expected_status = expected_status_arg; |
| 3018 | psa_status_t status; |
| 3019 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3020 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3021 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3022 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3023 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 3024 | /* Hash Compute, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3025 | status = psa_hash_compute(alg, input->x, input->len, |
| 3026 | output, output_size, &output_length); |
| 3027 | TEST_EQUAL(status, expected_status); |
| 3028 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3029 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 3030 | /* Hash Compute, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3031 | status = psa_hash_setup(&operation, alg); |
| 3032 | if (status == PSA_SUCCESS) { |
| 3033 | status = psa_hash_update(&operation, input->x, input->len); |
| 3034 | if (status == PSA_SUCCESS) { |
| 3035 | status = psa_hash_finish(&operation, output, output_size, |
| 3036 | &output_length); |
| 3037 | if (status == PSA_SUCCESS) { |
| 3038 | TEST_LE_U(output_length, output_size); |
| 3039 | } else { |
| 3040 | TEST_EQUAL(status, expected_status); |
| 3041 | } |
| 3042 | } else { |
| 3043 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 3044 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3045 | } else { |
| 3046 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 3047 | } |
| 3048 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3049 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3050 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3051 | mbedtls_free(output); |
| 3052 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3053 | } |
| 3054 | /* END_CASE */ |
| 3055 | |
| 3056 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3057 | void hash_compare_fail(int alg_arg, data_t *input, |
| 3058 | data_t *reference_hash, |
| 3059 | int expected_status_arg) |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 3060 | { |
| 3061 | psa_algorithm_t alg = alg_arg; |
| 3062 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 3063 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 3064 | psa_status_t status; |
| 3065 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3066 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 3067 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 3068 | /* Hash Compare, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3069 | status = psa_hash_compare(alg, input->x, input->len, |
| 3070 | reference_hash->x, reference_hash->len); |
| 3071 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 3072 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 3073 | /* Hash Compare, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3074 | status = psa_hash_setup(&operation, alg); |
| 3075 | if (status == PSA_SUCCESS) { |
| 3076 | status = psa_hash_update(&operation, input->x, input->len); |
| 3077 | if (status == PSA_SUCCESS) { |
| 3078 | status = psa_hash_verify(&operation, reference_hash->x, |
| 3079 | reference_hash->len); |
| 3080 | TEST_EQUAL(status, expected_status); |
| 3081 | } else { |
| 3082 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 3083 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3084 | } else { |
| 3085 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 3086 | } |
| 3087 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 3088 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3089 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3090 | PSA_DONE(); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 3091 | } |
| 3092 | /* END_CASE */ |
| 3093 | |
| 3094 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3095 | void hash_compute_compare(int alg_arg, data_t *input, |
| 3096 | data_t *expected_output) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3097 | { |
| 3098 | psa_algorithm_t alg = alg_arg; |
| 3099 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 3100 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3101 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3102 | size_t i; |
| 3103 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3104 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3105 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3106 | /* Compute with tight buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3107 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 3108 | output, PSA_HASH_LENGTH(alg), |
| 3109 | &output_length)); |
| 3110 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3111 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3112 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3113 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3114 | /* Compute with tight buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3115 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3116 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 3117 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 3118 | PSA_HASH_LENGTH(alg), |
| 3119 | &output_length)); |
| 3120 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3121 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3122 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3123 | |
| 3124 | /* Compute with larger buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3125 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 3126 | output, sizeof(output), |
| 3127 | &output_length)); |
| 3128 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3129 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3130 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3131 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3132 | /* Compute with larger buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3133 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3134 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 3135 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 3136 | sizeof(output), &output_length)); |
| 3137 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3138 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3139 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3140 | |
| 3141 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3142 | PSA_ASSERT(psa_hash_compare(alg, input->x, input->len, |
| 3143 | output, output_length)); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3144 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3145 | /* Compare with correct hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3146 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3147 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 3148 | PSA_ASSERT(psa_hash_verify(&operation, output, |
| 3149 | output_length)); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3150 | |
| 3151 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3152 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 3153 | output, output_length + 1), |
| 3154 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3155 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3156 | /* Compare with trailing garbage, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3157 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3158 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 3159 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1), |
| 3160 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3161 | |
| 3162 | /* Compare with truncated hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3163 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 3164 | output, output_length - 1), |
| 3165 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3166 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3167 | /* Compare with truncated hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3168 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3169 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 3170 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1), |
| 3171 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3172 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3173 | /* Compare with corrupted value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3174 | for (i = 0; i < output_length; i++) { |
| 3175 | mbedtls_test_set_step(i); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3176 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3177 | |
| 3178 | /* One-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3179 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 3180 | output, output_length), |
| 3181 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3182 | |
| 3183 | /* Multi-Part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3184 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3185 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 3186 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length), |
| 3187 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3188 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3189 | output[i] ^= 1; |
| 3190 | } |
| 3191 | |
| 3192 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3193 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3194 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3195 | } |
| 3196 | /* END_CASE */ |
| 3197 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3198 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3199 | void hash_bad_order() |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3200 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3201 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3202 | unsigned char input[] = ""; |
| 3203 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3204 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3205 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 3206 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3207 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 |
| 3208 | }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3209 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3210 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 3211 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3212 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3213 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3214 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3215 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3216 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3217 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3218 | TEST_EQUAL(psa_hash_setup(&operation, alg), |
| 3219 | PSA_ERROR_BAD_STATE); |
| 3220 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3221 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3222 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3223 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3224 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3225 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 3226 | PSA_ERROR_BAD_STATE); |
| 3227 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3228 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3229 | /* Check that update calls abort on error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3230 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 3231 | operation.id = UINT_MAX; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3232 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3233 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 3234 | PSA_ERROR_BAD_STATE); |
| 3235 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3236 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3237 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3238 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3239 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3240 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3241 | PSA_ASSERT(psa_hash_finish(&operation, |
| 3242 | hash, sizeof(hash), &hash_len)); |
| 3243 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 3244 | PSA_ERROR_BAD_STATE); |
| 3245 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3246 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3247 | /* Call verify without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3248 | TEST_EQUAL(psa_hash_verify(&operation, |
| 3249 | valid_hash, sizeof(valid_hash)), |
| 3250 | PSA_ERROR_BAD_STATE); |
| 3251 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3252 | |
| 3253 | /* Call verify after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3254 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3255 | PSA_ASSERT(psa_hash_finish(&operation, |
| 3256 | hash, sizeof(hash), &hash_len)); |
| 3257 | TEST_EQUAL(psa_hash_verify(&operation, |
| 3258 | valid_hash, sizeof(valid_hash)), |
| 3259 | PSA_ERROR_BAD_STATE); |
| 3260 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3261 | |
| 3262 | /* Call verify twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3263 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3264 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3265 | PSA_ASSERT(psa_hash_verify(&operation, |
| 3266 | valid_hash, sizeof(valid_hash))); |
| 3267 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3268 | TEST_EQUAL(psa_hash_verify(&operation, |
| 3269 | valid_hash, sizeof(valid_hash)), |
| 3270 | PSA_ERROR_BAD_STATE); |
| 3271 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3272 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3273 | |
| 3274 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3275 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3276 | hash, sizeof(hash), &hash_len), |
| 3277 | PSA_ERROR_BAD_STATE); |
| 3278 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3279 | |
| 3280 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3281 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3282 | PSA_ASSERT(psa_hash_finish(&operation, |
| 3283 | hash, sizeof(hash), &hash_len)); |
| 3284 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3285 | hash, sizeof(hash), &hash_len), |
| 3286 | PSA_ERROR_BAD_STATE); |
| 3287 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3288 | |
| 3289 | /* Call finish after calling verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3290 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3291 | PSA_ASSERT(psa_hash_verify(&operation, |
| 3292 | valid_hash, sizeof(valid_hash))); |
| 3293 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3294 | hash, sizeof(hash), &hash_len), |
| 3295 | PSA_ERROR_BAD_STATE); |
| 3296 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3297 | |
| 3298 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3299 | PSA_DONE(); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3300 | } |
| 3301 | /* END_CASE */ |
| 3302 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3303 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3304 | void hash_verify_bad_args() |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3305 | { |
| 3306 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3307 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 3308 | * appended to it */ |
| 3309 | unsigned char hash[] = { |
| 3310 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 3311 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3312 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb |
| 3313 | }; |
| 3314 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 3315 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3316 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3317 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3318 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3319 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3320 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3321 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3322 | TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1), |
| 3323 | PSA_ERROR_INVALID_SIGNATURE); |
| 3324 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3325 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3326 | ASSERT_OPERATION_IS_INACTIVE(operation); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3327 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3328 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3329 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3330 | TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size), |
| 3331 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3332 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3333 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3334 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3335 | TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)), |
| 3336 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 3337 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3338 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3339 | PSA_DONE(); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3340 | } |
| 3341 | /* END_CASE */ |
| 3342 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3343 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3344 | void hash_finish_bad_args() |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3345 | { |
| 3346 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 3347 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3348 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 3349 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3350 | size_t hash_len; |
| 3351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3352 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3353 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3354 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3355 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3356 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3357 | hash, expected_size - 1, &hash_len), |
| 3358 | PSA_ERROR_BUFFER_TOO_SMALL); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3359 | |
| 3360 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3361 | PSA_DONE(); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3362 | } |
| 3363 | /* END_CASE */ |
| 3364 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3365 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3366 | void hash_clone_source_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3367 | { |
| 3368 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3369 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3370 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 3371 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3372 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3373 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3374 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3375 | size_t hash_len; |
| 3376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3377 | PSA_ASSERT(psa_crypto_init()); |
| 3378 | PSA_ASSERT(psa_hash_setup(&op_source, alg)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3379 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3380 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3381 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3382 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3383 | hash, sizeof(hash), &hash_len)); |
| 3384 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3385 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3386 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3387 | TEST_EQUAL(psa_hash_clone(&op_source, &op_setup), |
| 3388 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3389 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3390 | PSA_ASSERT(psa_hash_clone(&op_source, &op_init)); |
| 3391 | PSA_ASSERT(psa_hash_finish(&op_init, |
| 3392 | hash, sizeof(hash), &hash_len)); |
| 3393 | PSA_ASSERT(psa_hash_clone(&op_source, &op_finished)); |
| 3394 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3395 | hash, sizeof(hash), &hash_len)); |
| 3396 | PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted)); |
| 3397 | PSA_ASSERT(psa_hash_finish(&op_aborted, |
| 3398 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3399 | |
| 3400 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3401 | psa_hash_abort(&op_source); |
| 3402 | psa_hash_abort(&op_init); |
| 3403 | psa_hash_abort(&op_setup); |
| 3404 | psa_hash_abort(&op_finished); |
| 3405 | psa_hash_abort(&op_aborted); |
| 3406 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3407 | } |
| 3408 | /* END_CASE */ |
| 3409 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3410 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3411 | void hash_clone_target_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3412 | { |
| 3413 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3414 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3415 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3416 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3417 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3418 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3419 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 3420 | size_t hash_len; |
| 3421 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3422 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3423 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3424 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3425 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3426 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3427 | hash, sizeof(hash), &hash_len)); |
| 3428 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3429 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3430 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3431 | PSA_ASSERT(psa_hash_clone(&op_setup, &op_target)); |
| 3432 | PSA_ASSERT(psa_hash_finish(&op_target, |
| 3433 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3434 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3435 | TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE); |
| 3436 | TEST_EQUAL(psa_hash_clone(&op_finished, &op_target), |
| 3437 | PSA_ERROR_BAD_STATE); |
| 3438 | TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target), |
| 3439 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3440 | |
| 3441 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3442 | psa_hash_abort(&op_target); |
| 3443 | psa_hash_abort(&op_init); |
| 3444 | psa_hash_abort(&op_setup); |
| 3445 | psa_hash_abort(&op_finished); |
| 3446 | psa_hash_abort(&op_aborted); |
| 3447 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3448 | } |
| 3449 | /* END_CASE */ |
| 3450 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3451 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3452 | void mac_operation_init() |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3453 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3454 | const uint8_t input[1] = { 0 }; |
| 3455 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3456 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3457 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3458 | * 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] | 3459 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3460 | psa_mac_operation_t func = psa_mac_operation_init(); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3461 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 3462 | psa_mac_operation_t zero; |
| 3463 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3464 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3465 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3466 | /* A freshly-initialized MAC operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3467 | TEST_EQUAL(psa_mac_update(&func, |
| 3468 | input, sizeof(input)), |
| 3469 | PSA_ERROR_BAD_STATE); |
| 3470 | TEST_EQUAL(psa_mac_update(&init, |
| 3471 | input, sizeof(input)), |
| 3472 | PSA_ERROR_BAD_STATE); |
| 3473 | TEST_EQUAL(psa_mac_update(&zero, |
| 3474 | input, sizeof(input)), |
| 3475 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3476 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3477 | /* A default MAC operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3478 | PSA_ASSERT(psa_mac_abort(&func)); |
| 3479 | PSA_ASSERT(psa_mac_abort(&init)); |
| 3480 | PSA_ASSERT(psa_mac_abort(&zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3481 | } |
| 3482 | /* END_CASE */ |
| 3483 | |
| 3484 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3485 | void mac_setup(int key_type_arg, |
| 3486 | data_t *key, |
| 3487 | int alg_arg, |
| 3488 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3489 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3490 | psa_key_type_t key_type = key_type_arg; |
| 3491 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3492 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3493 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3494 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3495 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3496 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3497 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3498 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3499 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3500 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3501 | if (!exercise_mac_setup(key_type, key->x, key->len, alg, |
| 3502 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3503 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3504 | } |
| 3505 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3506 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3507 | /* The operation object should be reusable. */ |
| 3508 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3509 | if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 3510 | smoke_test_key_data, |
| 3511 | sizeof(smoke_test_key_data), |
| 3512 | KNOWN_SUPPORTED_MAC_ALG, |
| 3513 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3514 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3515 | } |
| 3516 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3517 | #endif |
| 3518 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3519 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3520 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3521 | } |
| 3522 | /* END_CASE */ |
| 3523 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3524 | /* 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] | 3525 | void mac_bad_order() |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3526 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3527 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3528 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 3529 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3530 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3531 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3532 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3533 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa |
| 3534 | }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3535 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3536 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3537 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 3538 | size_t sign_mac_length = 0; |
| 3539 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3540 | const uint8_t verify_mac[] = { |
| 3541 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 3542 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3543 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 |
| 3544 | }; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3545 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3546 | PSA_ASSERT(psa_crypto_init()); |
| 3547 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 3548 | psa_set_key_algorithm(&attributes, alg); |
| 3549 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3550 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3551 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3552 | &key)); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3553 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3554 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3555 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3556 | PSA_ERROR_BAD_STATE); |
| 3557 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3558 | |
| 3559 | /* Call sign finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3560 | TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac), |
| 3561 | &sign_mac_length), |
| 3562 | PSA_ERROR_BAD_STATE); |
| 3563 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3564 | |
| 3565 | /* Call verify finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3566 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3567 | verify_mac, sizeof(verify_mac)), |
| 3568 | PSA_ERROR_BAD_STATE); |
| 3569 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3570 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3571 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3572 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3573 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3574 | TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg), |
| 3575 | PSA_ERROR_BAD_STATE); |
| 3576 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3577 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3578 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3579 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3580 | /* Call update after sign finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3581 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3582 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3583 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3584 | sign_mac, sizeof(sign_mac), |
| 3585 | &sign_mac_length)); |
| 3586 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3587 | PSA_ERROR_BAD_STATE); |
| 3588 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3589 | |
| 3590 | /* Call update after verify finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3591 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3592 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3593 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3594 | verify_mac, sizeof(verify_mac))); |
| 3595 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3596 | PSA_ERROR_BAD_STATE); |
| 3597 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3598 | |
| 3599 | /* Call sign finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3600 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3601 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3602 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3603 | sign_mac, sizeof(sign_mac), |
| 3604 | &sign_mac_length)); |
| 3605 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3606 | sign_mac, sizeof(sign_mac), |
| 3607 | &sign_mac_length), |
| 3608 | PSA_ERROR_BAD_STATE); |
| 3609 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3610 | |
| 3611 | /* Call verify finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3612 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3613 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3614 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3615 | verify_mac, sizeof(verify_mac))); |
| 3616 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3617 | verify_mac, sizeof(verify_mac)), |
| 3618 | PSA_ERROR_BAD_STATE); |
| 3619 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3620 | |
| 3621 | /* Setup sign but try verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3622 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3623 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3624 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3625 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3626 | verify_mac, sizeof(verify_mac)), |
| 3627 | PSA_ERROR_BAD_STATE); |
| 3628 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3629 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3630 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3631 | |
| 3632 | /* Setup verify but try sign. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3633 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3634 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3635 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3636 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3637 | sign_mac, sizeof(sign_mac), |
| 3638 | &sign_mac_length), |
| 3639 | PSA_ERROR_BAD_STATE); |
| 3640 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3641 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3642 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3643 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3644 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3645 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3646 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3647 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3648 | } |
| 3649 | /* END_CASE */ |
| 3650 | |
| 3651 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3652 | void mac_sign_verify_multi(int key_type_arg, |
| 3653 | data_t *key_data, |
| 3654 | int alg_arg, |
| 3655 | data_t *input, |
| 3656 | int is_verify, |
| 3657 | data_t *expected_mac) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3658 | { |
| 3659 | size_t data_part_len = 0; |
| 3660 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3661 | 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] | 3662 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3663 | mbedtls_test_set_step(2000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3664 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3665 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3666 | alg_arg, |
| 3667 | input, data_part_len, |
| 3668 | expected_mac, |
| 3669 | is_verify, 0) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3670 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3671 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3672 | |
| 3673 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3674 | mbedtls_test_set_step(3000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3676 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3677 | alg_arg, |
| 3678 | input, data_part_len, |
| 3679 | expected_mac, |
| 3680 | is_verify, 1) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3681 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3682 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3683 | } |
| 3684 | |
| 3685 | /* Goto is required to silence warnings about unused labels, as we |
| 3686 | * don't actually do any test assertions in this function. */ |
| 3687 | goto exit; |
| 3688 | } |
| 3689 | /* END_CASE */ |
| 3690 | |
| 3691 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3692 | void mac_sign(int key_type_arg, |
| 3693 | data_t *key_data, |
| 3694 | int alg_arg, |
| 3695 | data_t *input, |
| 3696 | data_t *expected_mac) |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3697 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3698 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3699 | psa_key_type_t key_type = key_type_arg; |
| 3700 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3701 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3702 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3703 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3704 | size_t mac_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3705 | 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] | 3706 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3707 | const size_t output_sizes_to_test[] = { |
| 3708 | 0, |
| 3709 | 1, |
| 3710 | expected_mac->len - 1, |
| 3711 | expected_mac->len, |
| 3712 | expected_mac->len + 1, |
| 3713 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3714 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3715 | TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3716 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3717 | TEST_ASSERT(expected_mac->len == mac_buffer_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3718 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3719 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3720 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3721 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 3722 | psa_set_key_algorithm(&attributes, alg); |
| 3723 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3724 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3725 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3726 | &key)); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3727 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3728 | 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] | 3729 | const size_t output_size = output_sizes_to_test[i]; |
| 3730 | psa_status_t expected_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3731 | (output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3732 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3733 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3734 | mbedtls_test_set_step(output_size); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3735 | TEST_CALLOC(actual_mac, output_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3736 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3737 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3738 | TEST_EQUAL(psa_mac_compute(key, alg, |
| 3739 | input->x, input->len, |
| 3740 | actual_mac, output_size, &mac_length), |
| 3741 | expected_status); |
| 3742 | if (expected_status == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3743 | TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3744 | actual_mac, mac_length); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3745 | } |
| 3746 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3747 | if (output_size > 0) { |
| 3748 | memset(actual_mac, 0, output_size); |
| 3749 | } |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3750 | |
| 3751 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3752 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3753 | PSA_ASSERT(psa_mac_update(&operation, |
| 3754 | input->x, input->len)); |
| 3755 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3756 | actual_mac, output_size, |
| 3757 | &mac_length), |
| 3758 | expected_status); |
| 3759 | PSA_ASSERT(psa_mac_abort(&operation)); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3760 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3761 | if (expected_status == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3762 | TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3763 | actual_mac, mac_length); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3764 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3765 | mbedtls_free(actual_mac); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3766 | actual_mac = NULL; |
| 3767 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3768 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3769 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3770 | psa_mac_abort(&operation); |
| 3771 | psa_destroy_key(key); |
| 3772 | PSA_DONE(); |
| 3773 | mbedtls_free(actual_mac); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3774 | } |
| 3775 | /* END_CASE */ |
| 3776 | |
| 3777 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3778 | void mac_verify(int key_type_arg, |
| 3779 | data_t *key_data, |
| 3780 | int alg_arg, |
| 3781 | data_t *input, |
| 3782 | data_t *expected_mac) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3783 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3784 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3785 | psa_key_type_t key_type = key_type_arg; |
| 3786 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3787 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3788 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3789 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3790 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3791 | TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3792 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3793 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3794 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3795 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 3796 | psa_set_key_algorithm(&attributes, alg); |
| 3797 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3798 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3799 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3800 | &key)); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3801 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3802 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3803 | PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len, |
| 3804 | expected_mac->x, expected_mac->len)); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3805 | |
| 3806 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3807 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3808 | PSA_ASSERT(psa_mac_update(&operation, |
| 3809 | input->x, input->len)); |
| 3810 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3811 | expected_mac->x, |
| 3812 | expected_mac->len)); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3813 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3814 | /* Test a MAC that's too short, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3815 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3816 | input->x, input->len, |
| 3817 | expected_mac->x, |
| 3818 | expected_mac->len - 1), |
| 3819 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3820 | |
| 3821 | /* Test a MAC that's too short, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3822 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3823 | PSA_ASSERT(psa_mac_update(&operation, |
| 3824 | input->x, input->len)); |
| 3825 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3826 | expected_mac->x, |
| 3827 | expected_mac->len - 1), |
| 3828 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3829 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3830 | /* Test a MAC that's too long, one-shot case. */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3831 | TEST_CALLOC(perturbed_mac, expected_mac->len + 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3832 | memcpy(perturbed_mac, expected_mac->x, expected_mac->len); |
| 3833 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3834 | input->x, input->len, |
| 3835 | perturbed_mac, expected_mac->len + 1), |
| 3836 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3837 | |
| 3838 | /* Test a MAC that's too long, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3839 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3840 | PSA_ASSERT(psa_mac_update(&operation, |
| 3841 | input->x, input->len)); |
| 3842 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3843 | perturbed_mac, |
| 3844 | expected_mac->len + 1), |
| 3845 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3846 | |
| 3847 | /* Test changing one byte. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3848 | for (size_t i = 0; i < expected_mac->len; i++) { |
| 3849 | mbedtls_test_set_step(i); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3850 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3851 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3852 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3853 | input->x, input->len, |
| 3854 | perturbed_mac, expected_mac->len), |
| 3855 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3856 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3857 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3858 | PSA_ASSERT(psa_mac_update(&operation, |
| 3859 | input->x, input->len)); |
| 3860 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3861 | perturbed_mac, |
| 3862 | expected_mac->len), |
| 3863 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3864 | perturbed_mac[i] ^= 1; |
| 3865 | } |
| 3866 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3867 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3868 | psa_mac_abort(&operation); |
| 3869 | psa_destroy_key(key); |
| 3870 | PSA_DONE(); |
| 3871 | mbedtls_free(perturbed_mac); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3872 | } |
| 3873 | /* END_CASE */ |
| 3874 | |
| 3875 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3876 | void cipher_operation_init() |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3877 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3878 | const uint8_t input[1] = { 0 }; |
| 3879 | unsigned char output[1] = { 0 }; |
| 3880 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3881 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3882 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3883 | * 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] | 3884 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3885 | psa_cipher_operation_t func = psa_cipher_operation_init(); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3886 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3887 | psa_cipher_operation_t zero; |
| 3888 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3889 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3890 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3891 | /* A freshly-initialized cipher operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3892 | TEST_EQUAL(psa_cipher_update(&func, |
| 3893 | input, sizeof(input), |
| 3894 | output, sizeof(output), |
| 3895 | &output_length), |
| 3896 | PSA_ERROR_BAD_STATE); |
| 3897 | TEST_EQUAL(psa_cipher_update(&init, |
| 3898 | input, sizeof(input), |
| 3899 | output, sizeof(output), |
| 3900 | &output_length), |
| 3901 | PSA_ERROR_BAD_STATE); |
| 3902 | TEST_EQUAL(psa_cipher_update(&zero, |
| 3903 | input, sizeof(input), |
| 3904 | output, sizeof(output), |
| 3905 | &output_length), |
| 3906 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3907 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3908 | /* A default cipher operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3909 | PSA_ASSERT(psa_cipher_abort(&func)); |
| 3910 | PSA_ASSERT(psa_cipher_abort(&init)); |
| 3911 | PSA_ASSERT(psa_cipher_abort(&zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3912 | } |
| 3913 | /* END_CASE */ |
| 3914 | |
| 3915 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3916 | void cipher_setup(int key_type_arg, |
| 3917 | data_t *key, |
| 3918 | int alg_arg, |
| 3919 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3920 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3921 | psa_key_type_t key_type = key_type_arg; |
| 3922 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3923 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3924 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3925 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3926 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3927 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3928 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3929 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3930 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3931 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3932 | if (!exercise_cipher_setup(key_type, key->x, key->len, alg, |
| 3933 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3934 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3935 | } |
| 3936 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3937 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3938 | /* The operation object should be reusable. */ |
| 3939 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3940 | if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3941 | smoke_test_key_data, |
| 3942 | sizeof(smoke_test_key_data), |
| 3943 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3944 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3945 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3946 | } |
| 3947 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3948 | #endif |
| 3949 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3950 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3951 | psa_cipher_abort(&operation); |
| 3952 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3953 | } |
| 3954 | /* END_CASE */ |
| 3955 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3956 | /* 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] | 3957 | void cipher_bad_order() |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3958 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3959 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3960 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3961 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3962 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3963 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3964 | 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] | 3965 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3966 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3967 | 0xaa, 0xaa, 0xaa, 0xaa |
| 3968 | }; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3969 | const uint8_t text[] = { |
| 3970 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3971 | 0xbb, 0xbb, 0xbb, 0xbb |
| 3972 | }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3973 | 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] | 3974 | size_t length = 0; |
| 3975 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3976 | PSA_ASSERT(psa_crypto_init()); |
| 3977 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3978 | psa_set_key_algorithm(&attributes, alg); |
| 3979 | psa_set_key_type(&attributes, key_type); |
| 3980 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3981 | &key)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3982 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3983 | /* Call encrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3984 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3985 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3986 | TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg), |
| 3987 | PSA_ERROR_BAD_STATE); |
| 3988 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3989 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3990 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3991 | |
| 3992 | /* Call decrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3993 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3994 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3995 | TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg), |
| 3996 | PSA_ERROR_BAD_STATE); |
| 3997 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3998 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3999 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 4000 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4001 | /* Generate an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4002 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 4003 | buffer, sizeof(buffer), |
| 4004 | &length), |
| 4005 | PSA_ERROR_BAD_STATE); |
| 4006 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4007 | |
| 4008 | /* Generate an IV twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4009 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4010 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 4011 | buffer, sizeof(buffer), |
| 4012 | &length)); |
| 4013 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 4014 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 4015 | buffer, sizeof(buffer), |
| 4016 | &length), |
| 4017 | PSA_ERROR_BAD_STATE); |
| 4018 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 4019 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4020 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4021 | |
| 4022 | /* Generate an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4023 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4024 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 4025 | iv, sizeof(iv))); |
| 4026 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 4027 | buffer, sizeof(buffer), |
| 4028 | &length), |
| 4029 | PSA_ERROR_BAD_STATE); |
| 4030 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4031 | |
| 4032 | /* Set an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4033 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 4034 | iv, sizeof(iv)), |
| 4035 | PSA_ERROR_BAD_STATE); |
| 4036 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4037 | |
| 4038 | /* Set an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4039 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4040 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 4041 | iv, sizeof(iv))); |
| 4042 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 4043 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 4044 | iv, sizeof(iv)), |
| 4045 | PSA_ERROR_BAD_STATE); |
| 4046 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 4047 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4048 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4049 | |
| 4050 | /* Set an IV after it's already generated. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4051 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4052 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 4053 | buffer, sizeof(buffer), |
| 4054 | &length)); |
| 4055 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 4056 | iv, sizeof(iv)), |
| 4057 | PSA_ERROR_BAD_STATE); |
| 4058 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4059 | |
| 4060 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4061 | TEST_EQUAL(psa_cipher_update(&operation, |
| 4062 | text, sizeof(text), |
| 4063 | buffer, sizeof(buffer), |
| 4064 | &length), |
| 4065 | PSA_ERROR_BAD_STATE); |
| 4066 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4067 | |
| 4068 | /* Call update without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4069 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4070 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 4071 | TEST_EQUAL(psa_cipher_update(&operation, |
| 4072 | text, sizeof(text), |
| 4073 | buffer, sizeof(buffer), |
| 4074 | &length), |
| 4075 | PSA_ERROR_BAD_STATE); |
| 4076 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 4077 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4078 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4079 | |
| 4080 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4081 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4082 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 4083 | iv, sizeof(iv))); |
| 4084 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4085 | buffer, sizeof(buffer), &length)); |
| 4086 | TEST_EQUAL(psa_cipher_update(&operation, |
| 4087 | text, sizeof(text), |
| 4088 | buffer, sizeof(buffer), |
| 4089 | &length), |
| 4090 | PSA_ERROR_BAD_STATE); |
| 4091 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4092 | |
| 4093 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4094 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 4095 | buffer, sizeof(buffer), &length), |
| 4096 | PSA_ERROR_BAD_STATE); |
| 4097 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4098 | |
| 4099 | /* Call finish without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4100 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4101 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 4102 | * for cipher modes with padding. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4103 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 4104 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 4105 | buffer, sizeof(buffer), &length), |
| 4106 | PSA_ERROR_BAD_STATE); |
| 4107 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 4108 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4109 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4110 | |
| 4111 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4112 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4113 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 4114 | iv, sizeof(iv))); |
| 4115 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4116 | buffer, sizeof(buffer), &length)); |
| 4117 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 4118 | buffer, sizeof(buffer), &length), |
| 4119 | PSA_ERROR_BAD_STATE); |
| 4120 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4121 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4122 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 4123 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4124 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4125 | psa_cipher_abort(&operation); |
| 4126 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4127 | } |
| 4128 | /* END_CASE */ |
| 4129 | |
| 4130 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4131 | void cipher_encrypt_fail(int alg_arg, |
| 4132 | int key_type_arg, |
| 4133 | data_t *key_data, |
| 4134 | data_t *input, |
| 4135 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4136 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4137 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4138 | psa_status_t status; |
| 4139 | psa_key_type_t key_type = key_type_arg; |
| 4140 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 4141 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4142 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 }; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4143 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; |
| 4144 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4145 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4146 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4147 | size_t output_length = 0; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4148 | size_t function_output_length; |
| 4149 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4150 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4151 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4152 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 4153 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4154 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4155 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4156 | psa_set_key_algorithm(&attributes, alg); |
| 4157 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4159 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 4160 | input->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4161 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4162 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4163 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4164 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4165 | } |
| 4166 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4167 | /* Encrypt, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4168 | status = psa_cipher_encrypt(key, alg, input->x, input->len, output, |
| 4169 | output_buffer_size, &output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4170 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4171 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4172 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4173 | /* Encrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4174 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
| 4175 | if (status == PSA_SUCCESS) { |
| 4176 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 4177 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 4178 | iv, iv_size, |
| 4179 | &iv_length)); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4180 | } |
| 4181 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4182 | status = psa_cipher_update(&operation, input->x, input->len, |
| 4183 | output, output_buffer_size, |
| 4184 | &function_output_length); |
| 4185 | if (status == PSA_SUCCESS) { |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4186 | output_length += function_output_length; |
| 4187 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4188 | status = psa_cipher_finish(&operation, output + output_length, |
| 4189 | output_buffer_size - output_length, |
| 4190 | &function_output_length); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4191 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4192 | TEST_EQUAL(status, expected_status); |
| 4193 | } else { |
| 4194 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4195 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4196 | } else { |
| 4197 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4198 | } |
| 4199 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4200 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4201 | psa_cipher_abort(&operation); |
| 4202 | mbedtls_free(output); |
| 4203 | psa_destroy_key(key); |
| 4204 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +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_validate_iv_length(int alg, int key_type, data_t *key_data, |
| 4210 | data_t *input, int iv_length, |
| 4211 | int expected_result) |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4212 | { |
| 4213 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4214 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4215 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4216 | size_t output_buffer_size = 0; |
| 4217 | unsigned char *output = NULL; |
| 4218 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4219 | 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] | 4220 | TEST_CALLOC(output, output_buffer_size); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4221 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4222 | PSA_ASSERT(psa_crypto_init()); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4223 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4224 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4225 | psa_set_key_algorithm(&attributes, alg); |
| 4226 | psa_set_key_type(&attributes, key_type); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4227 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4228 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4229 | &key)); |
| 4230 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4231 | TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output, |
| 4232 | iv_length)); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4233 | |
| 4234 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4235 | psa_cipher_abort(&operation); |
| 4236 | mbedtls_free(output); |
| 4237 | psa_destroy_key(key); |
| 4238 | PSA_DONE(); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4239 | } |
| 4240 | /* END_CASE */ |
| 4241 | |
| 4242 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4243 | void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data, |
| 4244 | data_t *plaintext, data_t *ciphertext) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4245 | { |
| 4246 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4247 | psa_key_type_t key_type = key_type_arg; |
| 4248 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 4249 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4250 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4251 | unsigned char *output = NULL; |
| 4252 | size_t output_buffer_size = 0; |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4253 | size_t output_length, length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4254 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4256 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4257 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4258 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4259 | TEST_LE_U(ciphertext->len, |
| 4260 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len)); |
| 4261 | TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len), |
| 4262 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len)); |
| 4263 | TEST_LE_U(plaintext->len, |
| 4264 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len)); |
| 4265 | TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len), |
| 4266 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len)); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4267 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4268 | |
| 4269 | /* Set up key and output buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4270 | psa_set_key_usage_flags(&attributes, |
| 4271 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4272 | psa_set_key_algorithm(&attributes, alg); |
| 4273 | psa_set_key_type(&attributes, key_type); |
| 4274 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4275 | &key)); |
| 4276 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 4277 | plaintext->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4278 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4279 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4280 | /* set_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4281 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4282 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 4283 | PSA_ERROR_BAD_STATE); |
| 4284 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 4285 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 4286 | PSA_ERROR_BAD_STATE); |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 4287 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4288 | /* generate_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4289 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4290 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 4291 | &length), |
| 4292 | PSA_ERROR_BAD_STATE); |
| 4293 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 4294 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 4295 | &length), |
| 4296 | PSA_ERROR_BAD_STATE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4297 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4298 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4299 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4300 | output_length = 0; |
| 4301 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4302 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4303 | plaintext->x, plaintext->len, |
| 4304 | output, output_buffer_size, |
| 4305 | &length)); |
| 4306 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4307 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4308 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4309 | mbedtls_buffer_offset(output, output_length), |
| 4310 | output_buffer_size - output_length, |
| 4311 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4312 | output_length += length; |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4313 | TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4314 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4315 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4316 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4317 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4318 | output_length = 0; |
| 4319 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4320 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4321 | ciphertext->x, ciphertext->len, |
| 4322 | output, output_buffer_size, |
| 4323 | &length)); |
| 4324 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4325 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4326 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4327 | mbedtls_buffer_offset(output, output_length), |
| 4328 | output_buffer_size - output_length, |
| 4329 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4330 | output_length += length; |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4331 | TEST_MEMORY_COMPARE(plaintext->x, plaintext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4332 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4333 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4334 | /* One-shot encryption */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4335 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4336 | PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len, |
| 4337 | output, output_buffer_size, |
| 4338 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4339 | TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4340 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4341 | |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4342 | /* One-shot decryption */ |
| 4343 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4344 | PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len, |
| 4345 | output, output_buffer_size, |
| 4346 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4347 | TEST_MEMORY_COMPARE(plaintext->x, plaintext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4348 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4349 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4350 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4351 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4352 | mbedtls_free(output); |
| 4353 | psa_cipher_abort(&operation); |
| 4354 | psa_destroy_key(key); |
| 4355 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4356 | } |
| 4357 | /* END_CASE */ |
| 4358 | |
| 4359 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4360 | 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] | 4361 | { |
| 4362 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4363 | psa_algorithm_t alg = alg_arg; |
| 4364 | psa_key_type_t key_type = key_type_arg; |
| 4365 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4366 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4367 | psa_status_t status; |
| 4368 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4369 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4371 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4372 | psa_set_key_algorithm(&attributes, alg); |
| 4373 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4374 | |
| 4375 | /* Usage of either of these two size macros would cause divide by zero |
| 4376 | * with incorrect key types previously. Input length should be irrelevant |
| 4377 | * here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4378 | TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16), |
| 4379 | 0); |
| 4380 | TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4381 | |
| 4382 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4383 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4384 | &key)); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4385 | |
| 4386 | /* Should fail due to invalid alg type (to support invalid key type). |
| 4387 | * Encrypt or decrypt will end up in the same place. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4388 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4389 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4390 | TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4391 | |
| 4392 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4393 | psa_cipher_abort(&operation); |
| 4394 | psa_destroy_key(key); |
| 4395 | PSA_DONE(); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4396 | } |
| 4397 | /* END_CASE */ |
| 4398 | |
| 4399 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4400 | void cipher_encrypt_validation(int alg_arg, |
| 4401 | int key_type_arg, |
| 4402 | data_t *key_data, |
| 4403 | data_t *input) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4404 | { |
| 4405 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4406 | psa_key_type_t key_type = key_type_arg; |
| 4407 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4408 | size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4409 | unsigned char *output1 = NULL; |
| 4410 | size_t output1_buffer_size = 0; |
| 4411 | size_t output1_length = 0; |
| 4412 | unsigned char *output2 = NULL; |
| 4413 | size_t output2_buffer_size = 0; |
| 4414 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4415 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4416 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4417 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4418 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4419 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4420 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4421 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4422 | psa_set_key_algorithm(&attributes, alg); |
| 4423 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4424 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4425 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4426 | output2_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(output1, output1_buffer_size); |
| 4429 | TEST_CALLOC(output2, output2_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4430 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4431 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4432 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4433 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 4434 | /* The one-shot cipher encryption uses generated iv so validating |
| 4435 | the output is not possible. Validating with multipart encryption. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4436 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1, |
| 4437 | output1_buffer_size, &output1_length)); |
| 4438 | TEST_LE_U(output1_length, |
| 4439 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4440 | TEST_LE_U(output1_length, |
| 4441 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4442 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4443 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4444 | PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4445 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4446 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4447 | input->x, input->len, |
| 4448 | output2, output2_buffer_size, |
| 4449 | &function_output_length)); |
| 4450 | TEST_LE_U(function_output_length, |
| 4451 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4452 | TEST_LE_U(function_output_length, |
| 4453 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4454 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4455 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4456 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4457 | output2 + output2_length, |
| 4458 | output2_buffer_size - output2_length, |
| 4459 | &function_output_length)); |
| 4460 | TEST_LE_U(function_output_length, |
| 4461 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4462 | TEST_LE_U(function_output_length, |
| 4463 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4464 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4465 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4466 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4467 | TEST_MEMORY_COMPARE(output1 + iv_size, output1_length - iv_size, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4468 | output2, output2_length); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4469 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4470 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4471 | psa_cipher_abort(&operation); |
| 4472 | mbedtls_free(output1); |
| 4473 | mbedtls_free(output2); |
| 4474 | psa_destroy_key(key); |
| 4475 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4476 | } |
| 4477 | /* END_CASE */ |
| 4478 | |
| 4479 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4480 | void cipher_encrypt_multipart(int alg_arg, int key_type_arg, |
| 4481 | data_t *key_data, data_t *iv, |
| 4482 | data_t *input, |
| 4483 | int first_part_size_arg, |
| 4484 | int output1_length_arg, int output2_length_arg, |
| 4485 | data_t *expected_output, |
| 4486 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4487 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4488 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4489 | psa_key_type_t key_type = key_type_arg; |
| 4490 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4491 | psa_status_t status; |
| 4492 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4493 | size_t first_part_size = first_part_size_arg; |
| 4494 | size_t output1_length = output1_length_arg; |
| 4495 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4496 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4497 | size_t output_buffer_size = 0; |
| 4498 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4499 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4500 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4501 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4502 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4503 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4504 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4505 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4506 | psa_set_key_algorithm(&attributes, alg); |
| 4507 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4508 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4509 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4510 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4511 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4512 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4513 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4514 | if (iv->len > 0) { |
| 4515 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4516 | } |
| 4517 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4518 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4519 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4520 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4521 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4522 | TEST_LE_U(first_part_size, input->len); |
| 4523 | PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size, |
| 4524 | output, output_buffer_size, |
| 4525 | &function_output_length)); |
| 4526 | TEST_ASSERT(function_output_length == output1_length); |
| 4527 | TEST_LE_U(function_output_length, |
| 4528 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4529 | TEST_LE_U(function_output_length, |
| 4530 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4531 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4532 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4533 | if (first_part_size < input->len) { |
| 4534 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4535 | input->x + first_part_size, |
| 4536 | input->len - first_part_size, |
| 4537 | (output_buffer_size == 0 ? NULL : |
| 4538 | output + total_output_length), |
| 4539 | output_buffer_size - total_output_length, |
| 4540 | &function_output_length)); |
| 4541 | TEST_ASSERT(function_output_length == output2_length); |
| 4542 | TEST_LE_U(function_output_length, |
| 4543 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4544 | alg, |
| 4545 | input->len - first_part_size)); |
| 4546 | TEST_LE_U(function_output_length, |
| 4547 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4548 | total_output_length += function_output_length; |
| 4549 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4550 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4551 | status = psa_cipher_finish(&operation, |
| 4552 | (output_buffer_size == 0 ? NULL : |
| 4553 | output + total_output_length), |
| 4554 | output_buffer_size - total_output_length, |
| 4555 | &function_output_length); |
| 4556 | TEST_LE_U(function_output_length, |
| 4557 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4558 | TEST_LE_U(function_output_length, |
| 4559 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4560 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4561 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4562 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4563 | if (expected_status == PSA_SUCCESS) { |
| 4564 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4565 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4566 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4567 | output, total_output_length); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4568 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4569 | |
| 4570 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4571 | psa_cipher_abort(&operation); |
| 4572 | mbedtls_free(output); |
| 4573 | psa_destroy_key(key); |
| 4574 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4575 | } |
| 4576 | /* END_CASE */ |
| 4577 | |
| 4578 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4579 | void cipher_decrypt_multipart(int alg_arg, int key_type_arg, |
| 4580 | data_t *key_data, data_t *iv, |
| 4581 | data_t *input, |
| 4582 | int first_part_size_arg, |
| 4583 | int output1_length_arg, int output2_length_arg, |
| 4584 | data_t *expected_output, |
| 4585 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4586 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4587 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4588 | psa_key_type_t key_type = key_type_arg; |
| 4589 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4590 | psa_status_t status; |
| 4591 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4592 | size_t first_part_size = first_part_size_arg; |
| 4593 | size_t output1_length = output1_length_arg; |
| 4594 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4595 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4596 | size_t output_buffer_size = 0; |
| 4597 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4598 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4599 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4600 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4601 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4602 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4603 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4604 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4605 | psa_set_key_algorithm(&attributes, alg); |
| 4606 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4607 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4608 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4609 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4610 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4611 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4612 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4613 | if (iv->len > 0) { |
| 4614 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4615 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4616 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4617 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4618 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4619 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4620 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4621 | TEST_LE_U(first_part_size, input->len); |
| 4622 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4623 | input->x, first_part_size, |
| 4624 | output, output_buffer_size, |
| 4625 | &function_output_length)); |
| 4626 | TEST_ASSERT(function_output_length == output1_length); |
| 4627 | TEST_LE_U(function_output_length, |
| 4628 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4629 | TEST_LE_U(function_output_length, |
| 4630 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4631 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4632 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4633 | if (first_part_size < input->len) { |
| 4634 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4635 | input->x + first_part_size, |
| 4636 | input->len - first_part_size, |
| 4637 | (output_buffer_size == 0 ? NULL : |
| 4638 | output + total_output_length), |
| 4639 | output_buffer_size - total_output_length, |
| 4640 | &function_output_length)); |
| 4641 | TEST_ASSERT(function_output_length == output2_length); |
| 4642 | TEST_LE_U(function_output_length, |
| 4643 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4644 | alg, |
| 4645 | input->len - first_part_size)); |
| 4646 | TEST_LE_U(function_output_length, |
| 4647 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4648 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4649 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4650 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4651 | status = psa_cipher_finish(&operation, |
| 4652 | (output_buffer_size == 0 ? NULL : |
| 4653 | output + total_output_length), |
| 4654 | output_buffer_size - total_output_length, |
| 4655 | &function_output_length); |
| 4656 | TEST_LE_U(function_output_length, |
| 4657 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4658 | TEST_LE_U(function_output_length, |
| 4659 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4660 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4661 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4662 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4663 | if (expected_status == PSA_SUCCESS) { |
| 4664 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4665 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4666 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4667 | output, total_output_length); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4668 | } |
| 4669 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4670 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4671 | psa_cipher_abort(&operation); |
| 4672 | mbedtls_free(output); |
| 4673 | psa_destroy_key(key); |
| 4674 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4675 | } |
| 4676 | /* END_CASE */ |
| 4677 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4678 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4679 | void cipher_decrypt_fail(int alg_arg, |
| 4680 | int key_type_arg, |
| 4681 | data_t *key_data, |
| 4682 | data_t *iv, |
| 4683 | data_t *input_arg, |
| 4684 | int expected_status_arg) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4685 | { |
| 4686 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4687 | psa_status_t status; |
| 4688 | psa_key_type_t key_type = key_type_arg; |
| 4689 | psa_algorithm_t alg = alg_arg; |
| 4690 | psa_status_t expected_status = expected_status_arg; |
| 4691 | unsigned char *input = NULL; |
| 4692 | size_t input_buffer_size = 0; |
| 4693 | unsigned char *output = NULL; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4694 | unsigned char *output_multi = NULL; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4695 | size_t output_buffer_size = 0; |
| 4696 | size_t output_length = 0; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4697 | size_t function_output_length; |
| 4698 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4699 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4700 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4701 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 4702 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4703 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4704 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4705 | psa_set_key_algorithm(&attributes, alg); |
| 4706 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4707 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4708 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4709 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4710 | } |
| 4711 | |
| 4712 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4713 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4714 | if (input_buffer_size > 0) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4715 | TEST_CALLOC(input, input_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4716 | memcpy(input, iv->x, iv->len); |
| 4717 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4718 | } |
| 4719 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4720 | 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] | 4721 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4722 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4723 | /* Decrypt, one-short */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4724 | status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4725 | output_buffer_size, &output_length); |
| 4726 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4727 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4728 | /* Decrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4729 | status = psa_cipher_decrypt_setup(&operation, key, alg); |
| 4730 | if (status == PSA_SUCCESS) { |
| 4731 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 4732 | input_arg->len) + |
| 4733 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4734 | TEST_CALLOC(output_multi, output_buffer_size); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4735 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4736 | if (iv->len > 0) { |
| 4737 | status = psa_cipher_set_iv(&operation, iv->x, iv->len); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4738 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4739 | if (status != PSA_SUCCESS) { |
| 4740 | TEST_EQUAL(status, expected_status); |
| 4741 | } |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4742 | } |
| 4743 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4744 | if (status == PSA_SUCCESS) { |
| 4745 | status = psa_cipher_update(&operation, |
| 4746 | input_arg->x, input_arg->len, |
| 4747 | output_multi, output_buffer_size, |
| 4748 | &function_output_length); |
| 4749 | if (status == PSA_SUCCESS) { |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4750 | output_length = function_output_length; |
| 4751 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4752 | status = psa_cipher_finish(&operation, |
| 4753 | output_multi + output_length, |
| 4754 | output_buffer_size - output_length, |
| 4755 | &function_output_length); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4756 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4757 | TEST_EQUAL(status, expected_status); |
| 4758 | } else { |
| 4759 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4760 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4761 | } else { |
| 4762 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4763 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4764 | } else { |
| 4765 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4766 | } |
| 4767 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4768 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4769 | psa_cipher_abort(&operation); |
| 4770 | mbedtls_free(input); |
| 4771 | mbedtls_free(output); |
| 4772 | mbedtls_free(output_multi); |
| 4773 | psa_destroy_key(key); |
| 4774 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4775 | } |
| 4776 | /* END_CASE */ |
| 4777 | |
| 4778 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4779 | void cipher_decrypt(int alg_arg, |
| 4780 | int key_type_arg, |
| 4781 | data_t *key_data, |
| 4782 | data_t *iv, |
| 4783 | data_t *input_arg, |
| 4784 | data_t *expected_output) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4785 | { |
| 4786 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4787 | psa_key_type_t key_type = key_type_arg; |
| 4788 | psa_algorithm_t alg = alg_arg; |
| 4789 | unsigned char *input = NULL; |
| 4790 | size_t input_buffer_size = 0; |
| 4791 | unsigned char *output = NULL; |
| 4792 | size_t output_buffer_size = 0; |
| 4793 | size_t output_length = 0; |
| 4794 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4795 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4796 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4797 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4798 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4799 | psa_set_key_algorithm(&attributes, alg); |
| 4800 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4801 | |
| 4802 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4803 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4804 | if (input_buffer_size > 0) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4805 | TEST_CALLOC(input, input_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4806 | memcpy(input, iv->x, iv->len); |
| 4807 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4808 | } |
| 4809 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4810 | 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] | 4811 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4812 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4813 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4814 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4815 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4816 | PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4817 | output_buffer_size, &output_length)); |
| 4818 | TEST_LE_U(output_length, |
| 4819 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size)); |
| 4820 | TEST_LE_U(output_length, |
| 4821 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4822 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4823 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4824 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4825 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4826 | mbedtls_free(input); |
| 4827 | mbedtls_free(output); |
| 4828 | psa_destroy_key(key); |
| 4829 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4830 | } |
| 4831 | /* END_CASE */ |
| 4832 | |
| 4833 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4834 | void cipher_verify_output(int alg_arg, |
| 4835 | int key_type_arg, |
| 4836 | data_t *key_data, |
| 4837 | data_t *input) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4838 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4839 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4840 | psa_key_type_t key_type = key_type_arg; |
| 4841 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4842 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4843 | size_t output1_size = 0; |
| 4844 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4845 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4846 | size_t output2_size = 0; |
| 4847 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4848 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4849 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4850 | PSA_ASSERT(psa_crypto_init()); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4851 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4852 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4853 | psa_set_key_algorithm(&attributes, alg); |
| 4854 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4855 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4856 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4857 | &key)); |
| 4858 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4859 | TEST_CALLOC(output1, output1_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4860 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4861 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, |
| 4862 | output1, output1_size, |
| 4863 | &output1_length)); |
| 4864 | TEST_LE_U(output1_length, |
| 4865 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4866 | TEST_LE_U(output1_length, |
| 4867 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4868 | |
| 4869 | output2_size = output1_length; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4870 | TEST_CALLOC(output2, output2_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4871 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4872 | PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length, |
| 4873 | output2, output2_size, |
| 4874 | &output2_length)); |
| 4875 | TEST_LE_U(output2_length, |
| 4876 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4877 | TEST_LE_U(output2_length, |
| 4878 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4879 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4880 | TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4881 | |
| 4882 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4883 | mbedtls_free(output1); |
| 4884 | mbedtls_free(output2); |
| 4885 | psa_destroy_key(key); |
| 4886 | PSA_DONE(); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4887 | } |
| 4888 | /* END_CASE */ |
| 4889 | |
| 4890 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4891 | void cipher_verify_output_multipart(int alg_arg, |
| 4892 | int key_type_arg, |
| 4893 | data_t *key_data, |
| 4894 | data_t *input, |
| 4895 | int first_part_size_arg) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4896 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4897 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4898 | psa_key_type_t key_type = key_type_arg; |
| 4899 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4900 | size_t first_part_size = first_part_size_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4901 | unsigned char iv[16] = { 0 }; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4902 | size_t iv_size = 16; |
| 4903 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4904 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4905 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4906 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4907 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4908 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4909 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4910 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4911 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 4912 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4913 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4914 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4915 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4916 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4917 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4918 | psa_set_key_algorithm(&attributes, alg); |
| 4919 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4920 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4921 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4922 | &key)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4923 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4924 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg)); |
| 4925 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4926 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4927 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 4928 | PSA_ASSERT(psa_cipher_generate_iv(&operation1, |
| 4929 | iv, iv_size, |
| 4930 | &iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4931 | } |
| 4932 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4933 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4934 | TEST_LE_U(output1_buffer_size, |
| 4935 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4936 | TEST_CALLOC(output1, output1_buffer_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4937 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4938 | TEST_LE_U(first_part_size, input->len); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4939 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4940 | PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size, |
| 4941 | output1, output1_buffer_size, |
| 4942 | &function_output_length)); |
| 4943 | TEST_LE_U(function_output_length, |
| 4944 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4945 | TEST_LE_U(function_output_length, |
| 4946 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4947 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4948 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4949 | PSA_ASSERT(psa_cipher_update(&operation1, |
| 4950 | input->x + first_part_size, |
| 4951 | input->len - first_part_size, |
David Horstmann | b8dc245 | 2024-02-06 17:03:13 +0000 | [diff] [blame] | 4952 | output1 + output1_length, |
| 4953 | output1_buffer_size - output1_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4954 | &function_output_length)); |
| 4955 | TEST_LE_U(function_output_length, |
| 4956 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4957 | alg, |
| 4958 | input->len - first_part_size)); |
| 4959 | TEST_LE_U(function_output_length, |
| 4960 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4961 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4962 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4963 | PSA_ASSERT(psa_cipher_finish(&operation1, |
| 4964 | output1 + output1_length, |
| 4965 | output1_buffer_size - output1_length, |
| 4966 | &function_output_length)); |
| 4967 | TEST_LE_U(function_output_length, |
| 4968 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4969 | TEST_LE_U(function_output_length, |
| 4970 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4971 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4972 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4973 | PSA_ASSERT(psa_cipher_abort(&operation1)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4974 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4975 | output2_buffer_size = output1_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4976 | TEST_LE_U(output2_buffer_size, |
| 4977 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4978 | TEST_LE_U(output2_buffer_size, |
| 4979 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4980 | TEST_CALLOC(output2, output2_buffer_size); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4981 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4982 | if (iv_length > 0) { |
| 4983 | PSA_ASSERT(psa_cipher_set_iv(&operation2, |
| 4984 | iv, iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4985 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4986 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4987 | PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size, |
| 4988 | output2, output2_buffer_size, |
| 4989 | &function_output_length)); |
| 4990 | TEST_LE_U(function_output_length, |
| 4991 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4992 | TEST_LE_U(function_output_length, |
| 4993 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4994 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4995 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4996 | PSA_ASSERT(psa_cipher_update(&operation2, |
| 4997 | output1 + first_part_size, |
| 4998 | output1_length - first_part_size, |
David Horstmann | b8dc245 | 2024-02-06 17:03:13 +0000 | [diff] [blame] | 4999 | output2 + output2_length, |
| 5000 | output2_buffer_size - output2_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5001 | &function_output_length)); |
| 5002 | TEST_LE_U(function_output_length, |
| 5003 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 5004 | alg, |
| 5005 | output1_length - first_part_size)); |
| 5006 | TEST_LE_U(function_output_length, |
| 5007 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 5008 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 5009 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5010 | PSA_ASSERT(psa_cipher_finish(&operation2, |
| 5011 | output2 + output2_length, |
| 5012 | output2_buffer_size - output2_length, |
| 5013 | &function_output_length)); |
| 5014 | TEST_LE_U(function_output_length, |
| 5015 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 5016 | TEST_LE_U(function_output_length, |
| 5017 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 5018 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 5019 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5020 | PSA_ASSERT(psa_cipher_abort(&operation2)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 5021 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 5022 | TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 5023 | |
| 5024 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5025 | psa_cipher_abort(&operation1); |
| 5026 | psa_cipher_abort(&operation2); |
| 5027 | mbedtls_free(output1); |
| 5028 | mbedtls_free(output2); |
| 5029 | psa_destroy_key(key); |
| 5030 | PSA_DONE(); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 5031 | } |
| 5032 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 5033 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5034 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5035 | void aead_encrypt_decrypt(int key_type_arg, data_t *key_data, |
| 5036 | int alg_arg, |
| 5037 | data_t *nonce, |
| 5038 | data_t *additional_data, |
| 5039 | data_t *input_data, |
| 5040 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5041 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5042 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5043 | psa_key_type_t key_type = key_type_arg; |
| 5044 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5045 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5046 | unsigned char *output_data = NULL; |
| 5047 | size_t output_size = 0; |
| 5048 | size_t output_length = 0; |
| 5049 | unsigned char *output_data2 = NULL; |
| 5050 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 5051 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 5052 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5053 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5054 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5055 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5056 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5057 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5058 | psa_set_key_algorithm(&attributes, alg); |
| 5059 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5060 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5061 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5062 | &key)); |
| 5063 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5064 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5065 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5066 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 5067 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5068 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 5069 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5070 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 5071 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
| 5072 | TEST_EQUAL(output_size, |
| 5073 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 5074 | TEST_LE_U(output_size, |
| 5075 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5076 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5077 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5078 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5079 | status = psa_aead_encrypt(key, alg, |
| 5080 | nonce->x, nonce->len, |
| 5081 | additional_data->x, |
| 5082 | additional_data->len, |
| 5083 | input_data->x, input_data->len, |
| 5084 | output_data, output_size, |
| 5085 | &output_length); |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 5086 | |
| 5087 | /* If the operation is not supported, just skip and not fail in case the |
| 5088 | * encryption involves a common limitation of cryptography hardwares and |
| 5089 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5090 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5091 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5092 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len); |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 5093 | } |
| 5094 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5095 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5096 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5097 | if (PSA_SUCCESS == expected_result) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5098 | TEST_CALLOC(output_data2, output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5099 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 5100 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 5101 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5102 | TEST_EQUAL(input_data->len, |
| 5103 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length)); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 5104 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5105 | TEST_LE_U(input_data->len, |
| 5106 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length)); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5107 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5108 | TEST_EQUAL(psa_aead_decrypt(key, alg, |
| 5109 | nonce->x, nonce->len, |
| 5110 | additional_data->x, |
| 5111 | additional_data->len, |
| 5112 | output_data, output_length, |
| 5113 | output_data2, output_length, |
| 5114 | &output_length2), |
| 5115 | expected_result); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5116 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 5117 | TEST_MEMORY_COMPARE(input_data->x, input_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 5118 | output_data2, output_length2); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5119 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5120 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5121 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5122 | psa_destroy_key(key); |
| 5123 | mbedtls_free(output_data); |
| 5124 | mbedtls_free(output_data2); |
| 5125 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5126 | } |
| 5127 | /* END_CASE */ |
| 5128 | |
| 5129 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5130 | void aead_encrypt(int key_type_arg, data_t *key_data, |
| 5131 | int alg_arg, |
| 5132 | data_t *nonce, |
| 5133 | data_t *additional_data, |
| 5134 | data_t *input_data, |
| 5135 | data_t *expected_result) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5136 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5137 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5138 | psa_key_type_t key_type = key_type_arg; |
| 5139 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5140 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5141 | unsigned char *output_data = NULL; |
| 5142 | size_t output_size = 0; |
| 5143 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5144 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 5145 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5146 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5147 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5149 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5150 | psa_set_key_algorithm(&attributes, alg); |
| 5151 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5153 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5154 | &key)); |
| 5155 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5156 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5157 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5158 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 5159 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5160 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 5161 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5162 | TEST_EQUAL(output_size, |
| 5163 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 5164 | TEST_LE_U(output_size, |
| 5165 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5166 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5167 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5168 | status = psa_aead_encrypt(key, alg, |
| 5169 | nonce->x, nonce->len, |
| 5170 | additional_data->x, additional_data->len, |
| 5171 | input_data->x, input_data->len, |
| 5172 | output_data, output_size, |
| 5173 | &output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5174 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 5175 | /* If the operation is not supported, just skip and not fail in case the |
| 5176 | * encryption involves a common limitation of cryptography hardwares and |
| 5177 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5178 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5179 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5180 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len); |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 5181 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 5182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5183 | PSA_ASSERT(status); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 5184 | TEST_MEMORY_COMPARE(expected_result->x, expected_result->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 5185 | output_data, output_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5186 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5187 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5188 | psa_destroy_key(key); |
| 5189 | mbedtls_free(output_data); |
| 5190 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5191 | } |
| 5192 | /* END_CASE */ |
| 5193 | |
| 5194 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5195 | void aead_decrypt(int key_type_arg, data_t *key_data, |
| 5196 | int alg_arg, |
| 5197 | data_t *nonce, |
| 5198 | data_t *additional_data, |
| 5199 | data_t *input_data, |
| 5200 | data_t *expected_data, |
| 5201 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5202 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5203 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5204 | psa_key_type_t key_type = key_type_arg; |
| 5205 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5206 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5207 | unsigned char *output_data = NULL; |
| 5208 | size_t output_size = 0; |
| 5209 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5210 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 5211 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 5212 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5213 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5214 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5215 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5216 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 5217 | psa_set_key_algorithm(&attributes, alg); |
| 5218 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5219 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5220 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5221 | &key)); |
| 5222 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5223 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5224 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5225 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 5226 | alg); |
| 5227 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 5228 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5229 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 5230 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5231 | TEST_EQUAL(output_size, |
| 5232 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 5233 | TEST_LE_U(output_size, |
| 5234 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5235 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5236 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5237 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5238 | status = psa_aead_decrypt(key, alg, |
| 5239 | nonce->x, nonce->len, |
| 5240 | additional_data->x, |
| 5241 | additional_data->len, |
| 5242 | input_data->x, input_data->len, |
| 5243 | output_data, output_size, |
| 5244 | &output_length); |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 5245 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 5246 | /* If the operation is not supported, just skip and not fail in case the |
| 5247 | * decryption involves a common limitation of cryptography hardwares and |
| 5248 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5249 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5250 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5251 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len); |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 5252 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 5253 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5254 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5256 | if (expected_result == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 5257 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 5258 | output_data, output_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5259 | } |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5260 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5261 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5262 | psa_destroy_key(key); |
| 5263 | mbedtls_free(output_data); |
| 5264 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5265 | } |
| 5266 | /* END_CASE */ |
| 5267 | |
| 5268 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5269 | void aead_multipart_encrypt(int key_type_arg, data_t *key_data, |
| 5270 | int alg_arg, |
| 5271 | data_t *nonce, |
| 5272 | data_t *additional_data, |
| 5273 | data_t *input_data, |
| 5274 | int do_set_lengths, |
| 5275 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5276 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5277 | size_t ad_part_len = 0; |
| 5278 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5279 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5280 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5281 | for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) { |
| 5282 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5283 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5284 | if (do_set_lengths) { |
| 5285 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5286 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5287 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5288 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5289 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5290 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5291 | |
| 5292 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5293 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5294 | alg_arg, nonce, |
| 5295 | additional_data, |
| 5296 | ad_part_len, |
| 5297 | input_data, -1, |
| 5298 | set_lengths_method, |
| 5299 | expected_output, |
| 5300 | 1, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5301 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5302 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5303 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5304 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 5305 | mbedtls_test_set_step(1000 + ad_part_len); |
| 5306 | |
| 5307 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5308 | alg_arg, nonce, |
| 5309 | additional_data, |
| 5310 | ad_part_len, |
| 5311 | input_data, -1, |
| 5312 | set_lengths_method, |
| 5313 | expected_output, |
| 5314 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5315 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5316 | } |
| 5317 | } |
| 5318 | |
| 5319 | for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) { |
| 5320 | /* Split data into length(data_part_len) parts. */ |
| 5321 | mbedtls_test_set_step(2000 + data_part_len); |
| 5322 | |
| 5323 | if (do_set_lengths) { |
| 5324 | if (data_part_len & 0x01) { |
| 5325 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 5326 | } else { |
| 5327 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
| 5328 | } |
| 5329 | } |
| 5330 | |
| 5331 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5332 | alg_arg, nonce, |
| 5333 | additional_data, -1, |
| 5334 | input_data, data_part_len, |
| 5335 | set_lengths_method, |
| 5336 | expected_output, |
| 5337 | 1, 0)) { |
| 5338 | break; |
| 5339 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5340 | |
| 5341 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5342 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5343 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5344 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5345 | alg_arg, nonce, |
| 5346 | additional_data, -1, |
| 5347 | input_data, data_part_len, |
| 5348 | set_lengths_method, |
| 5349 | expected_output, |
| 5350 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5351 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5352 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5353 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5354 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5355 | /* Goto is required to silence warnings about unused labels, as we |
| 5356 | * don't actually do any test assertions in this function. */ |
| 5357 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5358 | } |
| 5359 | /* END_CASE */ |
| 5360 | |
| 5361 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5362 | void aead_multipart_decrypt(int key_type_arg, data_t *key_data, |
| 5363 | int alg_arg, |
| 5364 | data_t *nonce, |
| 5365 | data_t *additional_data, |
| 5366 | data_t *input_data, |
| 5367 | int do_set_lengths, |
| 5368 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5369 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5370 | size_t ad_part_len = 0; |
| 5371 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5372 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5374 | for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5375 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5376 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5377 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5378 | if (do_set_lengths) { |
| 5379 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5380 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5381 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5382 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5383 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5384 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5386 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5387 | alg_arg, nonce, |
| 5388 | additional_data, |
| 5389 | ad_part_len, |
| 5390 | input_data, -1, |
| 5391 | set_lengths_method, |
| 5392 | expected_output, |
| 5393 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5394 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5395 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5396 | |
| 5397 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5398 | mbedtls_test_set_step(1000 + ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5399 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5400 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5401 | alg_arg, nonce, |
| 5402 | additional_data, |
| 5403 | ad_part_len, |
| 5404 | input_data, -1, |
| 5405 | set_lengths_method, |
| 5406 | expected_output, |
| 5407 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5408 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5409 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5410 | } |
| 5411 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5412 | for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5413 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5414 | mbedtls_test_set_step(2000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5415 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5416 | if (do_set_lengths) { |
| 5417 | if (data_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5418 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5419 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5420 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5421 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5422 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5423 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5424 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5425 | alg_arg, nonce, |
| 5426 | additional_data, -1, |
| 5427 | input_data, data_part_len, |
| 5428 | set_lengths_method, |
| 5429 | expected_output, |
| 5430 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5431 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5432 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5433 | |
| 5434 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5435 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5436 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5437 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5438 | alg_arg, nonce, |
| 5439 | additional_data, -1, |
| 5440 | input_data, data_part_len, |
| 5441 | set_lengths_method, |
| 5442 | expected_output, |
| 5443 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5444 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5445 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5446 | } |
| 5447 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5448 | /* Goto is required to silence warnings about unused labels, as we |
| 5449 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5450 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5451 | } |
| 5452 | /* END_CASE */ |
| 5453 | |
| 5454 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5455 | void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data, |
| 5456 | int alg_arg, |
| 5457 | int nonce_length, |
| 5458 | int expected_nonce_length_arg, |
| 5459 | data_t *additional_data, |
| 5460 | data_t *input_data, |
| 5461 | int expected_status_arg) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5462 | { |
| 5463 | |
| 5464 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5465 | psa_key_type_t key_type = key_type_arg; |
| 5466 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5467 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
David Horstmann | 52402ec | 2023-12-11 15:09:46 +0000 | [diff] [blame] | 5468 | /* Some tests try to get more than the maximum nonce length, |
| 5469 | * so allocate double. */ |
| 5470 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE * 2]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5471 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5472 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5473 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5474 | size_t actual_nonce_length = 0; |
| 5475 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 5476 | unsigned char *output = NULL; |
| 5477 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5478 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5479 | size_t ciphertext_size = 0; |
| 5480 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5481 | size_t tag_length = 0; |
| 5482 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5483 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5484 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5485 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5486 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5487 | psa_set_key_algorithm(&attributes, alg); |
| 5488 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5489 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5490 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5491 | &key)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5492 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5493 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5494 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5495 | 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] | 5496 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5497 | TEST_CALLOC(output, output_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5498 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5499 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5500 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5501 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5502 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5503 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5504 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5505 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5506 | |
| 5507 | /* If the operation is not supported, just skip and not fail in case the |
| 5508 | * encryption involves a common limitation of cryptography hardwares and |
| 5509 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5510 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5511 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5512 | 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] | 5513 | } |
| 5514 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5515 | PSA_ASSERT(status); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5516 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5517 | status = psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5518 | nonce_length, |
| 5519 | &actual_nonce_length); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5520 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5521 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5522 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5523 | TEST_EQUAL(actual_nonce_length, expected_nonce_length); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 5524 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5525 | if (expected_status == PSA_SUCCESS) { |
| 5526 | TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type, |
| 5527 | alg)); |
| 5528 | } |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 5529 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5530 | TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 5531 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5532 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5533 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5534 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5535 | input_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5536 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5537 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5538 | additional_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5539 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5540 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5541 | output, output_size, |
| 5542 | &ciphertext_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5543 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5544 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5545 | &ciphertext_length, tag_buffer, |
| 5546 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5547 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5548 | |
| 5549 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5550 | psa_destroy_key(key); |
| 5551 | mbedtls_free(output); |
| 5552 | mbedtls_free(ciphertext); |
| 5553 | psa_aead_abort(&operation); |
| 5554 | PSA_DONE(); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5555 | } |
| 5556 | /* END_CASE */ |
| 5557 | |
| 5558 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5559 | void aead_multipart_set_nonce(int key_type_arg, data_t *key_data, |
| 5560 | int alg_arg, |
| 5561 | int nonce_length_arg, |
| 5562 | int set_lengths_method_arg, |
| 5563 | data_t *additional_data, |
| 5564 | data_t *input_data, |
| 5565 | int expected_status_arg) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5566 | { |
| 5567 | |
| 5568 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5569 | psa_key_type_t key_type = key_type_arg; |
| 5570 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5571 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5572 | uint8_t *nonce_buffer = NULL; |
| 5573 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5574 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5575 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5576 | unsigned char *output = NULL; |
| 5577 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5578 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5579 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5580 | size_t ciphertext_size = 0; |
| 5581 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5582 | size_t tag_length = 0; |
| 5583 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5584 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5585 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5586 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5587 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5588 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5589 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5590 | psa_set_key_algorithm(&attributes, alg); |
| 5591 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5592 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5593 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5594 | &key)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5595 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5596 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5597 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5598 | 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] | 5599 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5600 | TEST_CALLOC(output, output_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5601 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5602 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5603 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5604 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5605 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5606 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5607 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5608 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5609 | |
| 5610 | /* If the operation is not supported, just skip and not fail in case the |
| 5611 | * encryption involves a common limitation of cryptography hardwares and |
| 5612 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5613 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5614 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5615 | 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] | 5616 | } |
| 5617 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5618 | PSA_ASSERT(status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5619 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5620 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5621 | if (nonce_length_arg == -1) { |
| 5622 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5623 | TEST_CALLOC(nonce_buffer, 4); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5624 | nonce_length = 0; |
| 5625 | } else { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5626 | /* If length is zero, then this will return NULL. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5627 | nonce_length = (size_t) nonce_length_arg; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5628 | TEST_CALLOC(nonce_buffer, nonce_length); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5629 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5630 | if (nonce_buffer) { |
| 5631 | for (index = 0; index < nonce_length - 1; ++index) { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5632 | nonce_buffer[index] = 'a' + index; |
| 5633 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5634 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5635 | } |
| 5636 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5637 | if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { |
| 5638 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5639 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5640 | } |
| 5641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5642 | status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5643 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5644 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5645 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5646 | if (expected_status == PSA_SUCCESS) { |
| 5647 | if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) { |
| 5648 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5649 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5650 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5651 | 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] | 5652 | expected_status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5653 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5654 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5655 | /* 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] | 5656 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5657 | additional_data->len), |
| 5658 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5659 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5660 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5661 | output, output_size, |
| 5662 | &ciphertext_length), |
| 5663 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5664 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5665 | TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5666 | &ciphertext_length, tag_buffer, |
| 5667 | PSA_AEAD_TAG_MAX_SIZE, &tag_length), |
| 5668 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5669 | } |
| 5670 | |
| 5671 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5672 | psa_destroy_key(key); |
| 5673 | mbedtls_free(output); |
| 5674 | mbedtls_free(ciphertext); |
| 5675 | mbedtls_free(nonce_buffer); |
| 5676 | psa_aead_abort(&operation); |
| 5677 | PSA_DONE(); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5678 | } |
| 5679 | /* END_CASE */ |
| 5680 | |
| 5681 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5682 | 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] | 5683 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5684 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5685 | data_t *nonce, |
| 5686 | data_t *additional_data, |
| 5687 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5688 | int expected_status_arg) |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5689 | { |
| 5690 | |
| 5691 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5692 | psa_key_type_t key_type = key_type_arg; |
| 5693 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5694 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5695 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5696 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5697 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5698 | unsigned char *output = NULL; |
| 5699 | unsigned char *ciphertext = NULL; |
| 5700 | size_t output_size = output_size_arg; |
| 5701 | size_t ciphertext_size = 0; |
| 5702 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5703 | size_t tag_length = 0; |
| 5704 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5705 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5706 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5707 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5708 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5709 | psa_set_key_algorithm(&attributes, alg); |
| 5710 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5711 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5712 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5713 | &key)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5714 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5715 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5716 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5717 | TEST_CALLOC(output, output_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5718 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5719 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5720 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5721 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5722 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5723 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5724 | |
| 5725 | /* If the operation is not supported, just skip and not fail in case the |
| 5726 | * encryption involves a common limitation of cryptography hardwares and |
| 5727 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5728 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5729 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5730 | 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] | 5731 | } |
| 5732 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5733 | PSA_ASSERT(status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5734 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5735 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5736 | input_data->len)); |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5737 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5738 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5739 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5740 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5741 | additional_data->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5742 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5743 | status = psa_aead_update(&operation, input_data->x, input_data->len, |
| 5744 | output, output_size, &ciphertext_length); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5745 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5746 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5747 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5748 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5749 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5750 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5751 | &ciphertext_length, tag_buffer, |
| 5752 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5753 | } |
| 5754 | |
| 5755 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5756 | psa_destroy_key(key); |
| 5757 | mbedtls_free(output); |
| 5758 | mbedtls_free(ciphertext); |
| 5759 | psa_aead_abort(&operation); |
| 5760 | PSA_DONE(); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5761 | } |
| 5762 | /* END_CASE */ |
| 5763 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5764 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5765 | void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data, |
| 5766 | int alg_arg, |
| 5767 | int finish_ciphertext_size_arg, |
| 5768 | int tag_size_arg, |
| 5769 | data_t *nonce, |
| 5770 | data_t *additional_data, |
| 5771 | data_t *input_data, |
| 5772 | int expected_status_arg) |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5773 | { |
| 5774 | |
| 5775 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5776 | psa_key_type_t key_type = key_type_arg; |
| 5777 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5778 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5779 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5780 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5781 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5782 | unsigned char *ciphertext = NULL; |
| 5783 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5784 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5785 | size_t ciphertext_size = 0; |
| 5786 | size_t ciphertext_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5787 | size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg; |
| 5788 | size_t tag_size = (size_t) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5789 | size_t tag_length = 0; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5790 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5791 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5792 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5793 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5794 | psa_set_key_algorithm(&attributes, alg); |
| 5795 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5796 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5797 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5798 | &key)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5799 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5800 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5801 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5802 | 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] | 5803 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5804 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5805 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5806 | TEST_CALLOC(finish_ciphertext, finish_ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5807 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5808 | TEST_CALLOC(tag_buffer, tag_size); |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5809 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5810 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5811 | |
| 5812 | /* If the operation is not supported, just skip and not fail in case the |
| 5813 | * encryption involves a common limitation of cryptography hardwares and |
| 5814 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5815 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5816 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5817 | 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] | 5818 | } |
| 5819 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5820 | PSA_ASSERT(status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5821 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5822 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5823 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5824 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5825 | input_data->len)); |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5826 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5827 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5828 | additional_data->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5829 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5830 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5831 | ciphertext, ciphertext_size, &ciphertext_length)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5832 | |
| 5833 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5834 | status = psa_aead_finish(&operation, finish_ciphertext, |
| 5835 | finish_ciphertext_size, |
| 5836 | &ciphertext_length, tag_buffer, |
| 5837 | tag_size, &tag_length); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5838 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5839 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5840 | |
| 5841 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5842 | psa_destroy_key(key); |
| 5843 | mbedtls_free(ciphertext); |
| 5844 | mbedtls_free(finish_ciphertext); |
| 5845 | mbedtls_free(tag_buffer); |
| 5846 | psa_aead_abort(&operation); |
| 5847 | PSA_DONE(); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5848 | } |
| 5849 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5850 | |
| 5851 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5852 | void aead_multipart_verify(int key_type_arg, data_t *key_data, |
| 5853 | int alg_arg, |
| 5854 | data_t *nonce, |
| 5855 | data_t *additional_data, |
| 5856 | data_t *input_data, |
| 5857 | data_t *tag, |
| 5858 | int tag_usage_arg, |
| 5859 | int expected_setup_status_arg, |
| 5860 | int expected_status_arg) |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5861 | { |
| 5862 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5863 | psa_key_type_t key_type = key_type_arg; |
| 5864 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5865 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5866 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5867 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5868 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5869 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5870 | unsigned char *plaintext = NULL; |
| 5871 | unsigned char *finish_plaintext = NULL; |
| 5872 | size_t plaintext_size = 0; |
| 5873 | size_t plaintext_length = 0; |
| 5874 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5875 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5876 | unsigned char *tag_buffer = NULL; |
| 5877 | size_t tag_size = 0; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5878 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5879 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5880 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5881 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 5882 | psa_set_key_algorithm(&attributes, alg); |
| 5883 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5884 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5885 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5886 | &key)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5887 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5888 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5889 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5890 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 5891 | input_data->len); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5892 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5893 | TEST_CALLOC(plaintext, plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5894 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5895 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5896 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5897 | TEST_CALLOC(finish_plaintext, verify_plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5898 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5899 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5900 | |
| 5901 | /* If the operation is not supported, just skip and not fail in case the |
| 5902 | * encryption involves a common limitation of cryptography hardwares and |
| 5903 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5904 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5905 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5906 | 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] | 5907 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5908 | TEST_EQUAL(status, expected_setup_status); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5909 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5910 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5911 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5912 | } |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5913 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5914 | PSA_ASSERT(status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5915 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5916 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5917 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5918 | status = psa_aead_set_lengths(&operation, additional_data->len, |
| 5919 | input_data->len); |
| 5920 | PSA_ASSERT(status); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5921 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5922 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5923 | additional_data->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5924 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5925 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 5926 | input_data->len, |
| 5927 | plaintext, plaintext_size, |
| 5928 | &plaintext_length)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5929 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5930 | if (tag_usage == USE_GIVEN_TAG) { |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5931 | tag_buffer = tag->x; |
| 5932 | tag_size = tag->len; |
| 5933 | } |
| 5934 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5935 | status = psa_aead_verify(&operation, finish_plaintext, |
| 5936 | verify_plaintext_size, |
| 5937 | &plaintext_length, |
| 5938 | tag_buffer, tag_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5939 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5940 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5941 | |
| 5942 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5943 | psa_destroy_key(key); |
| 5944 | mbedtls_free(plaintext); |
| 5945 | mbedtls_free(finish_plaintext); |
| 5946 | psa_aead_abort(&operation); |
| 5947 | PSA_DONE(); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5948 | } |
| 5949 | /* END_CASE */ |
| 5950 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5951 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5952 | void aead_multipart_setup(int key_type_arg, data_t *key_data, |
| 5953 | int alg_arg, int expected_status_arg) |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5954 | { |
| 5955 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5956 | psa_key_type_t key_type = key_type_arg; |
| 5957 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5958 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5959 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5960 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5961 | psa_status_t expected_status = expected_status_arg; |
| 5962 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5963 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5964 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5965 | psa_set_key_usage_flags(&attributes, |
| 5966 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5967 | psa_set_key_algorithm(&attributes, alg); |
| 5968 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5969 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5970 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5971 | &key)); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5972 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5973 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5974 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5975 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5976 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5977 | psa_aead_abort(&operation); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5978 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5979 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5980 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5981 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5982 | |
| 5983 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5984 | psa_destroy_key(key); |
| 5985 | psa_aead_abort(&operation); |
| 5986 | PSA_DONE(); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5987 | } |
| 5988 | /* END_CASE */ |
| 5989 | |
| 5990 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5991 | void aead_multipart_state_test(int key_type_arg, data_t *key_data, |
| 5992 | int alg_arg, |
| 5993 | data_t *nonce, |
| 5994 | data_t *additional_data, |
| 5995 | data_t *input_data) |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5996 | { |
| 5997 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5998 | psa_key_type_t key_type = key_type_arg; |
| 5999 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 6000 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6001 | unsigned char *output_data = NULL; |
| 6002 | unsigned char *final_data = NULL; |
| 6003 | size_t output_size = 0; |
| 6004 | size_t finish_output_size = 0; |
| 6005 | size_t output_length = 0; |
| 6006 | size_t key_bits = 0; |
| 6007 | size_t tag_length = 0; |
| 6008 | size_t tag_size = 0; |
| 6009 | size_t nonce_length = 0; |
| 6010 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 6011 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 6012 | size_t output_part_length = 0; |
| 6013 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6014 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6015 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6016 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6017 | psa_set_key_usage_flags(&attributes, |
| 6018 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 6019 | psa_set_key_algorithm(&attributes, alg); |
| 6020 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6021 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6022 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6023 | &key)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6024 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6025 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6026 | key_bits = psa_get_key_bits(&attributes); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6027 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6028 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6029 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6030 | TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6031 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6032 | 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] | 6033 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6034 | TEST_CALLOC(output_data, output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6035 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6036 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6037 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6038 | TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6039 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6040 | TEST_CALLOC(final_data, finish_output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6041 | |
| 6042 | /* Test all operations error without calling setup first. */ |
| 6043 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6044 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6045 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6046 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6047 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6048 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6049 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6050 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6051 | &nonce_length), |
| 6052 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6053 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6054 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6055 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 6056 | /* ------------------------------------------------------- */ |
| 6057 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6058 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6059 | input_data->len), |
| 6060 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6061 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6062 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6063 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 6064 | /* ------------------------------------------------------- */ |
| 6065 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6066 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6067 | additional_data->len), |
| 6068 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6069 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6070 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6071 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 6072 | /* ------------------------------------------------------- */ |
| 6073 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6074 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6075 | input_data->len, output_data, |
| 6076 | output_size, &output_length), |
| 6077 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6078 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6079 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6080 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 6081 | /* ------------------------------------------------------- */ |
| 6082 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6083 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6084 | finish_output_size, |
| 6085 | &output_part_length, |
| 6086 | tag_buffer, tag_length, |
| 6087 | &tag_size), |
| 6088 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6089 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6090 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6091 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 6092 | /* ------------------------------------------------------- */ |
| 6093 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6094 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6095 | finish_output_size, |
| 6096 | &output_part_length, |
| 6097 | tag_buffer, |
| 6098 | tag_length), |
| 6099 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6100 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6101 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6102 | |
| 6103 | /* Test for double setups. */ |
| 6104 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6105 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6106 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6107 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 6108 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6109 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6110 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6111 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 6112 | /* ------------------------------------------------------- */ |
| 6113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6114 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6115 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6116 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 6117 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6118 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6119 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6120 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6121 | /* ------------------------------------------------------- */ |
| 6122 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6123 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6124 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6125 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 6126 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6127 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6128 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6129 | |
| 6130 | /* ------------------------------------------------------- */ |
| 6131 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6132 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6133 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6134 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 6135 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6136 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6137 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6138 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6139 | /* Test for not setting a nonce. */ |
| 6140 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6141 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6142 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6143 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6144 | additional_data->len), |
| 6145 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6146 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6147 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6148 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 6149 | /* ------------------------------------------------------- */ |
| 6150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6151 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 6152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6153 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6154 | input_data->len, output_data, |
| 6155 | output_size, &output_length), |
| 6156 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 6157 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6158 | psa_aead_abort(&operation); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 6159 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6160 | /* ------------------------------------------------------- */ |
| 6161 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6162 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6164 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6165 | finish_output_size, |
| 6166 | &output_part_length, |
| 6167 | tag_buffer, tag_length, |
| 6168 | &tag_size), |
| 6169 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6170 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6171 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6172 | |
| 6173 | /* ------------------------------------------------------- */ |
| 6174 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6175 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6176 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6177 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6178 | finish_output_size, |
| 6179 | &output_part_length, |
| 6180 | tag_buffer, |
| 6181 | tag_length), |
| 6182 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6183 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6184 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6185 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6186 | /* Test for double setting nonce. */ |
| 6187 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6188 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6189 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6190 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6191 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6192 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6193 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6194 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6195 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6196 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6197 | /* Test for double generating nonce. */ |
| 6198 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6199 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6200 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6201 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6202 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6203 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6204 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6205 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6206 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6207 | &nonce_length), |
| 6208 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6209 | |
| 6210 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6211 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6212 | |
| 6213 | /* Test for generate nonce then set and vice versa */ |
| 6214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6215 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6218 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6219 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6221 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6222 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6223 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6224 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6225 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6226 | /* Test for generating nonce after calling set lengths */ |
| 6227 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6228 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6230 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6231 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6232 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6233 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6234 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6235 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6236 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6237 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6238 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6239 | /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */ |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6240 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6241 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6242 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6243 | if (operation.alg == PSA_ALG_CCM) { |
| 6244 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6245 | input_data->len), |
| 6246 | PSA_ERROR_INVALID_ARGUMENT); |
| 6247 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6248 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6249 | &nonce_length), |
| 6250 | PSA_ERROR_BAD_STATE); |
| 6251 | } else { |
| 6252 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6253 | input_data->len)); |
| 6254 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6255 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6256 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6257 | } |
| 6258 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6259 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6260 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6261 | /* 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] | 6262 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6263 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6264 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6265 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 6266 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 6267 | input_data->len), |
| 6268 | PSA_ERROR_INVALID_ARGUMENT); |
| 6269 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6270 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6271 | &nonce_length), |
| 6272 | PSA_ERROR_BAD_STATE); |
| 6273 | } else { |
| 6274 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 6275 | input_data->len)); |
| 6276 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6277 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6278 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6279 | } |
| 6280 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6281 | psa_aead_abort(&operation); |
Dave Rodgman | d26d744 | 2023-02-11 17:14:54 +0000 | [diff] [blame] | 6282 | #endif |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6283 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6284 | /* 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] | 6285 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6286 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6287 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6288 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6289 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6290 | &nonce_length)); |
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 | if (operation.alg == PSA_ALG_CCM) { |
| 6293 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6294 | input_data->len), |
| 6295 | PSA_ERROR_INVALID_ARGUMENT); |
| 6296 | } else { |
| 6297 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6298 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6299 | } |
| 6300 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6301 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6302 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6303 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6304 | /* Test for setting nonce after calling set lengths */ |
| 6305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6306 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6307 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6308 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6309 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6310 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6311 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6312 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6313 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6314 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6315 | /* 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] | 6316 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6317 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6318 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6319 | if (operation.alg == PSA_ALG_CCM) { |
| 6320 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6321 | input_data->len), |
| 6322 | PSA_ERROR_INVALID_ARGUMENT); |
| 6323 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6324 | PSA_ERROR_BAD_STATE); |
| 6325 | } else { |
| 6326 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6327 | input_data->len)); |
| 6328 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6329 | } |
| 6330 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6331 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6332 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6333 | /* 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] | 6334 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6335 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6336 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6337 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 6338 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 6339 | input_data->len), |
| 6340 | PSA_ERROR_INVALID_ARGUMENT); |
| 6341 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6342 | PSA_ERROR_BAD_STATE); |
| 6343 | } else { |
| 6344 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 6345 | input_data->len)); |
| 6346 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6347 | } |
| 6348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6349 | psa_aead_abort(&operation); |
Dave Rodgman | a476363 | 2023-02-11 18:36:23 +0000 | [diff] [blame] | 6350 | #endif |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6351 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6352 | /* 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] | 6353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6354 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6356 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6358 | if (operation.alg == PSA_ALG_CCM) { |
| 6359 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6360 | input_data->len), |
| 6361 | PSA_ERROR_INVALID_ARGUMENT); |
| 6362 | } else { |
| 6363 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6364 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6365 | } |
| 6366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6367 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6368 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6369 | /* 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] | 6370 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6371 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6372 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6373 | if (operation.alg == PSA_ALG_GCM) { |
| 6374 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6375 | SIZE_MAX), |
| 6376 | PSA_ERROR_INVALID_ARGUMENT); |
| 6377 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6378 | PSA_ERROR_BAD_STATE); |
| 6379 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6380 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6381 | SIZE_MAX)); |
| 6382 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6383 | } |
| 6384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6385 | psa_aead_abort(&operation); |
Dave Rodgman | 91e8321 | 2023-02-11 20:07:43 +0000 | [diff] [blame] | 6386 | #endif |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6387 | |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 6388 | /* 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] | 6389 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6390 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6392 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6393 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6394 | if (operation.alg == PSA_ALG_GCM) { |
| 6395 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6396 | SIZE_MAX), |
| 6397 | PSA_ERROR_INVALID_ARGUMENT); |
| 6398 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6399 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6400 | SIZE_MAX)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6401 | } |
| 6402 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6403 | psa_aead_abort(&operation); |
Dave Rodgman | 641288b | 2023-02-11 22:02:04 +0000 | [diff] [blame] | 6404 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6405 | |
| 6406 | /* ------------------------------------------------------- */ |
| 6407 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6408 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6409 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6410 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6411 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6412 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6413 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6414 | &nonce_length), |
| 6415 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6416 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6417 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6418 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6419 | /* Test for generating nonce in decrypt setup. */ |
| 6420 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6421 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6422 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6423 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6424 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6425 | &nonce_length), |
| 6426 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6427 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6428 | psa_aead_abort(&operation); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6429 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6430 | /* Test for setting lengths twice. */ |
| 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_set_lengths(&operation, additional_data->len, |
| 6440 | input_data->len), |
| 6441 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6442 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6443 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6444 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6445 | /* Test for setting lengths after setting nonce + already starting data. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6446 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6447 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6449 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6450 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6451 | if (operation.alg == PSA_ALG_CCM) { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6452 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6453 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6454 | additional_data->len), |
| 6455 | PSA_ERROR_BAD_STATE); |
| 6456 | } else { |
| 6457 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6458 | additional_data->len)); |
| 6459 | |
| 6460 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6461 | input_data->len), |
| 6462 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6463 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6464 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6465 | |
| 6466 | /* ------------------------------------------------------- */ |
| 6467 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6468 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6469 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6470 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6471 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6472 | if (operation.alg == PSA_ALG_CCM) { |
| 6473 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6474 | input_data->len, output_data, |
| 6475 | output_size, &output_length), |
| 6476 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6477 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6478 | } else { |
| 6479 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6480 | input_data->len, output_data, |
| 6481 | output_size, &output_length)); |
| 6482 | |
| 6483 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6484 | input_data->len), |
| 6485 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6486 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6487 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6488 | |
| 6489 | /* ------------------------------------------------------- */ |
| 6490 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6491 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6492 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6493 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6494 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6495 | if (operation.alg == PSA_ALG_CCM) { |
| 6496 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6497 | finish_output_size, |
| 6498 | &output_part_length, |
| 6499 | tag_buffer, tag_length, |
| 6500 | &tag_size)); |
| 6501 | } else { |
| 6502 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6503 | finish_output_size, |
| 6504 | &output_part_length, |
| 6505 | tag_buffer, tag_length, |
| 6506 | &tag_size)); |
| 6507 | |
| 6508 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6509 | input_data->len), |
| 6510 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6511 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6512 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6513 | |
| 6514 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 6515 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6516 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6517 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6518 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6519 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6520 | &nonce_length)); |
| 6521 | if (operation.alg == PSA_ALG_CCM) { |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6522 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6523 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6524 | additional_data->len), |
| 6525 | PSA_ERROR_BAD_STATE); |
| 6526 | } else { |
| 6527 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6528 | additional_data->len)); |
| 6529 | |
| 6530 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6531 | input_data->len), |
| 6532 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6533 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6534 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6535 | |
| 6536 | /* ------------------------------------------------------- */ |
| 6537 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6538 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6539 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6540 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6541 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6542 | &nonce_length)); |
| 6543 | if (operation.alg == PSA_ALG_CCM) { |
| 6544 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6545 | input_data->len, output_data, |
| 6546 | output_size, &output_length), |
| 6547 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6548 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6549 | } else { |
| 6550 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6551 | input_data->len, output_data, |
| 6552 | output_size, &output_length)); |
| 6553 | |
| 6554 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6555 | input_data->len), |
| 6556 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6557 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6558 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6559 | |
| 6560 | /* ------------------------------------------------------- */ |
| 6561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6562 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6563 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6564 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6565 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6566 | &nonce_length)); |
| 6567 | if (operation.alg == PSA_ALG_CCM) { |
| 6568 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6569 | finish_output_size, |
| 6570 | &output_part_length, |
| 6571 | tag_buffer, tag_length, |
| 6572 | &tag_size)); |
| 6573 | } else { |
| 6574 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6575 | finish_output_size, |
| 6576 | &output_part_length, |
| 6577 | tag_buffer, tag_length, |
| 6578 | &tag_size)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6579 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6580 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6581 | input_data->len), |
| 6582 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6583 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6584 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6585 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6586 | /* Test for not sending any additional data or data after setting non zero |
| 6587 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6588 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6589 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6590 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6591 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6592 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6593 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6594 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6595 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6596 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6597 | finish_output_size, |
| 6598 | &output_part_length, |
| 6599 | tag_buffer, tag_length, |
| 6600 | &tag_size), |
| 6601 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6602 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6603 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6604 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6605 | /* Test for not sending any additional data or data after setting non-zero |
| 6606 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6607 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6608 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6609 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6610 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6611 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6612 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6613 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6614 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6615 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6616 | finish_output_size, |
| 6617 | &output_part_length, |
| 6618 | tag_buffer, |
| 6619 | tag_length), |
| 6620 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6621 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6622 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6623 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6624 | /* Test for not sending any additional data after setting a non-zero length |
| 6625 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6626 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6627 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6628 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6629 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6630 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6631 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6632 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6633 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6634 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6635 | input_data->len, output_data, |
| 6636 | output_size, &output_length), |
| 6637 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6638 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6639 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6640 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6641 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 6642 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6643 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6644 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6645 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6646 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6647 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6648 | input_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6649 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6650 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6651 | additional_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6652 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6653 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6654 | finish_output_size, |
| 6655 | &output_part_length, |
| 6656 | tag_buffer, tag_length, |
| 6657 | &tag_size), |
| 6658 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6659 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6660 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6661 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6662 | /* Test for sending too much additional data after setting lengths. */ |
| 6663 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6664 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6665 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6666 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6667 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6668 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6669 | |
| 6670 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6671 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6672 | additional_data->len), |
| 6673 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6674 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6675 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6676 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6677 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6678 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6679 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6680 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6681 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6682 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6683 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6684 | input_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6685 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6686 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6687 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6688 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6689 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6690 | 1), |
| 6691 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6692 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6693 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6694 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6695 | /* Test for sending too much data after setting lengths. */ |
| 6696 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6697 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6698 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6699 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6700 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6701 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6702 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6703 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6704 | input_data->len, output_data, |
| 6705 | output_size, &output_length), |
| 6706 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6707 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6708 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6709 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6710 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6711 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6712 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6713 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6714 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6715 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6716 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6717 | input_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6718 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6719 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6720 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6721 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6722 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6723 | input_data->len, output_data, |
| 6724 | output_size, &output_length)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6725 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6726 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6727 | 1, output_data, |
| 6728 | output_size, &output_length), |
| 6729 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6730 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6731 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6732 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6733 | /* Test sending additional data after data. */ |
| 6734 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6735 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6736 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6737 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6738 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6739 | if (operation.alg != PSA_ALG_CCM) { |
| 6740 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6741 | input_data->len, output_data, |
| 6742 | output_size, &output_length)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6743 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6744 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6745 | additional_data->len), |
| 6746 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6747 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6748 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6749 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6750 | /* Test calling finish on decryption. */ |
| 6751 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6752 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6753 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6754 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6755 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6756 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6757 | finish_output_size, |
| 6758 | &output_part_length, |
| 6759 | tag_buffer, tag_length, |
| 6760 | &tag_size), |
| 6761 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6762 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6763 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6764 | |
| 6765 | /* Test calling verify on encryption. */ |
| 6766 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6767 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6768 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6769 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6771 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6772 | finish_output_size, |
| 6773 | &output_part_length, |
| 6774 | tag_buffer, |
| 6775 | tag_length), |
| 6776 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6777 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6778 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6779 | |
| 6780 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6781 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6782 | psa_destroy_key(key); |
| 6783 | psa_aead_abort(&operation); |
| 6784 | mbedtls_free(output_data); |
| 6785 | mbedtls_free(final_data); |
| 6786 | PSA_DONE(); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6787 | } |
| 6788 | /* END_CASE */ |
| 6789 | |
| 6790 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6791 | void signature_size(int type_arg, |
| 6792 | int bits, |
| 6793 | int alg_arg, |
| 6794 | int expected_size_arg) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6795 | { |
| 6796 | psa_key_type_t type = type_arg; |
| 6797 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6798 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6799 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6800 | TEST_EQUAL(actual_size, (size_t) expected_size_arg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6801 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6802 | exit: |
| 6803 | ; |
| 6804 | } |
| 6805 | /* END_CASE */ |
| 6806 | |
| 6807 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6808 | void sign_hash_deterministic(int key_type_arg, data_t *key_data, |
| 6809 | int alg_arg, data_t *input_data, |
| 6810 | data_t *output_data) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6811 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6812 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6813 | psa_key_type_t key_type = key_type_arg; |
| 6814 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6815 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6816 | unsigned char *signature = NULL; |
| 6817 | size_t signature_size; |
| 6818 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6819 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6820 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6821 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6822 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6823 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6824 | psa_set_key_algorithm(&attributes, alg); |
| 6825 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6826 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6827 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6828 | &key)); |
| 6829 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6830 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6831 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6832 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6833 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6834 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6835 | key_bits, alg); |
| 6836 | TEST_ASSERT(signature_size != 0); |
| 6837 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6838 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6839 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6840 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6841 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6842 | input_data->x, input_data->len, |
| 6843 | signature, signature_size, |
| 6844 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6845 | /* Verify that the signature is what is expected. */ |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 6846 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 6847 | signature, signature_length); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6848 | |
| 6849 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6850 | /* |
| 6851 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6852 | * thus reset them as required. |
| 6853 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6854 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6855 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6856 | psa_destroy_key(key); |
| 6857 | mbedtls_free(signature); |
| 6858 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6859 | } |
| 6860 | /* END_CASE */ |
| 6861 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6862 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6863 | /** |
| 6864 | * sign_hash_interruptible() test intentions: |
| 6865 | * |
| 6866 | * Note: This test can currently only handle ECDSA. |
| 6867 | * |
| 6868 | * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 6869 | * and private keys / keypairs only). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6870 | * |
| 6871 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 6872 | * expected for different max_ops values. |
| 6873 | * |
| 6874 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 6875 | * and that each successful stage completes some ops (this is not mandated by |
| 6876 | * the PSA specification, but is currently the case). |
| 6877 | * |
| 6878 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 6879 | * complete() calls does not alter the number of ops returned. |
| 6880 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6881 | void sign_hash_interruptible(int key_type_arg, data_t *key_data, |
| 6882 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6883 | data_t *output_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6884 | { |
| 6885 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6886 | psa_key_type_t key_type = key_type_arg; |
| 6887 | psa_algorithm_t alg = alg_arg; |
| 6888 | size_t key_bits; |
| 6889 | unsigned char *signature = NULL; |
| 6890 | size_t signature_size; |
| 6891 | size_t signature_length = 0xdeadbeef; |
| 6892 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6893 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6894 | uint32_t num_ops = 0; |
| 6895 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6896 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6897 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6898 | size_t min_completes = 0; |
| 6899 | size_t max_completes = 0; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6900 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6901 | psa_sign_hash_interruptible_operation_t operation = |
| 6902 | psa_sign_hash_interruptible_operation_init(); |
| 6903 | |
| 6904 | PSA_ASSERT(psa_crypto_init()); |
| 6905 | |
| 6906 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6907 | psa_set_key_algorithm(&attributes, alg); |
| 6908 | psa_set_key_type(&attributes, key_type); |
| 6909 | |
| 6910 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6911 | &key)); |
| 6912 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6913 | key_bits = psa_get_key_bits(&attributes); |
| 6914 | |
| 6915 | /* Allocate a buffer which has the size advertised by the |
| 6916 | * library. */ |
| 6917 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6918 | key_bits, alg); |
| 6919 | TEST_ASSERT(signature_size != 0); |
| 6920 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6921 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6922 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6923 | psa_interruptible_set_max_ops(max_ops); |
| 6924 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6925 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6926 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6927 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6928 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6929 | TEST_ASSERT(num_ops_prior == 0); |
| 6930 | |
| 6931 | /* Start performing the signature. */ |
| 6932 | PSA_ASSERT(psa_sign_hash_start(&operation, key, alg, |
| 6933 | input_data->x, input_data->len)); |
| 6934 | |
| 6935 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6936 | TEST_ASSERT(num_ops_prior == 0); |
| 6937 | |
| 6938 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6939 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6940 | status = psa_sign_hash_complete(&operation, signature, signature_size, |
| 6941 | &signature_length); |
| 6942 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6943 | num_completes++; |
| 6944 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6945 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 6946 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6947 | /* We are asserting here that every complete makes progress |
| 6948 | * (completes some ops), which is true of the internal |
| 6949 | * implementation and probably any implementation, however this is |
| 6950 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6951 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6952 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6953 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 6954 | |
| 6955 | /* Ensure calling get_num_ops() twice still returns the same |
| 6956 | * number of ops as previously reported. */ |
| 6957 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6958 | |
| 6959 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6960 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6961 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6962 | |
| 6963 | TEST_ASSERT(status == PSA_SUCCESS); |
| 6964 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6965 | TEST_LE_U(min_completes, num_completes); |
| 6966 | TEST_LE_U(num_completes, max_completes); |
| 6967 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6968 | /* Verify that the signature is what is expected. */ |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 6969 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 6970 | signature, signature_length); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6971 | |
| 6972 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6973 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6974 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6975 | TEST_ASSERT(num_ops == 0); |
| 6976 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6977 | exit: |
| 6978 | |
| 6979 | /* |
| 6980 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6981 | * thus reset them as required. |
| 6982 | */ |
| 6983 | psa_reset_key_attributes(&attributes); |
| 6984 | |
| 6985 | psa_destroy_key(key); |
| 6986 | mbedtls_free(signature); |
| 6987 | PSA_DONE(); |
| 6988 | } |
| 6989 | /* END_CASE */ |
| 6990 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6991 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6992 | void sign_hash_fail(int key_type_arg, data_t *key_data, |
| 6993 | int alg_arg, data_t *input_data, |
| 6994 | int signature_size_arg, int expected_status_arg) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6995 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6996 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6997 | psa_key_type_t key_type = key_type_arg; |
| 6998 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6999 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 7000 | psa_status_t actual_status; |
| 7001 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 7002 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 7003 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7004 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 7005 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7006 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 7007 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7008 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 7009 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7010 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 7011 | psa_set_key_algorithm(&attributes, alg); |
| 7012 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 7013 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7014 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7015 | &key)); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 7016 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7017 | actual_status = psa_sign_hash(key, alg, |
| 7018 | input_data->x, input_data->len, |
| 7019 | signature, signature_size, |
| 7020 | &signature_length); |
| 7021 | TEST_EQUAL(actual_status, expected_status); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 7022 | /* The value of *signature_length is unspecified on error, but |
| 7023 | * whatever it is, it should be less than signature_size, so that |
| 7024 | * if the caller tries to read *signature_length bytes without |
| 7025 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7026 | TEST_LE_U(signature_length, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 7027 | |
| 7028 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7029 | psa_reset_key_attributes(&attributes); |
| 7030 | psa_destroy_key(key); |
| 7031 | mbedtls_free(signature); |
| 7032 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 7033 | } |
| 7034 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 7035 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7036 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7037 | /** |
| 7038 | * sign_hash_fail_interruptible() test intentions: |
| 7039 | * |
| 7040 | * Note: This test can currently only handle ECDSA. |
| 7041 | * |
| 7042 | * 1. Test that various failure cases for interruptible sign hash fail with the |
| 7043 | * correct error codes, and at the correct point (at start or during |
| 7044 | * complete). |
| 7045 | * |
| 7046 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 7047 | * expected for different max_ops values. |
| 7048 | * |
| 7049 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7050 | * and that each successful stage completes some ops (this is not mandated by |
| 7051 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7052 | * |
| 7053 | * 4. Check that calling complete() when start() fails and complete() |
| 7054 | * after completion results in a BAD_STATE error. |
| 7055 | * |
| 7056 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 7057 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7058 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7059 | void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 7060 | int alg_arg, data_t *input_data, |
| 7061 | int signature_size_arg, |
| 7062 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7063 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7064 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7065 | { |
| 7066 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7067 | psa_key_type_t key_type = key_type_arg; |
| 7068 | psa_algorithm_t alg = alg_arg; |
| 7069 | size_t signature_size = signature_size_arg; |
| 7070 | psa_status_t actual_status; |
| 7071 | psa_status_t expected_start_status = expected_start_status_arg; |
| 7072 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 7073 | unsigned char *signature = NULL; |
| 7074 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7075 | uint32_t num_ops = 0; |
| 7076 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7077 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7078 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7079 | size_t min_completes = 0; |
| 7080 | size_t max_completes = 0; |
| 7081 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7082 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7083 | psa_sign_hash_interruptible_operation_t operation = |
| 7084 | psa_sign_hash_interruptible_operation_init(); |
| 7085 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7086 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7087 | |
| 7088 | PSA_ASSERT(psa_crypto_init()); |
| 7089 | |
| 7090 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 7091 | psa_set_key_algorithm(&attributes, alg); |
| 7092 | psa_set_key_type(&attributes, key_type); |
| 7093 | |
| 7094 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7095 | &key)); |
| 7096 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7097 | psa_interruptible_set_max_ops(max_ops); |
| 7098 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7099 | interruptible_signverify_get_minmax_completes(max_ops, |
| 7100 | expected_complete_status, |
| 7101 | &min_completes, |
| 7102 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7103 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7104 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 7105 | TEST_ASSERT(num_ops_prior == 0); |
| 7106 | |
| 7107 | /* Start performing the signature. */ |
| 7108 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 7109 | input_data->x, input_data->len); |
| 7110 | |
| 7111 | TEST_EQUAL(actual_status, expected_start_status); |
| 7112 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7113 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 7114 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7115 | * start failed. */ |
| 7116 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 7117 | signature_size, |
| 7118 | &signature_length); |
| 7119 | |
| 7120 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7121 | |
| 7122 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7123 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 7124 | input_data->x, input_data->len); |
| 7125 | |
| 7126 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7127 | } |
| 7128 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7129 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 7130 | TEST_ASSERT(num_ops_prior == 0); |
| 7131 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7132 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7133 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7134 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 7135 | signature_size, |
| 7136 | &signature_length); |
| 7137 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7138 | num_completes++; |
| 7139 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7140 | if (actual_status == PSA_SUCCESS || |
| 7141 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7142 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7143 | /* We are asserting here that every complete makes progress |
| 7144 | * (completes some ops), which is true of the internal |
| 7145 | * implementation and probably any implementation, however this is |
| 7146 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7147 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7148 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7149 | num_ops_prior = num_ops; |
| 7150 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7151 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7152 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7153 | TEST_EQUAL(actual_status, expected_complete_status); |
| 7154 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 7155 | /* Check that another complete returns BAD_STATE. */ |
| 7156 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 7157 | signature_size, |
| 7158 | &signature_length); |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7159 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 7160 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7161 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7162 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 7163 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7164 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 7165 | TEST_ASSERT(num_ops == 0); |
| 7166 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7167 | /* The value of *signature_length is unspecified on error, but |
| 7168 | * whatever it is, it should be less than signature_size, so that |
| 7169 | * if the caller tries to read *signature_length bytes without |
| 7170 | * checking the error code then they don't overflow a buffer. */ |
| 7171 | TEST_LE_U(signature_length, signature_size); |
| 7172 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7173 | TEST_LE_U(min_completes, num_completes); |
| 7174 | TEST_LE_U(num_completes, max_completes); |
| 7175 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7176 | exit: |
| 7177 | psa_reset_key_attributes(&attributes); |
| 7178 | psa_destroy_key(key); |
| 7179 | mbedtls_free(signature); |
| 7180 | PSA_DONE(); |
| 7181 | } |
| 7182 | /* END_CASE */ |
| 7183 | |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 7184 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7185 | void sign_verify_hash(int key_type_arg, data_t *key_data, |
| 7186 | int alg_arg, data_t *input_data) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7187 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7188 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7189 | psa_key_type_t key_type = key_type_arg; |
| 7190 | psa_algorithm_t alg = alg_arg; |
| 7191 | size_t key_bits; |
| 7192 | unsigned char *signature = NULL; |
| 7193 | size_t signature_size; |
| 7194 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7195 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7196 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7197 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7198 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7199 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 7200 | psa_set_key_algorithm(&attributes, alg); |
| 7201 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7202 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7203 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7204 | &key)); |
| 7205 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7206 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7207 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 7208 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7209 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7210 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7211 | key_bits, alg); |
| 7212 | TEST_ASSERT(signature_size != 0); |
| 7213 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7214 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7215 | |
| 7216 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7217 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 7218 | input_data->x, input_data->len, |
| 7219 | signature, signature_size, |
| 7220 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7221 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7222 | TEST_LE_U(signature_length, signature_size); |
| 7223 | TEST_ASSERT(signature_length > 0); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7224 | |
| 7225 | /* Use the library to verify that the signature is correct. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7226 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 7227 | input_data->x, input_data->len, |
| 7228 | signature, signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7230 | if (input_data->len != 0) { |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7231 | /* Flip a bit in the input and verify that the signature is now |
| 7232 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 7233 | * because ECDSA may ignore the last few bits of the input. */ |
| 7234 | input_data->x[0] ^= 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7235 | TEST_EQUAL(psa_verify_hash(key, alg, |
| 7236 | input_data->x, input_data->len, |
| 7237 | signature, signature_length), |
| 7238 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7239 | } |
| 7240 | |
| 7241 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7242 | /* |
| 7243 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7244 | * thus reset them as required. |
| 7245 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7246 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7247 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7248 | psa_destroy_key(key); |
| 7249 | mbedtls_free(signature); |
| 7250 | PSA_DONE(); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7251 | } |
| 7252 | /* END_CASE */ |
| 7253 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7254 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7255 | /** |
| 7256 | * sign_verify_hash_interruptible() test intentions: |
| 7257 | * |
| 7258 | * Note: This test can currently only handle ECDSA. |
| 7259 | * |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 7260 | * 1. Test that we can sign an input hash with the given keypair and then |
| 7261 | * afterwards verify that signature. This is currently the only way to test |
| 7262 | * non deterministic ECDSA, but this test can also handle deterministic. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7263 | * |
| 7264 | * 2. Test that after corrupting the hash, the verification detects an invalid |
| 7265 | * signature. |
| 7266 | * |
| 7267 | * 3. Test the number of calls to psa_sign_hash_complete() required are as |
| 7268 | * expected for different max_ops values. |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7269 | * |
| 7270 | * 4. Test that the number of ops done prior to starting signing and after abort |
| 7271 | * is zero and that each successful signing stage completes some ops (this is |
| 7272 | * not mandated by the PSA specification, but is currently the case). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7273 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7274 | 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] | 7275 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7276 | int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7277 | { |
| 7278 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7279 | psa_key_type_t key_type = key_type_arg; |
| 7280 | psa_algorithm_t alg = alg_arg; |
| 7281 | size_t key_bits; |
| 7282 | unsigned char *signature = NULL; |
| 7283 | size_t signature_size; |
| 7284 | size_t signature_length = 0xdeadbeef; |
| 7285 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7286 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7287 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7288 | uint32_t num_ops = 0; |
| 7289 | uint32_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7290 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7291 | size_t min_completes = 0; |
| 7292 | size_t max_completes = 0; |
| 7293 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7294 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7295 | psa_sign_hash_interruptible_operation_init(); |
| 7296 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7297 | psa_verify_hash_interruptible_operation_init(); |
| 7298 | |
| 7299 | PSA_ASSERT(psa_crypto_init()); |
| 7300 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7301 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7302 | PSA_KEY_USAGE_VERIFY_HASH); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7303 | psa_set_key_algorithm(&attributes, alg); |
| 7304 | psa_set_key_type(&attributes, key_type); |
| 7305 | |
| 7306 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7307 | &key)); |
| 7308 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7309 | key_bits = psa_get_key_bits(&attributes); |
| 7310 | |
| 7311 | /* Allocate a buffer which has the size advertised by the |
| 7312 | * library. */ |
| 7313 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7314 | key_bits, alg); |
| 7315 | TEST_ASSERT(signature_size != 0); |
| 7316 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7317 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7318 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7319 | psa_interruptible_set_max_ops(max_ops); |
| 7320 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7321 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 7322 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7323 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7324 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); |
| 7325 | TEST_ASSERT(num_ops_prior == 0); |
| 7326 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7327 | /* Start performing the signature. */ |
| 7328 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7329 | input_data->x, input_data->len)); |
| 7330 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7331 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); |
| 7332 | TEST_ASSERT(num_ops_prior == 0); |
| 7333 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7334 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7335 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7336 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7337 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7338 | signature_size, |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7339 | &signature_length); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7340 | |
| 7341 | num_completes++; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7342 | |
| 7343 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 7344 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7345 | /* We are asserting here that every complete makes progress |
| 7346 | * (completes some ops), which is true of the internal |
| 7347 | * implementation and probably any implementation, however this is |
| 7348 | * not mandated by the PSA specification. */ |
| 7349 | TEST_ASSERT(num_ops > num_ops_prior); |
| 7350 | |
| 7351 | num_ops_prior = num_ops; |
| 7352 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7353 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7354 | |
| 7355 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7356 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7357 | TEST_LE_U(min_completes, num_completes); |
| 7358 | TEST_LE_U(num_completes, max_completes); |
| 7359 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7360 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7361 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7362 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7363 | TEST_ASSERT(num_ops == 0); |
| 7364 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7365 | /* Check that the signature length looks sensible. */ |
| 7366 | TEST_LE_U(signature_length, signature_size); |
| 7367 | TEST_ASSERT(signature_length > 0); |
| 7368 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7369 | num_completes = 0; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7370 | |
| 7371 | /* Start verification. */ |
| 7372 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7373 | input_data->x, input_data->len, |
| 7374 | signature, signature_length)); |
| 7375 | |
| 7376 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7377 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7378 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7379 | |
| 7380 | num_completes++; |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7381 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7382 | |
| 7383 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7384 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7385 | TEST_LE_U(min_completes, num_completes); |
| 7386 | TEST_LE_U(num_completes, max_completes); |
| 7387 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7388 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7389 | |
| 7390 | verify_operation = psa_verify_hash_interruptible_operation_init(); |
| 7391 | |
| 7392 | if (input_data->len != 0) { |
| 7393 | /* Flip a bit in the input and verify that the signature is now |
| 7394 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 7395 | * because ECDSA may ignore the last few bits of the input. */ |
| 7396 | input_data->x[0] ^= 1; |
| 7397 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7398 | /* Start verification. */ |
| 7399 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7400 | input_data->x, input_data->len, |
| 7401 | signature, signature_length)); |
| 7402 | |
| 7403 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7404 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7405 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7406 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7407 | |
| 7408 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7409 | } |
| 7410 | |
| 7411 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7412 | |
| 7413 | exit: |
| 7414 | /* |
| 7415 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7416 | * thus reset them as required. |
| 7417 | */ |
| 7418 | psa_reset_key_attributes(&attributes); |
| 7419 | |
| 7420 | psa_destroy_key(key); |
| 7421 | mbedtls_free(signature); |
| 7422 | PSA_DONE(); |
| 7423 | } |
| 7424 | /* END_CASE */ |
| 7425 | |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7426 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7427 | void verify_hash(int key_type_arg, data_t *key_data, |
| 7428 | int alg_arg, data_t *hash_data, |
| 7429 | data_t *signature_data) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7430 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7431 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7432 | psa_key_type_t key_type = key_type_arg; |
| 7433 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7434 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7435 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7436 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 7437 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7438 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7439 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7440 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7441 | psa_set_key_algorithm(&attributes, alg); |
| 7442 | psa_set_key_type(&attributes, key_type); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7444 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7445 | &key)); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7446 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7447 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 7448 | hash_data->x, hash_data->len, |
| 7449 | signature_data->x, signature_data->len)); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 7450 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7451 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7452 | psa_reset_key_attributes(&attributes); |
| 7453 | psa_destroy_key(key); |
| 7454 | PSA_DONE(); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7455 | } |
| 7456 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7457 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7458 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7459 | /** |
| 7460 | * verify_hash_interruptible() test intentions: |
| 7461 | * |
| 7462 | * Note: This test can currently only handle ECDSA. |
| 7463 | * |
| 7464 | * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 7465 | * only). Given this test only does verification it can accept public keys as |
| 7466 | * well as private keys / keypairs. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7467 | * |
| 7468 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7469 | * expected for different max_ops values. |
| 7470 | * |
| 7471 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7472 | * and that each successful stage completes some ops (this is not mandated by |
| 7473 | * the PSA specification, but is currently the case). |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7474 | * |
| 7475 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 7476 | * complete() calls does not alter the number of ops returned. |
| 7477 | * |
| 7478 | * 5. Test that after corrupting the hash, the verification detects an invalid |
| 7479 | * signature. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7480 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7481 | void verify_hash_interruptible(int key_type_arg, data_t *key_data, |
| 7482 | int alg_arg, data_t *hash_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7483 | data_t *signature_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7484 | { |
| 7485 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7486 | psa_key_type_t key_type = key_type_arg; |
| 7487 | psa_algorithm_t alg = alg_arg; |
| 7488 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7489 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7490 | uint32_t num_ops = 0; |
| 7491 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7492 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7493 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7494 | size_t min_completes = 0; |
| 7495 | size_t max_completes = 0; |
| 7496 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7497 | psa_verify_hash_interruptible_operation_t operation = |
| 7498 | psa_verify_hash_interruptible_operation_init(); |
| 7499 | |
| 7500 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 7501 | |
| 7502 | PSA_ASSERT(psa_crypto_init()); |
| 7503 | |
| 7504 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7505 | psa_set_key_algorithm(&attributes, alg); |
| 7506 | psa_set_key_type(&attributes, key_type); |
| 7507 | |
| 7508 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7509 | &key)); |
| 7510 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7511 | psa_interruptible_set_max_ops(max_ops); |
| 7512 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7513 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 7514 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7515 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7516 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7517 | |
| 7518 | TEST_ASSERT(num_ops_prior == 0); |
| 7519 | |
| 7520 | /* Start verification. */ |
| 7521 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7522 | hash_data->x, hash_data->len, |
| 7523 | signature_data->x, signature_data->len) |
| 7524 | ); |
| 7525 | |
| 7526 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7527 | |
| 7528 | TEST_ASSERT(num_ops_prior == 0); |
| 7529 | |
| 7530 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7531 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7532 | status = psa_verify_hash_complete(&operation); |
| 7533 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7534 | num_completes++; |
| 7535 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7536 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 7537 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7538 | /* We are asserting here that every complete makes progress |
| 7539 | * (completes some ops), which is true of the internal |
| 7540 | * implementation and probably any implementation, however this is |
| 7541 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7542 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7543 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7544 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 7545 | |
| 7546 | /* Ensure calling get_num_ops() twice still returns the same |
| 7547 | * number of ops as previously reported. */ |
| 7548 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7549 | |
| 7550 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7551 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7552 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7553 | |
| 7554 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7555 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7556 | TEST_LE_U(min_completes, num_completes); |
| 7557 | TEST_LE_U(num_completes, max_completes); |
| 7558 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7559 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7560 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7561 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7562 | TEST_ASSERT(num_ops == 0); |
| 7563 | |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7564 | if (hash_data->len != 0) { |
| 7565 | /* Flip a bit in the hash and verify that the signature is now detected |
| 7566 | * as invalid. Flip a bit at the beginning, not at the end, because |
| 7567 | * ECDSA may ignore the last few bits of the input. */ |
| 7568 | hash_data->x[0] ^= 1; |
| 7569 | |
| 7570 | /* Start verification. */ |
| 7571 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7572 | hash_data->x, hash_data->len, |
| 7573 | signature_data->x, signature_data->len)); |
| 7574 | |
| 7575 | /* Continue performing the signature until complete. */ |
| 7576 | do { |
| 7577 | status = psa_verify_hash_complete(&operation); |
| 7578 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7579 | |
| 7580 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7581 | } |
| 7582 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7583 | exit: |
| 7584 | psa_reset_key_attributes(&attributes); |
| 7585 | psa_destroy_key(key); |
| 7586 | PSA_DONE(); |
| 7587 | } |
| 7588 | /* END_CASE */ |
| 7589 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7590 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7591 | void verify_hash_fail(int key_type_arg, data_t *key_data, |
| 7592 | int alg_arg, data_t *hash_data, |
| 7593 | data_t *signature_data, |
| 7594 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7595 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7596 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7597 | psa_key_type_t key_type = key_type_arg; |
| 7598 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7599 | psa_status_t actual_status; |
| 7600 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7601 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7602 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7603 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7604 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7605 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7606 | psa_set_key_algorithm(&attributes, alg); |
| 7607 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7608 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7609 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7610 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7611 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7612 | actual_status = psa_verify_hash(key, alg, |
| 7613 | hash_data->x, hash_data->len, |
| 7614 | signature_data->x, signature_data->len); |
| 7615 | TEST_EQUAL(actual_status, expected_status); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7616 | |
| 7617 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7618 | psa_reset_key_attributes(&attributes); |
| 7619 | psa_destroy_key(key); |
| 7620 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7621 | } |
| 7622 | /* END_CASE */ |
| 7623 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7624 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7625 | /** |
| 7626 | * verify_hash_fail_interruptible() test intentions: |
| 7627 | * |
| 7628 | * Note: This test can currently only handle ECDSA. |
| 7629 | * |
| 7630 | * 1. Test that various failure cases for interruptible verify hash fail with |
| 7631 | * the correct error codes, and at the correct point (at start or during |
| 7632 | * complete). |
| 7633 | * |
| 7634 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7635 | * expected for different max_ops values. |
| 7636 | * |
| 7637 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7638 | * and that each successful stage completes some ops (this is not mandated by |
| 7639 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7640 | * |
| 7641 | * 4. Check that calling complete() when start() fails and complete() |
| 7642 | * after completion results in a BAD_STATE error. |
| 7643 | * |
| 7644 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 7645 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7646 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7647 | void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 7648 | int alg_arg, data_t *hash_data, |
| 7649 | data_t *signature_data, |
| 7650 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7651 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7652 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7653 | { |
| 7654 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7655 | psa_key_type_t key_type = key_type_arg; |
| 7656 | psa_algorithm_t alg = alg_arg; |
| 7657 | psa_status_t actual_status; |
| 7658 | psa_status_t expected_start_status = expected_start_status_arg; |
| 7659 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 7660 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7661 | uint32_t num_ops = 0; |
| 7662 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7663 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7664 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7665 | size_t min_completes = 0; |
| 7666 | size_t max_completes = 0; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7667 | psa_verify_hash_interruptible_operation_t operation = |
| 7668 | psa_verify_hash_interruptible_operation_init(); |
| 7669 | |
| 7670 | PSA_ASSERT(psa_crypto_init()); |
| 7671 | |
| 7672 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7673 | psa_set_key_algorithm(&attributes, alg); |
| 7674 | psa_set_key_type(&attributes, key_type); |
| 7675 | |
| 7676 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7677 | &key)); |
| 7678 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7679 | psa_interruptible_set_max_ops(max_ops); |
| 7680 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7681 | interruptible_signverify_get_minmax_completes(max_ops, |
| 7682 | expected_complete_status, |
| 7683 | &min_completes, |
| 7684 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7685 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7686 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7687 | TEST_ASSERT(num_ops_prior == 0); |
| 7688 | |
| 7689 | /* Start verification. */ |
| 7690 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7691 | hash_data->x, hash_data->len, |
| 7692 | signature_data->x, |
| 7693 | signature_data->len); |
| 7694 | |
| 7695 | TEST_EQUAL(actual_status, expected_start_status); |
| 7696 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7697 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 7698 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7699 | * start failed. */ |
| 7700 | actual_status = psa_verify_hash_complete(&operation); |
| 7701 | |
| 7702 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7703 | |
| 7704 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7705 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7706 | hash_data->x, hash_data->len, |
| 7707 | signature_data->x, |
| 7708 | signature_data->len); |
| 7709 | |
| 7710 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7711 | } |
| 7712 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7713 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7714 | TEST_ASSERT(num_ops_prior == 0); |
| 7715 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7716 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7717 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7718 | actual_status = psa_verify_hash_complete(&operation); |
| 7719 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7720 | num_completes++; |
| 7721 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7722 | if (actual_status == PSA_SUCCESS || |
| 7723 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7724 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7725 | /* We are asserting here that every complete makes progress |
| 7726 | * (completes some ops), which is true of the internal |
| 7727 | * implementation and probably any implementation, however this is |
| 7728 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7729 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7730 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7731 | num_ops_prior = num_ops; |
| 7732 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7733 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7734 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7735 | TEST_EQUAL(actual_status, expected_complete_status); |
| 7736 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 7737 | /* Check that another complete returns BAD_STATE. */ |
| 7738 | actual_status = psa_verify_hash_complete(&operation); |
| 7739 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7740 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7741 | TEST_LE_U(min_completes, num_completes); |
| 7742 | TEST_LE_U(num_completes, max_completes); |
| 7743 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7744 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7745 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7746 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7747 | TEST_ASSERT(num_ops == 0); |
| 7748 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7749 | exit: |
| 7750 | psa_reset_key_attributes(&attributes); |
| 7751 | psa_destroy_key(key); |
| 7752 | PSA_DONE(); |
| 7753 | } |
| 7754 | /* END_CASE */ |
| 7755 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7756 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7757 | /** |
| 7758 | * interruptible_signverify_hash_state_test() test intentions: |
| 7759 | * |
| 7760 | * Note: This test can currently only handle ECDSA. |
| 7761 | * |
| 7762 | * 1. Test that calling the various interruptible sign and verify hash functions |
| 7763 | * in incorrect orders returns BAD_STATE errors. |
| 7764 | */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7765 | void interruptible_signverify_hash_state_test(int key_type_arg, |
| 7766 | data_t *key_data, int alg_arg, data_t *input_data) |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7767 | { |
| 7768 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7769 | psa_key_type_t key_type = key_type_arg; |
| 7770 | psa_algorithm_t alg = alg_arg; |
| 7771 | size_t key_bits; |
| 7772 | unsigned char *signature = NULL; |
| 7773 | size_t signature_size; |
| 7774 | size_t signature_length = 0xdeadbeef; |
| 7775 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7776 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7777 | psa_sign_hash_interruptible_operation_init(); |
| 7778 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7779 | psa_verify_hash_interruptible_operation_init(); |
| 7780 | |
| 7781 | PSA_ASSERT(psa_crypto_init()); |
| 7782 | |
| 7783 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7784 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7785 | psa_set_key_algorithm(&attributes, alg); |
| 7786 | psa_set_key_type(&attributes, key_type); |
| 7787 | |
| 7788 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7789 | &key)); |
| 7790 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7791 | key_bits = psa_get_key_bits(&attributes); |
| 7792 | |
| 7793 | /* Allocate a buffer which has the size advertised by the |
| 7794 | * library. */ |
| 7795 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7796 | key_bits, alg); |
| 7797 | TEST_ASSERT(signature_size != 0); |
| 7798 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7799 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7800 | |
| 7801 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7802 | |
| 7803 | /* --- Attempt completes prior to starts --- */ |
| 7804 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7805 | signature_size, |
| 7806 | &signature_length), |
| 7807 | PSA_ERROR_BAD_STATE); |
| 7808 | |
| 7809 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7810 | |
| 7811 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7812 | PSA_ERROR_BAD_STATE); |
| 7813 | |
| 7814 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7815 | |
| 7816 | /* --- Aborts in all other places. --- */ |
| 7817 | psa_sign_hash_abort(&sign_operation); |
| 7818 | |
| 7819 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7820 | input_data->x, input_data->len)); |
| 7821 | |
| 7822 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7823 | |
| 7824 | psa_interruptible_set_max_ops(1); |
| 7825 | |
| 7826 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7827 | input_data->x, input_data->len)); |
| 7828 | |
| 7829 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7830 | signature_size, |
| 7831 | &signature_length), |
| 7832 | PSA_OPERATION_INCOMPLETE); |
| 7833 | |
| 7834 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7835 | |
| 7836 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7837 | |
| 7838 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7839 | input_data->x, input_data->len)); |
| 7840 | |
| 7841 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7842 | signature_size, |
| 7843 | &signature_length)); |
| 7844 | |
| 7845 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7846 | |
| 7847 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7848 | |
| 7849 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7850 | input_data->x, input_data->len, |
| 7851 | signature, signature_length)); |
| 7852 | |
| 7853 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7854 | |
| 7855 | psa_interruptible_set_max_ops(1); |
| 7856 | |
| 7857 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7858 | input_data->x, input_data->len, |
| 7859 | signature, signature_length)); |
| 7860 | |
| 7861 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7862 | PSA_OPERATION_INCOMPLETE); |
| 7863 | |
| 7864 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7865 | |
| 7866 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7867 | |
| 7868 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7869 | input_data->x, input_data->len, |
| 7870 | signature, signature_length)); |
| 7871 | |
| 7872 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7873 | |
| 7874 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7875 | |
| 7876 | /* --- Attempt double starts. --- */ |
| 7877 | |
| 7878 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7879 | input_data->x, input_data->len)); |
| 7880 | |
| 7881 | TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg, |
| 7882 | input_data->x, input_data->len), |
| 7883 | PSA_ERROR_BAD_STATE); |
| 7884 | |
| 7885 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7886 | |
| 7887 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7888 | input_data->x, input_data->len, |
| 7889 | signature, signature_length)); |
| 7890 | |
| 7891 | TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg, |
| 7892 | input_data->x, input_data->len, |
| 7893 | signature, signature_length), |
| 7894 | PSA_ERROR_BAD_STATE); |
| 7895 | |
| 7896 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7897 | |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7898 | exit: |
| 7899 | /* |
| 7900 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7901 | * thus reset them as required. |
| 7902 | */ |
| 7903 | psa_reset_key_attributes(&attributes); |
| 7904 | |
| 7905 | psa_destroy_key(key); |
| 7906 | mbedtls_free(signature); |
| 7907 | PSA_DONE(); |
| 7908 | } |
| 7909 | /* END_CASE */ |
| 7910 | |
| 7911 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7912 | /** |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7913 | * interruptible_signverify_hash_edgecase_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7914 | * |
| 7915 | * Note: This test can currently only handle ECDSA. |
| 7916 | * |
| 7917 | * 1. Test various edge cases in the interruptible sign and verify hash |
| 7918 | * interfaces. |
| 7919 | */ |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7920 | void interruptible_signverify_hash_edgecase_tests(int key_type_arg, |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7921 | data_t *key_data, int alg_arg, data_t *input_data) |
| 7922 | { |
| 7923 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7924 | psa_key_type_t key_type = key_type_arg; |
| 7925 | psa_algorithm_t alg = alg_arg; |
| 7926 | size_t key_bits; |
| 7927 | unsigned char *signature = NULL; |
| 7928 | size_t signature_size; |
| 7929 | size_t signature_length = 0xdeadbeef; |
| 7930 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7931 | uint8_t *input_buffer = NULL; |
| 7932 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7933 | psa_sign_hash_interruptible_operation_init(); |
| 7934 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7935 | psa_verify_hash_interruptible_operation_init(); |
| 7936 | |
| 7937 | PSA_ASSERT(psa_crypto_init()); |
| 7938 | |
| 7939 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7940 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7941 | psa_set_key_algorithm(&attributes, alg); |
| 7942 | psa_set_key_type(&attributes, key_type); |
| 7943 | |
| 7944 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7945 | &key)); |
| 7946 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7947 | key_bits = psa_get_key_bits(&attributes); |
| 7948 | |
| 7949 | /* Allocate a buffer which has the size advertised by the |
| 7950 | * library. */ |
| 7951 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7952 | key_bits, alg); |
| 7953 | TEST_ASSERT(signature_size != 0); |
| 7954 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7955 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7956 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7957 | /* --- Change function inputs mid run, to cause an error (sign only, |
| 7958 | * verify passes all inputs to start. --- */ |
| 7959 | |
| 7960 | psa_interruptible_set_max_ops(1); |
| 7961 | |
| 7962 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7963 | input_data->x, input_data->len)); |
| 7964 | |
| 7965 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7966 | signature_size, |
| 7967 | &signature_length), |
| 7968 | PSA_OPERATION_INCOMPLETE); |
| 7969 | |
| 7970 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7971 | 0, |
| 7972 | &signature_length), |
| 7973 | PSA_ERROR_BUFFER_TOO_SMALL); |
| 7974 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7975 | /* And test that this invalidates the operation. */ |
| 7976 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7977 | 0, |
| 7978 | &signature_length), |
| 7979 | PSA_ERROR_BAD_STATE); |
| 7980 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7981 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7982 | |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7983 | /* Trash the hash buffer in between start and complete, to ensure |
| 7984 | * no reliance on external buffers. */ |
| 7985 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7986 | |
Paul Elliott | 6c68df4 | 2023-10-23 15:33:37 +0100 | [diff] [blame] | 7987 | TEST_CALLOC(input_buffer, input_data->len); |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7988 | |
| 7989 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7990 | |
| 7991 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7992 | input_buffer, input_data->len)); |
| 7993 | |
| 7994 | memset(input_buffer, '!', input_data->len); |
| 7995 | mbedtls_free(input_buffer); |
| 7996 | input_buffer = NULL; |
| 7997 | |
| 7998 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7999 | signature_size, |
| 8000 | &signature_length)); |
| 8001 | |
| 8002 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 8003 | |
Paul Elliott | 6c68df4 | 2023-10-23 15:33:37 +0100 | [diff] [blame] | 8004 | TEST_CALLOC(input_buffer, input_data->len); |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 8005 | |
| 8006 | memcpy(input_buffer, input_data->x, input_data->len); |
| 8007 | |
| 8008 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 8009 | input_buffer, input_data->len, |
| 8010 | signature, signature_length)); |
| 8011 | |
| 8012 | memset(input_buffer, '!', input_data->len); |
| 8013 | mbedtls_free(input_buffer); |
| 8014 | input_buffer = NULL; |
| 8015 | |
| 8016 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 8017 | |
| 8018 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 8019 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 8020 | exit: |
| 8021 | /* |
| 8022 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8023 | * thus reset them as required. |
| 8024 | */ |
| 8025 | psa_reset_key_attributes(&attributes); |
| 8026 | |
| 8027 | psa_destroy_key(key); |
| 8028 | mbedtls_free(signature); |
Paul Elliott | 6c68df4 | 2023-10-23 15:33:37 +0100 | [diff] [blame] | 8029 | mbedtls_free(input_buffer); |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 8030 | PSA_DONE(); |
| 8031 | } |
| 8032 | /* END_CASE */ |
| 8033 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8034 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 8035 | /** |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 8036 | * interruptible_signverify_hash_ops_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 8037 | * |
| 8038 | * Note: This test can currently only handle ECDSA. |
| 8039 | * |
| 8040 | * 1. Test that setting max ops is reflected in both interruptible sign and |
| 8041 | * verify hash |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 8042 | * 2. Test that changing the value of max_ops to unlimited during an operation |
| 8043 | * causes that operation to complete in the next call. |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 8044 | * |
| 8045 | * 3. Test that calling get_num_ops() between complete calls gives the same |
| 8046 | * 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] | 8047 | */ |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 8048 | void interruptible_signverify_hash_ops_tests(int key_type_arg, |
| 8049 | data_t *key_data, int alg_arg, |
| 8050 | data_t *input_data) |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8051 | { |
| 8052 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8053 | psa_key_type_t key_type = key_type_arg; |
| 8054 | psa_algorithm_t alg = alg_arg; |
| 8055 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 8056 | size_t key_bits; |
| 8057 | unsigned char *signature = NULL; |
| 8058 | size_t signature_size; |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 8059 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 8060 | uint32_t num_ops = 0; |
| 8061 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 8062 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8063 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 8064 | psa_sign_hash_interruptible_operation_init(); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 8065 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 8066 | psa_verify_hash_interruptible_operation_init(); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8067 | |
| 8068 | PSA_ASSERT(psa_crypto_init()); |
| 8069 | |
| 8070 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 8071 | PSA_KEY_USAGE_VERIFY_HASH); |
| 8072 | psa_set_key_algorithm(&attributes, alg); |
| 8073 | psa_set_key_type(&attributes, key_type); |
| 8074 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 8075 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key)); |
| 8076 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8077 | key_bits = psa_get_key_bits(&attributes); |
| 8078 | |
| 8079 | /* Allocate a buffer which has the size advertised by the |
| 8080 | * library. */ |
| 8081 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8082 | |
| 8083 | TEST_ASSERT(signature_size != 0); |
| 8084 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8085 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8086 | |
| 8087 | /* Check that default max ops gets set if we don't set it. */ |
| 8088 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 8089 | input_data->x, input_data->len)); |
| 8090 | |
| 8091 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 8092 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 8093 | |
| 8094 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 8095 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 8096 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 8097 | input_data->x, input_data->len, |
| 8098 | signature, signature_size)); |
| 8099 | |
| 8100 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 8101 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 8102 | |
| 8103 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 8104 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8105 | /* Check that max ops gets set properly. */ |
| 8106 | |
| 8107 | psa_interruptible_set_max_ops(0xbeef); |
| 8108 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 8109 | TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8110 | |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 8111 | /* --- Ensure changing the max ops mid operation works (operation should |
| 8112 | * complete successfully after setting max ops to unlimited --- */ |
| 8113 | psa_interruptible_set_max_ops(1); |
| 8114 | |
| 8115 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 8116 | input_data->x, input_data->len)); |
| 8117 | |
| 8118 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 8119 | signature_size, |
| 8120 | &signature_length), |
| 8121 | PSA_OPERATION_INCOMPLETE); |
| 8122 | |
| 8123 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 8124 | |
| 8125 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 8126 | signature_size, |
| 8127 | &signature_length)); |
| 8128 | |
| 8129 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 8130 | |
| 8131 | psa_interruptible_set_max_ops(1); |
| 8132 | |
| 8133 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 8134 | input_data->x, input_data->len, |
| 8135 | signature, signature_length)); |
| 8136 | |
| 8137 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 8138 | PSA_OPERATION_INCOMPLETE); |
| 8139 | |
| 8140 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 8141 | |
| 8142 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 8143 | |
| 8144 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 8145 | |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 8146 | /* --- Test that not calling get_num_ops inbetween complete calls does not |
| 8147 | * result in lost ops. ---*/ |
| 8148 | |
| 8149 | psa_interruptible_set_max_ops(1); |
| 8150 | |
| 8151 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 8152 | input_data->x, input_data->len)); |
| 8153 | |
| 8154 | /* Continue performing the signature until complete. */ |
| 8155 | do { |
| 8156 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 8157 | signature_size, |
| 8158 | &signature_length); |
| 8159 | |
| 8160 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 8161 | |
| 8162 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 8163 | |
| 8164 | PSA_ASSERT(status); |
| 8165 | |
| 8166 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 8167 | |
| 8168 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 8169 | input_data->x, input_data->len)); |
| 8170 | |
| 8171 | /* Continue performing the signature until complete. */ |
| 8172 | do { |
| 8173 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 8174 | signature_size, |
| 8175 | &signature_length); |
| 8176 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 8177 | |
| 8178 | PSA_ASSERT(status); |
| 8179 | |
| 8180 | TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation)); |
| 8181 | |
| 8182 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 8183 | |
| 8184 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 8185 | input_data->x, input_data->len, |
| 8186 | signature, signature_length)); |
| 8187 | |
| 8188 | /* Continue performing the verification until complete. */ |
| 8189 | do { |
| 8190 | status = psa_verify_hash_complete(&verify_operation); |
| 8191 | |
| 8192 | num_ops = psa_verify_hash_get_num_ops(&verify_operation); |
| 8193 | |
| 8194 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 8195 | |
| 8196 | PSA_ASSERT(status); |
| 8197 | |
| 8198 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 8199 | |
| 8200 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 8201 | input_data->x, input_data->len, |
| 8202 | signature, signature_length)); |
| 8203 | |
| 8204 | /* Continue performing the verification until complete. */ |
| 8205 | do { |
| 8206 | status = psa_verify_hash_complete(&verify_operation); |
| 8207 | |
| 8208 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 8209 | |
| 8210 | PSA_ASSERT(status); |
| 8211 | |
| 8212 | TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation)); |
| 8213 | |
| 8214 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 8215 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8216 | exit: |
| 8217 | /* |
| 8218 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8219 | * thus reset them as required. |
| 8220 | */ |
| 8221 | psa_reset_key_attributes(&attributes); |
| 8222 | |
| 8223 | psa_destroy_key(key); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 8224 | mbedtls_free(signature); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8225 | PSA_DONE(); |
| 8226 | } |
| 8227 | /* END_CASE */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 8228 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8229 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8230 | void sign_message_deterministic(int key_type_arg, |
| 8231 | data_t *key_data, |
| 8232 | int alg_arg, |
| 8233 | data_t *input_data, |
| 8234 | data_t *output_data) |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8235 | { |
| 8236 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8237 | psa_key_type_t key_type = key_type_arg; |
| 8238 | psa_algorithm_t alg = alg_arg; |
| 8239 | size_t key_bits; |
| 8240 | unsigned char *signature = NULL; |
| 8241 | size_t signature_size; |
| 8242 | size_t signature_length = 0xdeadbeef; |
| 8243 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8244 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8245 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8246 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8247 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 8248 | psa_set_key_algorithm(&attributes, alg); |
| 8249 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8250 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8251 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8252 | &key)); |
| 8253 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8254 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8256 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8257 | TEST_ASSERT(signature_size != 0); |
| 8258 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8259 | TEST_CALLOC(signature, signature_size); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8260 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8261 | PSA_ASSERT(psa_sign_message(key, alg, |
| 8262 | input_data->x, input_data->len, |
| 8263 | signature, signature_size, |
| 8264 | &signature_length)); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8265 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8266 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8267 | signature, signature_length); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8268 | |
| 8269 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8270 | psa_reset_key_attributes(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8271 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8272 | psa_destroy_key(key); |
| 8273 | mbedtls_free(signature); |
| 8274 | PSA_DONE(); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8275 | |
| 8276 | } |
| 8277 | /* END_CASE */ |
| 8278 | |
| 8279 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8280 | void sign_message_fail(int key_type_arg, |
| 8281 | data_t *key_data, |
| 8282 | int alg_arg, |
| 8283 | data_t *input_data, |
| 8284 | int signature_size_arg, |
| 8285 | int expected_status_arg) |
| 8286 | { |
| 8287 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8288 | psa_key_type_t key_type = key_type_arg; |
| 8289 | psa_algorithm_t alg = alg_arg; |
| 8290 | size_t signature_size = signature_size_arg; |
| 8291 | psa_status_t actual_status; |
| 8292 | psa_status_t expected_status = expected_status_arg; |
| 8293 | unsigned char *signature = NULL; |
| 8294 | size_t signature_length = 0xdeadbeef; |
| 8295 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8296 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8297 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8298 | |
| 8299 | PSA_ASSERT(psa_crypto_init()); |
| 8300 | |
| 8301 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 8302 | psa_set_key_algorithm(&attributes, alg); |
| 8303 | psa_set_key_type(&attributes, key_type); |
| 8304 | |
| 8305 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8306 | &key)); |
| 8307 | |
| 8308 | actual_status = psa_sign_message(key, alg, |
| 8309 | input_data->x, input_data->len, |
| 8310 | signature, signature_size, |
| 8311 | &signature_length); |
| 8312 | TEST_EQUAL(actual_status, expected_status); |
| 8313 | /* The value of *signature_length is unspecified on error, but |
| 8314 | * whatever it is, it should be less than signature_size, so that |
| 8315 | * if the caller tries to read *signature_length bytes without |
| 8316 | * checking the error code then they don't overflow a buffer. */ |
| 8317 | TEST_LE_U(signature_length, signature_size); |
| 8318 | |
| 8319 | exit: |
| 8320 | psa_reset_key_attributes(&attributes); |
| 8321 | psa_destroy_key(key); |
| 8322 | mbedtls_free(signature); |
| 8323 | PSA_DONE(); |
| 8324 | } |
| 8325 | /* END_CASE */ |
| 8326 | |
| 8327 | /* BEGIN_CASE */ |
| 8328 | void sign_verify_message(int key_type_arg, |
| 8329 | data_t *key_data, |
| 8330 | int alg_arg, |
| 8331 | data_t *input_data) |
| 8332 | { |
| 8333 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8334 | psa_key_type_t key_type = key_type_arg; |
| 8335 | psa_algorithm_t alg = alg_arg; |
| 8336 | size_t key_bits; |
| 8337 | unsigned char *signature = NULL; |
| 8338 | size_t signature_size; |
| 8339 | size_t signature_length = 0xdeadbeef; |
| 8340 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8341 | |
| 8342 | PSA_ASSERT(psa_crypto_init()); |
| 8343 | |
| 8344 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 8345 | PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8346 | psa_set_key_algorithm(&attributes, alg); |
| 8347 | psa_set_key_type(&attributes, key_type); |
| 8348 | |
| 8349 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8350 | &key)); |
| 8351 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8352 | key_bits = psa_get_key_bits(&attributes); |
| 8353 | |
| 8354 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8355 | TEST_ASSERT(signature_size != 0); |
| 8356 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8357 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8358 | |
| 8359 | PSA_ASSERT(psa_sign_message(key, alg, |
| 8360 | input_data->x, input_data->len, |
| 8361 | signature, signature_size, |
| 8362 | &signature_length)); |
| 8363 | TEST_LE_U(signature_length, signature_size); |
| 8364 | TEST_ASSERT(signature_length > 0); |
| 8365 | |
| 8366 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8367 | input_data->x, input_data->len, |
| 8368 | signature, signature_length)); |
| 8369 | |
| 8370 | if (input_data->len != 0) { |
| 8371 | /* Flip a bit in the input and verify that the signature is now |
| 8372 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 8373 | * because ECDSA may ignore the last few bits of the input. */ |
| 8374 | input_data->x[0] ^= 1; |
| 8375 | TEST_EQUAL(psa_verify_message(key, alg, |
| 8376 | input_data->x, input_data->len, |
| 8377 | signature, signature_length), |
| 8378 | PSA_ERROR_INVALID_SIGNATURE); |
| 8379 | } |
| 8380 | |
| 8381 | exit: |
| 8382 | psa_reset_key_attributes(&attributes); |
| 8383 | |
| 8384 | psa_destroy_key(key); |
| 8385 | mbedtls_free(signature); |
| 8386 | PSA_DONE(); |
| 8387 | } |
| 8388 | /* END_CASE */ |
| 8389 | |
| 8390 | /* BEGIN_CASE */ |
| 8391 | void verify_message(int key_type_arg, |
| 8392 | data_t *key_data, |
| 8393 | int alg_arg, |
| 8394 | data_t *input_data, |
| 8395 | data_t *signature_data) |
| 8396 | { |
| 8397 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8398 | psa_key_type_t key_type = key_type_arg; |
| 8399 | psa_algorithm_t alg = alg_arg; |
| 8400 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8401 | |
| 8402 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 8403 | |
| 8404 | PSA_ASSERT(psa_crypto_init()); |
| 8405 | |
| 8406 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8407 | psa_set_key_algorithm(&attributes, alg); |
| 8408 | psa_set_key_type(&attributes, key_type); |
| 8409 | |
| 8410 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8411 | &key)); |
| 8412 | |
| 8413 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8414 | input_data->x, input_data->len, |
| 8415 | signature_data->x, signature_data->len)); |
| 8416 | |
| 8417 | exit: |
| 8418 | psa_reset_key_attributes(&attributes); |
| 8419 | psa_destroy_key(key); |
| 8420 | PSA_DONE(); |
| 8421 | } |
| 8422 | /* END_CASE */ |
| 8423 | |
| 8424 | /* BEGIN_CASE */ |
| 8425 | void verify_message_fail(int key_type_arg, |
| 8426 | data_t *key_data, |
| 8427 | int alg_arg, |
| 8428 | data_t *hash_data, |
| 8429 | data_t *signature_data, |
| 8430 | int expected_status_arg) |
| 8431 | { |
| 8432 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8433 | psa_key_type_t key_type = key_type_arg; |
| 8434 | psa_algorithm_t alg = alg_arg; |
| 8435 | psa_status_t actual_status; |
| 8436 | psa_status_t expected_status = expected_status_arg; |
| 8437 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8438 | |
| 8439 | PSA_ASSERT(psa_crypto_init()); |
| 8440 | |
| 8441 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8442 | psa_set_key_algorithm(&attributes, alg); |
| 8443 | psa_set_key_type(&attributes, key_type); |
| 8444 | |
| 8445 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8446 | &key)); |
| 8447 | |
| 8448 | actual_status = psa_verify_message(key, alg, |
| 8449 | hash_data->x, hash_data->len, |
| 8450 | signature_data->x, |
| 8451 | signature_data->len); |
| 8452 | TEST_EQUAL(actual_status, expected_status); |
| 8453 | |
| 8454 | exit: |
| 8455 | psa_reset_key_attributes(&attributes); |
| 8456 | psa_destroy_key(key); |
| 8457 | PSA_DONE(); |
| 8458 | } |
| 8459 | /* END_CASE */ |
| 8460 | |
| 8461 | /* BEGIN_CASE */ |
| 8462 | void asymmetric_encrypt(int key_type_arg, |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8463 | data_t *key_data, |
| 8464 | int alg_arg, |
| 8465 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8466 | data_t *label, |
| 8467 | int expected_output_length_arg, |
| 8468 | int expected_status_arg) |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8469 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8470 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8471 | psa_key_type_t key_type = key_type_arg; |
| 8472 | psa_algorithm_t alg = alg_arg; |
| 8473 | size_t expected_output_length = expected_output_length_arg; |
| 8474 | size_t key_bits; |
| 8475 | unsigned char *output = NULL; |
| 8476 | size_t output_size; |
| 8477 | size_t output_length = ~0; |
| 8478 | psa_status_t actual_status; |
| 8479 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8480 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8481 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8482 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 8483 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8484 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8485 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 8486 | psa_set_key_algorithm(&attributes, alg); |
| 8487 | psa_set_key_type(&attributes, key_type); |
| 8488 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8489 | &key)); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8490 | |
| 8491 | /* Determine the maximum output length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8492 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8493 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8494 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8495 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8496 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8497 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8498 | |
| 8499 | /* Encrypt the input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8500 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8501 | input_data->x, input_data->len, |
| 8502 | label->x, label->len, |
| 8503 | output, output_size, |
| 8504 | &output_length); |
| 8505 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8506 | if (actual_status == PSA_SUCCESS) { |
| 8507 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8508 | } else { |
| 8509 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8510 | } |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8511 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8512 | /* If the label is empty, the test framework puts a non-null pointer |
| 8513 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8514 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8515 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8516 | if (output_size != 0) { |
| 8517 | memset(output, 0, output_size); |
| 8518 | } |
| 8519 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8520 | input_data->x, input_data->len, |
| 8521 | NULL, label->len, |
| 8522 | output, output_size, |
| 8523 | &output_length); |
| 8524 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8525 | if (actual_status == PSA_SUCCESS) { |
| 8526 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8527 | } else { |
| 8528 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8529 | } |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8530 | } |
| 8531 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8532 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8533 | /* |
| 8534 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8535 | * thus reset them as required. |
| 8536 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8537 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8538 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8539 | psa_destroy_key(key); |
| 8540 | mbedtls_free(output); |
| 8541 | PSA_DONE(); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8542 | } |
| 8543 | /* END_CASE */ |
| 8544 | |
| 8545 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8546 | void asymmetric_encrypt_decrypt(int key_type_arg, |
| 8547 | data_t *key_data, |
| 8548 | int alg_arg, |
| 8549 | data_t *input_data, |
| 8550 | data_t *label) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8551 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8552 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8553 | psa_key_type_t key_type = key_type_arg; |
| 8554 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8555 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8556 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8557 | size_t output_size; |
| 8558 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8559 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8560 | size_t output2_size; |
| 8561 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8562 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8563 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8564 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8565 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8566 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 8567 | psa_set_key_algorithm(&attributes, alg); |
| 8568 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8569 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8570 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8571 | &key)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8572 | |
| 8573 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8574 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8575 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8576 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8577 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8578 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8579 | TEST_CALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8580 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8581 | output2_size = input_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8582 | TEST_LE_U(output2_size, |
| 8583 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)); |
| 8584 | TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8585 | TEST_CALLOC(output2, output2_size); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8586 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 8587 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 8588 | * the original plaintext because of the non-optional random |
| 8589 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8590 | PSA_ASSERT(psa_asymmetric_encrypt(key, alg, |
| 8591 | input_data->x, input_data->len, |
| 8592 | label->x, label->len, |
| 8593 | output, output_size, |
| 8594 | &output_length)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8595 | /* We don't know what ciphertext length to expect, but check that |
| 8596 | * it looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8597 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8598 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8599 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8600 | output, output_length, |
| 8601 | label->x, label->len, |
| 8602 | output2, output2_size, |
| 8603 | &output2_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8604 | TEST_MEMORY_COMPARE(input_data->x, input_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8605 | output2, output2_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8606 | |
| 8607 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8608 | /* |
| 8609 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8610 | * thus reset them as required. |
| 8611 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8612 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8613 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8614 | psa_destroy_key(key); |
| 8615 | mbedtls_free(output); |
| 8616 | mbedtls_free(output2); |
| 8617 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8618 | } |
| 8619 | /* END_CASE */ |
| 8620 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8621 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8622 | void asymmetric_decrypt(int key_type_arg, |
| 8623 | data_t *key_data, |
| 8624 | int alg_arg, |
| 8625 | data_t *input_data, |
| 8626 | data_t *label, |
| 8627 | data_t *expected_data) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8628 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8629 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8630 | psa_key_type_t key_type = key_type_arg; |
| 8631 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8632 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8633 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 8634 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8635 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8636 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8637 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8638 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8639 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8640 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8641 | psa_set_key_algorithm(&attributes, alg); |
| 8642 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8643 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8644 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8645 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8646 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8647 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8648 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8649 | |
| 8650 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8651 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8652 | TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8653 | TEST_CALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8654 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8655 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8656 | input_data->x, input_data->len, |
| 8657 | label->x, label->len, |
| 8658 | output, |
| 8659 | output_size, |
| 8660 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8661 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8662 | output, output_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8663 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8664 | /* If the label is empty, the test framework puts a non-null pointer |
| 8665 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8666 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8667 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8668 | if (output_size != 0) { |
| 8669 | memset(output, 0, output_size); |
| 8670 | } |
| 8671 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8672 | input_data->x, input_data->len, |
| 8673 | NULL, label->len, |
| 8674 | output, |
| 8675 | output_size, |
| 8676 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8677 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8678 | output, output_length); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8679 | } |
| 8680 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8681 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8682 | psa_reset_key_attributes(&attributes); |
| 8683 | psa_destroy_key(key); |
| 8684 | mbedtls_free(output); |
| 8685 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8686 | } |
| 8687 | /* END_CASE */ |
| 8688 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8689 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8690 | void asymmetric_decrypt_fail(int key_type_arg, |
| 8691 | data_t *key_data, |
| 8692 | int alg_arg, |
| 8693 | data_t *input_data, |
| 8694 | data_t *label, |
| 8695 | int output_size_arg, |
| 8696 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8697 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8698 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8699 | psa_key_type_t key_type = key_type_arg; |
| 8700 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8701 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 8702 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8703 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8704 | psa_status_t actual_status; |
| 8705 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8706 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8707 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8708 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8709 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8710 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8711 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8712 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8713 | psa_set_key_algorithm(&attributes, alg); |
| 8714 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8715 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8716 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8717 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8718 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8719 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8720 | input_data->x, input_data->len, |
| 8721 | label->x, label->len, |
| 8722 | output, output_size, |
| 8723 | &output_length); |
| 8724 | TEST_EQUAL(actual_status, expected_status); |
| 8725 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8726 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8727 | /* If the label is empty, the test framework puts a non-null pointer |
| 8728 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8729 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8730 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8731 | if (output_size != 0) { |
| 8732 | memset(output, 0, output_size); |
| 8733 | } |
| 8734 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8735 | input_data->x, input_data->len, |
| 8736 | NULL, label->len, |
| 8737 | output, output_size, |
| 8738 | &output_length); |
| 8739 | TEST_EQUAL(actual_status, expected_status); |
| 8740 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8741 | } |
| 8742 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8743 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8744 | psa_reset_key_attributes(&attributes); |
| 8745 | psa_destroy_key(key); |
| 8746 | mbedtls_free(output); |
| 8747 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8748 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8749 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8750 | |
| 8751 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8752 | void key_derivation_init() |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8753 | { |
| 8754 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 8755 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 8756 | * 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] | 8757 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8758 | size_t capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8759 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init(); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 8760 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8761 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8762 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8763 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8764 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8765 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8766 | TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity), |
| 8767 | PSA_ERROR_BAD_STATE); |
| 8768 | TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity), |
| 8769 | PSA_ERROR_BAD_STATE); |
| 8770 | TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity), |
| 8771 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8772 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8773 | /* A default operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8774 | PSA_ASSERT(psa_key_derivation_abort(&func)); |
| 8775 | PSA_ASSERT(psa_key_derivation_abort(&init)); |
| 8776 | PSA_ASSERT(psa_key_derivation_abort(&zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8777 | } |
| 8778 | /* END_CASE */ |
| 8779 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 8780 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8781 | void derive_setup(int alg_arg, int expected_status_arg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8782 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8783 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8784 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8785 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8786 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8787 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8788 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8789 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8790 | expected_status); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8791 | |
| 8792 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8793 | psa_key_derivation_abort(&operation); |
| 8794 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8795 | } |
| 8796 | /* END_CASE */ |
| 8797 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8798 | /* BEGIN_CASE */ |
Kusumit Ghoderao | 9ffd397 | 2023-12-01 16:40:13 +0530 | [diff] [blame] | 8799 | void derive_set_capacity(int alg_arg, int64_t capacity_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8800 | int expected_status_arg) |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8801 | { |
| 8802 | psa_algorithm_t alg = alg_arg; |
| 8803 | size_t capacity = capacity_arg; |
| 8804 | psa_status_t expected_status = expected_status_arg; |
| 8805 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8806 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8807 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8808 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8809 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8810 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8811 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 8812 | expected_status); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8813 | |
| 8814 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8815 | psa_key_derivation_abort(&operation); |
| 8816 | PSA_DONE(); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8817 | } |
| 8818 | /* END_CASE */ |
| 8819 | |
| 8820 | /* BEGIN_CASE */ |
Kusumit Ghoderao | d60dfc0 | 2023-05-01 17:39:27 +0530 | [diff] [blame] | 8821 | void parse_binary_string_test(data_t *input, int output) |
| 8822 | { |
| 8823 | uint64_t value; |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 8824 | value = mbedtls_test_parse_binary_string(input); |
Kusumit Ghoderao | d60dfc0 | 2023-05-01 17:39:27 +0530 | [diff] [blame] | 8825 | TEST_EQUAL(value, output); |
| 8826 | } |
| 8827 | /* END_CASE */ |
| 8828 | |
| 8829 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8830 | void derive_input(int alg_arg, |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8831 | int step_arg1, int key_type_arg1, data_t *input1, |
| 8832 | int expected_status_arg1, |
| 8833 | int step_arg2, int key_type_arg2, data_t *input2, |
| 8834 | int expected_status_arg2, |
| 8835 | int step_arg3, int key_type_arg3, data_t *input3, |
| 8836 | int expected_status_arg3, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8837 | int output_key_type_arg, int expected_output_status_arg) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8838 | { |
| 8839 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8840 | psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 }; |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8841 | 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] | 8842 | psa_status_t expected_statuses[] = { expected_status_arg1, |
| 8843 | expected_status_arg2, |
| 8844 | expected_status_arg3 }; |
| 8845 | data_t *inputs[] = { input1, input2, input3 }; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8846 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8847 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8848 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8849 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8850 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8851 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8852 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8853 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8854 | psa_status_t expected_output_status = expected_output_status_arg; |
| 8855 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8856 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8857 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8858 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8859 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8860 | psa_set_key_algorithm(&attributes, alg); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8861 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8862 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8863 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8864 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8865 | mbedtls_test_set_step(i); |
| 8866 | if (steps[i] == 0) { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 8867 | /* Skip this step */ |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8868 | } else if (((psa_key_type_t) key_types[i]) != PSA_KEY_TYPE_NONE && |
| 8869 | key_types[i] != INPUT_INTEGER) { |
| 8870 | psa_set_key_type(&attributes, ((psa_key_type_t) key_types[i])); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8871 | PSA_ASSERT(psa_import_key(&attributes, |
| 8872 | inputs[i]->x, inputs[i]->len, |
| 8873 | &keys[i])); |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8874 | 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] | 8875 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8876 | // When taking a private key as secret input, use key agreement |
| 8877 | // to add the shared secret to the derivation |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8878 | TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self( |
Ryan Everett | 73e4ea3 | 2024-03-12 16:29:55 +0000 | [diff] [blame] | 8879 | &operation, keys[i], 0), |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8880 | expected_statuses[i]); |
| 8881 | } else { |
| 8882 | TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i], |
| 8883 | keys[i]), |
| 8884 | expected_statuses[i]); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8885 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8886 | } else { |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8887 | if (key_types[i] == INPUT_INTEGER) { |
| 8888 | TEST_EQUAL(psa_key_derivation_input_integer( |
| 8889 | &operation, steps[i], |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 8890 | mbedtls_test_parse_binary_string(inputs[i])), |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8891 | expected_statuses[i]); |
| 8892 | } else { |
Kusumit Ghoderao | 02326d5 | 2023-04-06 17:47:59 +0530 | [diff] [blame] | 8893 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8894 | &operation, steps[i], |
| 8895 | inputs[i]->x, inputs[i]->len), |
| 8896 | expected_statuses[i]); |
Kusumit Ghoderao | 02326d5 | 2023-04-06 17:47:59 +0530 | [diff] [blame] | 8897 | } |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8898 | } |
| 8899 | } |
| 8900 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8901 | if (output_key_type != PSA_KEY_TYPE_NONE) { |
| 8902 | psa_reset_key_attributes(&attributes); |
| 8903 | psa_set_key_type(&attributes, output_key_type); |
| 8904 | psa_set_key_bits(&attributes, 8); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8905 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8906 | psa_key_derivation_output_key(&attributes, &operation, |
| 8907 | &output_key); |
| 8908 | } else { |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8909 | uint8_t buffer[1]; |
| 8910 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8911 | psa_key_derivation_output_bytes(&operation, |
| 8912 | buffer, sizeof(buffer)); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8913 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8914 | TEST_EQUAL(actual_output_status, expected_output_status); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8915 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8916 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8917 | psa_key_derivation_abort(&operation); |
| 8918 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8919 | psa_destroy_key(keys[i]); |
| 8920 | } |
| 8921 | psa_destroy_key(output_key); |
| 8922 | PSA_DONE(); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8923 | } |
| 8924 | /* END_CASE */ |
| 8925 | |
Kusumit Ghoderao | 42b02b9 | 2023-06-06 16:48:46 +0530 | [diff] [blame] | 8926 | /* BEGIN_CASE*/ |
| 8927 | void derive_input_invalid_cost(int alg_arg, int64_t cost) |
| 8928 | { |
| 8929 | psa_algorithm_t alg = alg_arg; |
| 8930 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8931 | |
| 8932 | PSA_ASSERT(psa_crypto_init()); |
| 8933 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8934 | |
| 8935 | TEST_EQUAL(psa_key_derivation_input_integer(&operation, |
| 8936 | PSA_KEY_DERIVATION_INPUT_COST, |
| 8937 | cost), |
| 8938 | PSA_ERROR_NOT_SUPPORTED); |
| 8939 | |
| 8940 | exit: |
| 8941 | psa_key_derivation_abort(&operation); |
| 8942 | PSA_DONE(); |
| 8943 | } |
| 8944 | /* END_CASE*/ |
| 8945 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8946 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8947 | void derive_over_capacity(int alg_arg) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8948 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8949 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8950 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 8951 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8952 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8953 | unsigned char input1[] = "Input 1"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8954 | size_t input1_length = sizeof(input1); |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8955 | unsigned char input2[] = "Input 2"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8956 | size_t input2_length = sizeof(input2); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8957 | uint8_t buffer[42]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8958 | size_t capacity = sizeof(buffer); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 8959 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 8960 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8961 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8962 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8963 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8964 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8965 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8966 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8967 | psa_set_key_algorithm(&attributes, alg); |
| 8968 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8969 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8970 | PSA_ASSERT(psa_import_key(&attributes, |
| 8971 | key_data, sizeof(key_data), |
| 8972 | &key)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8973 | |
| 8974 | /* valid key derivation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8975 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8976 | input1, input1_length, |
| 8977 | input2, input2_length, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 8978 | capacity, 0)) { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8979 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8980 | } |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8981 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8982 | /* state of operation shouldn't allow additional generation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8983 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8984 | PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8985 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8986 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8987 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8988 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity), |
| 8989 | PSA_ERROR_INSUFFICIENT_DATA); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8990 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8991 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8992 | psa_key_derivation_abort(&operation); |
| 8993 | psa_destroy_key(key); |
| 8994 | PSA_DONE(); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8995 | } |
| 8996 | /* END_CASE */ |
| 8997 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8998 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8999 | void derive_actions_without_setup() |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 9000 | { |
| 9001 | uint8_t output_buffer[16]; |
| 9002 | size_t buffer_size = 16; |
| 9003 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9004 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 9005 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9006 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9007 | output_buffer, buffer_size) |
| 9008 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 9009 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9010 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 9011 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 9012 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9013 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 9014 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9015 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9016 | output_buffer, buffer_size) |
| 9017 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 9018 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9019 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 9020 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 9021 | |
| 9022 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9023 | psa_key_derivation_abort(&operation); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 9024 | } |
| 9025 | /* END_CASE */ |
| 9026 | |
| 9027 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9028 | void derive_output(int alg_arg, |
| 9029 | int step1_arg, data_t *input1, int expected_status_arg1, |
| 9030 | int step2_arg, data_t *input2, int expected_status_arg2, |
| 9031 | int step3_arg, data_t *input3, int expected_status_arg3, |
| 9032 | int step4_arg, data_t *input4, int expected_status_arg4, |
| 9033 | data_t *key_agreement_peer_key, |
| 9034 | int requested_capacity_arg, |
| 9035 | data_t *expected_output1, |
| 9036 | data_t *expected_output2, |
| 9037 | int other_key_input_type, |
| 9038 | int key_input_type, |
| 9039 | int derive_type) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9040 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9041 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9042 | psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg }; |
| 9043 | data_t *inputs[] = { input1, input2, input3, input4 }; |
| 9044 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 9045 | MBEDTLS_SVC_KEY_ID_INIT, |
| 9046 | MBEDTLS_SVC_KEY_ID_INIT, |
| 9047 | MBEDTLS_SVC_KEY_ID_INIT }; |
| 9048 | psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2, |
| 9049 | expected_status_arg3, expected_status_arg4 }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9050 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9051 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9052 | uint8_t *expected_outputs[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9053 | { expected_output1->x, expected_output2->x }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9054 | size_t output_sizes[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9055 | { expected_output1->len, expected_output2->len }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9056 | size_t output_buffer_size = 0; |
| 9057 | uint8_t *output_buffer = NULL; |
| 9058 | size_t expected_capacity; |
| 9059 | size_t current_capacity; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9060 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; |
| 9061 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; |
| 9062 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; |
| 9063 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 9064 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9065 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 9066 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9067 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9068 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
| 9069 | if (output_sizes[i] > output_buffer_size) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9070 | output_buffer_size = output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9071 | } |
| 9072 | if (output_sizes[i] == 0) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9073 | expected_outputs[i] = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9074 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9075 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9076 | TEST_CALLOC(output_buffer, output_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9077 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9078 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9079 | /* Extraction phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9080 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9081 | PSA_ASSERT(psa_key_derivation_set_capacity(&operation, |
| 9082 | requested_capacity)); |
| 9083 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 9084 | switch (steps[i]) { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 9085 | case 0: |
| 9086 | break; |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 9087 | case PSA_KEY_DERIVATION_INPUT_COST: |
| 9088 | TEST_EQUAL(psa_key_derivation_input_integer( |
Kusumit Ghoderao | f28e0f5 | 2023-06-06 15:03:22 +0530 | [diff] [blame] | 9089 | &operation, steps[i], |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 9090 | mbedtls_test_parse_binary_string(inputs[i])), |
Kusumit Ghoderao | f28e0f5 | 2023-06-06 15:03:22 +0530 | [diff] [blame] | 9091 | statuses[i]); |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 9092 | if (statuses[i] != PSA_SUCCESS) { |
| 9093 | goto exit; |
| 9094 | } |
| 9095 | break; |
| 9096 | case PSA_KEY_DERIVATION_INPUT_PASSWORD: |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 9097 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9098 | switch (key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9099 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9100 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 9101 | &operation, steps[i], |
| 9102 | inputs[i]->x, inputs[i]->len), |
| 9103 | statuses[i]); |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 9104 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9105 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 9106 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9107 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9108 | break; |
| 9109 | case 1: // input key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9110 | psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE); |
| 9111 | psa_set_key_algorithm(&attributes1, alg); |
| 9112 | psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9114 | PSA_ASSERT(psa_import_key(&attributes1, |
| 9115 | inputs[i]->x, inputs[i]->len, |
| 9116 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9117 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9118 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 9119 | PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1)); |
| 9120 | TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)), |
| 9121 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9122 | } |
| 9123 | |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 9124 | TEST_EQUAL(psa_key_derivation_input_key(&operation, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9125 | steps[i], |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 9126 | keys[i]), |
| 9127 | statuses[i]); |
| 9128 | |
| 9129 | if (statuses[i] != PSA_SUCCESS) { |
| 9130 | goto exit; |
| 9131 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9132 | break; |
| 9133 | default: |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 9134 | TEST_FAIL("default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9135 | break; |
| 9136 | } |
| 9137 | break; |
| 9138 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9139 | switch (other_key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9140 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9141 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 9142 | steps[i], |
| 9143 | inputs[i]->x, |
| 9144 | inputs[i]->len), |
| 9145 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9146 | break; |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 9147 | case 1: // input key, type DERIVE |
| 9148 | case 11: // input key, type RAW |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9149 | psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE); |
| 9150 | psa_set_key_algorithm(&attributes2, alg); |
| 9151 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9152 | |
| 9153 | // other secret of type RAW_DATA passed with input_key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9154 | if (other_key_input_type == 11) { |
| 9155 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA); |
| 9156 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9157 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9158 | PSA_ASSERT(psa_import_key(&attributes2, |
| 9159 | inputs[i]->x, inputs[i]->len, |
| 9160 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9161 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9162 | TEST_EQUAL(psa_key_derivation_input_key(&operation, |
| 9163 | steps[i], |
| 9164 | keys[i]), |
| 9165 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9166 | break; |
| 9167 | case 2: // key agreement |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9168 | psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE); |
| 9169 | psa_set_key_algorithm(&attributes3, alg); |
| 9170 | psa_set_key_type(&attributes3, |
| 9171 | PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9172 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9173 | PSA_ASSERT(psa_import_key(&attributes3, |
| 9174 | inputs[i]->x, inputs[i]->len, |
| 9175 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9176 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9177 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 9178 | &operation, |
| 9179 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 9180 | keys[i], key_agreement_peer_key->x, |
| 9181 | key_agreement_peer_key->len), statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9182 | break; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9183 | default: |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 9184 | TEST_FAIL("default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9185 | break; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9186 | } |
| 9187 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9188 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9189 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9190 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 9191 | break; |
| 9192 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9193 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 9194 | &operation, steps[i], |
| 9195 | inputs[i]->x, inputs[i]->len), statuses[i]); |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 9196 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9197 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 9198 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9199 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 9200 | break; |
| 9201 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 9202 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 9203 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9204 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 9205 | ¤t_capacity)); |
| 9206 | TEST_EQUAL(current_capacity, requested_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9207 | expected_capacity = requested_capacity; |
| 9208 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9209 | if (derive_type == 1) { // output key |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 9210 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; |
| 9211 | |
| 9212 | /* For output key derivation secret must be provided using |
| 9213 | input key, otherwise operation is not permitted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9214 | if (key_input_type == 1) { |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 9215 | expected_status = PSA_SUCCESS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9216 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9217 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9218 | psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT); |
| 9219 | psa_set_key_algorithm(&attributes4, alg); |
| 9220 | psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE); |
| 9221 | psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9222 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9223 | TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation, |
| 9224 | &derived_key), expected_status); |
| 9225 | } else { // output bytes |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9226 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9227 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9228 | /* Read some bytes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9229 | status = psa_key_derivation_output_bytes(&operation, |
| 9230 | output_buffer, output_sizes[i]); |
| 9231 | if (expected_capacity == 0 && output_sizes[i] == 0) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9232 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9233 | TEST_ASSERT(status == PSA_SUCCESS || |
| 9234 | status == PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9235 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9236 | } else if (expected_capacity == 0 || |
| 9237 | output_sizes[i] > expected_capacity) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9238 | /* Capacity exceeded. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9239 | TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9240 | expected_capacity = 0; |
| 9241 | continue; |
| 9242 | } |
| 9243 | /* Success. Check the read data. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9244 | PSA_ASSERT(status); |
| 9245 | if (output_sizes[i] != 0) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9246 | TEST_MEMORY_COMPARE(output_buffer, output_sizes[i], |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9247 | expected_outputs[i], output_sizes[i]); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9248 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9249 | /* Check the operation status. */ |
| 9250 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9251 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 9252 | ¤t_capacity)); |
| 9253 | TEST_EQUAL(expected_capacity, current_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9254 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9255 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9256 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9257 | |
| 9258 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9259 | mbedtls_free(output_buffer); |
| 9260 | psa_key_derivation_abort(&operation); |
| 9261 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 9262 | psa_destroy_key(keys[i]); |
| 9263 | } |
| 9264 | psa_destroy_key(derived_key); |
| 9265 | PSA_DONE(); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9266 | } |
| 9267 | /* END_CASE */ |
| 9268 | |
| 9269 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9270 | void derive_full(int alg_arg, |
| 9271 | data_t *key_data, |
| 9272 | data_t *input1, |
| 9273 | data_t *input2, |
| 9274 | int requested_capacity_arg) |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9275 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9276 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9277 | psa_algorithm_t alg = alg_arg; |
| 9278 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9279 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Kusumit Ghoderao | 9ffd397 | 2023-12-01 16:40:13 +0530 | [diff] [blame] | 9280 | unsigned char output_buffer[32]; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9281 | size_t expected_capacity = requested_capacity; |
| 9282 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9283 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9284 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9285 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9286 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9287 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9288 | psa_set_key_algorithm(&attributes, alg); |
| 9289 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9290 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9291 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 9292 | &key)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9293 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9294 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 9295 | input1->x, input1->len, |
| 9296 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9297 | requested_capacity, 0)) { |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 9298 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9299 | } |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 9300 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9301 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 9302 | ¤t_capacity)); |
| 9303 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9304 | |
| 9305 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9306 | while (current_capacity > 0) { |
| 9307 | size_t read_size = sizeof(output_buffer); |
| 9308 | if (read_size > current_capacity) { |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9309 | read_size = current_capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9310 | } |
| 9311 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9312 | output_buffer, |
| 9313 | read_size)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9314 | expected_capacity -= read_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9315 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 9316 | ¤t_capacity)); |
| 9317 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9318 | } |
| 9319 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9320 | /* Check that the operation refuses to go over capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9321 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1), |
| 9322 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9323 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9324 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9325 | |
| 9326 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9327 | psa_key_derivation_abort(&operation); |
| 9328 | psa_destroy_key(key); |
| 9329 | PSA_DONE(); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9330 | } |
| 9331 | /* END_CASE */ |
| 9332 | |
Stephan Koch | 78109f5 | 2023-04-12 14:19:36 +0200 | [diff] [blame] | 9333 | /* 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] | 9334 | void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg, |
| 9335 | int derivation_step, |
| 9336 | int capacity, int expected_capacity_status_arg, |
| 9337 | data_t *expected_output, |
| 9338 | int expected_output_status_arg) |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9339 | { |
| 9340 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 9341 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | d378504 | 2022-09-16 06:45:44 -0400 | [diff] [blame] | 9342 | 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] | 9343 | uint8_t *output_buffer = NULL; |
| 9344 | psa_status_t status; |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 9345 | psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg; |
| 9346 | psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg; |
| 9347 | 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] | 9348 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9349 | TEST_CALLOC(output_buffer, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9350 | PSA_ASSERT(psa_crypto_init()); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9352 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9353 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 9354 | expected_capacity_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9356 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 9357 | step, input->x, input->len), |
| 9358 | expected_input_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9359 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9360 | if (((psa_status_t) expected_input_status) != PSA_SUCCESS) { |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9361 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9362 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9363 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9364 | status = psa_key_derivation_output_bytes(&operation, output_buffer, |
| 9365 | expected_output->len); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9367 | TEST_EQUAL(status, expected_output_status); |
| 9368 | if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9369 | TEST_MEMORY_COMPARE(output_buffer, expected_output->len, expected_output->x, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9370 | expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9371 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9372 | |
| 9373 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9374 | mbedtls_free(output_buffer); |
| 9375 | psa_key_derivation_abort(&operation); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9376 | PSA_DONE(); |
| 9377 | } |
| 9378 | /* END_CASE */ |
| 9379 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9380 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9381 | void derive_key_exercise(int alg_arg, |
| 9382 | data_t *key_data, |
| 9383 | data_t *input1, |
| 9384 | data_t *input2, |
| 9385 | int derived_type_arg, |
| 9386 | int derived_bits_arg, |
| 9387 | int derived_usage_arg, |
| 9388 | int derived_alg_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9389 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9390 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9391 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9392 | psa_algorithm_t alg = alg_arg; |
| 9393 | psa_key_type_t derived_type = derived_type_arg; |
| 9394 | size_t derived_bits = derived_bits_arg; |
| 9395 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 9396 | psa_algorithm_t derived_alg = derived_alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9397 | size_t capacity = PSA_BITS_TO_BYTES(derived_bits); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9398 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9399 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 9400 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9401 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9402 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9403 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9404 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9405 | psa_set_key_algorithm(&attributes, alg); |
| 9406 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
| 9407 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 9408 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9409 | |
| 9410 | /* Derive a key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9411 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9412 | input1->x, input1->len, |
| 9413 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9414 | capacity, 0)) { |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9415 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9416 | } |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9417 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9418 | psa_set_key_usage_flags(&attributes, derived_usage); |
| 9419 | psa_set_key_algorithm(&attributes, derived_alg); |
| 9420 | psa_set_key_type(&attributes, derived_type); |
| 9421 | psa_set_key_bits(&attributes, derived_bits); |
| 9422 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation, |
| 9423 | &derived_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9424 | |
| 9425 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9426 | PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes)); |
| 9427 | TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type); |
| 9428 | TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9429 | |
| 9430 | /* Exercise the derived key. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 9431 | 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] | 9432 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9433 | } |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9434 | |
| 9435 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9436 | /* |
| 9437 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9438 | * thus reset them as required. |
| 9439 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9440 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9441 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9442 | psa_key_derivation_abort(&operation); |
| 9443 | psa_destroy_key(base_key); |
| 9444 | psa_destroy_key(derived_key); |
| 9445 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9446 | } |
| 9447 | /* END_CASE */ |
| 9448 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9449 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9450 | void derive_key_export(int alg_arg, |
| 9451 | data_t *key_data, |
| 9452 | data_t *input1, |
| 9453 | data_t *input2, |
| 9454 | int bytes1_arg, |
| 9455 | int bytes2_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9456 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9457 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9458 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9459 | psa_algorithm_t alg = alg_arg; |
| 9460 | size_t bytes1 = bytes1_arg; |
| 9461 | size_t bytes2 = bytes2_arg; |
| 9462 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9463 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9464 | uint8_t *output_buffer = NULL; |
| 9465 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9466 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9467 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9468 | size_t length; |
| 9469 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9470 | TEST_CALLOC(output_buffer, capacity); |
| 9471 | TEST_CALLOC(export_buffer, capacity); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9472 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9473 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9474 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9475 | psa_set_key_algorithm(&base_attributes, alg); |
| 9476 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9477 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9478 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9479 | |
| 9480 | /* Derive some material and output it. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9481 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9482 | input1->x, input1->len, |
| 9483 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9484 | capacity, 0)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9485 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9486 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9487 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9488 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9489 | output_buffer, |
| 9490 | capacity)); |
| 9491 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9492 | |
| 9493 | /* 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] | 9494 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9495 | input1->x, input1->len, |
| 9496 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9497 | capacity, 0)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9498 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9499 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9500 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9501 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9502 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9503 | psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA); |
| 9504 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1)); |
| 9505 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9506 | &derived_key)); |
| 9507 | PSA_ASSERT(psa_export_key(derived_key, |
| 9508 | export_buffer, bytes1, |
| 9509 | &length)); |
| 9510 | TEST_EQUAL(length, bytes1); |
| 9511 | PSA_ASSERT(psa_destroy_key(derived_key)); |
| 9512 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2)); |
| 9513 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9514 | &derived_key)); |
| 9515 | PSA_ASSERT(psa_export_key(derived_key, |
| 9516 | export_buffer + bytes1, bytes2, |
| 9517 | &length)); |
| 9518 | TEST_EQUAL(length, bytes2); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9519 | |
| 9520 | /* Compare the outputs from the two runs. */ |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9521 | TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9522 | export_buffer, capacity); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9523 | |
| 9524 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9525 | mbedtls_free(output_buffer); |
| 9526 | mbedtls_free(export_buffer); |
| 9527 | psa_key_derivation_abort(&operation); |
| 9528 | psa_destroy_key(base_key); |
| 9529 | psa_destroy_key(derived_key); |
| 9530 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9531 | } |
| 9532 | /* END_CASE */ |
| 9533 | |
| 9534 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9535 | void derive_key_type(int alg_arg, |
| 9536 | data_t *key_data, |
| 9537 | data_t *input1, |
| 9538 | data_t *input2, |
| 9539 | int key_type_arg, int bits_arg, |
| 9540 | data_t *expected_export) |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9541 | { |
| 9542 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9543 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9544 | const psa_algorithm_t alg = alg_arg; |
| 9545 | const psa_key_type_t key_type = key_type_arg; |
| 9546 | const size_t bits = bits_arg; |
| 9547 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9548 | const size_t export_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9549 | PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9550 | uint8_t *export_buffer = NULL; |
| 9551 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9552 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9553 | size_t export_length; |
| 9554 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9555 | TEST_CALLOC(export_buffer, export_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9556 | PSA_ASSERT(psa_crypto_init()); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9557 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9558 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9559 | psa_set_key_algorithm(&base_attributes, alg); |
| 9560 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9561 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9562 | &base_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9563 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9564 | if (mbedtls_test_psa_setup_key_derivation_wrap( |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9565 | &operation, base_key, alg, |
| 9566 | input1->x, input1->len, |
| 9567 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9568 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY, 0) == 0) { |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9569 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9570 | } |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9571 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9572 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9573 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9574 | psa_set_key_type(&derived_attributes, key_type); |
| 9575 | psa_set_key_bits(&derived_attributes, bits); |
| 9576 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9577 | &derived_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9578 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9579 | PSA_ASSERT(psa_export_key(derived_key, |
| 9580 | export_buffer, export_buffer_size, |
| 9581 | &export_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9582 | TEST_MEMORY_COMPARE(export_buffer, export_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9583 | expected_export->x, expected_export->len); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9584 | |
| 9585 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9586 | mbedtls_free(export_buffer); |
| 9587 | psa_key_derivation_abort(&operation); |
| 9588 | psa_destroy_key(base_key); |
| 9589 | psa_destroy_key(derived_key); |
| 9590 | PSA_DONE(); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9591 | } |
| 9592 | /* END_CASE */ |
| 9593 | |
| 9594 | /* BEGIN_CASE */ |
Gilles Peskine | f36d785 | 2024-06-06 21:11:44 +0200 | [diff] [blame^] | 9595 | void derive_key_custom(int alg_arg, |
| 9596 | data_t *key_data, |
| 9597 | data_t *input1, |
| 9598 | data_t *input2, |
| 9599 | int key_type_arg, int bits_arg, |
| 9600 | int flags_arg, |
| 9601 | data_t *custom_data, |
| 9602 | psa_status_t expected_status, |
| 9603 | data_t *expected_export) |
| 9604 | { |
| 9605 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9606 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9607 | const psa_algorithm_t alg = alg_arg; |
| 9608 | const psa_key_type_t key_type = key_type_arg; |
| 9609 | const size_t bits = bits_arg; |
| 9610 | psa_custom_key_parameters_t custom = PSA_CUSTOM_KEY_PARAMETERS_INIT; |
| 9611 | custom.flags = flags_arg; |
| 9612 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9613 | const size_t export_buffer_size = |
| 9614 | PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits); |
| 9615 | uint8_t *export_buffer = NULL; |
| 9616 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9617 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9618 | size_t export_length; |
| 9619 | |
| 9620 | TEST_CALLOC(export_buffer, export_buffer_size); |
| 9621 | PSA_ASSERT(psa_crypto_init()); |
| 9622 | |
| 9623 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9624 | psa_set_key_algorithm(&base_attributes, alg); |
| 9625 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9626 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9627 | &base_key)); |
| 9628 | |
| 9629 | if (mbedtls_test_psa_setup_key_derivation_wrap( |
| 9630 | &operation, base_key, alg, |
| 9631 | input1->x, input1->len, |
| 9632 | input2->x, input2->len, |
| 9633 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY, 0) == 0) { |
| 9634 | goto exit; |
| 9635 | } |
| 9636 | |
| 9637 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9638 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9639 | psa_set_key_type(&derived_attributes, key_type); |
| 9640 | psa_set_key_bits(&derived_attributes, bits); |
| 9641 | |
| 9642 | TEST_EQUAL(psa_key_derivation_output_key_custom( |
| 9643 | &derived_attributes, &operation, |
| 9644 | &custom, custom_data->x, custom_data->len, |
| 9645 | &derived_key), |
| 9646 | expected_status); |
| 9647 | |
| 9648 | if (expected_status == PSA_SUCCESS) { |
| 9649 | PSA_ASSERT(psa_export_key(derived_key, |
| 9650 | export_buffer, export_buffer_size, |
| 9651 | &export_length)); |
| 9652 | TEST_MEMORY_COMPARE(export_buffer, export_length, |
| 9653 | expected_export->x, expected_export->len); |
| 9654 | } |
| 9655 | |
| 9656 | exit: |
| 9657 | mbedtls_free(export_buffer); |
| 9658 | psa_key_derivation_abort(&operation); |
| 9659 | psa_destroy_key(base_key); |
| 9660 | psa_destroy_key(derived_key); |
| 9661 | PSA_DONE(); |
| 9662 | } |
| 9663 | /* END_CASE */ |
| 9664 | |
| 9665 | /* BEGIN_CASE */ |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9666 | void derive_key_ext(int alg_arg, |
| 9667 | data_t *key_data, |
| 9668 | data_t *input1, |
| 9669 | data_t *input2, |
| 9670 | int key_type_arg, int bits_arg, |
Gilles Peskine | c81393b | 2024-02-14 20:51:28 +0100 | [diff] [blame] | 9671 | int flags_arg, |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 9672 | data_t *params_data, |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9673 | psa_status_t expected_status, |
| 9674 | data_t *expected_export) |
| 9675 | { |
| 9676 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9677 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9678 | const psa_algorithm_t alg = alg_arg; |
| 9679 | const psa_key_type_t key_type = key_type_arg; |
| 9680 | const size_t bits = bits_arg; |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 9681 | psa_key_production_parameters_t *params = NULL; |
| 9682 | size_t params_data_length = 0; |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9683 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9684 | const size_t export_buffer_size = |
| 9685 | PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits); |
| 9686 | uint8_t *export_buffer = NULL; |
| 9687 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9688 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9689 | size_t export_length; |
| 9690 | |
| 9691 | TEST_CALLOC(export_buffer, export_buffer_size); |
| 9692 | PSA_ASSERT(psa_crypto_init()); |
| 9693 | |
| 9694 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9695 | psa_set_key_algorithm(&base_attributes, alg); |
| 9696 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9697 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9698 | &base_key)); |
| 9699 | |
| 9700 | if (mbedtls_test_psa_setup_key_derivation_wrap( |
| 9701 | &operation, base_key, alg, |
| 9702 | input1->x, input1->len, |
| 9703 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9704 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY, 0) == 0) { |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9705 | goto exit; |
| 9706 | } |
| 9707 | |
| 9708 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9709 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9710 | psa_set_key_type(&derived_attributes, key_type); |
| 9711 | psa_set_key_bits(&derived_attributes, bits); |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 9712 | if (!setup_key_production_parameters(¶ms, ¶ms_data_length, |
| 9713 | flags_arg, params_data)) { |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9714 | goto exit; |
| 9715 | } |
| 9716 | |
| 9717 | TEST_EQUAL(psa_key_derivation_output_key_ext(&derived_attributes, &operation, |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 9718 | params, params_data_length, |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9719 | &derived_key), |
| 9720 | expected_status); |
| 9721 | |
| 9722 | if (expected_status == PSA_SUCCESS) { |
| 9723 | PSA_ASSERT(psa_export_key(derived_key, |
| 9724 | export_buffer, export_buffer_size, |
| 9725 | &export_length)); |
| 9726 | TEST_MEMORY_COMPARE(export_buffer, export_length, |
| 9727 | expected_export->x, expected_export->len); |
| 9728 | } |
| 9729 | |
| 9730 | exit: |
| 9731 | mbedtls_free(export_buffer); |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 9732 | mbedtls_free(params); |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9733 | psa_key_derivation_abort(&operation); |
| 9734 | psa_destroy_key(base_key); |
| 9735 | psa_destroy_key(derived_key); |
| 9736 | PSA_DONE(); |
| 9737 | } |
| 9738 | /* END_CASE */ |
| 9739 | |
| 9740 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9741 | void derive_key(int alg_arg, |
| 9742 | data_t *key_data, data_t *input1, data_t *input2, |
| 9743 | int type_arg, int bits_arg, |
| 9744 | int expected_status_arg, |
| 9745 | int is_large_output) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9746 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9747 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9748 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9749 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 9750 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9751 | size_t bits = bits_arg; |
| 9752 | psa_status_t expected_status = expected_status_arg; |
| 9753 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9754 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9755 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9756 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9757 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9758 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9759 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9760 | psa_set_key_algorithm(&base_attributes, alg); |
| 9761 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9762 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9763 | &base_key)); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9764 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9765 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9766 | input1->x, input1->len, |
| 9767 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9768 | SIZE_MAX, 0)) { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9769 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9770 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9771 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9772 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9773 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9774 | psa_set_key_type(&derived_attributes, type); |
| 9775 | psa_set_key_bits(&derived_attributes, bits); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9776 | |
| 9777 | psa_status_t status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9778 | psa_key_derivation_output_key(&derived_attributes, |
| 9779 | &operation, |
| 9780 | &derived_key); |
| 9781 | if (is_large_output > 0) { |
| 9782 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9783 | } |
| 9784 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9785 | |
| 9786 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9787 | psa_key_derivation_abort(&operation); |
| 9788 | psa_destroy_key(base_key); |
| 9789 | psa_destroy_key(derived_key); |
| 9790 | PSA_DONE(); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9791 | } |
| 9792 | /* END_CASE */ |
| 9793 | |
| 9794 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9795 | void key_agreement_setup(int alg_arg, |
| 9796 | int our_key_type_arg, int our_key_alg_arg, |
| 9797 | data_t *our_key_data, data_t *peer_key_data, |
| 9798 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9799 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9800 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9801 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 9802 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9803 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9804 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9805 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9806 | psa_status_t expected_status = expected_status_arg; |
| 9807 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9808 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9809 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9810 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9811 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9812 | psa_set_key_algorithm(&attributes, our_key_alg); |
| 9813 | psa_set_key_type(&attributes, our_key_type); |
| 9814 | PSA_ASSERT(psa_import_key(&attributes, |
| 9815 | our_key_data->x, our_key_data->len, |
| 9816 | &our_key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9817 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9818 | /* The tests currently include inputs that should fail at either step. |
| 9819 | * Test cases that fail at the setup step should be changed to call |
| 9820 | * key_derivation_setup instead, and this function should be renamed |
| 9821 | * to key_agreement_fail. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9822 | status = psa_key_derivation_setup(&operation, alg); |
| 9823 | if (status == PSA_SUCCESS) { |
| 9824 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 9825 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 9826 | our_key, |
| 9827 | peer_key_data->x, peer_key_data->len), |
| 9828 | expected_status); |
| 9829 | } else { |
| 9830 | TEST_ASSERT(status == expected_status); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9831 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9832 | |
| 9833 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9834 | psa_key_derivation_abort(&operation); |
| 9835 | psa_destroy_key(our_key); |
| 9836 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9837 | } |
| 9838 | /* END_CASE */ |
| 9839 | |
| 9840 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9841 | void raw_key_agreement(int alg_arg, |
| 9842 | int our_key_type_arg, data_t *our_key_data, |
| 9843 | data_t *peer_key_data, |
| 9844 | data_t *expected_output) |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9845 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9846 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9847 | psa_algorithm_t alg = alg_arg; |
| 9848 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9849 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9850 | unsigned char *output = NULL; |
| 9851 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9852 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9853 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9854 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9855 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9856 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9857 | psa_set_key_algorithm(&attributes, alg); |
| 9858 | psa_set_key_type(&attributes, our_key_type); |
| 9859 | PSA_ASSERT(psa_import_key(&attributes, |
| 9860 | our_key_data->x, our_key_data->len, |
| 9861 | &our_key)); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9862 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9863 | PSA_ASSERT(psa_get_key_attributes(our_key, &attributes)); |
| 9864 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9865 | |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9866 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9867 | TEST_LE_U(expected_output->len, |
| 9868 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits)); |
| 9869 | TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits), |
| 9870 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9871 | |
| 9872 | /* Good case with exact output size */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9873 | TEST_CALLOC(output, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9874 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9875 | peer_key_data->x, peer_key_data->len, |
| 9876 | output, expected_output->len, |
| 9877 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9878 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9879 | expected_output->x, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9880 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9881 | output = NULL; |
| 9882 | output_length = ~0; |
| 9883 | |
| 9884 | /* Larger buffer */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9885 | TEST_CALLOC(output, expected_output->len + 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9886 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9887 | peer_key_data->x, peer_key_data->len, |
| 9888 | output, expected_output->len + 1, |
| 9889 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9890 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9891 | expected_output->x, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9892 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9893 | output = NULL; |
| 9894 | output_length = ~0; |
| 9895 | |
| 9896 | /* Buffer too small */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9897 | TEST_CALLOC(output, expected_output->len - 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9898 | TEST_EQUAL(psa_raw_key_agreement(alg, our_key, |
| 9899 | peer_key_data->x, peer_key_data->len, |
| 9900 | output, expected_output->len - 1, |
| 9901 | &output_length), |
| 9902 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9903 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9904 | TEST_LE_U(output_length, expected_output->len - 1); |
| 9905 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9906 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9907 | |
| 9908 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9909 | mbedtls_free(output); |
| 9910 | psa_destroy_key(our_key); |
| 9911 | PSA_DONE(); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9912 | } |
| 9913 | /* END_CASE */ |
| 9914 | |
| 9915 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9916 | void key_agreement_capacity(int alg_arg, |
| 9917 | int our_key_type_arg, data_t *our_key_data, |
| 9918 | data_t *peer_key_data, |
| 9919 | int expected_capacity_arg) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9920 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9921 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9922 | psa_algorithm_t alg = alg_arg; |
| 9923 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9924 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9925 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9926 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9927 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9928 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9929 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9930 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9931 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9932 | psa_set_key_algorithm(&attributes, alg); |
| 9933 | psa_set_key_type(&attributes, our_key_type); |
| 9934 | PSA_ASSERT(psa_import_key(&attributes, |
| 9935 | our_key_data->x, our_key_data->len, |
| 9936 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9937 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9938 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9939 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9940 | &operation, |
| 9941 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9942 | peer_key_data->x, peer_key_data->len)); |
| 9943 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9944 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9945 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9946 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9947 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9948 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9949 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 9950 | /* Test the advertised capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9951 | PSA_ASSERT(psa_key_derivation_get_capacity( |
| 9952 | &operation, &actual_capacity)); |
| 9953 | TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9954 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9955 | /* Test the actual capacity by reading the output. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9956 | while (actual_capacity > sizeof(output)) { |
| 9957 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9958 | output, sizeof(output))); |
| 9959 | actual_capacity -= sizeof(output); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9960 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9961 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9962 | output, actual_capacity)); |
| 9963 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1), |
| 9964 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9965 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9966 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9967 | psa_key_derivation_abort(&operation); |
| 9968 | psa_destroy_key(our_key); |
| 9969 | PSA_DONE(); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9970 | } |
| 9971 | /* END_CASE */ |
| 9972 | |
Valerio Setti | ad81967 | 2023-12-29 12:14:41 +0100 | [diff] [blame] | 9973 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */ |
| 9974 | 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] | 9975 | { |
| 9976 | mbedtls_ecp_group_id grp_id = grp_id_arg; |
Valerio Setti | ad81967 | 2023-12-29 12:14:41 +0100 | [diff] [blame] | 9977 | psa_ecc_family_t ecc_family = psa_family_arg; |
| 9978 | size_t bits = bits_arg; |
| 9979 | size_t bits_tmp; |
Valerio Setti | bf999cb | 2023-12-28 17:48:13 +0100 | [diff] [blame] | 9980 | |
Valerio Setti | ad81967 | 2023-12-29 12:14:41 +0100 | [diff] [blame] | 9981 | TEST_EQUAL(ecc_family, mbedtls_ecc_group_to_psa(grp_id, &bits_tmp)); |
| 9982 | TEST_EQUAL(bits, bits_tmp); |
Valerio Setti | ac73952 | 2024-01-04 10:22:01 +0100 | [diff] [blame] | 9983 | TEST_EQUAL(grp_id, mbedtls_ecc_group_from_psa(ecc_family, bits)); |
Valerio Setti | bf999cb | 2023-12-28 17:48:13 +0100 | [diff] [blame] | 9984 | } |
| 9985 | /* END_CASE */ |
| 9986 | |
Valerio Setti | ac73952 | 2024-01-04 10:22:01 +0100 | [diff] [blame] | 9987 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */ |
| 9988 | void ecc_conversion_functions_fail() |
| 9989 | { |
| 9990 | size_t bits; |
| 9991 | |
Valerio Setti | db6e029 | 2024-01-05 10:15:45 +0100 | [diff] [blame] | 9992 | /* Invalid legacy curve identifiers. */ |
| 9993 | TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_MAX, &bits)); |
| 9994 | TEST_EQUAL(0, bits); |
Valerio Setti | ac73952 | 2024-01-04 10:22:01 +0100 | [diff] [blame] | 9995 | TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_NONE, &bits)); |
| 9996 | TEST_EQUAL(0, bits); |
| 9997 | |
| 9998 | /* Invalid PSA EC family. */ |
| 9999 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(0, 192)); |
| 10000 | /* Invalid bit-size for a valid EC family. */ |
| 10001 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_SECP_R1, 512)); |
| 10002 | |
| 10003 | /* Twisted-Edward curves are not supported yet. */ |
| 10004 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, |
| 10005 | mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 255)); |
| 10006 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, |
| 10007 | mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 448)); |
| 10008 | } |
| 10009 | /* END_CASE */ |
| 10010 | |
| 10011 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 10012 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10013 | void key_agreement_output(int alg_arg, |
| 10014 | int our_key_type_arg, data_t *our_key_data, |
| 10015 | data_t *peer_key_data, |
| 10016 | data_t *expected_output1, data_t *expected_output2) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 10017 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 10018 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 10019 | psa_algorithm_t alg = alg_arg; |
| 10020 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 10021 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 10022 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 10023 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 10024 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 10025 | TEST_CALLOC(actual_output, MAX(expected_output1->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 10026 | expected_output2->len)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 10027 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10028 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 10029 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10030 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10031 | psa_set_key_algorithm(&attributes, alg); |
| 10032 | psa_set_key_type(&attributes, our_key_type); |
| 10033 | PSA_ASSERT(psa_import_key(&attributes, |
| 10034 | our_key_data->x, our_key_data->len, |
| 10035 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 10036 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10037 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 10038 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 10039 | &operation, |
| 10040 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 10041 | peer_key_data->x, peer_key_data->len)); |
| 10042 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 10043 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10044 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 10045 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 10046 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 10047 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 10048 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10049 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 10050 | actual_output, |
| 10051 | expected_output1->len)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 10052 | TEST_MEMORY_COMPARE(actual_output, expected_output1->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 10053 | expected_output1->x, expected_output1->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10054 | if (expected_output2->len != 0) { |
| 10055 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 10056 | actual_output, |
| 10057 | expected_output2->len)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 10058 | TEST_MEMORY_COMPARE(actual_output, expected_output2->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 10059 | expected_output2->x, expected_output2->len); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 10060 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 10061 | |
| 10062 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10063 | psa_key_derivation_abort(&operation); |
| 10064 | psa_destroy_key(our_key); |
| 10065 | PSA_DONE(); |
| 10066 | mbedtls_free(actual_output); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 10067 | } |
| 10068 | /* END_CASE */ |
| 10069 | |
| 10070 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10071 | void generate_random(int bytes_arg) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 10072 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10073 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 10074 | unsigned char *output = NULL; |
| 10075 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10076 | size_t i; |
| 10077 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 10078 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10079 | TEST_ASSERT(bytes_arg >= 0); |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 10080 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 10081 | TEST_CALLOC(output, bytes); |
| 10082 | TEST_CALLOC(changed, bytes); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 10083 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10084 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 10085 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10086 | /* Run several times, to ensure that every output byte will be |
| 10087 | * nonzero at least once with overwhelming probability |
| 10088 | * (2^(-8*number_of_runs)). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10089 | for (run = 0; run < 10; run++) { |
| 10090 | if (bytes != 0) { |
| 10091 | memset(output, 0, bytes); |
| 10092 | } |
| 10093 | PSA_ASSERT(psa_generate_random(output, bytes)); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10094 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10095 | for (i = 0; i < bytes; i++) { |
| 10096 | if (output[i] != 0) { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10097 | ++changed[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10098 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10099 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 10100 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10101 | |
| 10102 | /* Check that every byte was changed to nonzero at least once. This |
| 10103 | * validates that psa_generate_random is overwriting every byte of |
| 10104 | * the output buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10105 | for (i = 0; i < bytes; i++) { |
| 10106 | TEST_ASSERT(changed[i] != 0); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10107 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 10108 | |
| 10109 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10110 | PSA_DONE(); |
| 10111 | mbedtls_free(output); |
| 10112 | mbedtls_free(changed); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 10113 | } |
| 10114 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10115 | |
Ryan | 3a1b786 | 2024-03-01 17:24:04 +0000 | [diff] [blame] | 10116 | #if defined MBEDTLS_THREADING_PTHREAD |
| 10117 | |
| 10118 | /* BEGIN_CASE depends_on:MBEDTLS_THREADING_PTHREAD */ |
| 10119 | void concurrently_generate_keys(int type_arg, |
| 10120 | int bits_arg, |
| 10121 | int usage_arg, |
| 10122 | int alg_arg, |
| 10123 | int expected_status_arg, |
| 10124 | int is_large_key_arg, |
| 10125 | int arg_thread_count, |
| 10126 | int reps_arg) |
| 10127 | { |
| 10128 | size_t thread_count = (size_t) arg_thread_count; |
| 10129 | mbedtls_test_thread_t *threads = NULL; |
| 10130 | generate_key_context gkc; |
| 10131 | gkc.type = type_arg; |
| 10132 | gkc.usage = usage_arg; |
| 10133 | gkc.bits = bits_arg; |
| 10134 | gkc.alg = alg_arg; |
| 10135 | gkc.expected_status = expected_status_arg; |
| 10136 | gkc.is_large_key = is_large_key_arg; |
| 10137 | gkc.reps = reps_arg; |
| 10138 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10139 | |
| 10140 | PSA_ASSERT(psa_crypto_init()); |
| 10141 | |
| 10142 | psa_set_key_usage_flags(&attributes, usage_arg); |
| 10143 | psa_set_key_algorithm(&attributes, alg_arg); |
| 10144 | psa_set_key_type(&attributes, type_arg); |
| 10145 | psa_set_key_bits(&attributes, bits_arg); |
| 10146 | gkc.attributes = &attributes; |
| 10147 | |
| 10148 | TEST_CALLOC(threads, sizeof(mbedtls_test_thread_t) * thread_count); |
| 10149 | |
| 10150 | /* Split threads to generate key then destroy key. */ |
| 10151 | for (size_t i = 0; i < thread_count; i++) { |
| 10152 | TEST_EQUAL( |
| 10153 | mbedtls_test_thread_create(&threads[i], thread_generate_key, |
| 10154 | (void *) &gkc), 0); |
| 10155 | } |
| 10156 | |
| 10157 | /* Join threads. */ |
| 10158 | for (size_t i = 0; i < thread_count; i++) { |
| 10159 | TEST_EQUAL(mbedtls_test_thread_join(&threads[i]), 0); |
| 10160 | } |
| 10161 | |
| 10162 | exit: |
| 10163 | mbedtls_free(threads); |
| 10164 | PSA_DONE(); |
| 10165 | } |
| 10166 | /* END_CASE */ |
| 10167 | #endif |
| 10168 | |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10169 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10170 | void generate_key(int type_arg, |
| 10171 | int bits_arg, |
| 10172 | int usage_arg, |
| 10173 | int alg_arg, |
| 10174 | int expected_status_arg, |
| 10175 | int is_large_key) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10176 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 10177 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10178 | psa_key_type_t type = type_arg; |
| 10179 | psa_key_usage_t usage = usage_arg; |
| 10180 | size_t bits = bits_arg; |
| 10181 | psa_algorithm_t alg = alg_arg; |
| 10182 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 10183 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 10184 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10185 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10186 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10187 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10188 | psa_set_key_usage_flags(&attributes, usage); |
| 10189 | psa_set_key_algorithm(&attributes, alg); |
| 10190 | psa_set_key_type(&attributes, type); |
| 10191 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10192 | |
| 10193 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10194 | psa_status_t status = psa_generate_key(&attributes, &key); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 10195 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10196 | if (is_large_key > 0) { |
| 10197 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 10198 | } |
| 10199 | TEST_EQUAL(status, expected_status); |
| 10200 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 10201 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10202 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10203 | |
| 10204 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10205 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 10206 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 10207 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10208 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 10209 | /* Do something with the key according to its type and permitted usage. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 10210 | if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 10211 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10212 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10213 | |
| 10214 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 10215 | /* |
| 10216 | * Key attributes may have been returned by psa_get_key_attributes() |
| 10217 | * thus reset them as required. |
| 10218 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10219 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 10220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10221 | psa_destroy_key(key); |
| 10222 | PSA_DONE(); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10223 | } |
| 10224 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 10225 | |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10226 | /* BEGIN_CASE */ |
Gilles Peskine | f36d785 | 2024-06-06 21:11:44 +0200 | [diff] [blame^] | 10227 | void generate_key_custom(int type_arg, |
| 10228 | int bits_arg, |
| 10229 | int usage_arg, |
| 10230 | int alg_arg, |
| 10231 | int flags_arg, |
| 10232 | data_t *custom_data, |
| 10233 | int expected_status_arg) |
| 10234 | { |
| 10235 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10236 | psa_key_type_t type = type_arg; |
| 10237 | psa_key_usage_t usage = usage_arg; |
| 10238 | size_t bits = bits_arg; |
| 10239 | psa_algorithm_t alg = alg_arg; |
| 10240 | psa_status_t expected_status = expected_status_arg; |
| 10241 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10242 | psa_custom_key_parameters_t custom = PSA_CUSTOM_KEY_PARAMETERS_INIT; |
| 10243 | custom.flags = flags_arg; |
| 10244 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10245 | |
| 10246 | PSA_ASSERT(psa_crypto_init()); |
| 10247 | |
| 10248 | psa_set_key_usage_flags(&attributes, usage); |
| 10249 | psa_set_key_algorithm(&attributes, alg); |
| 10250 | psa_set_key_type(&attributes, type); |
| 10251 | psa_set_key_bits(&attributes, bits); |
| 10252 | |
| 10253 | /* Generate a key */ |
| 10254 | psa_status_t status = |
| 10255 | psa_generate_key_custom(&attributes, |
| 10256 | &custom, custom_data->x, custom_data->len, |
| 10257 | &key); |
| 10258 | |
| 10259 | TEST_EQUAL(status, expected_status); |
| 10260 | if (expected_status != PSA_SUCCESS) { |
| 10261 | goto exit; |
| 10262 | } |
| 10263 | |
| 10264 | /* Test the key information */ |
| 10265 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 10266 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 10267 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
| 10268 | |
| 10269 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) |
| 10270 | if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
| 10271 | TEST_ASSERT(rsa_test_e(key, bits, custom_data)); |
| 10272 | } |
| 10273 | #endif |
| 10274 | |
| 10275 | /* Do something with the key according to its type and permitted usage. */ |
| 10276 | if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) { |
| 10277 | goto exit; |
| 10278 | } |
| 10279 | |
| 10280 | exit: |
| 10281 | /* |
| 10282 | * Key attributes may have been returned by psa_get_key_attributes() |
| 10283 | * thus reset them as required. |
| 10284 | */ |
| 10285 | psa_reset_key_attributes(&got_attributes); |
| 10286 | psa_destroy_key(key); |
| 10287 | PSA_DONE(); |
| 10288 | } |
| 10289 | /* END_CASE */ |
| 10290 | |
| 10291 | /* BEGIN_CASE */ |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10292 | void generate_key_ext(int type_arg, |
| 10293 | int bits_arg, |
| 10294 | int usage_arg, |
| 10295 | int alg_arg, |
Gilles Peskine | c81393b | 2024-02-14 20:51:28 +0100 | [diff] [blame] | 10296 | int flags_arg, |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10297 | data_t *params_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10298 | int expected_status_arg) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10299 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 10300 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10301 | psa_key_type_t type = type_arg; |
| 10302 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10303 | size_t bits = bits_arg; |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10304 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10305 | psa_status_t expected_status = expected_status_arg; |
| 10306 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10307 | psa_key_production_parameters_t *params = NULL; |
| 10308 | size_t params_data_length = 0; |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10309 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10310 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10311 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10312 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10313 | psa_set_key_usage_flags(&attributes, usage); |
| 10314 | psa_set_key_algorithm(&attributes, alg); |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10315 | psa_set_key_type(&attributes, type); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10316 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10317 | |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10318 | if (!setup_key_production_parameters(¶ms, ¶ms_data_length, |
| 10319 | flags_arg, params_data)) { |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10320 | goto exit; |
| 10321 | } |
| 10322 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10323 | /* Generate a key */ |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10324 | psa_status_t status = psa_generate_key_ext(&attributes, |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10325 | params, params_data_length, |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10326 | &key); |
| 10327 | |
| 10328 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10329 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10330 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10331 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10332 | |
| 10333 | /* Test the key information */ |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10334 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 10335 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 10336 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Pengyu Lv | d90fbf7 | 2023-12-08 17:13:22 +0800 | [diff] [blame] | 10337 | |
Gilles Peskine | 7a18f96 | 2024-02-12 16:48:11 +0100 | [diff] [blame] | 10338 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) |
| 10339 | if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10340 | TEST_ASSERT(rsa_test_e(key, bits, params_data)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10341 | } |
Pengyu Lv | 9e976f3 | 2023-12-06 16:58:05 +0800 | [diff] [blame] | 10342 | #endif |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10343 | |
| 10344 | /* Do something with the key according to its type and permitted usage. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 10345 | if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10346 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10347 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10348 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10349 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 10350 | /* |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10351 | * Key attributes may have been returned by psa_get_key_attributes() |
| 10352 | * thus reset them as required. |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 10353 | */ |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10354 | psa_reset_key_attributes(&got_attributes); |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10355 | mbedtls_free(params); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10356 | psa_destroy_key(key); |
| 10357 | PSA_DONE(); |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10358 | } |
| 10359 | /* END_CASE */ |
| 10360 | |
| 10361 | /* BEGIN_CASE */ |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10362 | void key_production_parameters_init() |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10363 | { |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10364 | psa_key_production_parameters_t init = PSA_KEY_PRODUCTION_PARAMETERS_INIT; |
| 10365 | psa_key_production_parameters_t zero; |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10366 | memset(&zero, 0, sizeof(zero)); |
| 10367 | |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10368 | TEST_EQUAL(init.flags, 0); |
| 10369 | TEST_EQUAL(zero.flags, 0); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10370 | } |
| 10371 | /* END_CASE */ |
| 10372 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10373 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10374 | void persistent_key_load_key_from_storage(data_t *data, |
| 10375 | int type_arg, int bits_arg, |
| 10376 | int usage_flags_arg, int alg_arg, |
| 10377 | int generation_method) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10378 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10379 | 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] | 10380 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 10381 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10382 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10383 | psa_key_type_t type = type_arg; |
| 10384 | size_t bits = bits_arg; |
| 10385 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 10386 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 10387 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10388 | unsigned char *first_export = NULL; |
| 10389 | unsigned char *second_export = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10390 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits); |
Sergey | bef1f63 | 2023-03-06 15:25:06 -0700 | [diff] [blame] | 10391 | size_t first_exported_length = 0; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10392 | size_t second_exported_length; |
| 10393 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10394 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 10395 | TEST_CALLOC(first_export, export_size); |
| 10396 | TEST_CALLOC(second_export, export_size); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10397 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10398 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10399 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10400 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10401 | psa_set_key_id(&attributes, key_id); |
| 10402 | psa_set_key_usage_flags(&attributes, usage_flags); |
| 10403 | psa_set_key_algorithm(&attributes, alg); |
| 10404 | psa_set_key_type(&attributes, type); |
| 10405 | psa_set_key_bits(&attributes, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10406 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10407 | switch (generation_method) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10408 | case IMPORT_KEY: |
| 10409 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10410 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, |
| 10411 | &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10412 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10413 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10414 | case GENERATE_KEY: |
| 10415 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10416 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10417 | break; |
| 10418 | |
| 10419 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 10420 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10421 | { |
| 10422 | /* Create base key */ |
| 10423 | psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256); |
| 10424 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10425 | psa_set_key_usage_flags(&base_attributes, |
| 10426 | PSA_KEY_USAGE_DERIVE); |
| 10427 | psa_set_key_algorithm(&base_attributes, derive_alg); |
| 10428 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 10429 | PSA_ASSERT(psa_import_key(&base_attributes, |
| 10430 | data->x, data->len, |
| 10431 | &base_key)); |
| 10432 | /* Derive a key. */ |
| 10433 | PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg)); |
| 10434 | PSA_ASSERT(psa_key_derivation_input_key( |
| 10435 | &operation, |
| 10436 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key)); |
| 10437 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 10438 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
| 10439 | NULL, 0)); |
| 10440 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, |
| 10441 | &operation, |
| 10442 | &key)); |
| 10443 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
| 10444 | PSA_ASSERT(psa_destroy_key(base_key)); |
| 10445 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10446 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 10447 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10448 | TEST_ASSUME(!"KDF not supported in this configuration"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 10449 | #endif |
| 10450 | break; |
| 10451 | |
| 10452 | default: |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 10453 | TEST_FAIL("generation_method not implemented in test"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 10454 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10455 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10456 | psa_reset_key_attributes(&attributes); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10457 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10458 | /* Export the key if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10459 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 10460 | PSA_ASSERT(psa_export_key(key, |
| 10461 | first_export, export_size, |
| 10462 | &first_exported_length)); |
| 10463 | if (generation_method == IMPORT_KEY) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 10464 | TEST_MEMORY_COMPARE(data->x, data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 10465 | first_export, first_exported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10466 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10467 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10468 | |
| 10469 | /* Shutdown and restart */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10470 | PSA_ASSERT(psa_purge_key(key)); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 10471 | PSA_DONE(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10472 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10473 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10474 | /* Check key slot still contains key data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10475 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 10476 | TEST_ASSERT(mbedtls_svc_key_id_equal( |
| 10477 | psa_get_key_id(&attributes), key_id)); |
| 10478 | TEST_EQUAL(psa_get_key_lifetime(&attributes), |
| 10479 | PSA_KEY_LIFETIME_PERSISTENT); |
| 10480 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 10481 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 10482 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 10483 | mbedtls_test_update_key_usage_flags(usage_flags)); |
| 10484 | TEST_EQUAL(psa_get_key_algorithm(&attributes), alg); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10485 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10486 | /* Export the key again if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10487 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 10488 | PSA_ASSERT(psa_export_key(key, |
| 10489 | second_export, export_size, |
| 10490 | &second_exported_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 10491 | TEST_MEMORY_COMPARE(first_export, first_exported_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 10492 | second_export, second_exported_length); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10493 | } |
| 10494 | |
| 10495 | /* Do something with the key according to its type and permitted usage. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 10496 | if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg, 0)) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10497 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10498 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10499 | |
| 10500 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 10501 | /* |
| 10502 | * Key attributes may have been returned by psa_get_key_attributes() |
| 10503 | * thus reset them as required. |
| 10504 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10505 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 10506 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10507 | mbedtls_free(first_export); |
| 10508 | mbedtls_free(second_export); |
| 10509 | psa_key_derivation_abort(&operation); |
| 10510 | psa_destroy_key(base_key); |
| 10511 | psa_destroy_key(key); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 10512 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10513 | } |
| 10514 | /* END_CASE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10515 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 10516 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10517 | void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg, |
| 10518 | int primitive_arg, int hash_arg, int role_arg, |
| 10519 | int test_input, data_t *pw_data, |
| 10520 | int inj_err_type_arg, |
| 10521 | int expected_error_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10522 | { |
| 10523 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10524 | psa_pake_operation_t operation = psa_pake_operation_init(); |
| 10525 | psa_algorithm_t alg = alg_arg; |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 10526 | psa_pake_primitive_t primitive = primitive_arg; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 10527 | psa_key_type_t key_type_pw = key_type_pw_arg; |
| 10528 | psa_key_usage_t key_usage_pw = key_usage_pw_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10529 | psa_algorithm_t hash_alg = hash_arg; |
| 10530 | psa_pake_role_t role = role_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10531 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10532 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10533 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
| 10534 | psa_status_t expected_error = expected_error_arg; |
| 10535 | psa_status_t status; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10536 | unsigned char *output_buffer = NULL; |
| 10537 | size_t output_len = 0; |
| 10538 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10539 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10540 | |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 10541 | size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10542 | PSA_PAKE_STEP_KEY_SHARE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 10543 | TEST_CALLOC(output_buffer, buf_size); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10544 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10545 | if (pw_data->len > 0) { |
| 10546 | psa_set_key_usage_flags(&attributes, key_usage_pw); |
| 10547 | psa_set_key_algorithm(&attributes, alg); |
| 10548 | psa_set_key_type(&attributes, key_type_pw); |
| 10549 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10550 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10551 | } |
| 10552 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10553 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10554 | psa_pake_cs_set_primitive(&cipher_suite, primitive); |
| 10555 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10556 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10557 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 10558 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10559 | if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) { |
| 10560 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 10561 | expected_error); |
| 10562 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10563 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 10564 | expected_error); |
| 10565 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10566 | TEST_EQUAL(psa_pake_set_password_key(&operation, key), |
| 10567 | expected_error); |
| 10568 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10569 | TEST_EQUAL(psa_pake_set_role(&operation, role), |
| 10570 | expected_error); |
| 10571 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10572 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10573 | NULL, 0, NULL), |
| 10574 | expected_error); |
| 10575 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10576 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0), |
| 10577 | expected_error); |
| 10578 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10579 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10580 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10581 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10582 | status = psa_pake_setup(&operation, &cipher_suite); |
| 10583 | if (status != PSA_SUCCESS) { |
| 10584 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10585 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10586 | } |
| 10587 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10588 | if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) { |
| 10589 | TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite), |
| 10590 | expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10591 | goto exit; |
| 10592 | } |
| 10593 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10594 | status = psa_pake_set_role(&operation, role); |
| 10595 | if (status != PSA_SUCCESS) { |
| 10596 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10597 | goto exit; |
| 10598 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10599 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10600 | if (pw_data->len > 0) { |
| 10601 | status = psa_pake_set_password_key(&operation, key); |
| 10602 | if (status != PSA_SUCCESS) { |
| 10603 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10604 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10605 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10606 | } |
| 10607 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10608 | if (inj_err_type == INJECT_ERR_INVALID_USER) { |
| 10609 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 10610 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10611 | goto exit; |
| 10612 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 10613 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10614 | if (inj_err_type == INJECT_ERR_INVALID_PEER) { |
| 10615 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 10616 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10617 | goto exit; |
| 10618 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 10619 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10620 | if (inj_err_type == INJECT_ERR_SET_USER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10621 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10622 | TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4), |
| 10623 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10624 | goto exit; |
| 10625 | } |
| 10626 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10627 | if (inj_err_type == INJECT_ERR_SET_PEER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10628 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10629 | TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4), |
| 10630 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10631 | goto exit; |
| 10632 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 10633 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10634 | const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10635 | PSA_PAKE_STEP_KEY_SHARE); |
| 10636 | const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10637 | PSA_PAKE_STEP_ZK_PUBLIC); |
| 10638 | const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10639 | PSA_PAKE_STEP_ZK_PROOF); |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 10640 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10641 | if (test_input) { |
| 10642 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 10643 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0), |
| 10644 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10645 | goto exit; |
| 10646 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10647 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10648 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 10649 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 10650 | output_buffer, size_zk_proof), |
| 10651 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10652 | goto exit; |
| 10653 | } |
| 10654 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10655 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 10656 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10657 | output_buffer, size_zk_proof), |
| 10658 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10659 | goto exit; |
| 10660 | } |
| 10661 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10662 | status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10663 | output_buffer, size_key_share); |
| 10664 | if (status != PSA_SUCCESS) { |
| 10665 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10666 | goto exit; |
| 10667 | } |
| 10668 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10669 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 10670 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10671 | output_buffer, size_zk_public + 1), |
| 10672 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10673 | goto exit; |
| 10674 | } |
| 10675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10676 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10677 | // 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] | 10678 | psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10679 | output_buffer, size_zk_public + 1); |
| 10680 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10681 | output_buffer, size_zk_public), |
| 10682 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10683 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10684 | } |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10685 | } else { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10686 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 10687 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10688 | NULL, 0, NULL), |
| 10689 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10690 | goto exit; |
| 10691 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10692 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10693 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 10694 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 10695 | output_buffer, buf_size, &output_len), |
| 10696 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10697 | goto exit; |
| 10698 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10699 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10700 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 10701 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10702 | output_buffer, buf_size, &output_len), |
| 10703 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10704 | goto exit; |
| 10705 | } |
| 10706 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10707 | status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10708 | output_buffer, buf_size, &output_len); |
| 10709 | if (status != PSA_SUCCESS) { |
| 10710 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10711 | goto exit; |
| 10712 | } |
| 10713 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10714 | TEST_ASSERT(output_len > 0); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10715 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10716 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 10717 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10718 | output_buffer, size_zk_public - 1, &output_len), |
| 10719 | PSA_ERROR_BUFFER_TOO_SMALL); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10720 | goto exit; |
| 10721 | } |
| 10722 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10723 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10724 | // 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] | 10725 | psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10726 | output_buffer, size_zk_public - 1, &output_len); |
| 10727 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10728 | output_buffer, buf_size, &output_len), |
| 10729 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10730 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10731 | } |
| 10732 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10733 | |
| 10734 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10735 | PSA_ASSERT(psa_destroy_key(key)); |
| 10736 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10737 | mbedtls_free(output_buffer); |
| 10738 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10739 | } |
| 10740 | /* END_CASE */ |
| 10741 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 10742 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10743 | void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg, |
| 10744 | int client_input_first, int inject_error, |
| 10745 | data_t *pw_data) |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10746 | { |
| 10747 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10748 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10749 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10750 | psa_algorithm_t alg = alg_arg; |
| 10751 | psa_algorithm_t hash_alg = hash_arg; |
| 10752 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10753 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10754 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10755 | PSA_INIT(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10756 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10757 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10758 | psa_set_key_algorithm(&attributes, alg); |
| 10759 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10760 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10761 | &key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10762 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10763 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10764 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10765 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10766 | |
| 10767 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10768 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10769 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10771 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10772 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10773 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10774 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10775 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10776 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10777 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10778 | client_input_first, 1, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10779 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10780 | if (inject_error == 1 || inject_error == 2) { |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10781 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10782 | } |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10783 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10784 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10785 | client_input_first, 2, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10786 | |
| 10787 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10788 | psa_destroy_key(key); |
| 10789 | psa_pake_abort(&server); |
| 10790 | psa_pake_abort(&client); |
| 10791 | PSA_DONE(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10792 | } |
| 10793 | /* END_CASE */ |
| 10794 | |
| 10795 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10796 | void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg, |
| 10797 | int derive_alg_arg, data_t *pw_data, |
| 10798 | int client_input_first, int inj_err_type_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10799 | { |
| 10800 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10801 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10802 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10803 | psa_algorithm_t alg = alg_arg; |
| 10804 | psa_algorithm_t hash_alg = hash_arg; |
| 10805 | psa_algorithm_t derive_alg = derive_alg_arg; |
| 10806 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10807 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10808 | psa_key_derivation_operation_t server_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10809 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10810 | psa_key_derivation_operation_t client_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10811 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10812 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10813 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10814 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10815 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10816 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10817 | psa_set_key_algorithm(&attributes, alg); |
| 10818 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10819 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10820 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10821 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10822 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10823 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10824 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10825 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10826 | /* Get shared key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10827 | PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg)); |
| 10828 | PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10829 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10830 | if (PSA_ALG_IS_TLS12_PRF(derive_alg) || |
| 10831 | PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) { |
| 10832 | PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive, |
| 10833 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10834 | (const uint8_t *) "", 0)); |
| 10835 | PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive, |
| 10836 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10837 | (const uint8_t *) "", 0)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10838 | } |
| 10839 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10840 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10841 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10842 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10843 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10844 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10845 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10846 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10847 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10848 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10849 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) { |
| 10850 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10851 | PSA_ERROR_BAD_STATE); |
| 10852 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10853 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10854 | goto exit; |
| 10855 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10856 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10857 | /* First round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10858 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10859 | client_input_first, 1, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10860 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10861 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) { |
| 10862 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10863 | PSA_ERROR_BAD_STATE); |
| 10864 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10865 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10866 | goto exit; |
| 10867 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10868 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10869 | /* Second round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10870 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10871 | client_input_first, 2, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10872 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10873 | PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive)); |
| 10874 | PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10875 | |
| 10876 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10877 | psa_key_derivation_abort(&server_derive); |
| 10878 | psa_key_derivation_abort(&client_derive); |
| 10879 | psa_destroy_key(key); |
| 10880 | psa_pake_abort(&server); |
| 10881 | psa_pake_abort(&client); |
| 10882 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10883 | } |
| 10884 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10885 | |
| 10886 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10887 | void ecjpake_size_macros() |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10888 | { |
| 10889 | const psa_algorithm_t alg = PSA_ALG_JPAKE; |
| 10890 | const size_t bits = 256; |
| 10891 | const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10892 | 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] | 10893 | 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] | 10894 | PSA_ECC_FAMILY_SECP_R1); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10895 | |
| 10896 | // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types |
| 10897 | /* 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] | 10898 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10899 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
| 10900 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10901 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10902 | /* The output for ZK_PROOF is the same bitsize as the curve */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10903 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10904 | PSA_BITS_TO_BYTES(bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10905 | |
| 10906 | /* Input sizes are the same as output sizes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10907 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10908 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE)); |
| 10909 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10910 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC)); |
| 10911 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10912 | 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] | 10913 | |
| 10914 | /* These inequalities will always hold even when other PAKEs are added */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10915 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10916 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10917 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10918 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10919 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10920 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10921 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10922 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10923 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10924 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10925 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10926 | PSA_PAKE_INPUT_MAX_SIZE); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10927 | } |
| 10928 | /* END_CASE */ |