Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2 | #include <stdint.h> |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 3 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 4 | #include "mbedtls/asn1.h" |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 5 | #include "mbedtls/asn1write.h" |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 6 | #include "mbedtls/oid.h" |
Gilles Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 7 | #include "common.h" |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 8 | |
Valerio Setti | bf999cb | 2023-12-28 17:48:13 +0100 | [diff] [blame] | 9 | #include "mbedtls/psa_util.h" |
| 10 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 11 | /* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random() |
| 12 | * uses mbedtls_ctr_drbg internally. */ |
| 13 | #include "mbedtls/ctr_drbg.h" |
| 14 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 15 | #include "psa/crypto.h" |
Ronald Cron | 4184107 | 2020-09-17 15:28:26 +0200 | [diff] [blame] | 16 | #include "psa_crypto_slot_management.h" |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 17 | |
Manuel Pégourié-Gonnard | 7abdf7e | 2023-03-09 11:17:43 +0100 | [diff] [blame] | 18 | /* For psa_can_do_hash() */ |
| 19 | #include "psa_crypto_core.h" |
| 20 | |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 21 | #include "test/asn1_helpers.h" |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 22 | #include "test/psa_crypto_helpers.h" |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 23 | #include "test/psa_exercise_key.h" |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 24 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 25 | #include "test/drivers/test_driver.h" |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 26 | #define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION |
| 27 | #else |
| 28 | #define TEST_DRIVER_LOCATION 0x7fffff |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 29 | #endif |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 30 | |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 31 | /* If this comes up, it's a bug in the test code or in the test data. */ |
| 32 | #define UNUSED 0xdeadbeef |
| 33 | |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 34 | /* Assert that an operation is (not) active. |
| 35 | * This serves as a proxy for checking if the operation is aborted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 36 | #define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0) |
| 37 | #define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 38 | |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 39 | #if defined(PSA_WANT_ALG_JPAKE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 40 | int ecjpake_operation_setup(psa_pake_operation_t *operation, |
| 41 | psa_pake_cipher_suite_t *cipher_suite, |
| 42 | psa_pake_role_t role, |
| 43 | mbedtls_svc_key_id_t key, |
| 44 | size_t key_available) |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 45 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 46 | PSA_ASSERT(psa_pake_abort(operation)); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 47 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 48 | PSA_ASSERT(psa_pake_setup(operation, cipher_suite)); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 49 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 50 | PSA_ASSERT(psa_pake_set_role(operation, role)); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 51 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 52 | if (key_available) { |
| 53 | PSA_ASSERT(psa_pake_set_password_key(operation, key)); |
| 54 | } |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 55 | return 0; |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 56 | exit: |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 57 | return 1; |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 58 | } |
| 59 | #endif |
| 60 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 61 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 62 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 63 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 64 | /** Test if a buffer contains a constant byte value. |
| 65 | * |
| 66 | * `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] | 67 | * |
| 68 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 69 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 70 | * \param size Size of the buffer in bytes. |
| 71 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 72 | * \return 1 if the buffer is all-bits-zero. |
| 73 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 74 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 75 | 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] | 76 | { |
| 77 | size_t i; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 78 | for (i = 0; i < size; i++) { |
| 79 | if (((unsigned char *) buffer)[i] != c) { |
| 80 | return 0; |
| 81 | } |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 82 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 83 | return 1; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 84 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 85 | #if defined(MBEDTLS_ASN1_WRITE_C) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 86 | /* 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] | 87 | static int asn1_write_10x(unsigned char **p, |
| 88 | unsigned char *start, |
| 89 | size_t bits, |
| 90 | unsigned char x) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 91 | { |
| 92 | int ret; |
| 93 | int len = bits / 8 + 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 94 | if (bits == 0) { |
| 95 | return MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 96 | } |
| 97 | if (bits <= 8 && x >= 1 << (bits - 1)) { |
| 98 | return MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 99 | } |
| 100 | if (*p < start || *p - start < (ptrdiff_t) len) { |
| 101 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 102 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 103 | *p -= len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 104 | (*p)[len-1] = x; |
| 105 | if (bits % 8 == 0) { |
| 106 | (*p)[1] |= 1; |
| 107 | } else { |
| 108 | (*p)[0] |= 1 << (bits % 8); |
| 109 | } |
| 110 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 111 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, |
| 112 | MBEDTLS_ASN1_INTEGER)); |
| 113 | return len; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 114 | } |
| 115 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 116 | static int construct_fake_rsa_key(unsigned char *buffer, |
| 117 | size_t buffer_size, |
| 118 | unsigned char **p, |
| 119 | size_t bits, |
| 120 | int keypair) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 121 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 122 | size_t half_bits = (bits + 1) / 2; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 123 | int ret; |
| 124 | int len = 0; |
| 125 | /* Construct something that looks like a DER encoding of |
| 126 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 127 | * RSAPrivateKey ::= SEQUENCE { |
| 128 | * version Version, |
| 129 | * modulus INTEGER, -- n |
| 130 | * publicExponent INTEGER, -- e |
| 131 | * privateExponent INTEGER, -- d |
| 132 | * prime1 INTEGER, -- p |
| 133 | * prime2 INTEGER, -- q |
| 134 | * exponent1 INTEGER, -- d mod (p-1) |
| 135 | * exponent2 INTEGER, -- d mod (q-1) |
| 136 | * coefficient INTEGER, -- (inverse of q) mod p |
| 137 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 138 | * } |
| 139 | * Or, for a public key, the same structure with only |
| 140 | * version, modulus and publicExponent. |
| 141 | */ |
| 142 | *p = buffer + buffer_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 143 | if (keypair) { |
| 144 | MBEDTLS_ASN1_CHK_ADD(len, /* pq */ |
| 145 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 146 | MBEDTLS_ASN1_CHK_ADD(len, /* dq */ |
| 147 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 148 | MBEDTLS_ASN1_CHK_ADD(len, /* dp */ |
| 149 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 150 | MBEDTLS_ASN1_CHK_ADD(len, /* q */ |
| 151 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 152 | MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */ |
| 153 | asn1_write_10x(p, buffer, half_bits, 3)); |
| 154 | MBEDTLS_ASN1_CHK_ADD(len, /* d */ |
| 155 | asn1_write_10x(p, buffer, bits, 1)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 156 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 157 | MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */ |
| 158 | asn1_write_10x(p, buffer, 17, 1)); |
| 159 | MBEDTLS_ASN1_CHK_ADD(len, /* n */ |
| 160 | asn1_write_10x(p, buffer, bits, 1)); |
| 161 | if (keypair) { |
| 162 | MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */ |
| 163 | mbedtls_asn1_write_int(p, buffer, 0)); |
| 164 | } |
| 165 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 166 | { |
| 167 | const unsigned char tag = |
| 168 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 169 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 170 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 171 | return len; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 172 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 173 | #endif /* MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 174 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 175 | int exercise_mac_setup(psa_key_type_t key_type, |
| 176 | const unsigned char *key_bytes, |
| 177 | size_t key_length, |
| 178 | psa_algorithm_t alg, |
| 179 | psa_mac_operation_t *operation, |
| 180 | psa_status_t *status) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 181 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 182 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 183 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 184 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 185 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 186 | psa_set_key_algorithm(&attributes, alg); |
| 187 | psa_set_key_type(&attributes, key_type); |
| 188 | PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 189 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 190 | *status = psa_mac_sign_setup(operation, key, alg); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 191 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 192 | PSA_ASSERT(psa_mac_abort(operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 193 | /* If setup failed, reproduce the failure, so that the caller can |
| 194 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 195 | if (*status != PSA_SUCCESS) { |
| 196 | TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 197 | } |
| 198 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 199 | psa_destroy_key(key); |
| 200 | return 1; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 201 | |
| 202 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 203 | psa_destroy_key(key); |
| 204 | return 0; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 205 | } |
| 206 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 207 | int exercise_cipher_setup(psa_key_type_t key_type, |
| 208 | const unsigned char *key_bytes, |
| 209 | size_t key_length, |
| 210 | psa_algorithm_t alg, |
| 211 | psa_cipher_operation_t *operation, |
| 212 | psa_status_t *status) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 213 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 214 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 215 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 216 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 217 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 218 | psa_set_key_algorithm(&attributes, alg); |
| 219 | psa_set_key_type(&attributes, key_type); |
| 220 | PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 221 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 222 | *status = psa_cipher_encrypt_setup(operation, key, alg); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 223 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 224 | PSA_ASSERT(psa_cipher_abort(operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 225 | /* If setup failed, reproduce the failure, so that the caller can |
| 226 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 227 | if (*status != PSA_SUCCESS) { |
| 228 | TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg), |
| 229 | *status); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 230 | } |
| 231 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 232 | psa_destroy_key(key); |
| 233 | return 1; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 234 | |
| 235 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 236 | psa_destroy_key(key); |
| 237 | return 0; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 238 | } |
| 239 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 240 | 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] | 241 | { |
| 242 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 243 | 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] | 244 | uint8_t buffer[1]; |
| 245 | size_t length; |
| 246 | int ok = 0; |
| 247 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 248 | psa_set_key_id(&attributes, key_id); |
| 249 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 250 | psa_set_key_algorithm(&attributes, PSA_ALG_CTR); |
| 251 | psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); |
| 252 | TEST_EQUAL(psa_get_key_attributes(key, &attributes), |
| 253 | PSA_ERROR_INVALID_HANDLE); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 254 | TEST_EQUAL( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 255 | 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] | 256 | TEST_EQUAL( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 257 | MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0); |
| 258 | TEST_EQUAL(psa_get_key_lifetime(&attributes), 0); |
| 259 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0); |
| 260 | TEST_EQUAL(psa_get_key_algorithm(&attributes), 0); |
| 261 | TEST_EQUAL(psa_get_key_type(&attributes), 0); |
| 262 | TEST_EQUAL(psa_get_key_bits(&attributes), 0); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 263 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 264 | TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length), |
| 265 | PSA_ERROR_INVALID_HANDLE); |
| 266 | TEST_EQUAL(psa_export_public_key(key, |
| 267 | buffer, sizeof(buffer), &length), |
| 268 | PSA_ERROR_INVALID_HANDLE); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 269 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 270 | ok = 1; |
| 271 | |
| 272 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 273 | /* |
| 274 | * Key attributes may have been returned by psa_get_key_attributes() |
| 275 | * thus reset them as required. |
| 276 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 277 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 278 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 279 | return ok; |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 280 | } |
| 281 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 282 | /* Assert that a key isn't reported as having a slot number. */ |
| 283 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 284 | #define ASSERT_NO_SLOT_NUMBER(attributes) \ |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 285 | do \ |
| 286 | { \ |
| 287 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 288 | TEST_EQUAL(psa_get_key_slot_number( \ |
| 289 | attributes, \ |
| 290 | &ASSERT_NO_SLOT_NUMBER_slot_number), \ |
| 291 | PSA_ERROR_INVALID_ARGUMENT); \ |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 292 | } \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 293 | while (0) |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 294 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 295 | #define ASSERT_NO_SLOT_NUMBER(attributes) \ |
| 296 | ((void) 0) |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 297 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 298 | |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 299 | #define INPUT_INTEGER 0x10000 /* Out of range of psa_key_type_t */ |
| 300 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 301 | /* An overapproximation of the amount of storage needed for a key of the |
| 302 | * given type and with the given content. The API doesn't make it easy |
| 303 | * to find a good value for the size. The current implementation doesn't |
| 304 | * care about the value anyway. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 305 | #define KEY_BITS_FROM_DATA(type, data) \ |
| 306 | (data)->len |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 307 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 308 | typedef enum { |
| 309 | IMPORT_KEY = 0, |
| 310 | GENERATE_KEY = 1, |
| 311 | DERIVE_KEY = 2 |
| 312 | } generate_method; |
| 313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 314 | typedef enum { |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 315 | DO_NOT_SET_LENGTHS = 0, |
| 316 | SET_LENGTHS_BEFORE_NONCE = 1, |
| 317 | SET_LENGTHS_AFTER_NONCE = 2 |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 318 | } set_lengths_method_t; |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 319 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 320 | typedef enum { |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 321 | USE_NULL_TAG = 0, |
| 322 | USE_GIVEN_TAG = 1, |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 323 | } tag_usage_method_t; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 324 | |
Kusumit Ghoderao | a14ae5a | 2023-04-19 14:16:26 +0530 | [diff] [blame] | 325 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 326 | /*! |
| 327 | * \brief Internal Function for AEAD multipart tests. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 328 | * \param key_type_arg Type of key passed in |
| 329 | * \param key_data The encryption / decryption key data |
| 330 | * \param alg_arg The type of algorithm used |
| 331 | * \param nonce Nonce data |
| 332 | * \param additional_data Additional data |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 333 | * \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] | 334 | * feed additional data in to be encrypted / |
| 335 | * decrypted. If -1, no chunking. |
| 336 | * \param input_data Data to encrypt / decrypt |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 337 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 338 | * the data in to be encrypted / decrypted. If |
| 339 | * -1, no chunking |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 340 | * \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] | 341 | * expected here, this controls whether or not |
| 342 | * to set lengths, and in what order with |
| 343 | * respect to set nonce. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 344 | * \param expected_output Expected output |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 345 | * \param is_encrypt If non-zero this is an encryption operation. |
Paul Elliott | 41ffae1 | 2021-07-22 21:52:01 +0100 | [diff] [blame] | 346 | * \param do_zero_parts If non-zero, interleave zero length chunks |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 347 | * with normal length chunks. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 348 | * \return int Zero on failure, non-zero on success. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 349 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 350 | static int aead_multipart_internal_func(int key_type_arg, data_t *key_data, |
| 351 | int alg_arg, |
| 352 | data_t *nonce, |
| 353 | data_t *additional_data, |
| 354 | int ad_part_len_arg, |
| 355 | data_t *input_data, |
| 356 | int data_part_len_arg, |
| 357 | set_lengths_method_t set_lengths_method, |
| 358 | data_t *expected_output, |
| 359 | int is_encrypt, |
| 360 | int do_zero_parts) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 361 | { |
| 362 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 363 | psa_key_type_t key_type = key_type_arg; |
| 364 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 365 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 366 | unsigned char *output_data = NULL; |
| 367 | unsigned char *part_data = NULL; |
| 368 | unsigned char *final_data = NULL; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 369 | size_t data_true_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 370 | size_t part_data_size = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 371 | size_t output_size = 0; |
| 372 | size_t final_output_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 373 | size_t output_length = 0; |
| 374 | size_t key_bits = 0; |
| 375 | size_t tag_length = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 376 | size_t part_offset = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 377 | size_t part_length = 0; |
| 378 | size_t output_part_length = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 379 | size_t tag_size = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 380 | size_t ad_part_len = 0; |
| 381 | size_t data_part_len = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 382 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 383 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 384 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 385 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 386 | int test_ok = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 387 | size_t part_count = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 389 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 390 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 391 | if (is_encrypt) { |
| 392 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 393 | } else { |
| 394 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 395 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 396 | |
| 397 | psa_set_key_algorithm(&attributes, alg); |
| 398 | psa_set_key_type(&attributes, key_type); |
| 399 | |
| 400 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 401 | &key)); |
| 402 | |
| 403 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 404 | key_bits = psa_get_key_bits(&attributes); |
| 405 | |
| 406 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); |
| 407 | |
| 408 | if (is_encrypt) { |
| 409 | /* Tag gets written at end of buffer. */ |
| 410 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 411 | (input_data->len + |
| 412 | tag_length)); |
| 413 | data_true_size = input_data->len; |
| 414 | } else { |
| 415 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 416 | (input_data->len - |
| 417 | tag_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 418 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 419 | /* Do not want to attempt to decrypt tag. */ |
| 420 | data_true_size = input_data->len - tag_length; |
| 421 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 422 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 423 | TEST_CALLOC(output_data, output_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 424 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 425 | if (is_encrypt) { |
| 426 | final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
| 427 | TEST_LE_U(final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
| 428 | } else { |
| 429 | final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); |
| 430 | TEST_LE_U(final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 431 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 432 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 433 | TEST_CALLOC(final_data, final_output_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 434 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 435 | if (is_encrypt) { |
| 436 | status = psa_aead_encrypt_setup(&operation, key, alg); |
| 437 | } else { |
| 438 | status = psa_aead_decrypt_setup(&operation, key, alg); |
| 439 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 440 | |
| 441 | /* If the operation is not supported, just skip and not fail in case the |
| 442 | * encryption involves a common limitation of cryptography hardwares and |
| 443 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 444 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 445 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 446 | 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] | 447 | } |
| 448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 449 | PSA_ASSERT(status); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 450 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 451 | if (set_lengths_method == DO_NOT_SET_LENGTHS) { |
| 452 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
| 453 | } else if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { |
| 454 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 455 | data_true_size)); |
| 456 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
| 457 | } else if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) { |
| 458 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 459 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 460 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 461 | data_true_size)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 462 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 463 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 464 | if (ad_part_len_arg != -1) { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 465 | /* Pass additional data in parts */ |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 466 | ad_part_len = (size_t) ad_part_len_arg; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 467 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 468 | for (part_offset = 0, part_count = 0; |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 469 | part_offset < additional_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 470 | part_offset += part_length, part_count++) { |
| 471 | if (do_zero_parts && (part_count & 0x01)) { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 472 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 473 | } else if (additional_data->len - part_offset < ad_part_len) { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 474 | part_length = additional_data->len - part_offset; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 475 | } else { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 476 | part_length = ad_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 477 | } |
| 478 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 479 | PSA_ASSERT(psa_aead_update_ad(&operation, |
| 480 | additional_data->x + part_offset, |
| 481 | part_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 482 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 483 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 484 | } else { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 485 | /* Pass additional data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 486 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 487 | additional_data->len)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 488 | } |
| 489 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 490 | if (data_part_len_arg != -1) { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 491 | /* Pass data in parts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 492 | data_part_len = (size_t) data_part_len_arg; |
| 493 | part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 494 | (size_t) data_part_len); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 495 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 496 | TEST_CALLOC(part_data, part_data_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 497 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 498 | for (part_offset = 0, part_count = 0; |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 499 | part_offset < data_true_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 500 | part_offset += part_length, part_count++) { |
| 501 | if (do_zero_parts && (part_count & 0x01)) { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 502 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 503 | } else if ((data_true_size - part_offset) < data_part_len) { |
| 504 | part_length = (data_true_size - part_offset); |
| 505 | } else { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 506 | part_length = data_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 507 | } |
| 508 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 509 | PSA_ASSERT(psa_aead_update(&operation, |
| 510 | (input_data->x + part_offset), |
| 511 | part_length, part_data, |
| 512 | part_data_size, |
| 513 | &output_part_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 514 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 515 | if (output_data && output_part_length) { |
| 516 | memcpy((output_data + output_length), part_data, |
| 517 | output_part_length); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 518 | } |
| 519 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 520 | output_length += output_part_length; |
| 521 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 522 | } else { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 523 | /* Pass all data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 524 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 525 | data_true_size, output_data, |
| 526 | output_size, &output_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 527 | } |
| 528 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 529 | if (is_encrypt) { |
| 530 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 531 | final_output_size, |
| 532 | &output_part_length, |
| 533 | tag_buffer, tag_length, |
| 534 | &tag_size)); |
| 535 | } else { |
| 536 | PSA_ASSERT(psa_aead_verify(&operation, final_data, |
| 537 | final_output_size, |
| 538 | &output_part_length, |
| 539 | (input_data->x + data_true_size), |
| 540 | tag_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 541 | } |
| 542 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 543 | if (output_data && output_part_length) { |
| 544 | memcpy((output_data + output_length), final_data, |
| 545 | output_part_length); |
| 546 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 547 | |
| 548 | output_length += output_part_length; |
| 549 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 550 | |
| 551 | /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE |
| 552 | * should be exact.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 553 | if (is_encrypt) { |
| 554 | TEST_EQUAL(tag_length, tag_size); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 555 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 556 | if (output_data && tag_length) { |
| 557 | memcpy((output_data + output_length), tag_buffer, |
| 558 | tag_length); |
| 559 | } |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 560 | |
| 561 | output_length += tag_length; |
| 562 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 563 | TEST_EQUAL(output_length, |
| 564 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 565 | input_data->len)); |
| 566 | TEST_LE_U(output_length, |
| 567 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
| 568 | } else { |
| 569 | TEST_EQUAL(output_length, |
| 570 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, |
| 571 | input_data->len)); |
| 572 | TEST_LE_U(output_length, |
| 573 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 574 | } |
| 575 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 576 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 577 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 578 | output_data, output_length); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 579 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 580 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 581 | test_ok = 1; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 582 | |
| 583 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 584 | psa_destroy_key(key); |
| 585 | psa_aead_abort(&operation); |
| 586 | mbedtls_free(output_data); |
| 587 | mbedtls_free(part_data); |
| 588 | mbedtls_free(final_data); |
| 589 | PSA_DONE(); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 590 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 591 | return test_ok; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 592 | } |
| 593 | |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 594 | /*! |
| 595 | * \brief Internal Function for MAC multipart tests. |
| 596 | * \param key_type_arg Type of key passed in |
| 597 | * \param key_data The encryption / decryption key data |
| 598 | * \param alg_arg The type of algorithm used |
| 599 | * \param input_data Data to encrypt / decrypt |
| 600 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 601 | * the data in to be encrypted / decrypted. If |
| 602 | * -1, no chunking |
| 603 | * \param expected_output Expected output |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 604 | * \param is_verify If non-zero this is a verify operation. |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 605 | * \param do_zero_parts If non-zero, interleave zero length chunks |
| 606 | * with normal length chunks. |
| 607 | * \return int Zero on failure, non-zero on success. |
| 608 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 609 | static int mac_multipart_internal_func(int key_type_arg, data_t *key_data, |
| 610 | int alg_arg, |
| 611 | data_t *input_data, |
| 612 | int data_part_len_arg, |
| 613 | data_t *expected_output, |
| 614 | int is_verify, |
| 615 | int do_zero_parts) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 616 | { |
| 617 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 618 | psa_key_type_t key_type = key_type_arg; |
| 619 | psa_algorithm_t alg = alg_arg; |
| 620 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 621 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
| 622 | size_t part_offset = 0; |
| 623 | size_t part_length = 0; |
| 624 | size_t data_part_len = 0; |
| 625 | size_t mac_len = 0; |
| 626 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 627 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 628 | |
| 629 | int test_ok = 0; |
| 630 | size_t part_count = 0; |
| 631 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 632 | PSA_INIT(); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 633 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 634 | if (is_verify) { |
| 635 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 636 | } else { |
| 637 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 638 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 639 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 640 | psa_set_key_algorithm(&attributes, alg); |
| 641 | psa_set_key_type(&attributes, key_type); |
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_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 644 | &key)); |
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 | if (is_verify) { |
| 647 | status = psa_mac_verify_setup(&operation, key, alg); |
| 648 | } else { |
| 649 | status = psa_mac_sign_setup(&operation, key, alg); |
| 650 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 651 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 652 | PSA_ASSERT(status); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 653 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 654 | if (data_part_len_arg != -1) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 655 | /* Pass data in parts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 656 | data_part_len = (size_t) data_part_len_arg; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 657 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 658 | for (part_offset = 0, part_count = 0; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 659 | part_offset < input_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 660 | part_offset += part_length, part_count++) { |
| 661 | if (do_zero_parts && (part_count & 0x01)) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 662 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 663 | } else if ((input_data->len - part_offset) < data_part_len) { |
| 664 | part_length = (input_data->len - part_offset); |
| 665 | } else { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 666 | part_length = data_part_len; |
| 667 | } |
| 668 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 669 | PSA_ASSERT(psa_mac_update(&operation, |
| 670 | (input_data->x + part_offset), |
| 671 | part_length)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 672 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 673 | } else { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 674 | /* Pass all data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 675 | PSA_ASSERT(psa_mac_update(&operation, input_data->x, |
| 676 | input_data->len)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 677 | } |
| 678 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 679 | if (is_verify) { |
| 680 | PSA_ASSERT(psa_mac_verify_finish(&operation, expected_output->x, |
| 681 | expected_output->len)); |
| 682 | } else { |
| 683 | PSA_ASSERT(psa_mac_sign_finish(&operation, mac, |
| 684 | PSA_MAC_MAX_SIZE, &mac_len)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 685 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 686 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 687 | mac, mac_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | test_ok = 1; |
| 691 | |
| 692 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 693 | psa_destroy_key(key); |
| 694 | psa_mac_abort(&operation); |
| 695 | PSA_DONE(); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 696 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 697 | return test_ok; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 698 | } |
| 699 | |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 700 | #if defined(PSA_WANT_ALG_JPAKE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 701 | static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive, |
| 702 | psa_pake_operation_t *server, |
| 703 | psa_pake_operation_t *client, |
| 704 | int client_input_first, |
| 705 | int round, int inject_error) |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 706 | { |
| 707 | unsigned char *buffer0 = NULL, *buffer1 = NULL; |
| 708 | size_t buffer_length = ( |
| 709 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) + |
| 710 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) + |
| 711 | 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] | 712 | /* The output should be exactly this size according to the spec */ |
| 713 | const size_t expected_size_key_share = |
| 714 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE); |
| 715 | /* The output should be exactly this size according to the spec */ |
| 716 | const size_t expected_size_zk_public = |
| 717 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC); |
| 718 | /* The output can be smaller: the spec allows stripping leading zeroes */ |
| 719 | const size_t max_expected_size_zk_proof = |
| 720 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 721 | size_t buffer0_off = 0; |
| 722 | size_t buffer1_off = 0; |
| 723 | size_t s_g1_len, s_g2_len, s_a_len; |
| 724 | size_t s_g1_off, s_g2_off, s_a_off; |
| 725 | size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len; |
| 726 | size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off; |
| 727 | size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len; |
| 728 | size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off; |
| 729 | size_t c_g1_len, c_g2_len, c_a_len; |
| 730 | size_t c_g1_off, c_g2_off, c_a_off; |
| 731 | size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len; |
| 732 | size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off; |
| 733 | size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len; |
| 734 | size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off; |
| 735 | psa_status_t expected_status = PSA_SUCCESS; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 736 | psa_status_t status; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 737 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 738 | TEST_CALLOC(buffer0, buffer_length); |
| 739 | TEST_CALLOC(buffer1, buffer_length); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 740 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 741 | switch (round) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 742 | case 1: |
| 743 | /* Server first round Output */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 744 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 745 | buffer0 + buffer0_off, |
| 746 | 512 - buffer0_off, &s_g1_len)); |
| 747 | TEST_EQUAL(s_g1_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 748 | s_g1_off = buffer0_off; |
| 749 | buffer0_off += s_g1_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 750 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 751 | buffer0 + buffer0_off, |
| 752 | 512 - buffer0_off, &s_x1_pk_len)); |
| 753 | TEST_EQUAL(s_x1_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 754 | s_x1_pk_off = buffer0_off; |
| 755 | buffer0_off += s_x1_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 756 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 757 | buffer0 + buffer0_off, |
| 758 | 512 - buffer0_off, &s_x1_pr_len)); |
| 759 | TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 760 | s_x1_pr_off = buffer0_off; |
| 761 | buffer0_off += s_x1_pr_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 762 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 763 | buffer0 + buffer0_off, |
| 764 | 512 - buffer0_off, &s_g2_len)); |
| 765 | TEST_EQUAL(s_g2_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 766 | s_g2_off = buffer0_off; |
| 767 | buffer0_off += s_g2_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 768 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 769 | buffer0 + buffer0_off, |
| 770 | 512 - buffer0_off, &s_x2_pk_len)); |
| 771 | TEST_EQUAL(s_x2_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 772 | s_x2_pk_off = buffer0_off; |
| 773 | buffer0_off += s_x2_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 774 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 775 | buffer0 + buffer0_off, |
| 776 | 512 - buffer0_off, &s_x2_pr_len)); |
| 777 | TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 778 | s_x2_pr_off = buffer0_off; |
| 779 | buffer0_off += s_x2_pr_len; |
| 780 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 781 | if (inject_error == 1) { |
Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 782 | buffer0[s_x1_pr_off + 8] ^= 1; |
| 783 | buffer0[s_x2_pr_off + 7] ^= 1; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 784 | expected_status = PSA_ERROR_DATA_INVALID; |
| 785 | } |
| 786 | |
Neil Armstrong | 51009d7 | 2022-09-05 17:59:54 +0200 | [diff] [blame] | 787 | /* |
| 788 | * When injecting errors in inputs, the implementation is |
| 789 | * free to detect it right away of with a delay. |
| 790 | * This permits delaying the error until the end of the input |
| 791 | * sequence, if no error appears then, this will be treated |
| 792 | * as an error. |
| 793 | */ |
| 794 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 795 | if (client_input_first == 1) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 796 | /* Client first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 797 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 798 | buffer0 + s_g1_off, s_g1_len); |
| 799 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 800 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 801 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 802 | } else { |
| 803 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 804 | } |
| 805 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 806 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 807 | buffer0 + s_x1_pk_off, |
| 808 | s_x1_pk_len); |
| 809 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 810 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 811 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 812 | } else { |
| 813 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 814 | } |
| 815 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 816 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 817 | buffer0 + s_x1_pr_off, |
| 818 | s_x1_pr_len); |
| 819 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 820 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 821 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 822 | } else { |
| 823 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 824 | } |
| 825 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 826 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 827 | buffer0 + s_g2_off, |
| 828 | s_g2_len); |
| 829 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 830 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 831 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 832 | } else { |
| 833 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 834 | } |
| 835 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 836 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 837 | buffer0 + s_x2_pk_off, |
| 838 | s_x2_pk_len); |
| 839 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 840 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 841 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 842 | } else { |
| 843 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 844 | } |
| 845 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 846 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 847 | buffer0 + s_x2_pr_off, |
| 848 | s_x2_pr_len); |
| 849 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 850 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 851 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 852 | } else { |
| 853 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 854 | } |
| 855 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 856 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 857 | if (inject_error == 1) { |
| 858 | TEST_ASSERT( |
| 859 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 860 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | /* Client first round Output */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 864 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 865 | buffer1 + buffer1_off, |
| 866 | 512 - buffer1_off, &c_g1_len)); |
| 867 | TEST_EQUAL(c_g1_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 868 | c_g1_off = buffer1_off; |
| 869 | buffer1_off += c_g1_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 870 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 871 | buffer1 + buffer1_off, |
| 872 | 512 - buffer1_off, &c_x1_pk_len)); |
| 873 | TEST_EQUAL(c_x1_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 874 | c_x1_pk_off = buffer1_off; |
| 875 | buffer1_off += c_x1_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 876 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 877 | buffer1 + buffer1_off, |
| 878 | 512 - buffer1_off, &c_x1_pr_len)); |
| 879 | TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 880 | c_x1_pr_off = buffer1_off; |
| 881 | buffer1_off += c_x1_pr_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 882 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 883 | buffer1 + buffer1_off, |
| 884 | 512 - buffer1_off, &c_g2_len)); |
| 885 | TEST_EQUAL(c_g2_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 886 | c_g2_off = buffer1_off; |
| 887 | buffer1_off += c_g2_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 888 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 889 | buffer1 + buffer1_off, |
| 890 | 512 - buffer1_off, &c_x2_pk_len)); |
| 891 | TEST_EQUAL(c_x2_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 892 | c_x2_pk_off = buffer1_off; |
| 893 | buffer1_off += c_x2_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 894 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 895 | buffer1 + buffer1_off, |
| 896 | 512 - buffer1_off, &c_x2_pr_len)); |
| 897 | TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 898 | c_x2_pr_off = buffer1_off; |
| 899 | buffer1_off += c_x2_pr_len; |
| 900 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 901 | if (client_input_first == 0) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 902 | /* Client first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 903 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 904 | buffer0 + s_g1_off, s_g1_len); |
| 905 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 906 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 907 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 908 | } else { |
| 909 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 910 | } |
| 911 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 912 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 913 | buffer0 + s_x1_pk_off, |
| 914 | s_x1_pk_len); |
| 915 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 916 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 917 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 918 | } else { |
| 919 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 920 | } |
| 921 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 922 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 923 | buffer0 + s_x1_pr_off, |
| 924 | s_x1_pr_len); |
| 925 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 926 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 927 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 928 | } else { |
| 929 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 930 | } |
| 931 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 932 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 933 | buffer0 + s_g2_off, |
| 934 | s_g2_len); |
| 935 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 936 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 937 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 938 | } else { |
| 939 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 940 | } |
| 941 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 942 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 943 | buffer0 + s_x2_pk_off, |
| 944 | s_x2_pk_len); |
| 945 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 946 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 947 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 948 | } else { |
| 949 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 950 | } |
| 951 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 952 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 953 | buffer0 + s_x2_pr_off, |
| 954 | s_x2_pr_len); |
| 955 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 956 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 957 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 958 | } else { |
| 959 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 960 | } |
| 961 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 962 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 963 | if (inject_error == 1) { |
| 964 | TEST_ASSERT( |
| 965 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 966 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 967 | } |
| 968 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 969 | if (inject_error == 2) { |
Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 970 | buffer1[c_x1_pr_off + 12] ^= 1; |
| 971 | buffer1[c_x2_pr_off + 7] ^= 1; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 972 | expected_status = PSA_ERROR_DATA_INVALID; |
| 973 | } |
| 974 | |
| 975 | /* Server first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 976 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 977 | buffer1 + c_g1_off, c_g1_len); |
| 978 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 979 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 980 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 981 | } else { |
| 982 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 983 | } |
| 984 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 985 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 986 | buffer1 + c_x1_pk_off, c_x1_pk_len); |
| 987 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 988 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 989 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 990 | } else { |
| 991 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 992 | } |
| 993 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 994 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 995 | buffer1 + c_x1_pr_off, c_x1_pr_len); |
| 996 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 997 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 998 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 999 | } else { |
| 1000 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1001 | } |
| 1002 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1003 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 1004 | buffer1 + c_g2_off, c_g2_len); |
| 1005 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1006 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1007 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1008 | } else { |
| 1009 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1010 | } |
| 1011 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1012 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1013 | buffer1 + c_x2_pk_off, c_x2_pk_len); |
| 1014 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1015 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1016 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1017 | } else { |
| 1018 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1019 | } |
| 1020 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1021 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1022 | buffer1 + c_x2_pr_off, c_x2_pr_len); |
| 1023 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1024 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1025 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1026 | } else { |
| 1027 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1028 | } |
| 1029 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1030 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1031 | if (inject_error == 2) { |
| 1032 | TEST_ASSERT( |
| 1033 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1034 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1035 | |
| 1036 | break; |
| 1037 | |
| 1038 | case 2: |
| 1039 | /* Server second round Output */ |
| 1040 | buffer0_off = 0; |
| 1041 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1042 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 1043 | buffer0 + buffer0_off, |
| 1044 | 512 - buffer0_off, &s_a_len)); |
| 1045 | TEST_EQUAL(s_a_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1046 | s_a_off = buffer0_off; |
| 1047 | buffer0_off += s_a_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1048 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1049 | buffer0 + buffer0_off, |
| 1050 | 512 - buffer0_off, &s_x2s_pk_len)); |
| 1051 | TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1052 | s_x2s_pk_off = buffer0_off; |
| 1053 | buffer0_off += s_x2s_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1054 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1055 | buffer0 + buffer0_off, |
| 1056 | 512 - buffer0_off, &s_x2s_pr_len)); |
| 1057 | TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1058 | s_x2s_pr_off = buffer0_off; |
| 1059 | buffer0_off += s_x2s_pr_len; |
| 1060 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1061 | if (inject_error == 3) { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1062 | buffer0[s_x2s_pk_off + 12] += 0x33; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1063 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1064 | } |
| 1065 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1066 | if (client_input_first == 1) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1067 | /* Client second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1068 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1069 | buffer0 + s_a_off, s_a_len); |
| 1070 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1071 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1072 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1073 | } else { |
| 1074 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1075 | } |
| 1076 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1077 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1078 | buffer0 + s_x2s_pk_off, |
| 1079 | s_x2s_pk_len); |
| 1080 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1081 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1082 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1083 | } else { |
| 1084 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1085 | } |
| 1086 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1087 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1088 | buffer0 + s_x2s_pr_off, |
| 1089 | s_x2s_pr_len); |
| 1090 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1091 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1092 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1093 | } else { |
| 1094 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1095 | } |
| 1096 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1097 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1098 | if (inject_error == 3) { |
| 1099 | TEST_ASSERT( |
| 1100 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1101 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | /* Client second round Output */ |
| 1105 | buffer1_off = 0; |
| 1106 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1107 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1108 | buffer1 + buffer1_off, |
| 1109 | 512 - buffer1_off, &c_a_len)); |
| 1110 | TEST_EQUAL(c_a_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1111 | c_a_off = buffer1_off; |
| 1112 | buffer1_off += c_a_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1113 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1114 | buffer1 + buffer1_off, |
| 1115 | 512 - buffer1_off, &c_x2s_pk_len)); |
| 1116 | TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1117 | c_x2s_pk_off = buffer1_off; |
| 1118 | buffer1_off += c_x2s_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1119 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1120 | buffer1 + buffer1_off, |
| 1121 | 512 - buffer1_off, &c_x2s_pr_len)); |
| 1122 | TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1123 | c_x2s_pr_off = buffer1_off; |
| 1124 | buffer1_off += c_x2s_pr_len; |
| 1125 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1126 | if (client_input_first == 0) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1127 | /* Client second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1128 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1129 | buffer0 + s_a_off, s_a_len); |
| 1130 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1131 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1132 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1133 | } else { |
| 1134 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1135 | } |
| 1136 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1137 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1138 | buffer0 + s_x2s_pk_off, |
| 1139 | s_x2s_pk_len); |
| 1140 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1141 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1142 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1143 | } else { |
| 1144 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1145 | } |
| 1146 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1147 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1148 | buffer0 + s_x2s_pr_off, |
| 1149 | s_x2s_pr_len); |
| 1150 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1151 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1152 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1153 | } else { |
| 1154 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1155 | } |
| 1156 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1157 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1158 | if (inject_error == 3) { |
| 1159 | TEST_ASSERT( |
| 1160 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1161 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1162 | } |
| 1163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1164 | if (inject_error == 4) { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1165 | buffer1[c_x2s_pk_off + 7] += 0x28; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1166 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1167 | } |
| 1168 | |
| 1169 | /* Server second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1170 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 1171 | buffer1 + c_a_off, c_a_len); |
| 1172 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1173 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1174 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1175 | } else { |
| 1176 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1177 | } |
| 1178 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1179 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1180 | buffer1 + c_x2s_pk_off, c_x2s_pk_len); |
| 1181 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1182 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1183 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1184 | } else { |
| 1185 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1186 | } |
| 1187 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1188 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1189 | buffer1 + c_x2s_pr_off, c_x2s_pr_len); |
| 1190 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1191 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1192 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1193 | } else { |
| 1194 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1195 | } |
| 1196 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1197 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1198 | if (inject_error == 4) { |
| 1199 | TEST_ASSERT( |
| 1200 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1201 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1202 | |
| 1203 | break; |
| 1204 | |
| 1205 | } |
| 1206 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1207 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1208 | mbedtls_free(buffer0); |
| 1209 | mbedtls_free(buffer1); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1210 | } |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 1211 | #endif /* PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1212 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1213 | typedef enum { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 1214 | INJECT_ERR_NONE = 0, |
| 1215 | INJECT_ERR_UNINITIALIZED_ACCESS, |
| 1216 | INJECT_ERR_DUPLICATE_SETUP, |
| 1217 | INJECT_ERR_INVALID_USER, |
| 1218 | INJECT_ERR_INVALID_PEER, |
| 1219 | INJECT_ERR_SET_USER, |
| 1220 | INJECT_ERR_SET_PEER, |
| 1221 | INJECT_EMPTY_IO_BUFFER, |
| 1222 | INJECT_UNKNOWN_STEP, |
| 1223 | INJECT_INVALID_FIRST_STEP, |
| 1224 | INJECT_WRONG_BUFFER_SIZE, |
| 1225 | INJECT_VALID_OPERATION_AFTER_FAILURE, |
| 1226 | INJECT_ANTICIPATE_KEY_DERIVATION_1, |
| 1227 | INJECT_ANTICIPATE_KEY_DERIVATION_2, |
| 1228 | } ecjpake_injected_failure_t; |
| 1229 | |
Paul Elliott | 01885fa | 2023-02-09 12:07:30 +0000 | [diff] [blame] | 1230 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Paul Elliott | 1243f93 | 2023-02-07 11:21:10 +0000 | [diff] [blame] | 1231 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1232 | static void interruptible_signverify_get_minmax_completes(uint32_t max_ops, |
| 1233 | psa_status_t expected_status, |
| 1234 | size_t *min_completes, |
| 1235 | size_t *max_completes) |
| 1236 | { |
| 1237 | |
| 1238 | /* This is slightly contrived, but we only really know that with a minimum |
| 1239 | value of max_ops that a successful operation should take more than one op |
| 1240 | to complete, and likewise that with a max_ops of |
| 1241 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */ |
| 1242 | if (max_ops == 0 || max_ops == 1) { |
Paul Elliott | c86d45e | 2023-02-15 17:38:05 +0000 | [diff] [blame] | 1243 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1244 | if (expected_status == PSA_SUCCESS) { |
| 1245 | *min_completes = 2; |
| 1246 | } else { |
| 1247 | *min_completes = 1; |
| 1248 | } |
| 1249 | |
| 1250 | *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED; |
| 1251 | } else { |
| 1252 | *min_completes = 1; |
| 1253 | *max_completes = 1; |
| 1254 | } |
| 1255 | } |
Paul Elliott | 01885fa | 2023-02-09 12:07:30 +0000 | [diff] [blame] | 1256 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1257 | |
Gilles Peskine | 1d25a0a | 2024-02-12 16:40:04 +0100 | [diff] [blame] | 1258 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) |
| 1259 | static int rsa_test_e(mbedtls_svc_key_id_t key, |
| 1260 | size_t bits, |
| 1261 | const data_t *e_arg) |
| 1262 | { |
| 1263 | uint8_t *exported = NULL; |
| 1264 | size_t exported_size = |
| 1265 | PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits); |
| 1266 | size_t exported_length = SIZE_MAX; |
| 1267 | int ok = 0; |
| 1268 | |
| 1269 | TEST_CALLOC(exported, exported_size); |
| 1270 | PSA_ASSERT(psa_export_public_key(key, |
| 1271 | exported, exported_size, |
| 1272 | &exported_length)); |
| 1273 | uint8_t *p = exported; |
| 1274 | uint8_t *end = exported + exported_length; |
| 1275 | size_t len; |
| 1276 | /* RSAPublicKey ::= SEQUENCE { |
| 1277 | * modulus INTEGER, -- n |
| 1278 | * publicExponent INTEGER } -- e |
| 1279 | */ |
| 1280 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 1281 | MBEDTLS_ASN1_SEQUENCE | |
| 1282 | MBEDTLS_ASN1_CONSTRUCTED)); |
| 1283 | TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1)); |
| 1284 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 1285 | MBEDTLS_ASN1_INTEGER)); |
| 1286 | if (len >= 1 && p[0] == 0) { |
| 1287 | ++p; |
| 1288 | --len; |
| 1289 | } |
| 1290 | if (e_arg->len == 0) { |
| 1291 | TEST_EQUAL(len, 3); |
| 1292 | TEST_EQUAL(p[0], 1); |
| 1293 | TEST_EQUAL(p[1], 0); |
| 1294 | TEST_EQUAL(p[2], 1); |
| 1295 | } else { |
Gilles Peskine | 7a18f96 | 2024-02-12 16:48:11 +0100 | [diff] [blame] | 1296 | const uint8_t *expected = e_arg->x; |
| 1297 | size_t expected_len = e_arg->len; |
| 1298 | while (expected_len > 0 && *expected == 0) { |
| 1299 | ++expected; |
| 1300 | --expected_len; |
| 1301 | } |
| 1302 | TEST_MEMORY_COMPARE(p, len, expected, expected_len); |
Gilles Peskine | 1d25a0a | 2024-02-12 16:40:04 +0100 | [diff] [blame] | 1303 | } |
| 1304 | ok = 1; |
| 1305 | |
| 1306 | exit: |
| 1307 | mbedtls_free(exported); |
| 1308 | return ok; |
| 1309 | } |
| 1310 | #endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE */ |
| 1311 | |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame^] | 1312 | static int setup_key_production_parameters( |
| 1313 | psa_key_production_parameters_t **params, size_t *params_data_length, |
| 1314 | int flags_arg, const data_t *params_data) |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 1315 | { |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame^] | 1316 | *params_data_length = params_data->len; |
Gilles Peskine | c81393b | 2024-02-14 20:51:28 +0100 | [diff] [blame] | 1317 | /* If there are N bytes of padding at the end of |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame^] | 1318 | * psa_key_production_parameters_t, then it's enough to allocate |
| 1319 | * MIN(sizeof(psa_key_production_parameters_t), |
| 1320 | * offsetof(psa_key_production_parameters_t, data) + params_data_length). |
Gilles Peskine | c81393b | 2024-02-14 20:51:28 +0100 | [diff] [blame] | 1321 | * |
| 1322 | * For simplicity, here, we allocate up to N more bytes than necessary. |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame^] | 1323 | * In practice, the current layout of psa_key_production_parameters_t |
Gilles Peskine | c81393b | 2024-02-14 20:51:28 +0100 | [diff] [blame] | 1324 | * makes padding extremely unlikely, so we don't worry about testing |
| 1325 | * that the library code doesn't try to access these extra N bytes. |
| 1326 | */ |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame^] | 1327 | *params = mbedtls_calloc(1, sizeof(**params) + *params_data_length); |
| 1328 | TEST_ASSERT(*params != NULL); |
| 1329 | (*params)->flags = (uint32_t) flags_arg; |
| 1330 | memcpy((*params)->data, params_data->x, params_data->len); |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 1331 | return 1; |
| 1332 | exit: |
| 1333 | return 0; |
| 1334 | } |
| 1335 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1336 | /* END_HEADER */ |
| 1337 | |
| 1338 | /* BEGIN_DEPENDENCIES |
| 1339 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1340 | * END_DEPENDENCIES |
| 1341 | */ |
| 1342 | |
| 1343 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 7abdf7e | 2023-03-09 11:17:43 +0100 | [diff] [blame] | 1344 | void psa_can_do_hash() |
| 1345 | { |
| 1346 | /* We can't test that this is specific to drivers until partial init has |
| 1347 | * been implemented, but we can at least test before/after full init. */ |
| 1348 | TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE)); |
| 1349 | PSA_INIT(); |
| 1350 | TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE)); |
| 1351 | PSA_DONE(); |
| 1352 | } |
| 1353 | /* END_CASE */ |
| 1354 | |
| 1355 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1356 | void static_checks() |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1357 | { |
| 1358 | size_t max_truncated_mac_size = |
| 1359 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1360 | |
| 1361 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1362 | * encoding. The shifted mask is the maximum truncated value. The |
| 1363 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1364 | TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size); |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1365 | } |
| 1366 | /* END_CASE */ |
| 1367 | |
| 1368 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1369 | void import_with_policy(int type_arg, |
| 1370 | int usage_arg, int alg_arg, |
| 1371 | int expected_status_arg) |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1372 | { |
| 1373 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1374 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1375 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1376 | psa_key_type_t type = type_arg; |
| 1377 | psa_key_usage_t usage = usage_arg; |
| 1378 | psa_algorithm_t alg = alg_arg; |
| 1379 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1380 | const uint8_t key_material[16] = { 0 }; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1381 | psa_status_t status; |
| 1382 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1383 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1385 | psa_set_key_type(&attributes, type); |
| 1386 | psa_set_key_usage_flags(&attributes, usage); |
| 1387 | psa_set_key_algorithm(&attributes, alg); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1389 | status = psa_import_key(&attributes, |
| 1390 | key_material, sizeof(key_material), |
| 1391 | &key); |
| 1392 | TEST_EQUAL(status, expected_status); |
| 1393 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1394 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1395 | } |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1396 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1397 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1398 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1399 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), |
| 1400 | mbedtls_test_update_key_usage_flags(usage)); |
| 1401 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 1402 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1403 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1404 | PSA_ASSERT(psa_destroy_key(key)); |
| 1405 | test_operations_on_invalid_key(key); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1406 | |
| 1407 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1408 | /* |
| 1409 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1410 | * thus reset them as required. |
| 1411 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1412 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1413 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1414 | psa_destroy_key(key); |
| 1415 | PSA_DONE(); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1416 | } |
| 1417 | /* END_CASE */ |
| 1418 | |
| 1419 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1420 | void import_with_data(data_t *data, int type_arg, |
| 1421 | int attr_bits_arg, |
| 1422 | int expected_status_arg) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1423 | { |
| 1424 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1425 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1426 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1427 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1428 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1429 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1430 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1431 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1432 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1433 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1434 | psa_set_key_type(&attributes, type); |
| 1435 | psa_set_key_bits(&attributes, attr_bits); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1436 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1437 | status = psa_import_key(&attributes, data->x, data->len, &key); |
Manuel Pégourié-Gonnard | 0509b58 | 2023-08-08 12:47:56 +0200 | [diff] [blame] | 1438 | /* When expecting INVALID_ARGUMENT, also accept NOT_SUPPORTED. |
| 1439 | * |
| 1440 | * This can happen with a type supported only by a driver: |
| 1441 | * - the driver sees the invalid data (for example wrong size) and thinks |
| 1442 | * "well perhaps this is a key size I don't support" so it returns |
| 1443 | * NOT_SUPPORTED which is correct at this point; |
| 1444 | * - we fallback to built-ins, which don't support this type, so return |
| 1445 | * NOT_SUPPORTED which again is correct at this point. |
| 1446 | */ |
| 1447 | if (expected_status == PSA_ERROR_INVALID_ARGUMENT && |
| 1448 | status == PSA_ERROR_NOT_SUPPORTED) { |
| 1449 | ; // OK |
| 1450 | } else { |
| 1451 | TEST_EQUAL(status, expected_status); |
| 1452 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1453 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1454 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1455 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1456 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1457 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1458 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1459 | if (attr_bits != 0) { |
| 1460 | TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes)); |
| 1461 | } |
| 1462 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1463 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1464 | PSA_ASSERT(psa_destroy_key(key)); |
| 1465 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1466 | |
| 1467 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1468 | /* |
| 1469 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1470 | * thus reset them as required. |
| 1471 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1472 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1473 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1474 | psa_destroy_key(key); |
| 1475 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1476 | } |
| 1477 | /* END_CASE */ |
| 1478 | |
| 1479 | /* BEGIN_CASE */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1480 | /* Construct and attempt to import a large unstructured key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1481 | void import_large_key(int type_arg, int byte_size_arg, |
| 1482 | int expected_status_arg) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1483 | { |
| 1484 | psa_key_type_t type = type_arg; |
| 1485 | size_t byte_size = byte_size_arg; |
| 1486 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1487 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1488 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1489 | psa_status_t status; |
| 1490 | uint8_t *buffer = NULL; |
| 1491 | size_t buffer_size = byte_size + 1; |
| 1492 | size_t n; |
| 1493 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1494 | /* Skip the test case if the target running the test cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1495 | * accommodate large keys due to heap size constraints */ |
Tom Cosgrove | 412a813 | 2023-07-20 16:55:14 +0100 | [diff] [blame] | 1496 | TEST_CALLOC_OR_SKIP(buffer, buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1497 | memset(buffer, 'K', byte_size); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1498 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1499 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1500 | |
| 1501 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1502 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1503 | psa_set_key_type(&attributes, type); |
| 1504 | status = psa_import_key(&attributes, buffer, byte_size, &key); |
| 1505 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 1506 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1507 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1508 | if (status == PSA_SUCCESS) { |
| 1509 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1510 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 1511 | TEST_EQUAL(psa_get_key_bits(&attributes), |
| 1512 | PSA_BYTES_TO_BITS(byte_size)); |
| 1513 | ASSERT_NO_SLOT_NUMBER(&attributes); |
| 1514 | memset(buffer, 0, byte_size + 1); |
| 1515 | PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n)); |
| 1516 | for (n = 0; n < byte_size; n++) { |
| 1517 | TEST_EQUAL(buffer[n], 'K'); |
| 1518 | } |
| 1519 | for (n = byte_size; n < buffer_size; n++) { |
| 1520 | TEST_EQUAL(buffer[n], 0); |
| 1521 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1522 | } |
| 1523 | |
| 1524 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1525 | /* |
| 1526 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1527 | * thus reset them as required. |
| 1528 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1529 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1530 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1531 | psa_destroy_key(key); |
| 1532 | PSA_DONE(); |
| 1533 | mbedtls_free(buffer); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1534 | } |
| 1535 | /* END_CASE */ |
| 1536 | |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 1537 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1538 | /* Import an RSA key with a valid structure (but not valid numbers |
| 1539 | * inside, beyond having sensible size and parity). This is expected to |
| 1540 | * fail for large keys. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1541 | 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] | 1542 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1543 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1544 | size_t bits = bits_arg; |
| 1545 | psa_status_t expected_status = expected_status_arg; |
| 1546 | psa_status_t status; |
| 1547 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1548 | 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] | 1549 | size_t buffer_size = /* Slight overapproximations */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1550 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1551 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1552 | unsigned char *p; |
| 1553 | int ret; |
| 1554 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1555 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1556 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1557 | PSA_ASSERT(psa_crypto_init()); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1558 | TEST_CALLOC(buffer, buffer_size); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1559 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1560 | TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p, |
| 1561 | bits, keypair)) >= 0); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1562 | length = ret; |
| 1563 | |
| 1564 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1565 | psa_set_key_type(&attributes, type); |
| 1566 | status = psa_import_key(&attributes, p, length, &key); |
| 1567 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1568 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1569 | if (status == PSA_SUCCESS) { |
| 1570 | PSA_ASSERT(psa_destroy_key(key)); |
| 1571 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1572 | |
| 1573 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1574 | mbedtls_free(buffer); |
| 1575 | PSA_DONE(); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1576 | } |
| 1577 | /* END_CASE */ |
| 1578 | |
| 1579 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1580 | void import_export(data_t *data, |
| 1581 | int type_arg, |
| 1582 | int usage_arg, int alg_arg, |
| 1583 | int lifetime_arg, |
| 1584 | int expected_bits, |
| 1585 | int export_size_delta, |
| 1586 | int expected_export_status_arg, |
| 1587 | /*whether reexport must give the original input exactly*/ |
| 1588 | int canonical_input) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1589 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1590 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1591 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1592 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1593 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1594 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1595 | psa_key_lifetime_t lifetime = lifetime_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1596 | unsigned char *exported = NULL; |
| 1597 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1598 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1599 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1600 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1601 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1602 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1603 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1604 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1605 | TEST_CALLOC(exported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1606 | if (!canonical_input) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1607 | TEST_CALLOC(reexported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1608 | } |
| 1609 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1610 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1611 | psa_set_key_lifetime(&attributes, lifetime); |
| 1612 | psa_set_key_usage_flags(&attributes, usage_arg); |
| 1613 | psa_set_key_algorithm(&attributes, alg); |
| 1614 | psa_set_key_type(&attributes, type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1615 | |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1616 | if (PSA_KEY_TYPE_IS_DH(type) && |
| 1617 | expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) { |
Przemek Stekiel | 654bef0 | 2022-12-15 13:28:02 +0100 | [diff] [blame] | 1618 | /* Simulate that buffer is too small, by decreasing its size by 1 byte. */ |
| 1619 | export_size -= 1; |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1620 | } |
| 1621 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1622 | /* Import the key */ |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1623 | TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key), |
Przemek Stekiel | 2e7c33d | 2023-04-27 12:29:45 +0200 | [diff] [blame] | 1624 | PSA_SUCCESS); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1625 | |
| 1626 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1627 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1628 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1629 | TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits); |
| 1630 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1631 | |
| 1632 | /* Export the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1633 | status = psa_export_key(key, exported, export_size, &exported_length); |
| 1634 | TEST_EQUAL(status, expected_export_status); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1635 | |
| 1636 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1637 | * and export_size. On errors, the exported length must be 0. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1638 | TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH); |
| 1639 | TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0); |
| 1640 | TEST_LE_U(exported_length, export_size); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1642 | TEST_ASSERT(mem_is_char(exported + exported_length, 0, |
| 1643 | export_size - exported_length)); |
| 1644 | if (status != PSA_SUCCESS) { |
| 1645 | TEST_EQUAL(exported_length, 0); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1646 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1647 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1648 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1649 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1650 | * this validates the canonical representations. For canonical inputs, |
| 1651 | * this doesn't directly validate the implementation, but it still helps |
| 1652 | * by cross-validating the test data with the sanity check code. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1653 | if (!psa_key_lifetime_is_external(lifetime)) { |
| 1654 | if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) { |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1655 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1656 | } |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1657 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1658 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1659 | if (canonical_input) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1660 | TEST_MEMORY_COMPARE(data->x, data->len, exported, exported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1661 | } else { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1662 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1663 | PSA_ASSERT(psa_import_key(&attributes, exported, exported_length, |
| 1664 | &key2)); |
| 1665 | PSA_ASSERT(psa_export_key(key2, |
| 1666 | reexported, |
| 1667 | export_size, |
| 1668 | &reexported_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1669 | TEST_MEMORY_COMPARE(exported, exported_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1670 | reexported, reexported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1671 | PSA_ASSERT(psa_destroy_key(key2)); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1672 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1673 | TEST_LE_U(exported_length, |
| 1674 | PSA_EXPORT_KEY_OUTPUT_SIZE(type, |
| 1675 | psa_get_key_bits(&got_attributes))); |
Valerio Setti | 1eacae8 | 2023-07-28 16:07:03 +0200 | [diff] [blame] | 1676 | if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 1677 | TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
| 1678 | } else if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) { |
| 1679 | TEST_LE_U(exported_length, PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
| 1680 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1681 | |
| 1682 | destroy: |
| 1683 | /* Destroy the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1684 | PSA_ASSERT(psa_destroy_key(key)); |
| 1685 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1686 | |
| 1687 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1688 | /* |
| 1689 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1690 | * thus reset them as required. |
| 1691 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1692 | psa_reset_key_attributes(&got_attributes); |
| 1693 | psa_destroy_key(key); |
| 1694 | mbedtls_free(exported); |
| 1695 | mbedtls_free(reexported); |
| 1696 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1697 | } |
| 1698 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1699 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1700 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1701 | void import_export_public_key(data_t *data, |
| 1702 | int type_arg, // key pair or public key |
| 1703 | int alg_arg, |
| 1704 | int lifetime_arg, |
| 1705 | int export_size_delta, |
| 1706 | int expected_export_status_arg, |
| 1707 | data_t *expected_public_key) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1708 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1709 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1710 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1711 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1712 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1713 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1714 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1715 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1716 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1717 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1718 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1719 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1720 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1721 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1722 | psa_set_key_lifetime(&attributes, lifetime); |
| 1723 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1724 | psa_set_key_algorithm(&attributes, alg); |
| 1725 | psa_set_key_type(&attributes, type); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1726 | |
| 1727 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1728 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1729 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1730 | /* Export the public key */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1731 | TEST_CALLOC(exported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1732 | status = psa_export_public_key(key, |
| 1733 | exported, export_size, |
| 1734 | &exported_length); |
| 1735 | TEST_EQUAL(status, expected_export_status); |
| 1736 | if (status == PSA_SUCCESS) { |
| 1737 | 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] | 1738 | size_t bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1739 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1740 | bits = psa_get_key_bits(&attributes); |
| 1741 | TEST_LE_U(expected_public_key->len, |
| 1742 | PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1743 | TEST_LE_U(expected_public_key->len, |
| 1744 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1745 | TEST_LE_U(expected_public_key->len, |
| 1746 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1747 | TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1748 | exported, exported_length); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1749 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1750 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1751 | /* |
| 1752 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1753 | * thus reset them as required. |
| 1754 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1755 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1756 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1757 | mbedtls_free(exported); |
| 1758 | psa_destroy_key(key); |
| 1759 | PSA_DONE(); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1760 | } |
| 1761 | /* END_CASE */ |
| 1762 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1763 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1764 | void import_and_exercise_key(data_t *data, |
| 1765 | int type_arg, |
| 1766 | int bits_arg, |
| 1767 | int alg_arg) |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1768 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1769 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1770 | psa_key_type_t type = type_arg; |
| 1771 | size_t bits = bits_arg; |
| 1772 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1773 | 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] | 1774 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1775 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1776 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1777 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1778 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1779 | psa_set_key_usage_flags(&attributes, usage); |
| 1780 | psa_set_key_algorithm(&attributes, alg); |
| 1781 | psa_set_key_type(&attributes, type); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1782 | |
| 1783 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1784 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1785 | |
| 1786 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1787 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1788 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1789 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1790 | |
| 1791 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1792 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1793 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1794 | } |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1795 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1796 | PSA_ASSERT(psa_destroy_key(key)); |
| 1797 | test_operations_on_invalid_key(key); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1798 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1799 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1800 | /* |
| 1801 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1802 | * thus reset them as required. |
| 1803 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1804 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1805 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1806 | psa_reset_key_attributes(&attributes); |
| 1807 | psa_destroy_key(key); |
| 1808 | PSA_DONE(); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1809 | } |
| 1810 | /* END_CASE */ |
| 1811 | |
| 1812 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1813 | void effective_key_attributes(int type_arg, int expected_type_arg, |
| 1814 | int bits_arg, int expected_bits_arg, |
| 1815 | int usage_arg, int expected_usage_arg, |
| 1816 | int alg_arg, int expected_alg_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1817 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1818 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1819 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1820 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1821 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1822 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1823 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1824 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1825 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1826 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1827 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1828 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1829 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1830 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1831 | psa_set_key_usage_flags(&attributes, usage); |
| 1832 | psa_set_key_algorithm(&attributes, alg); |
| 1833 | psa_set_key_type(&attributes, key_type); |
| 1834 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1835 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1836 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
| 1837 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1838 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1839 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1840 | TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type); |
| 1841 | TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits); |
| 1842 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
| 1843 | TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1844 | |
| 1845 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1846 | /* |
| 1847 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1848 | * thus reset them as required. |
| 1849 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1850 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1851 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1852 | psa_destroy_key(key); |
| 1853 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1854 | } |
| 1855 | /* END_CASE */ |
| 1856 | |
| 1857 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1858 | void check_key_policy(int type_arg, int bits_arg, |
| 1859 | int usage_arg, int alg_arg) |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1860 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1861 | test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg, |
| 1862 | usage_arg, |
| 1863 | mbedtls_test_update_key_usage_flags(usage_arg), |
| 1864 | alg_arg, alg_arg); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1865 | goto exit; |
| 1866 | } |
| 1867 | /* END_CASE */ |
| 1868 | |
| 1869 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1870 | void key_attributes_init() |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1871 | { |
| 1872 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1873 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1874 | * 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] | 1875 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1876 | psa_key_attributes_t func = psa_key_attributes_init(); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1877 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1878 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1879 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1880 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1881 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1882 | TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE); |
| 1883 | TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE); |
| 1884 | TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1885 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1886 | TEST_EQUAL(psa_get_key_type(&func), 0); |
| 1887 | TEST_EQUAL(psa_get_key_type(&init), 0); |
| 1888 | TEST_EQUAL(psa_get_key_type(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1889 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1890 | TEST_EQUAL(psa_get_key_bits(&func), 0); |
| 1891 | TEST_EQUAL(psa_get_key_bits(&init), 0); |
| 1892 | TEST_EQUAL(psa_get_key_bits(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1893 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1894 | TEST_EQUAL(psa_get_key_usage_flags(&func), 0); |
| 1895 | TEST_EQUAL(psa_get_key_usage_flags(&init), 0); |
| 1896 | TEST_EQUAL(psa_get_key_usage_flags(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1897 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1898 | TEST_EQUAL(psa_get_key_algorithm(&func), 0); |
| 1899 | TEST_EQUAL(psa_get_key_algorithm(&init), 0); |
| 1900 | TEST_EQUAL(psa_get_key_algorithm(&zero), 0); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1901 | } |
| 1902 | /* END_CASE */ |
| 1903 | |
| 1904 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1905 | void mac_key_policy(int policy_usage_arg, |
| 1906 | int policy_alg_arg, |
| 1907 | int key_type_arg, |
| 1908 | data_t *key_data, |
| 1909 | int exercise_alg_arg, |
| 1910 | int expected_status_sign_arg, |
| 1911 | int expected_status_verify_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1912 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1913 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1914 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1915 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1916 | psa_key_type_t key_type = key_type_arg; |
| 1917 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 1918 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 1919 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1920 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1921 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 1922 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1923 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1924 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1925 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1926 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1927 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1928 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1929 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1930 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1931 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1932 | &key)); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1933 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1934 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 1935 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1936 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1937 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 1938 | TEST_EQUAL(status, expected_status_sign); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1939 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1940 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1941 | uint8_t input[128] = { 0 }; |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1942 | size_t mac_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1943 | TEST_EQUAL(psa_mac_compute(key, exercise_alg, |
| 1944 | input, 128, |
| 1945 | mac, PSA_MAC_MAX_SIZE, &mac_len), |
| 1946 | expected_status_sign); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1947 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1948 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1949 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 1950 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 1951 | if (status == PSA_SUCCESS) { |
| 1952 | status = psa_mac_update(&operation, input, 128); |
| 1953 | if (status == PSA_SUCCESS) { |
| 1954 | TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE, |
| 1955 | &mac_len), |
| 1956 | expected_status_sign); |
| 1957 | } else { |
| 1958 | TEST_EQUAL(status, expected_status_sign); |
| 1959 | } |
| 1960 | } else { |
| 1961 | TEST_EQUAL(status, expected_status_sign); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1962 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1963 | PSA_ASSERT(psa_mac_abort(&operation)); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1964 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1965 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1966 | status = psa_mac_verify(key, exercise_alg, input, 128, |
| 1967 | mac, mac_len); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1968 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1969 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 1970 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1971 | } else { |
| 1972 | TEST_EQUAL(status, expected_status_verify); |
| 1973 | } |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1974 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1975 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1976 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 1977 | if (status == PSA_SUCCESS) { |
| 1978 | status = psa_mac_update(&operation, input, 128); |
| 1979 | if (status == PSA_SUCCESS) { |
| 1980 | status = psa_mac_verify_finish(&operation, mac, mac_len); |
| 1981 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 1982 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1983 | } else { |
| 1984 | TEST_EQUAL(status, expected_status_verify); |
| 1985 | } |
| 1986 | } else { |
| 1987 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1988 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1989 | } else { |
| 1990 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1991 | } |
| 1992 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1993 | psa_mac_abort(&operation); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1994 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1995 | memset(mac, 0, sizeof(mac)); |
| 1996 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 1997 | TEST_EQUAL(status, expected_status_verify); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1998 | |
| 1999 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2000 | psa_mac_abort(&operation); |
| 2001 | psa_destroy_key(key); |
| 2002 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2003 | } |
| 2004 | /* END_CASE */ |
| 2005 | |
| 2006 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2007 | void cipher_key_policy(int policy_usage_arg, |
| 2008 | int policy_alg, |
| 2009 | int key_type, |
| 2010 | data_t *key_data, |
| 2011 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2012 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2013 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2014 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2015 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2016 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2017 | size_t output_buffer_size = 0; |
| 2018 | size_t input_buffer_size = 0; |
| 2019 | size_t output_length = 0; |
| 2020 | uint8_t *output = NULL; |
| 2021 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2022 | psa_status_t status; |
| 2023 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2024 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg); |
| 2025 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg, |
| 2026 | input_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2027 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2028 | TEST_CALLOC(input, input_buffer_size); |
| 2029 | TEST_CALLOC(output, output_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2030 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2031 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2032 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2033 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2034 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2035 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2036 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2037 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2038 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2039 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2040 | /* Check if no key usage flag implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2041 | TEST_EQUAL(policy_usage, |
| 2042 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2043 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2044 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2045 | status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size, |
| 2046 | output, output_buffer_size, |
| 2047 | &output_length); |
| 2048 | if (policy_alg == exercise_alg && |
| 2049 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2050 | PSA_ASSERT(status); |
| 2051 | } else { |
| 2052 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2053 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2054 | |
| 2055 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2056 | status = psa_cipher_encrypt_setup(&operation, key, exercise_alg); |
| 2057 | if (policy_alg == exercise_alg && |
| 2058 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2059 | PSA_ASSERT(status); |
| 2060 | } else { |
| 2061 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2062 | } |
| 2063 | psa_cipher_abort(&operation); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2064 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2065 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2066 | status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size, |
| 2067 | input, input_buffer_size, |
| 2068 | &output_length); |
| 2069 | if (policy_alg == exercise_alg && |
| 2070 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2071 | PSA_ASSERT(status); |
| 2072 | } else { |
| 2073 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2074 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2075 | |
| 2076 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2077 | status = psa_cipher_decrypt_setup(&operation, key, exercise_alg); |
| 2078 | if (policy_alg == exercise_alg && |
| 2079 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2080 | PSA_ASSERT(status); |
| 2081 | } else { |
| 2082 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2083 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2084 | |
| 2085 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2086 | psa_cipher_abort(&operation); |
| 2087 | mbedtls_free(input); |
| 2088 | mbedtls_free(output); |
| 2089 | psa_destroy_key(key); |
| 2090 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2091 | } |
| 2092 | /* END_CASE */ |
| 2093 | |
| 2094 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2095 | void aead_key_policy(int policy_usage_arg, |
| 2096 | int policy_alg, |
| 2097 | int key_type, |
| 2098 | data_t *key_data, |
| 2099 | int nonce_length_arg, |
| 2100 | int tag_length_arg, |
| 2101 | int exercise_alg, |
| 2102 | int expected_status_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2103 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2104 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2105 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2106 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2107 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2108 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2109 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2110 | unsigned char nonce[16] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2111 | size_t nonce_length = nonce_length_arg; |
| 2112 | unsigned char tag[16]; |
| 2113 | size_t tag_length = tag_length_arg; |
| 2114 | size_t output_length; |
| 2115 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2116 | TEST_LE_U(nonce_length, sizeof(nonce)); |
| 2117 | TEST_LE_U(tag_length, sizeof(tag)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2118 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2119 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2120 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2121 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2122 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2123 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2124 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2125 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2126 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2127 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2128 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2129 | TEST_EQUAL(policy_usage, |
| 2130 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2131 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2132 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2133 | status = psa_aead_encrypt(key, exercise_alg, |
| 2134 | nonce, nonce_length, |
| 2135 | NULL, 0, |
| 2136 | NULL, 0, |
| 2137 | tag, tag_length, |
| 2138 | &output_length); |
| 2139 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2140 | TEST_EQUAL(status, expected_status); |
| 2141 | } else { |
| 2142 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2143 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2144 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2145 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2146 | status = psa_aead_encrypt_setup(&operation, key, exercise_alg); |
| 2147 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2148 | TEST_EQUAL(status, expected_status); |
| 2149 | } else { |
| 2150 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2151 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2152 | |
| 2153 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2154 | memset(tag, 0, sizeof(tag)); |
| 2155 | status = psa_aead_decrypt(key, exercise_alg, |
| 2156 | nonce, nonce_length, |
| 2157 | NULL, 0, |
| 2158 | tag, tag_length, |
| 2159 | NULL, 0, |
| 2160 | &output_length); |
| 2161 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2162 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2163 | } else if (expected_status == PSA_SUCCESS) { |
| 2164 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2165 | } else { |
| 2166 | TEST_EQUAL(status, expected_status); |
| 2167 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2168 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2169 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2170 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2171 | status = psa_aead_decrypt_setup(&operation, key, exercise_alg); |
| 2172 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2173 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2174 | } else { |
| 2175 | TEST_EQUAL(status, expected_status); |
| 2176 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2177 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2178 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2179 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2180 | psa_destroy_key(key); |
| 2181 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2182 | } |
| 2183 | /* END_CASE */ |
| 2184 | |
| 2185 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2186 | void asymmetric_encryption_key_policy(int policy_usage_arg, |
| 2187 | int policy_alg, |
| 2188 | int key_type, |
| 2189 | data_t *key_data, |
Valerio Setti | f202c29 | 2024-01-15 10:42:37 +0100 | [diff] [blame] | 2190 | int exercise_alg, |
| 2191 | int use_opaque_key) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2192 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2193 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2194 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2195 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2196 | psa_status_t status; |
| 2197 | size_t key_bits; |
| 2198 | size_t buffer_length; |
| 2199 | unsigned char *buffer = NULL; |
| 2200 | size_t output_length; |
| 2201 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2202 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2203 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2204 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2205 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2206 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2207 | |
Valerio Setti | f202c29 | 2024-01-15 10:42:37 +0100 | [diff] [blame] | 2208 | if (use_opaque_key) { |
| 2209 | psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION( |
| 2210 | PSA_KEY_PERSISTENCE_VOLATILE, TEST_DRIVER_LOCATION)); |
| 2211 | } |
| 2212 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2213 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2214 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2215 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2216 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2217 | TEST_EQUAL(policy_usage, |
| 2218 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2219 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2220 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 2221 | key_bits = psa_get_key_bits(&attributes); |
| 2222 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, |
| 2223 | exercise_alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2224 | TEST_CALLOC(buffer, buffer_length); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2225 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2226 | status = psa_asymmetric_encrypt(key, exercise_alg, |
| 2227 | NULL, 0, |
| 2228 | NULL, 0, |
| 2229 | buffer, buffer_length, |
| 2230 | &output_length); |
| 2231 | if (policy_alg == exercise_alg && |
| 2232 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2233 | PSA_ASSERT(status); |
| 2234 | } else { |
| 2235 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2236 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2237 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2238 | if (buffer_length != 0) { |
| 2239 | memset(buffer, 0, buffer_length); |
| 2240 | } |
| 2241 | status = psa_asymmetric_decrypt(key, exercise_alg, |
| 2242 | buffer, buffer_length, |
| 2243 | NULL, 0, |
| 2244 | buffer, buffer_length, |
| 2245 | &output_length); |
| 2246 | if (policy_alg == exercise_alg && |
| 2247 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2248 | TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING); |
| 2249 | } else { |
| 2250 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2251 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2252 | |
| 2253 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2254 | /* |
| 2255 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2256 | * thus reset them as required. |
| 2257 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2258 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2259 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2260 | psa_destroy_key(key); |
| 2261 | PSA_DONE(); |
| 2262 | mbedtls_free(buffer); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2263 | } |
| 2264 | /* END_CASE */ |
| 2265 | |
| 2266 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2267 | void asymmetric_signature_key_policy(int policy_usage_arg, |
| 2268 | int policy_alg, |
| 2269 | int key_type, |
| 2270 | data_t *key_data, |
| 2271 | int exercise_alg, |
| 2272 | int payload_length_arg, |
| 2273 | int expected_usage_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2274 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2275 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2276 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2277 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 2278 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2279 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2280 | unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 }; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2281 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2282 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2283 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2284 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2285 | int compatible_alg = payload_length_arg > 0; |
| 2286 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2287 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2288 | size_t signature_length; |
| 2289 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2290 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2291 | in the expected usage flags. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2292 | TEST_EQUAL(expected_usage, |
| 2293 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2294 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2295 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2296 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2297 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2298 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2299 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2300 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2301 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2302 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2303 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2304 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2306 | status = psa_sign_hash(key, exercise_alg, |
| 2307 | payload, payload_length, |
| 2308 | signature, sizeof(signature), |
| 2309 | &signature_length); |
| 2310 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) { |
| 2311 | PSA_ASSERT(status); |
| 2312 | } else { |
| 2313 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2314 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2315 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2316 | memset(signature, 0, sizeof(signature)); |
| 2317 | status = psa_verify_hash(key, exercise_alg, |
| 2318 | payload, payload_length, |
| 2319 | signature, sizeof(signature)); |
| 2320 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) { |
| 2321 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2322 | } else { |
| 2323 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2324 | } |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2325 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2326 | if (PSA_ALG_IS_SIGN_HASH(exercise_alg) && |
| 2327 | PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) { |
| 2328 | status = psa_sign_message(key, exercise_alg, |
| 2329 | payload, payload_length, |
| 2330 | signature, sizeof(signature), |
| 2331 | &signature_length); |
| 2332 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) { |
| 2333 | PSA_ASSERT(status); |
| 2334 | } else { |
| 2335 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2336 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2337 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2338 | memset(signature, 0, sizeof(signature)); |
| 2339 | status = psa_verify_message(key, exercise_alg, |
| 2340 | payload, payload_length, |
| 2341 | signature, sizeof(signature)); |
| 2342 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) { |
| 2343 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2344 | } else { |
| 2345 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2346 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2347 | } |
| 2348 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2349 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2350 | psa_destroy_key(key); |
| 2351 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2352 | } |
| 2353 | /* END_CASE */ |
| 2354 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2355 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2356 | void derive_key_policy(int policy_usage, |
| 2357 | int policy_alg, |
| 2358 | int key_type, |
| 2359 | data_t *key_data, |
| 2360 | int exercise_alg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2361 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2362 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2363 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2364 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2365 | psa_status_t status; |
| 2366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2367 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2368 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2369 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2370 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2371 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2372 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2373 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2374 | &key)); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2375 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2376 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2377 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2378 | if (PSA_ALG_IS_TLS12_PRF(exercise_alg) || |
| 2379 | PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) { |
| 2380 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 2381 | &operation, |
| 2382 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2383 | (const uint8_t *) "", 0)); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2384 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2386 | status = psa_key_derivation_input_key(&operation, |
| 2387 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 2388 | key); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2389 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2390 | if (policy_alg == exercise_alg && |
| 2391 | (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) { |
| 2392 | PSA_ASSERT(status); |
| 2393 | } else { |
| 2394 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2395 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2396 | |
| 2397 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2398 | psa_key_derivation_abort(&operation); |
| 2399 | psa_destroy_key(key); |
| 2400 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2401 | } |
| 2402 | /* END_CASE */ |
| 2403 | |
| 2404 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2405 | void agreement_key_policy(int policy_usage, |
| 2406 | int policy_alg, |
| 2407 | int key_type_arg, |
| 2408 | data_t *key_data, |
| 2409 | int exercise_alg, |
| 2410 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2411 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2412 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2413 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2414 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2415 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2416 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2417 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2418 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2419 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2420 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2421 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2422 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2423 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2424 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2425 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2426 | &key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2427 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2428 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
| 2429 | status = mbedtls_test_psa_key_agreement_with_self(&operation, key); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2430 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2431 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2432 | |
| 2433 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2434 | psa_key_derivation_abort(&operation); |
| 2435 | psa_destroy_key(key); |
| 2436 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +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 key_policy_alg2(int key_type_arg, data_t *key_data, |
| 2442 | int usage_arg, int alg_arg, int alg2_arg) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2443 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2444 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2445 | psa_key_type_t key_type = key_type_arg; |
| 2446 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2447 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2448 | psa_key_usage_t usage = usage_arg; |
| 2449 | psa_algorithm_t alg = alg_arg; |
| 2450 | psa_algorithm_t alg2 = alg2_arg; |
| 2451 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2452 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2453 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2454 | psa_set_key_usage_flags(&attributes, usage); |
| 2455 | psa_set_key_algorithm(&attributes, alg); |
| 2456 | psa_set_key_enrollment_algorithm(&attributes, alg2); |
| 2457 | psa_set_key_type(&attributes, key_type); |
| 2458 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2459 | &key)); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2460 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2461 | /* Update the usage flags to obtain implicit usage flags */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2462 | usage = mbedtls_test_update_key_usage_flags(usage); |
| 2463 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 2464 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage); |
| 2465 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 2466 | TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2467 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2468 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2469 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2470 | } |
| 2471 | if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2472 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2473 | } |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2474 | |
| 2475 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2476 | /* |
| 2477 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2478 | * thus reset them as required. |
| 2479 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2480 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2481 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2482 | psa_destroy_key(key); |
| 2483 | PSA_DONE(); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2484 | } |
| 2485 | /* END_CASE */ |
| 2486 | |
| 2487 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2488 | void raw_agreement_key_policy(int policy_usage, |
| 2489 | int policy_alg, |
| 2490 | int key_type_arg, |
| 2491 | data_t *key_data, |
| 2492 | int exercise_alg, |
| 2493 | int expected_status_arg) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2494 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2495 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2496 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2497 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2498 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2499 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2500 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2501 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2502 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2503 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2504 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2505 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2506 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2507 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2508 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2509 | &key)); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2510 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2511 | status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2512 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2513 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2514 | |
| 2515 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2516 | psa_key_derivation_abort(&operation); |
| 2517 | psa_destroy_key(key); |
| 2518 | PSA_DONE(); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2519 | } |
| 2520 | /* END_CASE */ |
| 2521 | |
| 2522 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2523 | void copy_success(int source_usage_arg, |
| 2524 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4ea4ad0 | 2022-12-04 15:11:00 +0100 | [diff] [blame] | 2525 | int source_lifetime_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2526 | int type_arg, data_t *material, |
| 2527 | int copy_attributes, |
| 2528 | int target_usage_arg, |
| 2529 | int target_alg_arg, int target_alg2_arg, |
Gilles Peskine | 4ea4ad0 | 2022-12-04 15:11:00 +0100 | [diff] [blame] | 2530 | int target_lifetime_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2531 | int expected_usage_arg, |
| 2532 | int expected_alg_arg, int expected_alg2_arg) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2533 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2534 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2535 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2536 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2537 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2538 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2539 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 2540 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2541 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2542 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2543 | uint8_t *export_buffer = NULL; |
| 2544 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2545 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2546 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2547 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2548 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2549 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2550 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2551 | psa_set_key_type(&source_attributes, type_arg); |
| 2552 | psa_set_key_lifetime(&source_attributes, source_lifetime); |
| 2553 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2554 | material->x, material->len, |
| 2555 | &source_key)); |
| 2556 | PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2557 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2558 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2559 | if (copy_attributes) { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2560 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2561 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2562 | psa_set_key_lifetime(&target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2563 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2564 | if (target_usage_arg != -1) { |
| 2565 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2566 | } |
| 2567 | if (target_alg_arg != -1) { |
| 2568 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2569 | } |
| 2570 | if (target_alg2_arg != -1) { |
| 2571 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
| 2572 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2573 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2574 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2575 | /* Copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2576 | PSA_ASSERT(psa_copy_key(source_key, |
| 2577 | &target_attributes, &target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2578 | |
| 2579 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2580 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2581 | |
| 2582 | /* Test that the target slot has the expected content and policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2583 | PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes)); |
| 2584 | TEST_EQUAL(psa_get_key_type(&source_attributes), |
| 2585 | psa_get_key_type(&target_attributes)); |
| 2586 | TEST_EQUAL(psa_get_key_bits(&source_attributes), |
| 2587 | psa_get_key_bits(&target_attributes)); |
| 2588 | TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes)); |
| 2589 | TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes)); |
| 2590 | TEST_EQUAL(expected_alg2, |
| 2591 | psa_get_key_enrollment_algorithm(&target_attributes)); |
| 2592 | if (expected_usage & PSA_KEY_USAGE_EXPORT) { |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2593 | size_t length; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2594 | TEST_CALLOC(export_buffer, material->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2595 | PSA_ASSERT(psa_export_key(target_key, export_buffer, |
| 2596 | material->len, &length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2597 | TEST_MEMORY_COMPARE(material->x, material->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2598 | export_buffer, length); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2599 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2600 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2601 | if (!psa_key_lifetime_is_external(target_lifetime)) { |
| 2602 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2603 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2604 | } |
| 2605 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2606 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2607 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2608 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2609 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2610 | PSA_ASSERT(psa_destroy_key(target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2611 | |
| 2612 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2613 | /* |
| 2614 | * Source and target key attributes may have been returned by |
| 2615 | * psa_get_key_attributes() thus reset them as required. |
| 2616 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2617 | psa_reset_key_attributes(&source_attributes); |
| 2618 | psa_reset_key_attributes(&target_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2619 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2620 | PSA_DONE(); |
| 2621 | mbedtls_free(export_buffer); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2622 | } |
| 2623 | /* END_CASE */ |
| 2624 | |
| 2625 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2626 | void copy_fail(int source_usage_arg, |
| 2627 | int source_alg_arg, int source_alg2_arg, |
| 2628 | int source_lifetime_arg, |
| 2629 | int type_arg, data_t *material, |
| 2630 | int target_type_arg, int target_bits_arg, |
| 2631 | int target_usage_arg, |
| 2632 | int target_alg_arg, int target_alg2_arg, |
| 2633 | int target_id_arg, int target_lifetime_arg, |
| 2634 | int expected_status_arg) |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2635 | { |
| 2636 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2637 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2638 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2639 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2640 | 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] | 2641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2642 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2643 | |
| 2644 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2645 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2646 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2647 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2648 | psa_set_key_type(&source_attributes, type_arg); |
| 2649 | psa_set_key_lifetime(&source_attributes, source_lifetime_arg); |
| 2650 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2651 | material->x, material->len, |
| 2652 | &source_key)); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2653 | |
| 2654 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2655 | psa_set_key_id(&target_attributes, key_id); |
| 2656 | psa_set_key_lifetime(&target_attributes, target_lifetime_arg); |
| 2657 | psa_set_key_type(&target_attributes, target_type_arg); |
| 2658 | psa_set_key_bits(&target_attributes, target_bits_arg); |
| 2659 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2660 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2661 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2662 | |
| 2663 | /* Try to copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2664 | TEST_EQUAL(psa_copy_key(source_key, |
| 2665 | &target_attributes, &target_key), |
| 2666 | expected_status_arg); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2667 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2668 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2669 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2670 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2671 | psa_reset_key_attributes(&source_attributes); |
| 2672 | psa_reset_key_attributes(&target_attributes); |
| 2673 | PSA_DONE(); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2674 | } |
| 2675 | /* END_CASE */ |
| 2676 | |
| 2677 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2678 | void hash_operation_init() |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2679 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2680 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2681 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2682 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2683 | * 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] | 2684 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2685 | psa_hash_operation_t func = psa_hash_operation_init(); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2686 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2687 | psa_hash_operation_t zero; |
| 2688 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2689 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2690 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2691 | /* A freshly-initialized hash operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2692 | TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)), |
| 2693 | PSA_ERROR_BAD_STATE); |
| 2694 | TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)), |
| 2695 | PSA_ERROR_BAD_STATE); |
| 2696 | TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)), |
| 2697 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2698 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2699 | /* A default hash operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2700 | PSA_ASSERT(psa_hash_abort(&func)); |
| 2701 | PSA_ASSERT(psa_hash_abort(&init)); |
| 2702 | PSA_ASSERT(psa_hash_abort(&zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2703 | } |
| 2704 | /* END_CASE */ |
| 2705 | |
| 2706 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2707 | void hash_setup(int alg_arg, |
| 2708 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2709 | { |
| 2710 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2711 | uint8_t *output = NULL; |
| 2712 | size_t output_size = 0; |
| 2713 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2714 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2715 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2716 | psa_status_t status; |
| 2717 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2718 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2719 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2720 | /* Hash Setup, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2721 | output_size = PSA_HASH_LENGTH(alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2722 | TEST_CALLOC(output, output_size); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2723 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2724 | status = psa_hash_compute(alg, NULL, 0, |
| 2725 | output, output_size, &output_length); |
| 2726 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2727 | |
| 2728 | /* Hash Setup, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2729 | status = psa_hash_setup(&operation, alg); |
| 2730 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2731 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2732 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2733 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2734 | |
| 2735 | /* If setup failed, reproduce the failure, so as to |
| 2736 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2737 | if (status != PSA_SUCCESS) { |
| 2738 | TEST_EQUAL(psa_hash_setup(&operation, alg), status); |
| 2739 | } |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2740 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2741 | /* Now the operation object should be reusable. */ |
| 2742 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2743 | PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG)); |
| 2744 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2745 | #endif |
| 2746 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2747 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2748 | mbedtls_free(output); |
| 2749 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2750 | } |
| 2751 | /* END_CASE */ |
| 2752 | |
| 2753 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2754 | void hash_compute_fail(int alg_arg, data_t *input, |
| 2755 | int output_size_arg, int expected_status_arg) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2756 | { |
| 2757 | psa_algorithm_t alg = alg_arg; |
| 2758 | uint8_t *output = NULL; |
| 2759 | size_t output_size = output_size_arg; |
| 2760 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2761 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2762 | psa_status_t expected_status = expected_status_arg; |
| 2763 | psa_status_t status; |
| 2764 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2765 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2766 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2767 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2768 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2769 | /* Hash Compute, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2770 | status = psa_hash_compute(alg, input->x, input->len, |
| 2771 | output, output_size, &output_length); |
| 2772 | TEST_EQUAL(status, expected_status); |
| 2773 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2774 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2775 | /* Hash Compute, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2776 | status = psa_hash_setup(&operation, alg); |
| 2777 | if (status == PSA_SUCCESS) { |
| 2778 | status = psa_hash_update(&operation, input->x, input->len); |
| 2779 | if (status == PSA_SUCCESS) { |
| 2780 | status = psa_hash_finish(&operation, output, output_size, |
| 2781 | &output_length); |
| 2782 | if (status == PSA_SUCCESS) { |
| 2783 | TEST_LE_U(output_length, output_size); |
| 2784 | } else { |
| 2785 | TEST_EQUAL(status, expected_status); |
| 2786 | } |
| 2787 | } else { |
| 2788 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2789 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2790 | } else { |
| 2791 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2792 | } |
| 2793 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2794 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2795 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2796 | mbedtls_free(output); |
| 2797 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2798 | } |
| 2799 | /* END_CASE */ |
| 2800 | |
| 2801 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2802 | void hash_compare_fail(int alg_arg, data_t *input, |
| 2803 | data_t *reference_hash, |
| 2804 | int expected_status_arg) |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2805 | { |
| 2806 | psa_algorithm_t alg = alg_arg; |
| 2807 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2808 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2809 | psa_status_t status; |
| 2810 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2811 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2812 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2813 | /* Hash Compare, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2814 | status = psa_hash_compare(alg, input->x, input->len, |
| 2815 | reference_hash->x, reference_hash->len); |
| 2816 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2817 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2818 | /* Hash Compare, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2819 | status = psa_hash_setup(&operation, alg); |
| 2820 | if (status == PSA_SUCCESS) { |
| 2821 | status = psa_hash_update(&operation, input->x, input->len); |
| 2822 | if (status == PSA_SUCCESS) { |
| 2823 | status = psa_hash_verify(&operation, reference_hash->x, |
| 2824 | reference_hash->len); |
| 2825 | TEST_EQUAL(status, expected_status); |
| 2826 | } else { |
| 2827 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2828 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2829 | } else { |
| 2830 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2831 | } |
| 2832 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2833 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2834 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2835 | PSA_DONE(); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2836 | } |
| 2837 | /* END_CASE */ |
| 2838 | |
| 2839 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2840 | void hash_compute_compare(int alg_arg, data_t *input, |
| 2841 | data_t *expected_output) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2842 | { |
| 2843 | psa_algorithm_t alg = alg_arg; |
| 2844 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2845 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2846 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2847 | size_t i; |
| 2848 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2849 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2850 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2851 | /* Compute with tight buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2852 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 2853 | output, PSA_HASH_LENGTH(alg), |
| 2854 | &output_length)); |
| 2855 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2856 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2857 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2858 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2859 | /* Compute with tight buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2860 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2861 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2862 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 2863 | PSA_HASH_LENGTH(alg), |
| 2864 | &output_length)); |
| 2865 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2866 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2867 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2868 | |
| 2869 | /* Compute with larger buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2870 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 2871 | output, sizeof(output), |
| 2872 | &output_length)); |
| 2873 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2874 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2875 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2876 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2877 | /* Compute with larger buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2878 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2879 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2880 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 2881 | sizeof(output), &output_length)); |
| 2882 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2883 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2884 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2885 | |
| 2886 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2887 | PSA_ASSERT(psa_hash_compare(alg, input->x, input->len, |
| 2888 | output, output_length)); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2889 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2890 | /* Compare with correct hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2891 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2892 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2893 | PSA_ASSERT(psa_hash_verify(&operation, output, |
| 2894 | output_length)); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2895 | |
| 2896 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2897 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2898 | output, output_length + 1), |
| 2899 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2900 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2901 | /* Compare with trailing garbage, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2902 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2903 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2904 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1), |
| 2905 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2906 | |
| 2907 | /* Compare with truncated hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2908 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2909 | output, output_length - 1), |
| 2910 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2911 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2912 | /* Compare with truncated hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2913 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2914 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2915 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1), |
| 2916 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2917 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2918 | /* Compare with corrupted value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2919 | for (i = 0; i < output_length; i++) { |
| 2920 | mbedtls_test_set_step(i); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2921 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2922 | |
| 2923 | /* One-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2924 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2925 | output, output_length), |
| 2926 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2927 | |
| 2928 | /* Multi-Part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2929 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2930 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2931 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length), |
| 2932 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2933 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2934 | output[i] ^= 1; |
| 2935 | } |
| 2936 | |
| 2937 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2938 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2939 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2940 | } |
| 2941 | /* END_CASE */ |
| 2942 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2943 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2944 | void hash_bad_order() |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2945 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2946 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2947 | unsigned char input[] = ""; |
| 2948 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2949 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2950 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2951 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2952 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 |
| 2953 | }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2954 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2955 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2956 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2957 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2958 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2959 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2960 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2961 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2962 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2963 | TEST_EQUAL(psa_hash_setup(&operation, alg), |
| 2964 | PSA_ERROR_BAD_STATE); |
| 2965 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2966 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2967 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2968 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2969 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2970 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2971 | PSA_ERROR_BAD_STATE); |
| 2972 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2973 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2974 | /* Check that update calls abort on error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2975 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 2976 | operation.id = UINT_MAX; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2977 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2978 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2979 | PSA_ERROR_BAD_STATE); |
| 2980 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2981 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2982 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2983 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2984 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2985 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2986 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2987 | hash, sizeof(hash), &hash_len)); |
| 2988 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2989 | PSA_ERROR_BAD_STATE); |
| 2990 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2991 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2992 | /* Call verify without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2993 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2994 | valid_hash, sizeof(valid_hash)), |
| 2995 | PSA_ERROR_BAD_STATE); |
| 2996 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2997 | |
| 2998 | /* Call verify after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2999 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3000 | PSA_ASSERT(psa_hash_finish(&operation, |
| 3001 | hash, sizeof(hash), &hash_len)); |
| 3002 | TEST_EQUAL(psa_hash_verify(&operation, |
| 3003 | valid_hash, sizeof(valid_hash)), |
| 3004 | PSA_ERROR_BAD_STATE); |
| 3005 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3006 | |
| 3007 | /* Call verify twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3008 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3009 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3010 | PSA_ASSERT(psa_hash_verify(&operation, |
| 3011 | valid_hash, sizeof(valid_hash))); |
| 3012 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3013 | TEST_EQUAL(psa_hash_verify(&operation, |
| 3014 | valid_hash, sizeof(valid_hash)), |
| 3015 | PSA_ERROR_BAD_STATE); |
| 3016 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3017 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3018 | |
| 3019 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3020 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3021 | hash, sizeof(hash), &hash_len), |
| 3022 | PSA_ERROR_BAD_STATE); |
| 3023 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3024 | |
| 3025 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3026 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3027 | PSA_ASSERT(psa_hash_finish(&operation, |
| 3028 | hash, sizeof(hash), &hash_len)); |
| 3029 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3030 | hash, sizeof(hash), &hash_len), |
| 3031 | PSA_ERROR_BAD_STATE); |
| 3032 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3033 | |
| 3034 | /* Call finish after calling verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3035 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3036 | PSA_ASSERT(psa_hash_verify(&operation, |
| 3037 | valid_hash, sizeof(valid_hash))); |
| 3038 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3039 | hash, sizeof(hash), &hash_len), |
| 3040 | PSA_ERROR_BAD_STATE); |
| 3041 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3042 | |
| 3043 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3044 | PSA_DONE(); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3045 | } |
| 3046 | /* END_CASE */ |
| 3047 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3048 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3049 | void hash_verify_bad_args() |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3050 | { |
| 3051 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3052 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 3053 | * appended to it */ |
| 3054 | unsigned char hash[] = { |
| 3055 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 3056 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3057 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb |
| 3058 | }; |
| 3059 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 3060 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3061 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3062 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3063 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3064 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3065 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3066 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3067 | TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1), |
| 3068 | PSA_ERROR_INVALID_SIGNATURE); |
| 3069 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3070 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3071 | ASSERT_OPERATION_IS_INACTIVE(operation); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3072 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3073 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3074 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3075 | TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size), |
| 3076 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3077 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3078 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3079 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3080 | TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)), |
| 3081 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 3082 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3083 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3084 | PSA_DONE(); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3085 | } |
| 3086 | /* END_CASE */ |
| 3087 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3088 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3089 | void hash_finish_bad_args() |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3090 | { |
| 3091 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 3092 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3093 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 3094 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3095 | size_t hash_len; |
| 3096 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3097 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3098 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3099 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3100 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3101 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3102 | hash, expected_size - 1, &hash_len), |
| 3103 | PSA_ERROR_BUFFER_TOO_SMALL); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3104 | |
| 3105 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3106 | PSA_DONE(); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3107 | } |
| 3108 | /* END_CASE */ |
| 3109 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3110 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3111 | void hash_clone_source_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3112 | { |
| 3113 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3114 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3115 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 3116 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3117 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3118 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3119 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3120 | size_t hash_len; |
| 3121 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3122 | PSA_ASSERT(psa_crypto_init()); |
| 3123 | PSA_ASSERT(psa_hash_setup(&op_source, alg)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3124 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3125 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3126 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3127 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3128 | hash, sizeof(hash), &hash_len)); |
| 3129 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3130 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3131 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3132 | TEST_EQUAL(psa_hash_clone(&op_source, &op_setup), |
| 3133 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3135 | PSA_ASSERT(psa_hash_clone(&op_source, &op_init)); |
| 3136 | PSA_ASSERT(psa_hash_finish(&op_init, |
| 3137 | hash, sizeof(hash), &hash_len)); |
| 3138 | PSA_ASSERT(psa_hash_clone(&op_source, &op_finished)); |
| 3139 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3140 | hash, sizeof(hash), &hash_len)); |
| 3141 | PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted)); |
| 3142 | PSA_ASSERT(psa_hash_finish(&op_aborted, |
| 3143 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3144 | |
| 3145 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3146 | psa_hash_abort(&op_source); |
| 3147 | psa_hash_abort(&op_init); |
| 3148 | psa_hash_abort(&op_setup); |
| 3149 | psa_hash_abort(&op_finished); |
| 3150 | psa_hash_abort(&op_aborted); |
| 3151 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3152 | } |
| 3153 | /* END_CASE */ |
| 3154 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3155 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3156 | void hash_clone_target_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3157 | { |
| 3158 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3159 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3160 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3161 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3162 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3163 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3164 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 3165 | size_t hash_len; |
| 3166 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3167 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3168 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3169 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3170 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3171 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3172 | hash, sizeof(hash), &hash_len)); |
| 3173 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3174 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3175 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3176 | PSA_ASSERT(psa_hash_clone(&op_setup, &op_target)); |
| 3177 | PSA_ASSERT(psa_hash_finish(&op_target, |
| 3178 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3179 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3180 | TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE); |
| 3181 | TEST_EQUAL(psa_hash_clone(&op_finished, &op_target), |
| 3182 | PSA_ERROR_BAD_STATE); |
| 3183 | TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target), |
| 3184 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3185 | |
| 3186 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3187 | psa_hash_abort(&op_target); |
| 3188 | psa_hash_abort(&op_init); |
| 3189 | psa_hash_abort(&op_setup); |
| 3190 | psa_hash_abort(&op_finished); |
| 3191 | psa_hash_abort(&op_aborted); |
| 3192 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3193 | } |
| 3194 | /* END_CASE */ |
| 3195 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3196 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3197 | void mac_operation_init() |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3198 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3199 | const uint8_t input[1] = { 0 }; |
| 3200 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3201 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3202 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3203 | * 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] | 3204 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3205 | psa_mac_operation_t func = psa_mac_operation_init(); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3206 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 3207 | psa_mac_operation_t zero; |
| 3208 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3209 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3210 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3211 | /* A freshly-initialized MAC operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3212 | TEST_EQUAL(psa_mac_update(&func, |
| 3213 | input, sizeof(input)), |
| 3214 | PSA_ERROR_BAD_STATE); |
| 3215 | TEST_EQUAL(psa_mac_update(&init, |
| 3216 | input, sizeof(input)), |
| 3217 | PSA_ERROR_BAD_STATE); |
| 3218 | TEST_EQUAL(psa_mac_update(&zero, |
| 3219 | input, sizeof(input)), |
| 3220 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3221 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3222 | /* A default MAC operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3223 | PSA_ASSERT(psa_mac_abort(&func)); |
| 3224 | PSA_ASSERT(psa_mac_abort(&init)); |
| 3225 | PSA_ASSERT(psa_mac_abort(&zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3226 | } |
| 3227 | /* END_CASE */ |
| 3228 | |
| 3229 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3230 | void mac_setup(int key_type_arg, |
| 3231 | data_t *key, |
| 3232 | int alg_arg, |
| 3233 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3234 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3235 | psa_key_type_t key_type = key_type_arg; |
| 3236 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3237 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3238 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3239 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3240 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3241 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3242 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3243 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3244 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3245 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3246 | if (!exercise_mac_setup(key_type, key->x, key->len, alg, |
| 3247 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3248 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3249 | } |
| 3250 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3251 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3252 | /* The operation object should be reusable. */ |
| 3253 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3254 | if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 3255 | smoke_test_key_data, |
| 3256 | sizeof(smoke_test_key_data), |
| 3257 | KNOWN_SUPPORTED_MAC_ALG, |
| 3258 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3259 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3260 | } |
| 3261 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3262 | #endif |
| 3263 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3264 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3265 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3266 | } |
| 3267 | /* END_CASE */ |
| 3268 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3269 | /* 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] | 3270 | void mac_bad_order() |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3271 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3272 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3273 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 3274 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3275 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3276 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3277 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3278 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa |
| 3279 | }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3280 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3281 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3282 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 3283 | size_t sign_mac_length = 0; |
| 3284 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3285 | const uint8_t verify_mac[] = { |
| 3286 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 3287 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3288 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 |
| 3289 | }; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3290 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3291 | PSA_ASSERT(psa_crypto_init()); |
| 3292 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 3293 | psa_set_key_algorithm(&attributes, alg); |
| 3294 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3295 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3296 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3297 | &key)); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3298 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3299 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3300 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3301 | PSA_ERROR_BAD_STATE); |
| 3302 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3303 | |
| 3304 | /* Call sign finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3305 | TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac), |
| 3306 | &sign_mac_length), |
| 3307 | PSA_ERROR_BAD_STATE); |
| 3308 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3309 | |
| 3310 | /* Call verify finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3311 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3312 | verify_mac, sizeof(verify_mac)), |
| 3313 | PSA_ERROR_BAD_STATE); |
| 3314 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3315 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3316 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3317 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3318 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3319 | TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg), |
| 3320 | PSA_ERROR_BAD_STATE); |
| 3321 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3322 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3323 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3324 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3325 | /* Call update after sign finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3326 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3327 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3328 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3329 | sign_mac, sizeof(sign_mac), |
| 3330 | &sign_mac_length)); |
| 3331 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3332 | PSA_ERROR_BAD_STATE); |
| 3333 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3334 | |
| 3335 | /* Call update after verify finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3336 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3337 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3338 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3339 | verify_mac, sizeof(verify_mac))); |
| 3340 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3341 | PSA_ERROR_BAD_STATE); |
| 3342 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3343 | |
| 3344 | /* Call sign finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3345 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3346 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3347 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3348 | sign_mac, sizeof(sign_mac), |
| 3349 | &sign_mac_length)); |
| 3350 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3351 | sign_mac, sizeof(sign_mac), |
| 3352 | &sign_mac_length), |
| 3353 | PSA_ERROR_BAD_STATE); |
| 3354 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3355 | |
| 3356 | /* Call verify finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3357 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3358 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3359 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3360 | verify_mac, sizeof(verify_mac))); |
| 3361 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3362 | verify_mac, sizeof(verify_mac)), |
| 3363 | PSA_ERROR_BAD_STATE); |
| 3364 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3365 | |
| 3366 | /* Setup sign but try verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3367 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3368 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3369 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3370 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3371 | verify_mac, sizeof(verify_mac)), |
| 3372 | PSA_ERROR_BAD_STATE); |
| 3373 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3374 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3375 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3376 | |
| 3377 | /* Setup verify but try sign. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3378 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3379 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3380 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3381 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3382 | sign_mac, sizeof(sign_mac), |
| 3383 | &sign_mac_length), |
| 3384 | PSA_ERROR_BAD_STATE); |
| 3385 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3386 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3387 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3389 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3390 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3391 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3392 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3393 | } |
| 3394 | /* END_CASE */ |
| 3395 | |
| 3396 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3397 | void mac_sign_verify_multi(int key_type_arg, |
| 3398 | data_t *key_data, |
| 3399 | int alg_arg, |
| 3400 | data_t *input, |
| 3401 | int is_verify, |
| 3402 | data_t *expected_mac) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3403 | { |
| 3404 | size_t data_part_len = 0; |
| 3405 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3406 | 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] | 3407 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3408 | mbedtls_test_set_step(2000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3409 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3410 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3411 | alg_arg, |
| 3412 | input, data_part_len, |
| 3413 | expected_mac, |
| 3414 | is_verify, 0) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3415 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3416 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3417 | |
| 3418 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3419 | mbedtls_test_set_step(3000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3420 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3421 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3422 | alg_arg, |
| 3423 | input, data_part_len, |
| 3424 | expected_mac, |
| 3425 | is_verify, 1) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3426 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3427 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3428 | } |
| 3429 | |
| 3430 | /* Goto is required to silence warnings about unused labels, as we |
| 3431 | * don't actually do any test assertions in this function. */ |
| 3432 | goto exit; |
| 3433 | } |
| 3434 | /* END_CASE */ |
| 3435 | |
| 3436 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3437 | void mac_sign(int key_type_arg, |
| 3438 | data_t *key_data, |
| 3439 | int alg_arg, |
| 3440 | data_t *input, |
| 3441 | data_t *expected_mac) |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3442 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3443 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3444 | psa_key_type_t key_type = key_type_arg; |
| 3445 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3446 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3447 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3448 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3449 | size_t mac_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3450 | 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] | 3451 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3452 | const size_t output_sizes_to_test[] = { |
| 3453 | 0, |
| 3454 | 1, |
| 3455 | expected_mac->len - 1, |
| 3456 | expected_mac->len, |
| 3457 | expected_mac->len + 1, |
| 3458 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3459 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3460 | TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3461 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3462 | TEST_ASSERT(expected_mac->len == mac_buffer_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3463 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3464 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3465 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3466 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 3467 | psa_set_key_algorithm(&attributes, alg); |
| 3468 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3469 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3470 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3471 | &key)); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3472 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3473 | 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] | 3474 | const size_t output_size = output_sizes_to_test[i]; |
| 3475 | psa_status_t expected_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3476 | (output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3477 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3478 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3479 | mbedtls_test_set_step(output_size); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3480 | TEST_CALLOC(actual_mac, output_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3481 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3482 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3483 | TEST_EQUAL(psa_mac_compute(key, alg, |
| 3484 | input->x, input->len, |
| 3485 | actual_mac, output_size, &mac_length), |
| 3486 | expected_status); |
| 3487 | if (expected_status == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3488 | TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3489 | actual_mac, mac_length); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3490 | } |
| 3491 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3492 | if (output_size > 0) { |
| 3493 | memset(actual_mac, 0, output_size); |
| 3494 | } |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3495 | |
| 3496 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3497 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3498 | PSA_ASSERT(psa_mac_update(&operation, |
| 3499 | input->x, input->len)); |
| 3500 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3501 | actual_mac, output_size, |
| 3502 | &mac_length), |
| 3503 | expected_status); |
| 3504 | PSA_ASSERT(psa_mac_abort(&operation)); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3505 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3506 | if (expected_status == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3507 | TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3508 | actual_mac, mac_length); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3509 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3510 | mbedtls_free(actual_mac); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3511 | actual_mac = NULL; |
| 3512 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3513 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3514 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3515 | psa_mac_abort(&operation); |
| 3516 | psa_destroy_key(key); |
| 3517 | PSA_DONE(); |
| 3518 | mbedtls_free(actual_mac); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3519 | } |
| 3520 | /* END_CASE */ |
| 3521 | |
| 3522 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3523 | void mac_verify(int key_type_arg, |
| 3524 | data_t *key_data, |
| 3525 | int alg_arg, |
| 3526 | data_t *input, |
| 3527 | data_t *expected_mac) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3528 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3529 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3530 | psa_key_type_t key_type = key_type_arg; |
| 3531 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3532 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3533 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3534 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3535 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3536 | TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3537 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3538 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3539 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3540 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 3541 | psa_set_key_algorithm(&attributes, alg); |
| 3542 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3543 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3544 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3545 | &key)); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3546 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3547 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3548 | PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len, |
| 3549 | expected_mac->x, expected_mac->len)); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3550 | |
| 3551 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3552 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3553 | PSA_ASSERT(psa_mac_update(&operation, |
| 3554 | input->x, input->len)); |
| 3555 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3556 | expected_mac->x, |
| 3557 | expected_mac->len)); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3558 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3559 | /* Test a MAC that's too short, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3560 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3561 | input->x, input->len, |
| 3562 | expected_mac->x, |
| 3563 | expected_mac->len - 1), |
| 3564 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3565 | |
| 3566 | /* Test a MAC that's too short, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3567 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3568 | PSA_ASSERT(psa_mac_update(&operation, |
| 3569 | input->x, input->len)); |
| 3570 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3571 | expected_mac->x, |
| 3572 | expected_mac->len - 1), |
| 3573 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3574 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3575 | /* Test a MAC that's too long, one-shot case. */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3576 | TEST_CALLOC(perturbed_mac, expected_mac->len + 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3577 | memcpy(perturbed_mac, expected_mac->x, expected_mac->len); |
| 3578 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3579 | input->x, input->len, |
| 3580 | perturbed_mac, expected_mac->len + 1), |
| 3581 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3582 | |
| 3583 | /* Test a MAC that's too long, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3584 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3585 | PSA_ASSERT(psa_mac_update(&operation, |
| 3586 | input->x, input->len)); |
| 3587 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3588 | perturbed_mac, |
| 3589 | expected_mac->len + 1), |
| 3590 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3591 | |
| 3592 | /* Test changing one byte. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3593 | for (size_t i = 0; i < expected_mac->len; i++) { |
| 3594 | mbedtls_test_set_step(i); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3595 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3596 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3597 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3598 | input->x, input->len, |
| 3599 | perturbed_mac, expected_mac->len), |
| 3600 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3601 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3602 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3603 | PSA_ASSERT(psa_mac_update(&operation, |
| 3604 | input->x, input->len)); |
| 3605 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3606 | perturbed_mac, |
| 3607 | expected_mac->len), |
| 3608 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3609 | perturbed_mac[i] ^= 1; |
| 3610 | } |
| 3611 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3612 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3613 | psa_mac_abort(&operation); |
| 3614 | psa_destroy_key(key); |
| 3615 | PSA_DONE(); |
| 3616 | mbedtls_free(perturbed_mac); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3617 | } |
| 3618 | /* END_CASE */ |
| 3619 | |
| 3620 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3621 | void cipher_operation_init() |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3622 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3623 | const uint8_t input[1] = { 0 }; |
| 3624 | unsigned char output[1] = { 0 }; |
| 3625 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3626 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3627 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3628 | * 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] | 3629 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3630 | psa_cipher_operation_t func = psa_cipher_operation_init(); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3631 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3632 | psa_cipher_operation_t zero; |
| 3633 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3634 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3635 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3636 | /* A freshly-initialized cipher operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3637 | TEST_EQUAL(psa_cipher_update(&func, |
| 3638 | input, sizeof(input), |
| 3639 | output, sizeof(output), |
| 3640 | &output_length), |
| 3641 | PSA_ERROR_BAD_STATE); |
| 3642 | TEST_EQUAL(psa_cipher_update(&init, |
| 3643 | input, sizeof(input), |
| 3644 | output, sizeof(output), |
| 3645 | &output_length), |
| 3646 | PSA_ERROR_BAD_STATE); |
| 3647 | TEST_EQUAL(psa_cipher_update(&zero, |
| 3648 | input, sizeof(input), |
| 3649 | output, sizeof(output), |
| 3650 | &output_length), |
| 3651 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3652 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3653 | /* A default cipher operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3654 | PSA_ASSERT(psa_cipher_abort(&func)); |
| 3655 | PSA_ASSERT(psa_cipher_abort(&init)); |
| 3656 | PSA_ASSERT(psa_cipher_abort(&zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3657 | } |
| 3658 | /* END_CASE */ |
| 3659 | |
| 3660 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3661 | void cipher_setup(int key_type_arg, |
| 3662 | data_t *key, |
| 3663 | int alg_arg, |
| 3664 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3665 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3666 | psa_key_type_t key_type = key_type_arg; |
| 3667 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3668 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3669 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3670 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3671 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3672 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3673 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3674 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3675 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3676 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3677 | if (!exercise_cipher_setup(key_type, key->x, key->len, alg, |
| 3678 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3679 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3680 | } |
| 3681 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3682 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3683 | /* The operation object should be reusable. */ |
| 3684 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3685 | if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3686 | smoke_test_key_data, |
| 3687 | sizeof(smoke_test_key_data), |
| 3688 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3689 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3690 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3691 | } |
| 3692 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3693 | #endif |
| 3694 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3695 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3696 | psa_cipher_abort(&operation); |
| 3697 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3698 | } |
| 3699 | /* END_CASE */ |
| 3700 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3701 | /* 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] | 3702 | void cipher_bad_order() |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3703 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3704 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3705 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3706 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3707 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3708 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3709 | 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] | 3710 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3711 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3712 | 0xaa, 0xaa, 0xaa, 0xaa |
| 3713 | }; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3714 | const uint8_t text[] = { |
| 3715 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3716 | 0xbb, 0xbb, 0xbb, 0xbb |
| 3717 | }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3718 | 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] | 3719 | size_t length = 0; |
| 3720 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3721 | PSA_ASSERT(psa_crypto_init()); |
| 3722 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3723 | psa_set_key_algorithm(&attributes, alg); |
| 3724 | psa_set_key_type(&attributes, key_type); |
| 3725 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3726 | &key)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3727 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3728 | /* Call encrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3729 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3730 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3731 | TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg), |
| 3732 | PSA_ERROR_BAD_STATE); |
| 3733 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3734 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3735 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3736 | |
| 3737 | /* Call decrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3738 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3739 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3740 | TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg), |
| 3741 | PSA_ERROR_BAD_STATE); |
| 3742 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3743 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3744 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3745 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3746 | /* Generate an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3747 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3748 | buffer, sizeof(buffer), |
| 3749 | &length), |
| 3750 | PSA_ERROR_BAD_STATE); |
| 3751 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3752 | |
| 3753 | /* Generate an IV twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3754 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3755 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3756 | buffer, sizeof(buffer), |
| 3757 | &length)); |
| 3758 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3759 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3760 | buffer, sizeof(buffer), |
| 3761 | &length), |
| 3762 | PSA_ERROR_BAD_STATE); |
| 3763 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3764 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3765 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3766 | |
| 3767 | /* Generate an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3768 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3769 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3770 | iv, sizeof(iv))); |
| 3771 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3772 | buffer, sizeof(buffer), |
| 3773 | &length), |
| 3774 | PSA_ERROR_BAD_STATE); |
| 3775 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3776 | |
| 3777 | /* Set an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3778 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3779 | iv, sizeof(iv)), |
| 3780 | PSA_ERROR_BAD_STATE); |
| 3781 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3782 | |
| 3783 | /* Set an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3784 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3785 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3786 | iv, sizeof(iv))); |
| 3787 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3788 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3789 | iv, sizeof(iv)), |
| 3790 | PSA_ERROR_BAD_STATE); |
| 3791 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3792 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3793 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3794 | |
| 3795 | /* Set an IV after it's already generated. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3796 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3797 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3798 | buffer, sizeof(buffer), |
| 3799 | &length)); |
| 3800 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3801 | iv, sizeof(iv)), |
| 3802 | PSA_ERROR_BAD_STATE); |
| 3803 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3804 | |
| 3805 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3806 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3807 | text, sizeof(text), |
| 3808 | buffer, sizeof(buffer), |
| 3809 | &length), |
| 3810 | PSA_ERROR_BAD_STATE); |
| 3811 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3812 | |
| 3813 | /* Call update without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3814 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3815 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3816 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3817 | text, sizeof(text), |
| 3818 | buffer, sizeof(buffer), |
| 3819 | &length), |
| 3820 | PSA_ERROR_BAD_STATE); |
| 3821 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3822 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3823 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3824 | |
| 3825 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3826 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3827 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3828 | iv, sizeof(iv))); |
| 3829 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3830 | buffer, sizeof(buffer), &length)); |
| 3831 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3832 | text, sizeof(text), |
| 3833 | buffer, sizeof(buffer), |
| 3834 | &length), |
| 3835 | PSA_ERROR_BAD_STATE); |
| 3836 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3837 | |
| 3838 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3839 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3840 | buffer, sizeof(buffer), &length), |
| 3841 | PSA_ERROR_BAD_STATE); |
| 3842 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3843 | |
| 3844 | /* Call finish without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3845 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3846 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3847 | * for cipher modes with padding. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3848 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3849 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3850 | buffer, sizeof(buffer), &length), |
| 3851 | PSA_ERROR_BAD_STATE); |
| 3852 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3853 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3854 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3855 | |
| 3856 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3857 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3858 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3859 | iv, sizeof(iv))); |
| 3860 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3861 | buffer, sizeof(buffer), &length)); |
| 3862 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3863 | buffer, sizeof(buffer), &length), |
| 3864 | PSA_ERROR_BAD_STATE); |
| 3865 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3866 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3867 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3868 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3869 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3870 | psa_cipher_abort(&operation); |
| 3871 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [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_encrypt_fail(int alg_arg, |
| 3877 | int key_type_arg, |
| 3878 | data_t *key_data, |
| 3879 | data_t *input, |
| 3880 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3881 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3882 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3883 | psa_status_t status; |
| 3884 | psa_key_type_t key_type = key_type_arg; |
| 3885 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3886 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3887 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 }; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3888 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; |
| 3889 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3890 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3891 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3892 | size_t output_length = 0; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3893 | size_t function_output_length; |
| 3894 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3895 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3896 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3897 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 3898 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3899 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3900 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3901 | psa_set_key_algorithm(&attributes, alg); |
| 3902 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3903 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3904 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 3905 | input->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3906 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3907 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3908 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3909 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3910 | } |
| 3911 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3912 | /* Encrypt, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3913 | status = psa_cipher_encrypt(key, alg, input->x, input->len, output, |
| 3914 | output_buffer_size, &output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3915 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3916 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3917 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3918 | /* Encrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3919 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
| 3920 | if (status == PSA_SUCCESS) { |
| 3921 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 3922 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3923 | iv, iv_size, |
| 3924 | &iv_length)); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3925 | } |
| 3926 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3927 | status = psa_cipher_update(&operation, input->x, input->len, |
| 3928 | output, output_buffer_size, |
| 3929 | &function_output_length); |
| 3930 | if (status == PSA_SUCCESS) { |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3931 | output_length += function_output_length; |
| 3932 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3933 | status = psa_cipher_finish(&operation, output + output_length, |
| 3934 | output_buffer_size - output_length, |
| 3935 | &function_output_length); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3936 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3937 | TEST_EQUAL(status, expected_status); |
| 3938 | } else { |
| 3939 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3940 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3941 | } else { |
| 3942 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3943 | } |
| 3944 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3945 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3946 | psa_cipher_abort(&operation); |
| 3947 | mbedtls_free(output); |
| 3948 | psa_destroy_key(key); |
| 3949 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3950 | } |
| 3951 | /* END_CASE */ |
| 3952 | |
| 3953 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3954 | void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data, |
| 3955 | data_t *input, int iv_length, |
| 3956 | int expected_result) |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3957 | { |
| 3958 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3959 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3960 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3961 | size_t output_buffer_size = 0; |
| 3962 | unsigned char *output = NULL; |
| 3963 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3964 | 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] | 3965 | TEST_CALLOC(output, output_buffer_size); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3966 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3967 | PSA_ASSERT(psa_crypto_init()); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3968 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3969 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3970 | psa_set_key_algorithm(&attributes, alg); |
| 3971 | psa_set_key_type(&attributes, key_type); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3972 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3973 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3974 | &key)); |
| 3975 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3976 | TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output, |
| 3977 | iv_length)); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3978 | |
| 3979 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3980 | psa_cipher_abort(&operation); |
| 3981 | mbedtls_free(output); |
| 3982 | psa_destroy_key(key); |
| 3983 | PSA_DONE(); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3984 | } |
| 3985 | /* END_CASE */ |
| 3986 | |
| 3987 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3988 | void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data, |
| 3989 | data_t *plaintext, data_t *ciphertext) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3990 | { |
| 3991 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3992 | psa_key_type_t key_type = key_type_arg; |
| 3993 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3994 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3995 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3996 | unsigned char *output = NULL; |
| 3997 | size_t output_buffer_size = 0; |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3998 | size_t output_length, length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3999 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4000 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4001 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4002 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4003 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4004 | TEST_LE_U(ciphertext->len, |
| 4005 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len)); |
| 4006 | TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len), |
| 4007 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len)); |
| 4008 | TEST_LE_U(plaintext->len, |
| 4009 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len)); |
| 4010 | TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len), |
| 4011 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len)); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4012 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4013 | |
| 4014 | /* Set up key and output buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4015 | psa_set_key_usage_flags(&attributes, |
| 4016 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4017 | psa_set_key_algorithm(&attributes, alg); |
| 4018 | psa_set_key_type(&attributes, key_type); |
| 4019 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4020 | &key)); |
| 4021 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 4022 | plaintext->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4023 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4024 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4025 | /* set_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4026 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4027 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 4028 | PSA_ERROR_BAD_STATE); |
| 4029 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 4030 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 4031 | PSA_ERROR_BAD_STATE); |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 4032 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4033 | /* generate_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4034 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4035 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 4036 | &length), |
| 4037 | PSA_ERROR_BAD_STATE); |
| 4038 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 4039 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 4040 | &length), |
| 4041 | PSA_ERROR_BAD_STATE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4042 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4043 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4044 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4045 | output_length = 0; |
| 4046 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4047 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4048 | plaintext->x, plaintext->len, |
| 4049 | output, output_buffer_size, |
| 4050 | &length)); |
| 4051 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4052 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4053 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4054 | mbedtls_buffer_offset(output, output_length), |
| 4055 | output_buffer_size - output_length, |
| 4056 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4057 | output_length += length; |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4058 | TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4059 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4060 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4061 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4062 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4063 | output_length = 0; |
| 4064 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4065 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4066 | ciphertext->x, ciphertext->len, |
| 4067 | output, output_buffer_size, |
| 4068 | &length)); |
| 4069 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4070 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4071 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4072 | mbedtls_buffer_offset(output, output_length), |
| 4073 | output_buffer_size - output_length, |
| 4074 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4075 | output_length += length; |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4076 | TEST_MEMORY_COMPARE(plaintext->x, plaintext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4077 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4078 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4079 | /* One-shot encryption */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4080 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4081 | PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len, |
| 4082 | output, output_buffer_size, |
| 4083 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4084 | TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4085 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4086 | |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4087 | /* One-shot decryption */ |
| 4088 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4089 | PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len, |
| 4090 | output, output_buffer_size, |
| 4091 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4092 | TEST_MEMORY_COMPARE(plaintext->x, plaintext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4093 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4094 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4095 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4096 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4097 | mbedtls_free(output); |
| 4098 | psa_cipher_abort(&operation); |
| 4099 | psa_destroy_key(key); |
| 4100 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4101 | } |
| 4102 | /* END_CASE */ |
| 4103 | |
| 4104 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4105 | 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] | 4106 | { |
| 4107 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4108 | psa_algorithm_t alg = alg_arg; |
| 4109 | psa_key_type_t key_type = key_type_arg; |
| 4110 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4111 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4112 | psa_status_t status; |
| 4113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4114 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4115 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4116 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4117 | psa_set_key_algorithm(&attributes, alg); |
| 4118 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4119 | |
| 4120 | /* Usage of either of these two size macros would cause divide by zero |
| 4121 | * with incorrect key types previously. Input length should be irrelevant |
| 4122 | * here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4123 | TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16), |
| 4124 | 0); |
| 4125 | TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4126 | |
| 4127 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4128 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4129 | &key)); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4130 | |
| 4131 | /* Should fail due to invalid alg type (to support invalid key type). |
| 4132 | * Encrypt or decrypt will end up in the same place. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4133 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4135 | TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4136 | |
| 4137 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4138 | psa_cipher_abort(&operation); |
| 4139 | psa_destroy_key(key); |
| 4140 | PSA_DONE(); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4141 | } |
| 4142 | /* END_CASE */ |
| 4143 | |
| 4144 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4145 | void cipher_encrypt_validation(int alg_arg, |
| 4146 | int key_type_arg, |
| 4147 | data_t *key_data, |
| 4148 | data_t *input) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4149 | { |
| 4150 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4151 | psa_key_type_t key_type = key_type_arg; |
| 4152 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4153 | size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4154 | unsigned char *output1 = NULL; |
| 4155 | size_t output1_buffer_size = 0; |
| 4156 | size_t output1_length = 0; |
| 4157 | unsigned char *output2 = NULL; |
| 4158 | size_t output2_buffer_size = 0; |
| 4159 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4160 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4161 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4162 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4164 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4165 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4166 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4167 | psa_set_key_algorithm(&attributes, alg); |
| 4168 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4169 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4170 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4171 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4172 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4173 | TEST_CALLOC(output1, output1_buffer_size); |
| 4174 | TEST_CALLOC(output2, output2_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4175 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4176 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4177 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4178 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 4179 | /* The one-shot cipher encryption uses generated iv so validating |
| 4180 | the output is not possible. Validating with multipart encryption. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4181 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1, |
| 4182 | output1_buffer_size, &output1_length)); |
| 4183 | TEST_LE_U(output1_length, |
| 4184 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4185 | TEST_LE_U(output1_length, |
| 4186 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4187 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4188 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4189 | PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4190 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4191 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4192 | input->x, input->len, |
| 4193 | output2, output2_buffer_size, |
| 4194 | &function_output_length)); |
| 4195 | TEST_LE_U(function_output_length, |
| 4196 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4197 | TEST_LE_U(function_output_length, |
| 4198 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4199 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4200 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4201 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4202 | output2 + output2_length, |
| 4203 | output2_buffer_size - output2_length, |
| 4204 | &function_output_length)); |
| 4205 | TEST_LE_U(function_output_length, |
| 4206 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4207 | TEST_LE_U(function_output_length, |
| 4208 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4209 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4210 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4211 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4212 | TEST_MEMORY_COMPARE(output1 + iv_size, output1_length - iv_size, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4213 | output2, output2_length); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4214 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4215 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4216 | psa_cipher_abort(&operation); |
| 4217 | mbedtls_free(output1); |
| 4218 | mbedtls_free(output2); |
| 4219 | psa_destroy_key(key); |
| 4220 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4221 | } |
| 4222 | /* END_CASE */ |
| 4223 | |
| 4224 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4225 | void cipher_encrypt_multipart(int alg_arg, int key_type_arg, |
| 4226 | data_t *key_data, data_t *iv, |
| 4227 | data_t *input, |
| 4228 | int first_part_size_arg, |
| 4229 | int output1_length_arg, int output2_length_arg, |
| 4230 | data_t *expected_output, |
| 4231 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4232 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4233 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4234 | psa_key_type_t key_type = key_type_arg; |
| 4235 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4236 | psa_status_t status; |
| 4237 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4238 | size_t first_part_size = first_part_size_arg; |
| 4239 | size_t output1_length = output1_length_arg; |
| 4240 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4241 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4242 | size_t output_buffer_size = 0; |
| 4243 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4244 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4245 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4246 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4247 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4248 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4249 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4250 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4251 | psa_set_key_algorithm(&attributes, alg); |
| 4252 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4253 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4254 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4255 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4256 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4257 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4258 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4259 | if (iv->len > 0) { |
| 4260 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4261 | } |
| 4262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4263 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4264 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4265 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4266 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4267 | TEST_LE_U(first_part_size, input->len); |
| 4268 | PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size, |
| 4269 | output, output_buffer_size, |
| 4270 | &function_output_length)); |
| 4271 | TEST_ASSERT(function_output_length == output1_length); |
| 4272 | TEST_LE_U(function_output_length, |
| 4273 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4274 | TEST_LE_U(function_output_length, |
| 4275 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4276 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4277 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4278 | if (first_part_size < input->len) { |
| 4279 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4280 | input->x + first_part_size, |
| 4281 | input->len - first_part_size, |
| 4282 | (output_buffer_size == 0 ? NULL : |
| 4283 | output + total_output_length), |
| 4284 | output_buffer_size - total_output_length, |
| 4285 | &function_output_length)); |
| 4286 | TEST_ASSERT(function_output_length == output2_length); |
| 4287 | TEST_LE_U(function_output_length, |
| 4288 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4289 | alg, |
| 4290 | input->len - first_part_size)); |
| 4291 | TEST_LE_U(function_output_length, |
| 4292 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4293 | total_output_length += function_output_length; |
| 4294 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4295 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4296 | status = psa_cipher_finish(&operation, |
| 4297 | (output_buffer_size == 0 ? NULL : |
| 4298 | output + total_output_length), |
| 4299 | output_buffer_size - total_output_length, |
| 4300 | &function_output_length); |
| 4301 | TEST_LE_U(function_output_length, |
| 4302 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4303 | TEST_LE_U(function_output_length, |
| 4304 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4305 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4306 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4307 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4308 | if (expected_status == PSA_SUCCESS) { |
| 4309 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4310 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4311 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4312 | output, total_output_length); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4313 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4314 | |
| 4315 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4316 | psa_cipher_abort(&operation); |
| 4317 | mbedtls_free(output); |
| 4318 | psa_destroy_key(key); |
| 4319 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4320 | } |
| 4321 | /* END_CASE */ |
| 4322 | |
| 4323 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4324 | void cipher_decrypt_multipart(int alg_arg, int key_type_arg, |
| 4325 | data_t *key_data, data_t *iv, |
| 4326 | data_t *input, |
| 4327 | int first_part_size_arg, |
| 4328 | int output1_length_arg, int output2_length_arg, |
| 4329 | data_t *expected_output, |
| 4330 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4331 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4332 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4333 | psa_key_type_t key_type = key_type_arg; |
| 4334 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4335 | psa_status_t status; |
| 4336 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4337 | size_t first_part_size = first_part_size_arg; |
| 4338 | size_t output1_length = output1_length_arg; |
| 4339 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4340 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4341 | size_t output_buffer_size = 0; |
| 4342 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4343 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4344 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4345 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4346 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4347 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4349 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4350 | psa_set_key_algorithm(&attributes, alg); |
| 4351 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4353 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4354 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4356 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4358 | if (iv->len > 0) { |
| 4359 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4360 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4361 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4362 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4363 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4364 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4365 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4366 | TEST_LE_U(first_part_size, input->len); |
| 4367 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4368 | input->x, first_part_size, |
| 4369 | output, output_buffer_size, |
| 4370 | &function_output_length)); |
| 4371 | TEST_ASSERT(function_output_length == output1_length); |
| 4372 | TEST_LE_U(function_output_length, |
| 4373 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4374 | TEST_LE_U(function_output_length, |
| 4375 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4376 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4377 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4378 | if (first_part_size < input->len) { |
| 4379 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4380 | input->x + first_part_size, |
| 4381 | input->len - first_part_size, |
| 4382 | (output_buffer_size == 0 ? NULL : |
| 4383 | output + total_output_length), |
| 4384 | output_buffer_size - total_output_length, |
| 4385 | &function_output_length)); |
| 4386 | TEST_ASSERT(function_output_length == output2_length); |
| 4387 | TEST_LE_U(function_output_length, |
| 4388 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4389 | alg, |
| 4390 | input->len - first_part_size)); |
| 4391 | TEST_LE_U(function_output_length, |
| 4392 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4393 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4394 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4395 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4396 | status = psa_cipher_finish(&operation, |
| 4397 | (output_buffer_size == 0 ? NULL : |
| 4398 | output + total_output_length), |
| 4399 | output_buffer_size - total_output_length, |
| 4400 | &function_output_length); |
| 4401 | TEST_LE_U(function_output_length, |
| 4402 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4403 | TEST_LE_U(function_output_length, |
| 4404 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4405 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4406 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4407 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4408 | if (expected_status == PSA_SUCCESS) { |
| 4409 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4410 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4411 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4412 | output, total_output_length); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4413 | } |
| 4414 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4415 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4416 | psa_cipher_abort(&operation); |
| 4417 | mbedtls_free(output); |
| 4418 | psa_destroy_key(key); |
| 4419 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4420 | } |
| 4421 | /* END_CASE */ |
| 4422 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4423 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4424 | void cipher_decrypt_fail(int alg_arg, |
| 4425 | int key_type_arg, |
| 4426 | data_t *key_data, |
| 4427 | data_t *iv, |
| 4428 | data_t *input_arg, |
| 4429 | int expected_status_arg) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4430 | { |
| 4431 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4432 | psa_status_t status; |
| 4433 | psa_key_type_t key_type = key_type_arg; |
| 4434 | psa_algorithm_t alg = alg_arg; |
| 4435 | psa_status_t expected_status = expected_status_arg; |
| 4436 | unsigned char *input = NULL; |
| 4437 | size_t input_buffer_size = 0; |
| 4438 | unsigned char *output = NULL; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4439 | unsigned char *output_multi = NULL; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4440 | size_t output_buffer_size = 0; |
| 4441 | size_t output_length = 0; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4442 | size_t function_output_length; |
| 4443 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4444 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4445 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4446 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 4447 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4449 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4450 | psa_set_key_algorithm(&attributes, alg); |
| 4451 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4452 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4453 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4454 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4455 | } |
| 4456 | |
| 4457 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4458 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4459 | if (input_buffer_size > 0) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4460 | TEST_CALLOC(input, input_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4461 | memcpy(input, iv->x, iv->len); |
| 4462 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4463 | } |
| 4464 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4465 | 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] | 4466 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4467 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4468 | /* Decrypt, one-short */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4469 | status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4470 | output_buffer_size, &output_length); |
| 4471 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4472 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4473 | /* Decrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4474 | status = psa_cipher_decrypt_setup(&operation, key, alg); |
| 4475 | if (status == PSA_SUCCESS) { |
| 4476 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 4477 | input_arg->len) + |
| 4478 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4479 | TEST_CALLOC(output_multi, output_buffer_size); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4480 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4481 | if (iv->len > 0) { |
| 4482 | status = psa_cipher_set_iv(&operation, iv->x, iv->len); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4483 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4484 | if (status != PSA_SUCCESS) { |
| 4485 | TEST_EQUAL(status, expected_status); |
| 4486 | } |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4487 | } |
| 4488 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4489 | if (status == PSA_SUCCESS) { |
| 4490 | status = psa_cipher_update(&operation, |
| 4491 | input_arg->x, input_arg->len, |
| 4492 | output_multi, output_buffer_size, |
| 4493 | &function_output_length); |
| 4494 | if (status == PSA_SUCCESS) { |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4495 | output_length = function_output_length; |
| 4496 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4497 | status = psa_cipher_finish(&operation, |
| 4498 | output_multi + output_length, |
| 4499 | output_buffer_size - output_length, |
| 4500 | &function_output_length); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4501 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4502 | TEST_EQUAL(status, expected_status); |
| 4503 | } else { |
| 4504 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4505 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4506 | } else { |
| 4507 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4508 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4509 | } else { |
| 4510 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4511 | } |
| 4512 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4513 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4514 | psa_cipher_abort(&operation); |
| 4515 | mbedtls_free(input); |
| 4516 | mbedtls_free(output); |
| 4517 | mbedtls_free(output_multi); |
| 4518 | psa_destroy_key(key); |
| 4519 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4520 | } |
| 4521 | /* END_CASE */ |
| 4522 | |
| 4523 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4524 | void cipher_decrypt(int alg_arg, |
| 4525 | int key_type_arg, |
| 4526 | data_t *key_data, |
| 4527 | data_t *iv, |
| 4528 | data_t *input_arg, |
| 4529 | data_t *expected_output) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4530 | { |
| 4531 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4532 | psa_key_type_t key_type = key_type_arg; |
| 4533 | psa_algorithm_t alg = alg_arg; |
| 4534 | unsigned char *input = NULL; |
| 4535 | size_t input_buffer_size = 0; |
| 4536 | unsigned char *output = NULL; |
| 4537 | size_t output_buffer_size = 0; |
| 4538 | size_t output_length = 0; |
| 4539 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4540 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4541 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4542 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4543 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4544 | psa_set_key_algorithm(&attributes, alg); |
| 4545 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4546 | |
| 4547 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4548 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4549 | if (input_buffer_size > 0) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4550 | TEST_CALLOC(input, input_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4551 | memcpy(input, iv->x, iv->len); |
| 4552 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4553 | } |
| 4554 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4555 | 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] | 4556 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4557 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4558 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4559 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4560 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4561 | PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4562 | output_buffer_size, &output_length)); |
| 4563 | TEST_LE_U(output_length, |
| 4564 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size)); |
| 4565 | TEST_LE_U(output_length, |
| 4566 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4567 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4568 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4569 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4570 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4571 | mbedtls_free(input); |
| 4572 | mbedtls_free(output); |
| 4573 | psa_destroy_key(key); |
| 4574 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [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_verify_output(int alg_arg, |
| 4580 | int key_type_arg, |
| 4581 | data_t *key_data, |
| 4582 | data_t *input) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4583 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4584 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4585 | psa_key_type_t key_type = key_type_arg; |
| 4586 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4587 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4588 | size_t output1_size = 0; |
| 4589 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4590 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4591 | size_t output2_size = 0; |
| 4592 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4593 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4594 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4595 | PSA_ASSERT(psa_crypto_init()); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4596 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4597 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4598 | psa_set_key_algorithm(&attributes, alg); |
| 4599 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4600 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4601 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4602 | &key)); |
| 4603 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4604 | TEST_CALLOC(output1, output1_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4605 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4606 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, |
| 4607 | output1, output1_size, |
| 4608 | &output1_length)); |
| 4609 | TEST_LE_U(output1_length, |
| 4610 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4611 | TEST_LE_U(output1_length, |
| 4612 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4613 | |
| 4614 | output2_size = output1_length; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4615 | TEST_CALLOC(output2, output2_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4616 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4617 | PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length, |
| 4618 | output2, output2_size, |
| 4619 | &output2_length)); |
| 4620 | TEST_LE_U(output2_length, |
| 4621 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4622 | TEST_LE_U(output2_length, |
| 4623 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4624 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4625 | TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4626 | |
| 4627 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4628 | mbedtls_free(output1); |
| 4629 | mbedtls_free(output2); |
| 4630 | psa_destroy_key(key); |
| 4631 | PSA_DONE(); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4632 | } |
| 4633 | /* END_CASE */ |
| 4634 | |
| 4635 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4636 | void cipher_verify_output_multipart(int alg_arg, |
| 4637 | int key_type_arg, |
| 4638 | data_t *key_data, |
| 4639 | data_t *input, |
| 4640 | int first_part_size_arg) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4641 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4642 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4643 | psa_key_type_t key_type = key_type_arg; |
| 4644 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4645 | size_t first_part_size = first_part_size_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4646 | unsigned char iv[16] = { 0 }; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4647 | size_t iv_size = 16; |
| 4648 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4649 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4650 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4651 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4652 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4653 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4654 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4655 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4656 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 4657 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4658 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4659 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4660 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4661 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4662 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4663 | psa_set_key_algorithm(&attributes, alg); |
| 4664 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4665 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4666 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4667 | &key)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4668 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4669 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg)); |
| 4670 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4671 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4672 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 4673 | PSA_ASSERT(psa_cipher_generate_iv(&operation1, |
| 4674 | iv, iv_size, |
| 4675 | &iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4676 | } |
| 4677 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4678 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4679 | TEST_LE_U(output1_buffer_size, |
| 4680 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4681 | TEST_CALLOC(output1, output1_buffer_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4682 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4683 | TEST_LE_U(first_part_size, input->len); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4684 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4685 | PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size, |
| 4686 | output1, output1_buffer_size, |
| 4687 | &function_output_length)); |
| 4688 | TEST_LE_U(function_output_length, |
| 4689 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4690 | TEST_LE_U(function_output_length, |
| 4691 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4692 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4693 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4694 | PSA_ASSERT(psa_cipher_update(&operation1, |
| 4695 | input->x + first_part_size, |
| 4696 | input->len - first_part_size, |
| 4697 | output1, output1_buffer_size, |
| 4698 | &function_output_length)); |
| 4699 | TEST_LE_U(function_output_length, |
| 4700 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4701 | alg, |
| 4702 | input->len - first_part_size)); |
| 4703 | TEST_LE_U(function_output_length, |
| 4704 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4705 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4706 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4707 | PSA_ASSERT(psa_cipher_finish(&operation1, |
| 4708 | output1 + output1_length, |
| 4709 | output1_buffer_size - output1_length, |
| 4710 | &function_output_length)); |
| 4711 | TEST_LE_U(function_output_length, |
| 4712 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4713 | TEST_LE_U(function_output_length, |
| 4714 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4715 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4716 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4717 | PSA_ASSERT(psa_cipher_abort(&operation1)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4718 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4719 | output2_buffer_size = output1_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4720 | TEST_LE_U(output2_buffer_size, |
| 4721 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4722 | TEST_LE_U(output2_buffer_size, |
| 4723 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4724 | TEST_CALLOC(output2, output2_buffer_size); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4725 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4726 | if (iv_length > 0) { |
| 4727 | PSA_ASSERT(psa_cipher_set_iv(&operation2, |
| 4728 | iv, iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4729 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4730 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4731 | PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size, |
| 4732 | output2, output2_buffer_size, |
| 4733 | &function_output_length)); |
| 4734 | TEST_LE_U(function_output_length, |
| 4735 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4736 | TEST_LE_U(function_output_length, |
| 4737 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4738 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4739 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4740 | PSA_ASSERT(psa_cipher_update(&operation2, |
| 4741 | output1 + first_part_size, |
| 4742 | output1_length - first_part_size, |
| 4743 | output2, output2_buffer_size, |
| 4744 | &function_output_length)); |
| 4745 | TEST_LE_U(function_output_length, |
| 4746 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4747 | alg, |
| 4748 | output1_length - first_part_size)); |
| 4749 | TEST_LE_U(function_output_length, |
| 4750 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4751 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4752 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4753 | PSA_ASSERT(psa_cipher_finish(&operation2, |
| 4754 | output2 + output2_length, |
| 4755 | output2_buffer_size - output2_length, |
| 4756 | &function_output_length)); |
| 4757 | TEST_LE_U(function_output_length, |
| 4758 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4759 | TEST_LE_U(function_output_length, |
| 4760 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4761 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4762 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4763 | PSA_ASSERT(psa_cipher_abort(&operation2)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4764 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4765 | TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4766 | |
| 4767 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4768 | psa_cipher_abort(&operation1); |
| 4769 | psa_cipher_abort(&operation2); |
| 4770 | mbedtls_free(output1); |
| 4771 | mbedtls_free(output2); |
| 4772 | psa_destroy_key(key); |
| 4773 | PSA_DONE(); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4774 | } |
| 4775 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 4776 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4777 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4778 | void aead_encrypt_decrypt(int key_type_arg, data_t *key_data, |
| 4779 | int alg_arg, |
| 4780 | data_t *nonce, |
| 4781 | data_t *additional_data, |
| 4782 | data_t *input_data, |
| 4783 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4784 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4785 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4786 | psa_key_type_t key_type = key_type_arg; |
| 4787 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4788 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4789 | unsigned char *output_data = NULL; |
| 4790 | size_t output_size = 0; |
| 4791 | size_t output_length = 0; |
| 4792 | unsigned char *output_data2 = NULL; |
| 4793 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4794 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4795 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4796 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4797 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4798 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4799 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4800 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4801 | psa_set_key_algorithm(&attributes, alg); |
| 4802 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4803 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4804 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4805 | &key)); |
| 4806 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4807 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4808 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4809 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4810 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4811 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4812 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4813 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4814 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
| 4815 | TEST_EQUAL(output_size, |
| 4816 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4817 | TEST_LE_U(output_size, |
| 4818 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4819 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4820 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4821 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4822 | status = psa_aead_encrypt(key, alg, |
| 4823 | nonce->x, nonce->len, |
| 4824 | additional_data->x, |
| 4825 | additional_data->len, |
| 4826 | input_data->x, input_data->len, |
| 4827 | output_data, output_size, |
| 4828 | &output_length); |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4829 | |
| 4830 | /* If the operation is not supported, just skip and not fail in case the |
| 4831 | * encryption involves a common limitation of cryptography hardwares and |
| 4832 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4833 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4834 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4835 | 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] | 4836 | } |
| 4837 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4838 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4839 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4840 | if (PSA_SUCCESS == expected_result) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4841 | TEST_CALLOC(output_data2, output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4842 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4843 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4844 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4845 | TEST_EQUAL(input_data->len, |
| 4846 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length)); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4847 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4848 | TEST_LE_U(input_data->len, |
| 4849 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length)); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4850 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4851 | TEST_EQUAL(psa_aead_decrypt(key, alg, |
| 4852 | nonce->x, nonce->len, |
| 4853 | additional_data->x, |
| 4854 | additional_data->len, |
| 4855 | output_data, output_length, |
| 4856 | output_data2, output_length, |
| 4857 | &output_length2), |
| 4858 | expected_result); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4859 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4860 | TEST_MEMORY_COMPARE(input_data->x, input_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4861 | output_data2, output_length2); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4862 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4863 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4864 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4865 | psa_destroy_key(key); |
| 4866 | mbedtls_free(output_data); |
| 4867 | mbedtls_free(output_data2); |
| 4868 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4869 | } |
| 4870 | /* END_CASE */ |
| 4871 | |
| 4872 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4873 | void aead_encrypt(int key_type_arg, data_t *key_data, |
| 4874 | int alg_arg, |
| 4875 | data_t *nonce, |
| 4876 | data_t *additional_data, |
| 4877 | data_t *input_data, |
| 4878 | data_t *expected_result) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4879 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4880 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4881 | psa_key_type_t key_type = key_type_arg; |
| 4882 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4883 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4884 | unsigned char *output_data = NULL; |
| 4885 | size_t output_size = 0; |
| 4886 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4887 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4888 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4889 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4890 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4891 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4892 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4893 | psa_set_key_algorithm(&attributes, alg); |
| 4894 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4895 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4896 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4897 | &key)); |
| 4898 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4899 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4900 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4901 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4902 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4903 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4904 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4905 | TEST_EQUAL(output_size, |
| 4906 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4907 | TEST_LE_U(output_size, |
| 4908 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4909 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4910 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4911 | status = psa_aead_encrypt(key, alg, |
| 4912 | nonce->x, nonce->len, |
| 4913 | additional_data->x, additional_data->len, |
| 4914 | input_data->x, input_data->len, |
| 4915 | output_data, output_size, |
| 4916 | &output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4917 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4918 | /* If the operation is not supported, just skip and not fail in case the |
| 4919 | * encryption involves a common limitation of cryptography hardwares and |
| 4920 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4921 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4922 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4923 | 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] | 4924 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4925 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4926 | PSA_ASSERT(status); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4927 | TEST_MEMORY_COMPARE(expected_result->x, expected_result->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4928 | output_data, output_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4929 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4930 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4931 | psa_destroy_key(key); |
| 4932 | mbedtls_free(output_data); |
| 4933 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4934 | } |
| 4935 | /* END_CASE */ |
| 4936 | |
| 4937 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4938 | void aead_decrypt(int key_type_arg, data_t *key_data, |
| 4939 | int alg_arg, |
| 4940 | data_t *nonce, |
| 4941 | data_t *additional_data, |
| 4942 | data_t *input_data, |
| 4943 | data_t *expected_data, |
| 4944 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4945 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4946 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4947 | psa_key_type_t key_type = key_type_arg; |
| 4948 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4949 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4950 | unsigned char *output_data = NULL; |
| 4951 | size_t output_size = 0; |
| 4952 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4953 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4954 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4955 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4956 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4957 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4958 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4959 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4960 | psa_set_key_algorithm(&attributes, alg); |
| 4961 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4962 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4963 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4964 | &key)); |
| 4965 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4966 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4967 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4968 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4969 | alg); |
| 4970 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4971 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4972 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4973 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4974 | TEST_EQUAL(output_size, |
| 4975 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4976 | TEST_LE_U(output_size, |
| 4977 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4978 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4979 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4980 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4981 | status = psa_aead_decrypt(key, alg, |
| 4982 | nonce->x, nonce->len, |
| 4983 | additional_data->x, |
| 4984 | additional_data->len, |
| 4985 | input_data->x, input_data->len, |
| 4986 | output_data, output_size, |
| 4987 | &output_length); |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4988 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4989 | /* If the operation is not supported, just skip and not fail in case the |
| 4990 | * decryption involves a common limitation of cryptography hardwares and |
| 4991 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4992 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4993 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4994 | 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] | 4995 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4996 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4997 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4998 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4999 | if (expected_result == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 5000 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 5001 | output_data, output_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5002 | } |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5003 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5004 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5005 | psa_destroy_key(key); |
| 5006 | mbedtls_free(output_data); |
| 5007 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5008 | } |
| 5009 | /* END_CASE */ |
| 5010 | |
| 5011 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5012 | void aead_multipart_encrypt(int key_type_arg, data_t *key_data, |
| 5013 | int alg_arg, |
| 5014 | data_t *nonce, |
| 5015 | data_t *additional_data, |
| 5016 | data_t *input_data, |
| 5017 | int do_set_lengths, |
| 5018 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5019 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5020 | size_t ad_part_len = 0; |
| 5021 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5022 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5023 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5024 | for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) { |
| 5025 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5026 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5027 | if (do_set_lengths) { |
| 5028 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5029 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5030 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5031 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5032 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5033 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5034 | |
| 5035 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5036 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5037 | alg_arg, nonce, |
| 5038 | additional_data, |
| 5039 | ad_part_len, |
| 5040 | input_data, -1, |
| 5041 | set_lengths_method, |
| 5042 | expected_output, |
| 5043 | 1, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5044 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5045 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5046 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5047 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 5048 | mbedtls_test_set_step(1000 + ad_part_len); |
| 5049 | |
| 5050 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5051 | alg_arg, nonce, |
| 5052 | additional_data, |
| 5053 | ad_part_len, |
| 5054 | input_data, -1, |
| 5055 | set_lengths_method, |
| 5056 | expected_output, |
| 5057 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5058 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5059 | } |
| 5060 | } |
| 5061 | |
| 5062 | for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) { |
| 5063 | /* Split data into length(data_part_len) parts. */ |
| 5064 | mbedtls_test_set_step(2000 + data_part_len); |
| 5065 | |
| 5066 | if (do_set_lengths) { |
| 5067 | if (data_part_len & 0x01) { |
| 5068 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 5069 | } else { |
| 5070 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
| 5071 | } |
| 5072 | } |
| 5073 | |
| 5074 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5075 | alg_arg, nonce, |
| 5076 | additional_data, -1, |
| 5077 | input_data, data_part_len, |
| 5078 | set_lengths_method, |
| 5079 | expected_output, |
| 5080 | 1, 0)) { |
| 5081 | break; |
| 5082 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5083 | |
| 5084 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5085 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5086 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5087 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5088 | alg_arg, nonce, |
| 5089 | additional_data, -1, |
| 5090 | input_data, data_part_len, |
| 5091 | set_lengths_method, |
| 5092 | expected_output, |
| 5093 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5094 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5095 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5096 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5097 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5098 | /* Goto is required to silence warnings about unused labels, as we |
| 5099 | * don't actually do any test assertions in this function. */ |
| 5100 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5101 | } |
| 5102 | /* END_CASE */ |
| 5103 | |
| 5104 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5105 | void aead_multipart_decrypt(int key_type_arg, data_t *key_data, |
| 5106 | int alg_arg, |
| 5107 | data_t *nonce, |
| 5108 | data_t *additional_data, |
| 5109 | data_t *input_data, |
| 5110 | int do_set_lengths, |
| 5111 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5112 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5113 | size_t ad_part_len = 0; |
| 5114 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5115 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5116 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5117 | 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] | 5118 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5119 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5120 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5121 | if (do_set_lengths) { |
| 5122 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5123 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5124 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5125 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5126 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5127 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5128 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5129 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5130 | alg_arg, nonce, |
| 5131 | additional_data, |
| 5132 | ad_part_len, |
| 5133 | input_data, -1, |
| 5134 | set_lengths_method, |
| 5135 | expected_output, |
| 5136 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5137 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5138 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5139 | |
| 5140 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5141 | mbedtls_test_set_step(1000 + ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5142 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5143 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5144 | alg_arg, nonce, |
| 5145 | additional_data, |
| 5146 | ad_part_len, |
| 5147 | input_data, -1, |
| 5148 | set_lengths_method, |
| 5149 | expected_output, |
| 5150 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5151 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5152 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5153 | } |
| 5154 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5155 | 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] | 5156 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5157 | mbedtls_test_set_step(2000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5159 | if (do_set_lengths) { |
| 5160 | if (data_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5161 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5162 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5163 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5164 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5165 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5166 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5167 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5168 | alg_arg, nonce, |
| 5169 | additional_data, -1, |
| 5170 | input_data, data_part_len, |
| 5171 | set_lengths_method, |
| 5172 | expected_output, |
| 5173 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5174 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5175 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5176 | |
| 5177 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5178 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5179 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5180 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5181 | alg_arg, nonce, |
| 5182 | additional_data, -1, |
| 5183 | input_data, data_part_len, |
| 5184 | set_lengths_method, |
| 5185 | expected_output, |
| 5186 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5187 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5188 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5189 | } |
| 5190 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5191 | /* Goto is required to silence warnings about unused labels, as we |
| 5192 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5193 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5194 | } |
| 5195 | /* END_CASE */ |
| 5196 | |
| 5197 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5198 | void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data, |
| 5199 | int alg_arg, |
| 5200 | int nonce_length, |
| 5201 | int expected_nonce_length_arg, |
| 5202 | data_t *additional_data, |
| 5203 | data_t *input_data, |
| 5204 | int expected_status_arg) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5205 | { |
| 5206 | |
| 5207 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5208 | psa_key_type_t key_type = key_type_arg; |
| 5209 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5210 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5211 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5212 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5213 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5214 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5215 | size_t actual_nonce_length = 0; |
| 5216 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 5217 | unsigned char *output = NULL; |
| 5218 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5219 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5220 | size_t ciphertext_size = 0; |
| 5221 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5222 | size_t tag_length = 0; |
| 5223 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5224 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5225 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5226 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5227 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5228 | psa_set_key_algorithm(&attributes, alg); |
| 5229 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5230 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5231 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5232 | &key)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5233 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5234 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5235 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5236 | 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] | 5237 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5238 | TEST_CALLOC(output, output_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5239 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5240 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5241 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5242 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5243 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5244 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5245 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5246 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5247 | |
| 5248 | /* If the operation is not supported, just skip and not fail in case the |
| 5249 | * encryption involves a common limitation of cryptography hardwares and |
| 5250 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5251 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5252 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5253 | 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] | 5254 | } |
| 5255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5256 | PSA_ASSERT(status); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5257 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5258 | status = psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5259 | nonce_length, |
| 5260 | &actual_nonce_length); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5261 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5262 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5263 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5264 | TEST_EQUAL(actual_nonce_length, expected_nonce_length); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 5265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5266 | if (expected_status == PSA_SUCCESS) { |
| 5267 | TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type, |
| 5268 | alg)); |
| 5269 | } |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 5270 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5271 | TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 5272 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5273 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5274 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5275 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5276 | input_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5277 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5278 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5279 | additional_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5280 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5281 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5282 | output, output_size, |
| 5283 | &ciphertext_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5284 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5285 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5286 | &ciphertext_length, tag_buffer, |
| 5287 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5288 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5289 | |
| 5290 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5291 | psa_destroy_key(key); |
| 5292 | mbedtls_free(output); |
| 5293 | mbedtls_free(ciphertext); |
| 5294 | psa_aead_abort(&operation); |
| 5295 | PSA_DONE(); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5296 | } |
| 5297 | /* END_CASE */ |
| 5298 | |
| 5299 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5300 | void aead_multipart_set_nonce(int key_type_arg, data_t *key_data, |
| 5301 | int alg_arg, |
| 5302 | int nonce_length_arg, |
| 5303 | int set_lengths_method_arg, |
| 5304 | data_t *additional_data, |
| 5305 | data_t *input_data, |
| 5306 | int expected_status_arg) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5307 | { |
| 5308 | |
| 5309 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5310 | psa_key_type_t key_type = key_type_arg; |
| 5311 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5312 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5313 | uint8_t *nonce_buffer = NULL; |
| 5314 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5315 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5316 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5317 | unsigned char *output = NULL; |
| 5318 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5319 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5320 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5321 | size_t ciphertext_size = 0; |
| 5322 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5323 | size_t tag_length = 0; |
| 5324 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5325 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5326 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5327 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5328 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5329 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5330 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5331 | psa_set_key_algorithm(&attributes, alg); |
| 5332 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5333 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5334 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5335 | &key)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5336 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5337 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5338 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5339 | 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] | 5340 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5341 | TEST_CALLOC(output, output_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5342 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5343 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5344 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5345 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5346 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5347 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5349 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5350 | |
| 5351 | /* If the operation is not supported, just skip and not fail in case the |
| 5352 | * encryption involves a common limitation of cryptography hardwares and |
| 5353 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5354 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5355 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5356 | 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] | 5357 | } |
| 5358 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5359 | PSA_ASSERT(status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5360 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5361 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5362 | if (nonce_length_arg == -1) { |
| 5363 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5364 | TEST_CALLOC(nonce_buffer, 4); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5365 | nonce_length = 0; |
| 5366 | } else { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5367 | /* If length is zero, then this will return NULL. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5368 | nonce_length = (size_t) nonce_length_arg; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5369 | TEST_CALLOC(nonce_buffer, nonce_length); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5371 | if (nonce_buffer) { |
| 5372 | for (index = 0; index < nonce_length - 1; ++index) { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5373 | nonce_buffer[index] = 'a' + index; |
| 5374 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5375 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5376 | } |
| 5377 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5378 | if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { |
| 5379 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5380 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5381 | } |
| 5382 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5383 | status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5385 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5386 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5387 | if (expected_status == PSA_SUCCESS) { |
| 5388 | if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) { |
| 5389 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5390 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5391 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5392 | 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] | 5393 | expected_status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5394 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5395 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5396 | /* 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] | 5397 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5398 | additional_data->len), |
| 5399 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5400 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5401 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5402 | output, output_size, |
| 5403 | &ciphertext_length), |
| 5404 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5405 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5406 | TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5407 | &ciphertext_length, tag_buffer, |
| 5408 | PSA_AEAD_TAG_MAX_SIZE, &tag_length), |
| 5409 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5410 | } |
| 5411 | |
| 5412 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5413 | psa_destroy_key(key); |
| 5414 | mbedtls_free(output); |
| 5415 | mbedtls_free(ciphertext); |
| 5416 | mbedtls_free(nonce_buffer); |
| 5417 | psa_aead_abort(&operation); |
| 5418 | PSA_DONE(); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5419 | } |
| 5420 | /* END_CASE */ |
| 5421 | |
| 5422 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5423 | 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] | 5424 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5425 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5426 | data_t *nonce, |
| 5427 | data_t *additional_data, |
| 5428 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5429 | int expected_status_arg) |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5430 | { |
| 5431 | |
| 5432 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5433 | psa_key_type_t key_type = key_type_arg; |
| 5434 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5435 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5436 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5437 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5438 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5439 | unsigned char *output = NULL; |
| 5440 | unsigned char *ciphertext = NULL; |
| 5441 | size_t output_size = output_size_arg; |
| 5442 | size_t ciphertext_size = 0; |
| 5443 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5444 | size_t tag_length = 0; |
| 5445 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5446 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5447 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5449 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5450 | psa_set_key_algorithm(&attributes, alg); |
| 5451 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5452 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5453 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5454 | &key)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5455 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5456 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5457 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5458 | TEST_CALLOC(output, output_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5459 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5460 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5461 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5462 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5463 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5464 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5465 | |
| 5466 | /* If the operation is not supported, just skip and not fail in case the |
| 5467 | * encryption involves a common limitation of cryptography hardwares and |
| 5468 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5469 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5470 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5471 | 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] | 5472 | } |
| 5473 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5474 | PSA_ASSERT(status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5475 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5476 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5477 | input_data->len)); |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5478 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5479 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5480 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5481 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5482 | additional_data->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5483 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5484 | status = psa_aead_update(&operation, input_data->x, input_data->len, |
| 5485 | output, output_size, &ciphertext_length); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5486 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5487 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5488 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5489 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5490 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5491 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5492 | &ciphertext_length, tag_buffer, |
| 5493 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5494 | } |
| 5495 | |
| 5496 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5497 | psa_destroy_key(key); |
| 5498 | mbedtls_free(output); |
| 5499 | mbedtls_free(ciphertext); |
| 5500 | psa_aead_abort(&operation); |
| 5501 | PSA_DONE(); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5502 | } |
| 5503 | /* END_CASE */ |
| 5504 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5505 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5506 | void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data, |
| 5507 | int alg_arg, |
| 5508 | int finish_ciphertext_size_arg, |
| 5509 | int tag_size_arg, |
| 5510 | data_t *nonce, |
| 5511 | data_t *additional_data, |
| 5512 | data_t *input_data, |
| 5513 | int expected_status_arg) |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5514 | { |
| 5515 | |
| 5516 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5517 | psa_key_type_t key_type = key_type_arg; |
| 5518 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5519 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5520 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5521 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5522 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5523 | unsigned char *ciphertext = NULL; |
| 5524 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5525 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5526 | size_t ciphertext_size = 0; |
| 5527 | size_t ciphertext_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5528 | size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg; |
| 5529 | size_t tag_size = (size_t) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5530 | size_t tag_length = 0; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5531 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5532 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5533 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5534 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5535 | psa_set_key_algorithm(&attributes, alg); |
| 5536 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5537 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5538 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5539 | &key)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5540 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5541 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5542 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5543 | 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] | 5544 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5545 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5546 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5547 | TEST_CALLOC(finish_ciphertext, finish_ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5548 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5549 | TEST_CALLOC(tag_buffer, tag_size); |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5550 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5551 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5552 | |
| 5553 | /* If the operation is not supported, just skip and not fail in case the |
| 5554 | * encryption involves a common limitation of cryptography hardwares and |
| 5555 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5556 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5557 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5558 | 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] | 5559 | } |
| 5560 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5561 | PSA_ASSERT(status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5562 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5563 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5564 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5565 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5566 | input_data->len)); |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5567 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5568 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5569 | additional_data->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5570 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5571 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5572 | ciphertext, ciphertext_size, &ciphertext_length)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5573 | |
| 5574 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5575 | status = psa_aead_finish(&operation, finish_ciphertext, |
| 5576 | finish_ciphertext_size, |
| 5577 | &ciphertext_length, tag_buffer, |
| 5578 | tag_size, &tag_length); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5579 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5580 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5581 | |
| 5582 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5583 | psa_destroy_key(key); |
| 5584 | mbedtls_free(ciphertext); |
| 5585 | mbedtls_free(finish_ciphertext); |
| 5586 | mbedtls_free(tag_buffer); |
| 5587 | psa_aead_abort(&operation); |
| 5588 | PSA_DONE(); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5589 | } |
| 5590 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5591 | |
| 5592 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5593 | void aead_multipart_verify(int key_type_arg, data_t *key_data, |
| 5594 | int alg_arg, |
| 5595 | data_t *nonce, |
| 5596 | data_t *additional_data, |
| 5597 | data_t *input_data, |
| 5598 | data_t *tag, |
| 5599 | int tag_usage_arg, |
| 5600 | int expected_setup_status_arg, |
| 5601 | int expected_status_arg) |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5602 | { |
| 5603 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5604 | psa_key_type_t key_type = key_type_arg; |
| 5605 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5606 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5607 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5608 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5609 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5610 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5611 | unsigned char *plaintext = NULL; |
| 5612 | unsigned char *finish_plaintext = NULL; |
| 5613 | size_t plaintext_size = 0; |
| 5614 | size_t plaintext_length = 0; |
| 5615 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5616 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5617 | unsigned char *tag_buffer = NULL; |
| 5618 | size_t tag_size = 0; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5619 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5620 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5621 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5622 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 5623 | psa_set_key_algorithm(&attributes, alg); |
| 5624 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5625 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5626 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5627 | &key)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5628 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5629 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5630 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5631 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 5632 | input_data->len); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5633 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5634 | TEST_CALLOC(plaintext, plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5635 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5636 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5637 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5638 | TEST_CALLOC(finish_plaintext, verify_plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5639 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5640 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5641 | |
| 5642 | /* If the operation is not supported, just skip and not fail in case the |
| 5643 | * encryption involves a common limitation of cryptography hardwares and |
| 5644 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5645 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5646 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5647 | 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] | 5648 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5649 | TEST_EQUAL(status, expected_setup_status); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5650 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5651 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5652 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5653 | } |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5654 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5655 | PSA_ASSERT(status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5656 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5657 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5658 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5659 | status = psa_aead_set_lengths(&operation, additional_data->len, |
| 5660 | input_data->len); |
| 5661 | PSA_ASSERT(status); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5662 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5663 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5664 | additional_data->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5665 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5666 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 5667 | input_data->len, |
| 5668 | plaintext, plaintext_size, |
| 5669 | &plaintext_length)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5670 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5671 | if (tag_usage == USE_GIVEN_TAG) { |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5672 | tag_buffer = tag->x; |
| 5673 | tag_size = tag->len; |
| 5674 | } |
| 5675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5676 | status = psa_aead_verify(&operation, finish_plaintext, |
| 5677 | verify_plaintext_size, |
| 5678 | &plaintext_length, |
| 5679 | tag_buffer, tag_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5680 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5681 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5682 | |
| 5683 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5684 | psa_destroy_key(key); |
| 5685 | mbedtls_free(plaintext); |
| 5686 | mbedtls_free(finish_plaintext); |
| 5687 | psa_aead_abort(&operation); |
| 5688 | PSA_DONE(); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5689 | } |
| 5690 | /* END_CASE */ |
| 5691 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5692 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5693 | void aead_multipart_setup(int key_type_arg, data_t *key_data, |
| 5694 | int alg_arg, int expected_status_arg) |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5695 | { |
| 5696 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5697 | psa_key_type_t key_type = key_type_arg; |
| 5698 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5699 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5700 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5701 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5702 | psa_status_t expected_status = expected_status_arg; |
| 5703 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5704 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5705 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5706 | psa_set_key_usage_flags(&attributes, |
| 5707 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5708 | psa_set_key_algorithm(&attributes, alg); |
| 5709 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5710 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5711 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5712 | &key)); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5713 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5714 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5715 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5716 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5717 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5718 | psa_aead_abort(&operation); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5719 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5720 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5721 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5722 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5723 | |
| 5724 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5725 | psa_destroy_key(key); |
| 5726 | psa_aead_abort(&operation); |
| 5727 | PSA_DONE(); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5728 | } |
| 5729 | /* END_CASE */ |
| 5730 | |
| 5731 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5732 | void aead_multipart_state_test(int key_type_arg, data_t *key_data, |
| 5733 | int alg_arg, |
| 5734 | data_t *nonce, |
| 5735 | data_t *additional_data, |
| 5736 | data_t *input_data) |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5737 | { |
| 5738 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5739 | psa_key_type_t key_type = key_type_arg; |
| 5740 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5741 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5742 | unsigned char *output_data = NULL; |
| 5743 | unsigned char *final_data = NULL; |
| 5744 | size_t output_size = 0; |
| 5745 | size_t finish_output_size = 0; |
| 5746 | size_t output_length = 0; |
| 5747 | size_t key_bits = 0; |
| 5748 | size_t tag_length = 0; |
| 5749 | size_t tag_size = 0; |
| 5750 | size_t nonce_length = 0; |
| 5751 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5752 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5753 | size_t output_part_length = 0; |
| 5754 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5755 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5756 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5757 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5758 | psa_set_key_usage_flags(&attributes, |
| 5759 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5760 | psa_set_key_algorithm(&attributes, alg); |
| 5761 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5762 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5763 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5764 | &key)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5765 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5766 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5767 | key_bits = psa_get_key_bits(&attributes); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5768 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5769 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5771 | TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5772 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5773 | 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] | 5774 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5775 | TEST_CALLOC(output_data, output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5776 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5777 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5778 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5779 | TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5780 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5781 | TEST_CALLOC(final_data, finish_output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5782 | |
| 5783 | /* Test all operations error without calling setup first. */ |
| 5784 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5785 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5786 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5787 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5788 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5789 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5790 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5791 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5792 | &nonce_length), |
| 5793 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5794 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5795 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5796 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5797 | /* ------------------------------------------------------- */ |
| 5798 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5799 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 5800 | input_data->len), |
| 5801 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5802 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5803 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5804 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5805 | /* ------------------------------------------------------- */ |
| 5806 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5807 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5808 | additional_data->len), |
| 5809 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5810 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5811 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5812 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5813 | /* ------------------------------------------------------- */ |
| 5814 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5815 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 5816 | input_data->len, output_data, |
| 5817 | output_size, &output_length), |
| 5818 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5819 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5820 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5821 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5822 | /* ------------------------------------------------------- */ |
| 5823 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5824 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 5825 | finish_output_size, |
| 5826 | &output_part_length, |
| 5827 | tag_buffer, tag_length, |
| 5828 | &tag_size), |
| 5829 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5830 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5831 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5832 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5833 | /* ------------------------------------------------------- */ |
| 5834 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5835 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 5836 | finish_output_size, |
| 5837 | &output_part_length, |
| 5838 | tag_buffer, |
| 5839 | tag_length), |
| 5840 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5841 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5842 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5843 | |
| 5844 | /* Test for double setups. */ |
| 5845 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5846 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5847 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5848 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 5849 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5850 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5851 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5852 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5853 | /* ------------------------------------------------------- */ |
| 5854 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5855 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5856 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5857 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 5858 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5859 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5860 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5861 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5862 | /* ------------------------------------------------------- */ |
| 5863 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5864 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5865 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5866 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 5867 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5868 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5869 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5870 | |
| 5871 | /* ------------------------------------------------------- */ |
| 5872 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5873 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5874 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5875 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 5876 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5877 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5878 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5879 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5880 | /* Test for not setting a nonce. */ |
| 5881 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5882 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5883 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5884 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5885 | additional_data->len), |
| 5886 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5887 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5888 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5889 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5890 | /* ------------------------------------------------------- */ |
| 5891 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5892 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5893 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5894 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 5895 | input_data->len, output_data, |
| 5896 | output_size, &output_length), |
| 5897 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5898 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5899 | psa_aead_abort(&operation); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5900 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5901 | /* ------------------------------------------------------- */ |
| 5902 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5903 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5904 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5905 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 5906 | finish_output_size, |
| 5907 | &output_part_length, |
| 5908 | tag_buffer, tag_length, |
| 5909 | &tag_size), |
| 5910 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5911 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5912 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5913 | |
| 5914 | /* ------------------------------------------------------- */ |
| 5915 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5916 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5917 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5918 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 5919 | finish_output_size, |
| 5920 | &output_part_length, |
| 5921 | tag_buffer, |
| 5922 | tag_length), |
| 5923 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5924 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5925 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5926 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5927 | /* Test for double setting nonce. */ |
| 5928 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5929 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5930 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5931 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5932 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5933 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5934 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5935 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5936 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5937 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5938 | /* Test for double generating nonce. */ |
| 5939 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5940 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5941 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5942 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5943 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5944 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5945 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5946 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5947 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5948 | &nonce_length), |
| 5949 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5950 | |
| 5951 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5952 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5953 | |
| 5954 | /* Test for generate nonce then set and vice versa */ |
| 5955 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5956 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5957 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5958 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5959 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5960 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5961 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5962 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5963 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5964 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5965 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5966 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5967 | /* Test for generating nonce after calling set lengths */ |
| 5968 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5969 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5970 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5971 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5972 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5973 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5974 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5975 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5976 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5977 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5978 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5979 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5980 | /* 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] | 5981 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5982 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5983 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5984 | if (operation.alg == PSA_ALG_CCM) { |
| 5985 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5986 | input_data->len), |
| 5987 | PSA_ERROR_INVALID_ARGUMENT); |
| 5988 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5989 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5990 | &nonce_length), |
| 5991 | PSA_ERROR_BAD_STATE); |
| 5992 | } else { |
| 5993 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5994 | input_data->len)); |
| 5995 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5996 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5997 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5998 | } |
| 5999 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6000 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6001 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6002 | /* 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] | 6003 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6004 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6005 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6006 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 6007 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 6008 | input_data->len), |
| 6009 | PSA_ERROR_INVALID_ARGUMENT); |
| 6010 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6011 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6012 | &nonce_length), |
| 6013 | PSA_ERROR_BAD_STATE); |
| 6014 | } else { |
| 6015 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 6016 | input_data->len)); |
| 6017 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6018 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6019 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6020 | } |
| 6021 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6022 | psa_aead_abort(&operation); |
Dave Rodgman | d26d744 | 2023-02-11 17:14:54 +0000 | [diff] [blame] | 6023 | #endif |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6024 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6025 | /* 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] | 6026 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6027 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6028 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6029 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6030 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6031 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6032 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6033 | if (operation.alg == PSA_ALG_CCM) { |
| 6034 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6035 | input_data->len), |
| 6036 | PSA_ERROR_INVALID_ARGUMENT); |
| 6037 | } else { |
| 6038 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6039 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6040 | } |
| 6041 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6042 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6043 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6044 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6045 | /* Test for setting nonce after calling set lengths */ |
| 6046 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6047 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6048 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6049 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6050 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6051 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6052 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6053 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6054 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6055 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6056 | /* 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] | 6057 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6058 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6059 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6060 | if (operation.alg == PSA_ALG_CCM) { |
| 6061 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6062 | input_data->len), |
| 6063 | PSA_ERROR_INVALID_ARGUMENT); |
| 6064 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6065 | PSA_ERROR_BAD_STATE); |
| 6066 | } else { |
| 6067 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6068 | input_data->len)); |
| 6069 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6070 | } |
| 6071 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6072 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6073 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6074 | /* 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] | 6075 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6076 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6077 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6078 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 6079 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 6080 | input_data->len), |
| 6081 | PSA_ERROR_INVALID_ARGUMENT); |
| 6082 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6083 | PSA_ERROR_BAD_STATE); |
| 6084 | } else { |
| 6085 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 6086 | input_data->len)); |
| 6087 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6088 | } |
| 6089 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6090 | psa_aead_abort(&operation); |
Dave Rodgman | a476363 | 2023-02-11 18:36:23 +0000 | [diff] [blame] | 6091 | #endif |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6092 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6093 | /* 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] | 6094 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6095 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6096 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6097 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6098 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6099 | if (operation.alg == PSA_ALG_CCM) { |
| 6100 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6101 | input_data->len), |
| 6102 | PSA_ERROR_INVALID_ARGUMENT); |
| 6103 | } else { |
| 6104 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6105 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6106 | } |
| 6107 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6108 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6109 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6110 | /* 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] | 6111 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6112 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6114 | if (operation.alg == PSA_ALG_GCM) { |
| 6115 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6116 | SIZE_MAX), |
| 6117 | PSA_ERROR_INVALID_ARGUMENT); |
| 6118 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6119 | PSA_ERROR_BAD_STATE); |
| 6120 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6121 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6122 | SIZE_MAX)); |
| 6123 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6124 | } |
| 6125 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6126 | psa_aead_abort(&operation); |
Dave Rodgman | 91e8321 | 2023-02-11 20:07:43 +0000 | [diff] [blame] | 6127 | #endif |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6128 | |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 6129 | /* 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] | 6130 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6131 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6132 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6133 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6135 | if (operation.alg == PSA_ALG_GCM) { |
| 6136 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6137 | SIZE_MAX), |
| 6138 | PSA_ERROR_INVALID_ARGUMENT); |
| 6139 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6140 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6141 | SIZE_MAX)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6142 | } |
| 6143 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6144 | psa_aead_abort(&operation); |
Dave Rodgman | 641288b | 2023-02-11 22:02:04 +0000 | [diff] [blame] | 6145 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6146 | |
| 6147 | /* ------------------------------------------------------- */ |
| 6148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6149 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6151 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6153 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6154 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6155 | &nonce_length), |
| 6156 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6157 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6158 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6159 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6160 | /* Test for generating nonce in decrypt setup. */ |
| 6161 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6162 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6164 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6165 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6166 | &nonce_length), |
| 6167 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6168 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6169 | psa_aead_abort(&operation); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6170 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6171 | /* Test for setting lengths twice. */ |
| 6172 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6173 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6174 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6175 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6176 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6177 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6178 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6179 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6180 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6181 | input_data->len), |
| 6182 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6183 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6184 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6185 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6186 | /* Test for setting lengths after setting nonce + already starting data. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6187 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6188 | PSA_ASSERT(psa_aead_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 | if (operation.alg == PSA_ALG_CCM) { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6193 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6194 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6195 | additional_data->len), |
| 6196 | PSA_ERROR_BAD_STATE); |
| 6197 | } else { |
| 6198 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6199 | additional_data->len)); |
| 6200 | |
| 6201 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6202 | input_data->len), |
| 6203 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6204 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6205 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6206 | |
| 6207 | /* ------------------------------------------------------- */ |
| 6208 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6209 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6210 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6211 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6212 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6213 | if (operation.alg == PSA_ALG_CCM) { |
| 6214 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6215 | input_data->len, output_data, |
| 6216 | output_size, &output_length), |
| 6217 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6218 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6219 | } else { |
| 6220 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6221 | input_data->len, output_data, |
| 6222 | output_size, &output_length)); |
| 6223 | |
| 6224 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6225 | input_data->len), |
| 6226 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6227 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6228 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6229 | |
| 6230 | /* ------------------------------------------------------- */ |
| 6231 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6232 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6233 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6234 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6235 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6236 | if (operation.alg == PSA_ALG_CCM) { |
| 6237 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6238 | finish_output_size, |
| 6239 | &output_part_length, |
| 6240 | tag_buffer, tag_length, |
| 6241 | &tag_size)); |
| 6242 | } else { |
| 6243 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6244 | finish_output_size, |
| 6245 | &output_part_length, |
| 6246 | tag_buffer, tag_length, |
| 6247 | &tag_size)); |
| 6248 | |
| 6249 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6250 | input_data->len), |
| 6251 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6252 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6253 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6254 | |
| 6255 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 6256 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6257 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6258 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6259 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6260 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6261 | &nonce_length)); |
| 6262 | if (operation.alg == PSA_ALG_CCM) { |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6263 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6264 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6265 | additional_data->len), |
| 6266 | PSA_ERROR_BAD_STATE); |
| 6267 | } else { |
| 6268 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6269 | additional_data->len)); |
| 6270 | |
| 6271 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6272 | input_data->len), |
| 6273 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6274 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6275 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6276 | |
| 6277 | /* ------------------------------------------------------- */ |
| 6278 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6279 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6280 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6281 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6282 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6283 | &nonce_length)); |
| 6284 | if (operation.alg == PSA_ALG_CCM) { |
| 6285 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6286 | input_data->len, output_data, |
| 6287 | output_size, &output_length), |
| 6288 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6289 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6290 | } else { |
| 6291 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6292 | input_data->len, output_data, |
| 6293 | output_size, &output_length)); |
| 6294 | |
| 6295 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6296 | input_data->len), |
| 6297 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6298 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6299 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6300 | |
| 6301 | /* ------------------------------------------------------- */ |
| 6302 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6303 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6304 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6305 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6306 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6307 | &nonce_length)); |
| 6308 | if (operation.alg == PSA_ALG_CCM) { |
| 6309 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6310 | finish_output_size, |
| 6311 | &output_part_length, |
| 6312 | tag_buffer, tag_length, |
| 6313 | &tag_size)); |
| 6314 | } else { |
| 6315 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6316 | finish_output_size, |
| 6317 | &output_part_length, |
| 6318 | tag_buffer, tag_length, |
| 6319 | &tag_size)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6320 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6321 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6322 | input_data->len), |
| 6323 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6324 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6325 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6326 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6327 | /* Test for not sending any additional data or data after setting non zero |
| 6328 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6329 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6330 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6331 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6332 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6333 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6334 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6335 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6336 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6337 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6338 | finish_output_size, |
| 6339 | &output_part_length, |
| 6340 | tag_buffer, tag_length, |
| 6341 | &tag_size), |
| 6342 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6343 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6344 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6345 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6346 | /* Test for not sending any additional data or data after setting non-zero |
| 6347 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6349 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6350 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6351 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6353 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6354 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6356 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6357 | finish_output_size, |
| 6358 | &output_part_length, |
| 6359 | tag_buffer, |
| 6360 | tag_length), |
| 6361 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6362 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6363 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6364 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6365 | /* Test for not sending any additional data after setting a non-zero length |
| 6366 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6367 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6368 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6370 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6371 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6372 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6373 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6374 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6375 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6376 | input_data->len, output_data, |
| 6377 | output_size, &output_length), |
| 6378 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6379 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6380 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6381 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6382 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 6383 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6384 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6386 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6387 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6388 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6389 | input_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6390 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6391 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6392 | additional_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6393 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6394 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6395 | finish_output_size, |
| 6396 | &output_part_length, |
| 6397 | tag_buffer, tag_length, |
| 6398 | &tag_size), |
| 6399 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6400 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6401 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6402 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6403 | /* Test for sending too much additional data after setting lengths. */ |
| 6404 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6405 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6406 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6407 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6408 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6409 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6410 | |
| 6411 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6412 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6413 | additional_data->len), |
| 6414 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6415 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6416 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6417 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6418 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6419 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6420 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6421 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6422 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6423 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6424 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6425 | input_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6426 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6427 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6428 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6429 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6430 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6431 | 1), |
| 6432 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6433 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6434 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6435 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6436 | /* Test for sending too much data after setting lengths. */ |
| 6437 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6438 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6439 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6440 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6441 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6442 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6444 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6445 | input_data->len, output_data, |
| 6446 | output_size, &output_length), |
| 6447 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6449 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6450 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6451 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6452 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6453 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6454 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6455 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6456 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6457 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6458 | input_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6459 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6460 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6461 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6462 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6463 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6464 | input_data->len, output_data, |
| 6465 | output_size, &output_length)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6466 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6467 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6468 | 1, output_data, |
| 6469 | output_size, &output_length), |
| 6470 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6471 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6472 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6473 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6474 | /* Test sending additional data after data. */ |
| 6475 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6476 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6479 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6480 | if (operation.alg != PSA_ALG_CCM) { |
| 6481 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6482 | input_data->len, output_data, |
| 6483 | output_size, &output_length)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6484 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6485 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6486 | additional_data->len), |
| 6487 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6488 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6489 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6490 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6491 | /* Test calling finish on decryption. */ |
| 6492 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6493 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6494 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6495 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6496 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6497 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6498 | finish_output_size, |
| 6499 | &output_part_length, |
| 6500 | tag_buffer, tag_length, |
| 6501 | &tag_size), |
| 6502 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6503 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6504 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6505 | |
| 6506 | /* Test calling verify on encryption. */ |
| 6507 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6508 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6509 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6510 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6511 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6512 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6513 | finish_output_size, |
| 6514 | &output_part_length, |
| 6515 | tag_buffer, |
| 6516 | tag_length), |
| 6517 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6518 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6519 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6520 | |
| 6521 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6522 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6523 | psa_destroy_key(key); |
| 6524 | psa_aead_abort(&operation); |
| 6525 | mbedtls_free(output_data); |
| 6526 | mbedtls_free(final_data); |
| 6527 | PSA_DONE(); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6528 | } |
| 6529 | /* END_CASE */ |
| 6530 | |
| 6531 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6532 | void signature_size(int type_arg, |
| 6533 | int bits, |
| 6534 | int alg_arg, |
| 6535 | int expected_size_arg) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6536 | { |
| 6537 | psa_key_type_t type = type_arg; |
| 6538 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6539 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6540 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6541 | TEST_EQUAL(actual_size, (size_t) expected_size_arg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6542 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6543 | exit: |
| 6544 | ; |
| 6545 | } |
| 6546 | /* END_CASE */ |
| 6547 | |
| 6548 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6549 | void sign_hash_deterministic(int key_type_arg, data_t *key_data, |
| 6550 | int alg_arg, data_t *input_data, |
| 6551 | data_t *output_data) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6552 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6553 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6554 | psa_key_type_t key_type = key_type_arg; |
| 6555 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6556 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6557 | unsigned char *signature = NULL; |
| 6558 | size_t signature_size; |
| 6559 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6560 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6562 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6563 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6564 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6565 | psa_set_key_algorithm(&attributes, alg); |
| 6566 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6567 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6568 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6569 | &key)); |
| 6570 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6571 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6572 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6573 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6574 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6575 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6576 | key_bits, alg); |
| 6577 | TEST_ASSERT(signature_size != 0); |
| 6578 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6579 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6580 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6581 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6582 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6583 | input_data->x, input_data->len, |
| 6584 | signature, signature_size, |
| 6585 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6586 | /* Verify that the signature is what is expected. */ |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 6587 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 6588 | signature, signature_length); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6589 | |
| 6590 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6591 | /* |
| 6592 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6593 | * thus reset them as required. |
| 6594 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6595 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6596 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6597 | psa_destroy_key(key); |
| 6598 | mbedtls_free(signature); |
| 6599 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6600 | } |
| 6601 | /* END_CASE */ |
| 6602 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6603 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6604 | /** |
| 6605 | * sign_hash_interruptible() test intentions: |
| 6606 | * |
| 6607 | * Note: This test can currently only handle ECDSA. |
| 6608 | * |
| 6609 | * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 6610 | * and private keys / keypairs only). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6611 | * |
| 6612 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 6613 | * expected for different max_ops values. |
| 6614 | * |
| 6615 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 6616 | * and that each successful stage completes some ops (this is not mandated by |
| 6617 | * the PSA specification, but is currently the case). |
| 6618 | * |
| 6619 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 6620 | * complete() calls does not alter the number of ops returned. |
| 6621 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6622 | void sign_hash_interruptible(int key_type_arg, data_t *key_data, |
| 6623 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6624 | data_t *output_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6625 | { |
| 6626 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6627 | psa_key_type_t key_type = key_type_arg; |
| 6628 | psa_algorithm_t alg = alg_arg; |
| 6629 | size_t key_bits; |
| 6630 | unsigned char *signature = NULL; |
| 6631 | size_t signature_size; |
| 6632 | size_t signature_length = 0xdeadbeef; |
| 6633 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6634 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6635 | uint32_t num_ops = 0; |
| 6636 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6637 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6638 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6639 | size_t min_completes = 0; |
| 6640 | size_t max_completes = 0; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6641 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6642 | psa_sign_hash_interruptible_operation_t operation = |
| 6643 | psa_sign_hash_interruptible_operation_init(); |
| 6644 | |
| 6645 | PSA_ASSERT(psa_crypto_init()); |
| 6646 | |
| 6647 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6648 | psa_set_key_algorithm(&attributes, alg); |
| 6649 | psa_set_key_type(&attributes, key_type); |
| 6650 | |
| 6651 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6652 | &key)); |
| 6653 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6654 | key_bits = psa_get_key_bits(&attributes); |
| 6655 | |
| 6656 | /* Allocate a buffer which has the size advertised by the |
| 6657 | * library. */ |
| 6658 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6659 | key_bits, alg); |
| 6660 | TEST_ASSERT(signature_size != 0); |
| 6661 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6662 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6663 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6664 | psa_interruptible_set_max_ops(max_ops); |
| 6665 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6666 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6667 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6668 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6669 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6670 | TEST_ASSERT(num_ops_prior == 0); |
| 6671 | |
| 6672 | /* Start performing the signature. */ |
| 6673 | PSA_ASSERT(psa_sign_hash_start(&operation, key, alg, |
| 6674 | input_data->x, input_data->len)); |
| 6675 | |
| 6676 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6677 | TEST_ASSERT(num_ops_prior == 0); |
| 6678 | |
| 6679 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6680 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6681 | status = psa_sign_hash_complete(&operation, signature, signature_size, |
| 6682 | &signature_length); |
| 6683 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6684 | num_completes++; |
| 6685 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6686 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 6687 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6688 | /* We are asserting here that every complete makes progress |
| 6689 | * (completes some ops), which is true of the internal |
| 6690 | * implementation and probably any implementation, however this is |
| 6691 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6692 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6693 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6694 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 6695 | |
| 6696 | /* Ensure calling get_num_ops() twice still returns the same |
| 6697 | * number of ops as previously reported. */ |
| 6698 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6699 | |
| 6700 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6701 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6702 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6703 | |
| 6704 | TEST_ASSERT(status == PSA_SUCCESS); |
| 6705 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6706 | TEST_LE_U(min_completes, num_completes); |
| 6707 | TEST_LE_U(num_completes, max_completes); |
| 6708 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6709 | /* Verify that the signature is what is expected. */ |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 6710 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 6711 | signature, signature_length); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6712 | |
| 6713 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6714 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6715 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6716 | TEST_ASSERT(num_ops == 0); |
| 6717 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6718 | exit: |
| 6719 | |
| 6720 | /* |
| 6721 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6722 | * thus reset them as required. |
| 6723 | */ |
| 6724 | psa_reset_key_attributes(&attributes); |
| 6725 | |
| 6726 | psa_destroy_key(key); |
| 6727 | mbedtls_free(signature); |
| 6728 | PSA_DONE(); |
| 6729 | } |
| 6730 | /* END_CASE */ |
| 6731 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6732 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6733 | void sign_hash_fail(int key_type_arg, data_t *key_data, |
| 6734 | int alg_arg, data_t *input_data, |
| 6735 | int signature_size_arg, int expected_status_arg) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6736 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6737 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6738 | psa_key_type_t key_type = key_type_arg; |
| 6739 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6740 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6741 | psa_status_t actual_status; |
| 6742 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 6743 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 6744 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6745 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6746 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6747 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6748 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6749 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6750 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6751 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6752 | psa_set_key_algorithm(&attributes, alg); |
| 6753 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6754 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6755 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6756 | &key)); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6757 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6758 | actual_status = psa_sign_hash(key, alg, |
| 6759 | input_data->x, input_data->len, |
| 6760 | signature, signature_size, |
| 6761 | &signature_length); |
| 6762 | TEST_EQUAL(actual_status, expected_status); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6763 | /* The value of *signature_length is unspecified on error, but |
| 6764 | * whatever it is, it should be less than signature_size, so that |
| 6765 | * if the caller tries to read *signature_length bytes without |
| 6766 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6767 | TEST_LE_U(signature_length, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6768 | |
| 6769 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6770 | psa_reset_key_attributes(&attributes); |
| 6771 | psa_destroy_key(key); |
| 6772 | mbedtls_free(signature); |
| 6773 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6774 | } |
| 6775 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6776 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6777 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6778 | /** |
| 6779 | * sign_hash_fail_interruptible() test intentions: |
| 6780 | * |
| 6781 | * Note: This test can currently only handle ECDSA. |
| 6782 | * |
| 6783 | * 1. Test that various failure cases for interruptible sign hash fail with the |
| 6784 | * correct error codes, and at the correct point (at start or during |
| 6785 | * complete). |
| 6786 | * |
| 6787 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 6788 | * expected for different max_ops values. |
| 6789 | * |
| 6790 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 6791 | * and that each successful stage completes some ops (this is not mandated by |
| 6792 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 6793 | * |
| 6794 | * 4. Check that calling complete() when start() fails and complete() |
| 6795 | * after completion results in a BAD_STATE error. |
| 6796 | * |
| 6797 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 6798 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6799 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6800 | void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 6801 | int alg_arg, data_t *input_data, |
| 6802 | int signature_size_arg, |
| 6803 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6804 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6805 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6806 | { |
| 6807 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6808 | psa_key_type_t key_type = key_type_arg; |
| 6809 | psa_algorithm_t alg = alg_arg; |
| 6810 | size_t signature_size = signature_size_arg; |
| 6811 | psa_status_t actual_status; |
| 6812 | psa_status_t expected_start_status = expected_start_status_arg; |
| 6813 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 6814 | unsigned char *signature = NULL; |
| 6815 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6816 | uint32_t num_ops = 0; |
| 6817 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6818 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6819 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6820 | size_t min_completes = 0; |
| 6821 | size_t max_completes = 0; |
| 6822 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6823 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6824 | psa_sign_hash_interruptible_operation_t operation = |
| 6825 | psa_sign_hash_interruptible_operation_init(); |
| 6826 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6827 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6828 | |
| 6829 | PSA_ASSERT(psa_crypto_init()); |
| 6830 | |
| 6831 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6832 | psa_set_key_algorithm(&attributes, alg); |
| 6833 | psa_set_key_type(&attributes, key_type); |
| 6834 | |
| 6835 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6836 | &key)); |
| 6837 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6838 | psa_interruptible_set_max_ops(max_ops); |
| 6839 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6840 | interruptible_signverify_get_minmax_completes(max_ops, |
| 6841 | expected_complete_status, |
| 6842 | &min_completes, |
| 6843 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6844 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6845 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6846 | TEST_ASSERT(num_ops_prior == 0); |
| 6847 | |
| 6848 | /* Start performing the signature. */ |
| 6849 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 6850 | input_data->x, input_data->len); |
| 6851 | |
| 6852 | TEST_EQUAL(actual_status, expected_start_status); |
| 6853 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6854 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 6855 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 6856 | * start failed. */ |
| 6857 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6858 | signature_size, |
| 6859 | &signature_length); |
| 6860 | |
| 6861 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 6862 | |
| 6863 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6864 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 6865 | input_data->x, input_data->len); |
| 6866 | |
| 6867 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 6868 | } |
| 6869 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6870 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6871 | TEST_ASSERT(num_ops_prior == 0); |
| 6872 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6873 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6874 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6875 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6876 | signature_size, |
| 6877 | &signature_length); |
| 6878 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6879 | num_completes++; |
| 6880 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6881 | if (actual_status == PSA_SUCCESS || |
| 6882 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6883 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6884 | /* We are asserting here that every complete makes progress |
| 6885 | * (completes some ops), which is true of the internal |
| 6886 | * implementation and probably any implementation, however this is |
| 6887 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6888 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6889 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6890 | num_ops_prior = num_ops; |
| 6891 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6892 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6893 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6894 | TEST_EQUAL(actual_status, expected_complete_status); |
| 6895 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 6896 | /* Check that another complete returns BAD_STATE. */ |
| 6897 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6898 | signature_size, |
| 6899 | &signature_length); |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6900 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 6901 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6902 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6903 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6904 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6905 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6906 | TEST_ASSERT(num_ops == 0); |
| 6907 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6908 | /* The value of *signature_length is unspecified on error, but |
| 6909 | * whatever it is, it should be less than signature_size, so that |
| 6910 | * if the caller tries to read *signature_length bytes without |
| 6911 | * checking the error code then they don't overflow a buffer. */ |
| 6912 | TEST_LE_U(signature_length, signature_size); |
| 6913 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6914 | TEST_LE_U(min_completes, num_completes); |
| 6915 | TEST_LE_U(num_completes, max_completes); |
| 6916 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6917 | exit: |
| 6918 | psa_reset_key_attributes(&attributes); |
| 6919 | psa_destroy_key(key); |
| 6920 | mbedtls_free(signature); |
| 6921 | PSA_DONE(); |
| 6922 | } |
| 6923 | /* END_CASE */ |
| 6924 | |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6925 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6926 | void sign_verify_hash(int key_type_arg, data_t *key_data, |
| 6927 | int alg_arg, data_t *input_data) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6928 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6929 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6930 | psa_key_type_t key_type = key_type_arg; |
| 6931 | psa_algorithm_t alg = alg_arg; |
| 6932 | size_t key_bits; |
| 6933 | unsigned char *signature = NULL; |
| 6934 | size_t signature_size; |
| 6935 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6936 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6937 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6938 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6939 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6940 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 6941 | psa_set_key_algorithm(&attributes, alg); |
| 6942 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6943 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6944 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6945 | &key)); |
| 6946 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6947 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6948 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6949 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6950 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6951 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6952 | key_bits, alg); |
| 6953 | TEST_ASSERT(signature_size != 0); |
| 6954 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6955 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6956 | |
| 6957 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6958 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6959 | input_data->x, input_data->len, |
| 6960 | signature, signature_size, |
| 6961 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6962 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6963 | TEST_LE_U(signature_length, signature_size); |
| 6964 | TEST_ASSERT(signature_length > 0); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6965 | |
| 6966 | /* Use the library to verify that the signature is correct. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6967 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 6968 | input_data->x, input_data->len, |
| 6969 | signature, signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6970 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6971 | if (input_data->len != 0) { |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6972 | /* Flip a bit in the input and verify that the signature is now |
| 6973 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6974 | * because ECDSA may ignore the last few bits of the input. */ |
| 6975 | input_data->x[0] ^= 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6976 | TEST_EQUAL(psa_verify_hash(key, alg, |
| 6977 | input_data->x, input_data->len, |
| 6978 | signature, signature_length), |
| 6979 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6980 | } |
| 6981 | |
| 6982 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6983 | /* |
| 6984 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6985 | * thus reset them as required. |
| 6986 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6987 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6988 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6989 | psa_destroy_key(key); |
| 6990 | mbedtls_free(signature); |
| 6991 | PSA_DONE(); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6992 | } |
| 6993 | /* END_CASE */ |
| 6994 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6995 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6996 | /** |
| 6997 | * sign_verify_hash_interruptible() test intentions: |
| 6998 | * |
| 6999 | * Note: This test can currently only handle ECDSA. |
| 7000 | * |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 7001 | * 1. Test that we can sign an input hash with the given keypair and then |
| 7002 | * afterwards verify that signature. This is currently the only way to test |
| 7003 | * non deterministic ECDSA, but this test can also handle deterministic. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7004 | * |
| 7005 | * 2. Test that after corrupting the hash, the verification detects an invalid |
| 7006 | * signature. |
| 7007 | * |
| 7008 | * 3. Test the number of calls to psa_sign_hash_complete() required are as |
| 7009 | * expected for different max_ops values. |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7010 | * |
| 7011 | * 4. Test that the number of ops done prior to starting signing and after abort |
| 7012 | * is zero and that each successful signing stage completes some ops (this is |
| 7013 | * not mandated by the PSA specification, but is currently the case). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7014 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7015 | 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] | 7016 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7017 | int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7018 | { |
| 7019 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7020 | psa_key_type_t key_type = key_type_arg; |
| 7021 | psa_algorithm_t alg = alg_arg; |
| 7022 | size_t key_bits; |
| 7023 | unsigned char *signature = NULL; |
| 7024 | size_t signature_size; |
| 7025 | size_t signature_length = 0xdeadbeef; |
| 7026 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7027 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7028 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7029 | uint32_t num_ops = 0; |
| 7030 | uint32_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7031 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7032 | size_t min_completes = 0; |
| 7033 | size_t max_completes = 0; |
| 7034 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7035 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7036 | psa_sign_hash_interruptible_operation_init(); |
| 7037 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7038 | psa_verify_hash_interruptible_operation_init(); |
| 7039 | |
| 7040 | PSA_ASSERT(psa_crypto_init()); |
| 7041 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7042 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7043 | PSA_KEY_USAGE_VERIFY_HASH); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7044 | psa_set_key_algorithm(&attributes, alg); |
| 7045 | psa_set_key_type(&attributes, key_type); |
| 7046 | |
| 7047 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7048 | &key)); |
| 7049 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7050 | key_bits = psa_get_key_bits(&attributes); |
| 7051 | |
| 7052 | /* Allocate a buffer which has the size advertised by the |
| 7053 | * library. */ |
| 7054 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7055 | key_bits, alg); |
| 7056 | TEST_ASSERT(signature_size != 0); |
| 7057 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7058 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7059 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7060 | psa_interruptible_set_max_ops(max_ops); |
| 7061 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7062 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 7063 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7064 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7065 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); |
| 7066 | TEST_ASSERT(num_ops_prior == 0); |
| 7067 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7068 | /* Start performing the signature. */ |
| 7069 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7070 | input_data->x, input_data->len)); |
| 7071 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7072 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); |
| 7073 | TEST_ASSERT(num_ops_prior == 0); |
| 7074 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7075 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7076 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7077 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7078 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7079 | signature_size, |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7080 | &signature_length); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7081 | |
| 7082 | num_completes++; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7083 | |
| 7084 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 7085 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7086 | /* We are asserting here that every complete makes progress |
| 7087 | * (completes some ops), which is true of the internal |
| 7088 | * implementation and probably any implementation, however this is |
| 7089 | * not mandated by the PSA specification. */ |
| 7090 | TEST_ASSERT(num_ops > num_ops_prior); |
| 7091 | |
| 7092 | num_ops_prior = num_ops; |
| 7093 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7094 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7095 | |
| 7096 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7097 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7098 | TEST_LE_U(min_completes, num_completes); |
| 7099 | TEST_LE_U(num_completes, max_completes); |
| 7100 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7101 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7102 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7103 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7104 | TEST_ASSERT(num_ops == 0); |
| 7105 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7106 | /* Check that the signature length looks sensible. */ |
| 7107 | TEST_LE_U(signature_length, signature_size); |
| 7108 | TEST_ASSERT(signature_length > 0); |
| 7109 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7110 | num_completes = 0; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7111 | |
| 7112 | /* Start verification. */ |
| 7113 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7114 | input_data->x, input_data->len, |
| 7115 | signature, signature_length)); |
| 7116 | |
| 7117 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7118 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7119 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7120 | |
| 7121 | num_completes++; |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7122 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7123 | |
| 7124 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7125 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7126 | TEST_LE_U(min_completes, num_completes); |
| 7127 | TEST_LE_U(num_completes, max_completes); |
| 7128 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7129 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7130 | |
| 7131 | verify_operation = psa_verify_hash_interruptible_operation_init(); |
| 7132 | |
| 7133 | if (input_data->len != 0) { |
| 7134 | /* Flip a bit in the input and verify that the signature is now |
| 7135 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 7136 | * because ECDSA may ignore the last few bits of the input. */ |
| 7137 | input_data->x[0] ^= 1; |
| 7138 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7139 | /* Start verification. */ |
| 7140 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7141 | input_data->x, input_data->len, |
| 7142 | signature, signature_length)); |
| 7143 | |
| 7144 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7145 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7146 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7147 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7148 | |
| 7149 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7150 | } |
| 7151 | |
| 7152 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7153 | |
| 7154 | exit: |
| 7155 | /* |
| 7156 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7157 | * thus reset them as required. |
| 7158 | */ |
| 7159 | psa_reset_key_attributes(&attributes); |
| 7160 | |
| 7161 | psa_destroy_key(key); |
| 7162 | mbedtls_free(signature); |
| 7163 | PSA_DONE(); |
| 7164 | } |
| 7165 | /* END_CASE */ |
| 7166 | |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7167 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7168 | void verify_hash(int key_type_arg, data_t *key_data, |
| 7169 | int alg_arg, data_t *hash_data, |
| 7170 | data_t *signature_data) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7171 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7172 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7173 | psa_key_type_t key_type = key_type_arg; |
| 7174 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7175 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7176 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7177 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 7178 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7179 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7181 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7182 | psa_set_key_algorithm(&attributes, alg); |
| 7183 | psa_set_key_type(&attributes, key_type); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7184 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7185 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7186 | &key)); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7187 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7188 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 7189 | hash_data->x, hash_data->len, |
| 7190 | signature_data->x, signature_data->len)); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 7191 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7192 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7193 | psa_reset_key_attributes(&attributes); |
| 7194 | psa_destroy_key(key); |
| 7195 | PSA_DONE(); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7196 | } |
| 7197 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7198 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7199 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7200 | /** |
| 7201 | * verify_hash_interruptible() test intentions: |
| 7202 | * |
| 7203 | * Note: This test can currently only handle ECDSA. |
| 7204 | * |
| 7205 | * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 7206 | * only). Given this test only does verification it can accept public keys as |
| 7207 | * well as private keys / keypairs. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7208 | * |
| 7209 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7210 | * expected for different max_ops values. |
| 7211 | * |
| 7212 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7213 | * and that each successful stage completes some ops (this is not mandated by |
| 7214 | * the PSA specification, but is currently the case). |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7215 | * |
| 7216 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 7217 | * complete() calls does not alter the number of ops returned. |
| 7218 | * |
| 7219 | * 5. Test that after corrupting the hash, the verification detects an invalid |
| 7220 | * signature. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7221 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7222 | void verify_hash_interruptible(int key_type_arg, data_t *key_data, |
| 7223 | int alg_arg, data_t *hash_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7224 | data_t *signature_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7225 | { |
| 7226 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7227 | psa_key_type_t key_type = key_type_arg; |
| 7228 | psa_algorithm_t alg = alg_arg; |
| 7229 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7230 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7231 | uint32_t num_ops = 0; |
| 7232 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7233 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7234 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7235 | size_t min_completes = 0; |
| 7236 | size_t max_completes = 0; |
| 7237 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7238 | psa_verify_hash_interruptible_operation_t operation = |
| 7239 | psa_verify_hash_interruptible_operation_init(); |
| 7240 | |
| 7241 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 7242 | |
| 7243 | PSA_ASSERT(psa_crypto_init()); |
| 7244 | |
| 7245 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7246 | psa_set_key_algorithm(&attributes, alg); |
| 7247 | psa_set_key_type(&attributes, key_type); |
| 7248 | |
| 7249 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7250 | &key)); |
| 7251 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7252 | psa_interruptible_set_max_ops(max_ops); |
| 7253 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7254 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 7255 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7256 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7257 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7258 | |
| 7259 | TEST_ASSERT(num_ops_prior == 0); |
| 7260 | |
| 7261 | /* Start verification. */ |
| 7262 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7263 | hash_data->x, hash_data->len, |
| 7264 | signature_data->x, signature_data->len) |
| 7265 | ); |
| 7266 | |
| 7267 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7268 | |
| 7269 | TEST_ASSERT(num_ops_prior == 0); |
| 7270 | |
| 7271 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7272 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7273 | status = psa_verify_hash_complete(&operation); |
| 7274 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7275 | num_completes++; |
| 7276 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7277 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 7278 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7279 | /* We are asserting here that every complete makes progress |
| 7280 | * (completes some ops), which is true of the internal |
| 7281 | * implementation and probably any implementation, however this is |
| 7282 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7283 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7284 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7285 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 7286 | |
| 7287 | /* Ensure calling get_num_ops() twice still returns the same |
| 7288 | * number of ops as previously reported. */ |
| 7289 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7290 | |
| 7291 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7292 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7293 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7294 | |
| 7295 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7296 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7297 | TEST_LE_U(min_completes, num_completes); |
| 7298 | TEST_LE_U(num_completes, max_completes); |
| 7299 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7300 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7301 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7302 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7303 | TEST_ASSERT(num_ops == 0); |
| 7304 | |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7305 | if (hash_data->len != 0) { |
| 7306 | /* Flip a bit in the hash and verify that the signature is now detected |
| 7307 | * as invalid. Flip a bit at the beginning, not at the end, because |
| 7308 | * ECDSA may ignore the last few bits of the input. */ |
| 7309 | hash_data->x[0] ^= 1; |
| 7310 | |
| 7311 | /* Start verification. */ |
| 7312 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7313 | hash_data->x, hash_data->len, |
| 7314 | signature_data->x, signature_data->len)); |
| 7315 | |
| 7316 | /* Continue performing the signature until complete. */ |
| 7317 | do { |
| 7318 | status = psa_verify_hash_complete(&operation); |
| 7319 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7320 | |
| 7321 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7322 | } |
| 7323 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7324 | exit: |
| 7325 | psa_reset_key_attributes(&attributes); |
| 7326 | psa_destroy_key(key); |
| 7327 | PSA_DONE(); |
| 7328 | } |
| 7329 | /* END_CASE */ |
| 7330 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7331 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7332 | void verify_hash_fail(int key_type_arg, data_t *key_data, |
| 7333 | int alg_arg, data_t *hash_data, |
| 7334 | data_t *signature_data, |
| 7335 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7336 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7337 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7338 | psa_key_type_t key_type = key_type_arg; |
| 7339 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7340 | psa_status_t actual_status; |
| 7341 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7342 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7343 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7344 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7345 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7346 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7347 | psa_set_key_algorithm(&attributes, alg); |
| 7348 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7349 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7350 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7351 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7353 | actual_status = psa_verify_hash(key, alg, |
| 7354 | hash_data->x, hash_data->len, |
| 7355 | signature_data->x, signature_data->len); |
| 7356 | TEST_EQUAL(actual_status, expected_status); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7357 | |
| 7358 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7359 | psa_reset_key_attributes(&attributes); |
| 7360 | psa_destroy_key(key); |
| 7361 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7362 | } |
| 7363 | /* END_CASE */ |
| 7364 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7365 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7366 | /** |
| 7367 | * verify_hash_fail_interruptible() test intentions: |
| 7368 | * |
| 7369 | * Note: This test can currently only handle ECDSA. |
| 7370 | * |
| 7371 | * 1. Test that various failure cases for interruptible verify hash fail with |
| 7372 | * the correct error codes, and at the correct point (at start or during |
| 7373 | * complete). |
| 7374 | * |
| 7375 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7376 | * expected for different max_ops values. |
| 7377 | * |
| 7378 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7379 | * and that each successful stage completes some ops (this is not mandated by |
| 7380 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7381 | * |
| 7382 | * 4. Check that calling complete() when start() fails and complete() |
| 7383 | * after completion results in a BAD_STATE error. |
| 7384 | * |
| 7385 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 7386 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7387 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7388 | void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 7389 | int alg_arg, data_t *hash_data, |
| 7390 | data_t *signature_data, |
| 7391 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7392 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7393 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7394 | { |
| 7395 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7396 | psa_key_type_t key_type = key_type_arg; |
| 7397 | psa_algorithm_t alg = alg_arg; |
| 7398 | psa_status_t actual_status; |
| 7399 | psa_status_t expected_start_status = expected_start_status_arg; |
| 7400 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 7401 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7402 | uint32_t num_ops = 0; |
| 7403 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7404 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7405 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7406 | size_t min_completes = 0; |
| 7407 | size_t max_completes = 0; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7408 | psa_verify_hash_interruptible_operation_t operation = |
| 7409 | psa_verify_hash_interruptible_operation_init(); |
| 7410 | |
| 7411 | PSA_ASSERT(psa_crypto_init()); |
| 7412 | |
| 7413 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7414 | psa_set_key_algorithm(&attributes, alg); |
| 7415 | psa_set_key_type(&attributes, key_type); |
| 7416 | |
| 7417 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7418 | &key)); |
| 7419 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7420 | psa_interruptible_set_max_ops(max_ops); |
| 7421 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7422 | interruptible_signverify_get_minmax_completes(max_ops, |
| 7423 | expected_complete_status, |
| 7424 | &min_completes, |
| 7425 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7426 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7427 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7428 | TEST_ASSERT(num_ops_prior == 0); |
| 7429 | |
| 7430 | /* Start verification. */ |
| 7431 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7432 | hash_data->x, hash_data->len, |
| 7433 | signature_data->x, |
| 7434 | signature_data->len); |
| 7435 | |
| 7436 | TEST_EQUAL(actual_status, expected_start_status); |
| 7437 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7438 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 7439 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7440 | * start failed. */ |
| 7441 | actual_status = psa_verify_hash_complete(&operation); |
| 7442 | |
| 7443 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7444 | |
| 7445 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7446 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7447 | hash_data->x, hash_data->len, |
| 7448 | signature_data->x, |
| 7449 | signature_data->len); |
| 7450 | |
| 7451 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7452 | } |
| 7453 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7454 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7455 | TEST_ASSERT(num_ops_prior == 0); |
| 7456 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7457 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7458 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7459 | actual_status = psa_verify_hash_complete(&operation); |
| 7460 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7461 | num_completes++; |
| 7462 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7463 | if (actual_status == PSA_SUCCESS || |
| 7464 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7465 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7466 | /* We are asserting here that every complete makes progress |
| 7467 | * (completes some ops), which is true of the internal |
| 7468 | * implementation and probably any implementation, however this is |
| 7469 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7470 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7471 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7472 | num_ops_prior = num_ops; |
| 7473 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7474 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7475 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7476 | TEST_EQUAL(actual_status, expected_complete_status); |
| 7477 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 7478 | /* Check that another complete returns BAD_STATE. */ |
| 7479 | actual_status = psa_verify_hash_complete(&operation); |
| 7480 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7481 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7482 | TEST_LE_U(min_completes, num_completes); |
| 7483 | TEST_LE_U(num_completes, max_completes); |
| 7484 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7485 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7486 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7487 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7488 | TEST_ASSERT(num_ops == 0); |
| 7489 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7490 | exit: |
| 7491 | psa_reset_key_attributes(&attributes); |
| 7492 | psa_destroy_key(key); |
| 7493 | PSA_DONE(); |
| 7494 | } |
| 7495 | /* END_CASE */ |
| 7496 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7497 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7498 | /** |
| 7499 | * interruptible_signverify_hash_state_test() test intentions: |
| 7500 | * |
| 7501 | * Note: This test can currently only handle ECDSA. |
| 7502 | * |
| 7503 | * 1. Test that calling the various interruptible sign and verify hash functions |
| 7504 | * in incorrect orders returns BAD_STATE errors. |
| 7505 | */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7506 | void interruptible_signverify_hash_state_test(int key_type_arg, |
| 7507 | data_t *key_data, int alg_arg, data_t *input_data) |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7508 | { |
| 7509 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7510 | psa_key_type_t key_type = key_type_arg; |
| 7511 | psa_algorithm_t alg = alg_arg; |
| 7512 | size_t key_bits; |
| 7513 | unsigned char *signature = NULL; |
| 7514 | size_t signature_size; |
| 7515 | size_t signature_length = 0xdeadbeef; |
| 7516 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7517 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7518 | psa_sign_hash_interruptible_operation_init(); |
| 7519 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7520 | psa_verify_hash_interruptible_operation_init(); |
| 7521 | |
| 7522 | PSA_ASSERT(psa_crypto_init()); |
| 7523 | |
| 7524 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7525 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7526 | psa_set_key_algorithm(&attributes, alg); |
| 7527 | psa_set_key_type(&attributes, key_type); |
| 7528 | |
| 7529 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7530 | &key)); |
| 7531 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7532 | key_bits = psa_get_key_bits(&attributes); |
| 7533 | |
| 7534 | /* Allocate a buffer which has the size advertised by the |
| 7535 | * library. */ |
| 7536 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7537 | key_bits, alg); |
| 7538 | TEST_ASSERT(signature_size != 0); |
| 7539 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7540 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7541 | |
| 7542 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7543 | |
| 7544 | /* --- Attempt completes prior to starts --- */ |
| 7545 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7546 | signature_size, |
| 7547 | &signature_length), |
| 7548 | PSA_ERROR_BAD_STATE); |
| 7549 | |
| 7550 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7551 | |
| 7552 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7553 | PSA_ERROR_BAD_STATE); |
| 7554 | |
| 7555 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7556 | |
| 7557 | /* --- Aborts in all other places. --- */ |
| 7558 | psa_sign_hash_abort(&sign_operation); |
| 7559 | |
| 7560 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7561 | input_data->x, input_data->len)); |
| 7562 | |
| 7563 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7564 | |
| 7565 | psa_interruptible_set_max_ops(1); |
| 7566 | |
| 7567 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7568 | input_data->x, input_data->len)); |
| 7569 | |
| 7570 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7571 | signature_size, |
| 7572 | &signature_length), |
| 7573 | PSA_OPERATION_INCOMPLETE); |
| 7574 | |
| 7575 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7576 | |
| 7577 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7578 | |
| 7579 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7580 | input_data->x, input_data->len)); |
| 7581 | |
| 7582 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7583 | signature_size, |
| 7584 | &signature_length)); |
| 7585 | |
| 7586 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7587 | |
| 7588 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7589 | |
| 7590 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7591 | input_data->x, input_data->len, |
| 7592 | signature, signature_length)); |
| 7593 | |
| 7594 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7595 | |
| 7596 | psa_interruptible_set_max_ops(1); |
| 7597 | |
| 7598 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7599 | input_data->x, input_data->len, |
| 7600 | signature, signature_length)); |
| 7601 | |
| 7602 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7603 | PSA_OPERATION_INCOMPLETE); |
| 7604 | |
| 7605 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7606 | |
| 7607 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7608 | |
| 7609 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7610 | input_data->x, input_data->len, |
| 7611 | signature, signature_length)); |
| 7612 | |
| 7613 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7614 | |
| 7615 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7616 | |
| 7617 | /* --- Attempt double starts. --- */ |
| 7618 | |
| 7619 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7620 | input_data->x, input_data->len)); |
| 7621 | |
| 7622 | TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg, |
| 7623 | input_data->x, input_data->len), |
| 7624 | PSA_ERROR_BAD_STATE); |
| 7625 | |
| 7626 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7627 | |
| 7628 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7629 | input_data->x, input_data->len, |
| 7630 | signature, signature_length)); |
| 7631 | |
| 7632 | TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg, |
| 7633 | input_data->x, input_data->len, |
| 7634 | signature, signature_length), |
| 7635 | PSA_ERROR_BAD_STATE); |
| 7636 | |
| 7637 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7638 | |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7639 | exit: |
| 7640 | /* |
| 7641 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7642 | * thus reset them as required. |
| 7643 | */ |
| 7644 | psa_reset_key_attributes(&attributes); |
| 7645 | |
| 7646 | psa_destroy_key(key); |
| 7647 | mbedtls_free(signature); |
| 7648 | PSA_DONE(); |
| 7649 | } |
| 7650 | /* END_CASE */ |
| 7651 | |
| 7652 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7653 | /** |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7654 | * interruptible_signverify_hash_edgecase_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7655 | * |
| 7656 | * Note: This test can currently only handle ECDSA. |
| 7657 | * |
| 7658 | * 1. Test various edge cases in the interruptible sign and verify hash |
| 7659 | * interfaces. |
| 7660 | */ |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7661 | void interruptible_signverify_hash_edgecase_tests(int key_type_arg, |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7662 | data_t *key_data, int alg_arg, data_t *input_data) |
| 7663 | { |
| 7664 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7665 | psa_key_type_t key_type = key_type_arg; |
| 7666 | psa_algorithm_t alg = alg_arg; |
| 7667 | size_t key_bits; |
| 7668 | unsigned char *signature = NULL; |
| 7669 | size_t signature_size; |
| 7670 | size_t signature_length = 0xdeadbeef; |
| 7671 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7672 | uint8_t *input_buffer = NULL; |
| 7673 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7674 | psa_sign_hash_interruptible_operation_init(); |
| 7675 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7676 | psa_verify_hash_interruptible_operation_init(); |
| 7677 | |
| 7678 | PSA_ASSERT(psa_crypto_init()); |
| 7679 | |
| 7680 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7681 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7682 | psa_set_key_algorithm(&attributes, alg); |
| 7683 | psa_set_key_type(&attributes, key_type); |
| 7684 | |
| 7685 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7686 | &key)); |
| 7687 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7688 | key_bits = psa_get_key_bits(&attributes); |
| 7689 | |
| 7690 | /* Allocate a buffer which has the size advertised by the |
| 7691 | * library. */ |
| 7692 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7693 | key_bits, alg); |
| 7694 | TEST_ASSERT(signature_size != 0); |
| 7695 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7696 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7697 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7698 | /* --- Change function inputs mid run, to cause an error (sign only, |
| 7699 | * verify passes all inputs to start. --- */ |
| 7700 | |
| 7701 | psa_interruptible_set_max_ops(1); |
| 7702 | |
| 7703 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7704 | input_data->x, input_data->len)); |
| 7705 | |
| 7706 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7707 | signature_size, |
| 7708 | &signature_length), |
| 7709 | PSA_OPERATION_INCOMPLETE); |
| 7710 | |
| 7711 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7712 | 0, |
| 7713 | &signature_length), |
| 7714 | PSA_ERROR_BUFFER_TOO_SMALL); |
| 7715 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7716 | /* And test that this invalidates the operation. */ |
| 7717 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7718 | 0, |
| 7719 | &signature_length), |
| 7720 | PSA_ERROR_BAD_STATE); |
| 7721 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7722 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7723 | |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7724 | /* Trash the hash buffer in between start and complete, to ensure |
| 7725 | * no reliance on external buffers. */ |
| 7726 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7727 | |
Paul Elliott | 6c68df4 | 2023-10-23 15:33:37 +0100 | [diff] [blame] | 7728 | TEST_CALLOC(input_buffer, input_data->len); |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7729 | |
| 7730 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7731 | |
| 7732 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7733 | input_buffer, input_data->len)); |
| 7734 | |
| 7735 | memset(input_buffer, '!', input_data->len); |
| 7736 | mbedtls_free(input_buffer); |
| 7737 | input_buffer = NULL; |
| 7738 | |
| 7739 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7740 | signature_size, |
| 7741 | &signature_length)); |
| 7742 | |
| 7743 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7744 | |
Paul Elliott | 6c68df4 | 2023-10-23 15:33:37 +0100 | [diff] [blame] | 7745 | TEST_CALLOC(input_buffer, input_data->len); |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7746 | |
| 7747 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7748 | |
| 7749 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7750 | input_buffer, input_data->len, |
| 7751 | signature, signature_length)); |
| 7752 | |
| 7753 | memset(input_buffer, '!', input_data->len); |
| 7754 | mbedtls_free(input_buffer); |
| 7755 | input_buffer = NULL; |
| 7756 | |
| 7757 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7758 | |
| 7759 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7760 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7761 | exit: |
| 7762 | /* |
| 7763 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7764 | * thus reset them as required. |
| 7765 | */ |
| 7766 | psa_reset_key_attributes(&attributes); |
| 7767 | |
| 7768 | psa_destroy_key(key); |
| 7769 | mbedtls_free(signature); |
Paul Elliott | 6c68df4 | 2023-10-23 15:33:37 +0100 | [diff] [blame] | 7770 | mbedtls_free(input_buffer); |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7771 | PSA_DONE(); |
| 7772 | } |
| 7773 | /* END_CASE */ |
| 7774 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7775 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7776 | /** |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 7777 | * interruptible_signverify_hash_ops_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7778 | * |
| 7779 | * Note: This test can currently only handle ECDSA. |
| 7780 | * |
| 7781 | * 1. Test that setting max ops is reflected in both interruptible sign and |
| 7782 | * verify hash |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7783 | * 2. Test that changing the value of max_ops to unlimited during an operation |
| 7784 | * causes that operation to complete in the next call. |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7785 | * |
| 7786 | * 3. Test that calling get_num_ops() between complete calls gives the same |
| 7787 | * 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] | 7788 | */ |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 7789 | void interruptible_signverify_hash_ops_tests(int key_type_arg, |
| 7790 | data_t *key_data, int alg_arg, |
| 7791 | data_t *input_data) |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7792 | { |
| 7793 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7794 | psa_key_type_t key_type = key_type_arg; |
| 7795 | psa_algorithm_t alg = alg_arg; |
| 7796 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7797 | size_t key_bits; |
| 7798 | unsigned char *signature = NULL; |
| 7799 | size_t signature_size; |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7800 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7801 | uint32_t num_ops = 0; |
| 7802 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 7803 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7804 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7805 | psa_sign_hash_interruptible_operation_init(); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7806 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7807 | psa_verify_hash_interruptible_operation_init(); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7808 | |
| 7809 | PSA_ASSERT(psa_crypto_init()); |
| 7810 | |
| 7811 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7812 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7813 | psa_set_key_algorithm(&attributes, alg); |
| 7814 | psa_set_key_type(&attributes, key_type); |
| 7815 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7816 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key)); |
| 7817 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7818 | key_bits = psa_get_key_bits(&attributes); |
| 7819 | |
| 7820 | /* Allocate a buffer which has the size advertised by the |
| 7821 | * library. */ |
| 7822 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7823 | |
| 7824 | TEST_ASSERT(signature_size != 0); |
| 7825 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7826 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7827 | |
| 7828 | /* Check that default max ops gets set if we don't set it. */ |
| 7829 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7830 | input_data->x, input_data->len)); |
| 7831 | |
| 7832 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 7833 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7834 | |
| 7835 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7836 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7837 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7838 | input_data->x, input_data->len, |
| 7839 | signature, signature_size)); |
| 7840 | |
| 7841 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 7842 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7843 | |
| 7844 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7845 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7846 | /* Check that max ops gets set properly. */ |
| 7847 | |
| 7848 | psa_interruptible_set_max_ops(0xbeef); |
| 7849 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7850 | TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7851 | |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7852 | /* --- Ensure changing the max ops mid operation works (operation should |
| 7853 | * complete successfully after setting max ops to unlimited --- */ |
| 7854 | psa_interruptible_set_max_ops(1); |
| 7855 | |
| 7856 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7857 | input_data->x, input_data->len)); |
| 7858 | |
| 7859 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7860 | signature_size, |
| 7861 | &signature_length), |
| 7862 | PSA_OPERATION_INCOMPLETE); |
| 7863 | |
| 7864 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7865 | |
| 7866 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7867 | signature_size, |
| 7868 | &signature_length)); |
| 7869 | |
| 7870 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7871 | |
| 7872 | psa_interruptible_set_max_ops(1); |
| 7873 | |
| 7874 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7875 | input_data->x, input_data->len, |
| 7876 | signature, signature_length)); |
| 7877 | |
| 7878 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7879 | PSA_OPERATION_INCOMPLETE); |
| 7880 | |
| 7881 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7882 | |
| 7883 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7884 | |
| 7885 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7886 | |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7887 | /* --- Test that not calling get_num_ops inbetween complete calls does not |
| 7888 | * result in lost ops. ---*/ |
| 7889 | |
| 7890 | psa_interruptible_set_max_ops(1); |
| 7891 | |
| 7892 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7893 | input_data->x, input_data->len)); |
| 7894 | |
| 7895 | /* Continue performing the signature until complete. */ |
| 7896 | do { |
| 7897 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7898 | signature_size, |
| 7899 | &signature_length); |
| 7900 | |
| 7901 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7902 | |
| 7903 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7904 | |
| 7905 | PSA_ASSERT(status); |
| 7906 | |
| 7907 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7908 | |
| 7909 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7910 | input_data->x, input_data->len)); |
| 7911 | |
| 7912 | /* Continue performing the signature until complete. */ |
| 7913 | do { |
| 7914 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7915 | signature_size, |
| 7916 | &signature_length); |
| 7917 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7918 | |
| 7919 | PSA_ASSERT(status); |
| 7920 | |
| 7921 | TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation)); |
| 7922 | |
| 7923 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7924 | |
| 7925 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7926 | input_data->x, input_data->len, |
| 7927 | signature, signature_length)); |
| 7928 | |
| 7929 | /* Continue performing the verification until complete. */ |
| 7930 | do { |
| 7931 | status = psa_verify_hash_complete(&verify_operation); |
| 7932 | |
| 7933 | num_ops = psa_verify_hash_get_num_ops(&verify_operation); |
| 7934 | |
| 7935 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7936 | |
| 7937 | PSA_ASSERT(status); |
| 7938 | |
| 7939 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7940 | |
| 7941 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7942 | input_data->x, input_data->len, |
| 7943 | signature, signature_length)); |
| 7944 | |
| 7945 | /* Continue performing the verification until complete. */ |
| 7946 | do { |
| 7947 | status = psa_verify_hash_complete(&verify_operation); |
| 7948 | |
| 7949 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7950 | |
| 7951 | PSA_ASSERT(status); |
| 7952 | |
| 7953 | TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation)); |
| 7954 | |
| 7955 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7956 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7957 | exit: |
| 7958 | /* |
| 7959 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7960 | * thus reset them as required. |
| 7961 | */ |
| 7962 | psa_reset_key_attributes(&attributes); |
| 7963 | |
| 7964 | psa_destroy_key(key); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7965 | mbedtls_free(signature); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7966 | PSA_DONE(); |
| 7967 | } |
| 7968 | /* END_CASE */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7969 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7970 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7971 | void sign_message_deterministic(int key_type_arg, |
| 7972 | data_t *key_data, |
| 7973 | int alg_arg, |
| 7974 | data_t *input_data, |
| 7975 | data_t *output_data) |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7976 | { |
| 7977 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7978 | psa_key_type_t key_type = key_type_arg; |
| 7979 | psa_algorithm_t alg = alg_arg; |
| 7980 | size_t key_bits; |
| 7981 | unsigned char *signature = NULL; |
| 7982 | size_t signature_size; |
| 7983 | size_t signature_length = 0xdeadbeef; |
| 7984 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7985 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7986 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7987 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7988 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 7989 | psa_set_key_algorithm(&attributes, alg); |
| 7990 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7991 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7992 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7993 | &key)); |
| 7994 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7995 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7996 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7997 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7998 | TEST_ASSERT(signature_size != 0); |
| 7999 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8000 | TEST_CALLOC(signature, signature_size); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8001 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8002 | PSA_ASSERT(psa_sign_message(key, alg, |
| 8003 | input_data->x, input_data->len, |
| 8004 | signature, signature_size, |
| 8005 | &signature_length)); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8006 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8007 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8008 | signature, signature_length); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8009 | |
| 8010 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8011 | psa_reset_key_attributes(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8012 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8013 | psa_destroy_key(key); |
| 8014 | mbedtls_free(signature); |
| 8015 | PSA_DONE(); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8016 | |
| 8017 | } |
| 8018 | /* END_CASE */ |
| 8019 | |
| 8020 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8021 | void sign_message_fail(int key_type_arg, |
| 8022 | data_t *key_data, |
| 8023 | int alg_arg, |
| 8024 | data_t *input_data, |
| 8025 | int signature_size_arg, |
| 8026 | int expected_status_arg) |
| 8027 | { |
| 8028 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8029 | psa_key_type_t key_type = key_type_arg; |
| 8030 | psa_algorithm_t alg = alg_arg; |
| 8031 | size_t signature_size = signature_size_arg; |
| 8032 | psa_status_t actual_status; |
| 8033 | psa_status_t expected_status = expected_status_arg; |
| 8034 | unsigned char *signature = NULL; |
| 8035 | size_t signature_length = 0xdeadbeef; |
| 8036 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8037 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8038 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8039 | |
| 8040 | PSA_ASSERT(psa_crypto_init()); |
| 8041 | |
| 8042 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 8043 | psa_set_key_algorithm(&attributes, alg); |
| 8044 | psa_set_key_type(&attributes, key_type); |
| 8045 | |
| 8046 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8047 | &key)); |
| 8048 | |
| 8049 | actual_status = psa_sign_message(key, alg, |
| 8050 | input_data->x, input_data->len, |
| 8051 | signature, signature_size, |
| 8052 | &signature_length); |
| 8053 | TEST_EQUAL(actual_status, expected_status); |
| 8054 | /* The value of *signature_length is unspecified on error, but |
| 8055 | * whatever it is, it should be less than signature_size, so that |
| 8056 | * if the caller tries to read *signature_length bytes without |
| 8057 | * checking the error code then they don't overflow a buffer. */ |
| 8058 | TEST_LE_U(signature_length, signature_size); |
| 8059 | |
| 8060 | exit: |
| 8061 | psa_reset_key_attributes(&attributes); |
| 8062 | psa_destroy_key(key); |
| 8063 | mbedtls_free(signature); |
| 8064 | PSA_DONE(); |
| 8065 | } |
| 8066 | /* END_CASE */ |
| 8067 | |
| 8068 | /* BEGIN_CASE */ |
| 8069 | void sign_verify_message(int key_type_arg, |
| 8070 | data_t *key_data, |
| 8071 | int alg_arg, |
| 8072 | data_t *input_data) |
| 8073 | { |
| 8074 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8075 | psa_key_type_t key_type = key_type_arg; |
| 8076 | psa_algorithm_t alg = alg_arg; |
| 8077 | size_t key_bits; |
| 8078 | unsigned char *signature = NULL; |
| 8079 | size_t signature_size; |
| 8080 | size_t signature_length = 0xdeadbeef; |
| 8081 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8082 | |
| 8083 | PSA_ASSERT(psa_crypto_init()); |
| 8084 | |
| 8085 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 8086 | PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8087 | psa_set_key_algorithm(&attributes, alg); |
| 8088 | psa_set_key_type(&attributes, key_type); |
| 8089 | |
| 8090 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8091 | &key)); |
| 8092 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8093 | key_bits = psa_get_key_bits(&attributes); |
| 8094 | |
| 8095 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8096 | TEST_ASSERT(signature_size != 0); |
| 8097 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8098 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8099 | |
| 8100 | PSA_ASSERT(psa_sign_message(key, alg, |
| 8101 | input_data->x, input_data->len, |
| 8102 | signature, signature_size, |
| 8103 | &signature_length)); |
| 8104 | TEST_LE_U(signature_length, signature_size); |
| 8105 | TEST_ASSERT(signature_length > 0); |
| 8106 | |
| 8107 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8108 | input_data->x, input_data->len, |
| 8109 | signature, signature_length)); |
| 8110 | |
| 8111 | if (input_data->len != 0) { |
| 8112 | /* Flip a bit in the input and verify that the signature is now |
| 8113 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 8114 | * because ECDSA may ignore the last few bits of the input. */ |
| 8115 | input_data->x[0] ^= 1; |
| 8116 | TEST_EQUAL(psa_verify_message(key, alg, |
| 8117 | input_data->x, input_data->len, |
| 8118 | signature, signature_length), |
| 8119 | PSA_ERROR_INVALID_SIGNATURE); |
| 8120 | } |
| 8121 | |
| 8122 | exit: |
| 8123 | psa_reset_key_attributes(&attributes); |
| 8124 | |
| 8125 | psa_destroy_key(key); |
| 8126 | mbedtls_free(signature); |
| 8127 | PSA_DONE(); |
| 8128 | } |
| 8129 | /* END_CASE */ |
| 8130 | |
| 8131 | /* BEGIN_CASE */ |
| 8132 | void verify_message(int key_type_arg, |
| 8133 | data_t *key_data, |
| 8134 | int alg_arg, |
| 8135 | data_t *input_data, |
| 8136 | data_t *signature_data) |
| 8137 | { |
| 8138 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8139 | psa_key_type_t key_type = key_type_arg; |
| 8140 | psa_algorithm_t alg = alg_arg; |
| 8141 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8142 | |
| 8143 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 8144 | |
| 8145 | PSA_ASSERT(psa_crypto_init()); |
| 8146 | |
| 8147 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8148 | psa_set_key_algorithm(&attributes, alg); |
| 8149 | psa_set_key_type(&attributes, key_type); |
| 8150 | |
| 8151 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8152 | &key)); |
| 8153 | |
| 8154 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8155 | input_data->x, input_data->len, |
| 8156 | signature_data->x, signature_data->len)); |
| 8157 | |
| 8158 | exit: |
| 8159 | psa_reset_key_attributes(&attributes); |
| 8160 | psa_destroy_key(key); |
| 8161 | PSA_DONE(); |
| 8162 | } |
| 8163 | /* END_CASE */ |
| 8164 | |
| 8165 | /* BEGIN_CASE */ |
| 8166 | void verify_message_fail(int key_type_arg, |
| 8167 | data_t *key_data, |
| 8168 | int alg_arg, |
| 8169 | data_t *hash_data, |
| 8170 | data_t *signature_data, |
| 8171 | int expected_status_arg) |
| 8172 | { |
| 8173 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8174 | psa_key_type_t key_type = key_type_arg; |
| 8175 | psa_algorithm_t alg = alg_arg; |
| 8176 | psa_status_t actual_status; |
| 8177 | psa_status_t expected_status = expected_status_arg; |
| 8178 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8179 | |
| 8180 | PSA_ASSERT(psa_crypto_init()); |
| 8181 | |
| 8182 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8183 | psa_set_key_algorithm(&attributes, alg); |
| 8184 | psa_set_key_type(&attributes, key_type); |
| 8185 | |
| 8186 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8187 | &key)); |
| 8188 | |
| 8189 | actual_status = psa_verify_message(key, alg, |
| 8190 | hash_data->x, hash_data->len, |
| 8191 | signature_data->x, |
| 8192 | signature_data->len); |
| 8193 | TEST_EQUAL(actual_status, expected_status); |
| 8194 | |
| 8195 | exit: |
| 8196 | psa_reset_key_attributes(&attributes); |
| 8197 | psa_destroy_key(key); |
| 8198 | PSA_DONE(); |
| 8199 | } |
| 8200 | /* END_CASE */ |
| 8201 | |
| 8202 | /* BEGIN_CASE */ |
| 8203 | void asymmetric_encrypt(int key_type_arg, |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8204 | data_t *key_data, |
| 8205 | int alg_arg, |
| 8206 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8207 | data_t *label, |
| 8208 | int expected_output_length_arg, |
| 8209 | int expected_status_arg) |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8210 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8211 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8212 | psa_key_type_t key_type = key_type_arg; |
| 8213 | psa_algorithm_t alg = alg_arg; |
| 8214 | size_t expected_output_length = expected_output_length_arg; |
| 8215 | size_t key_bits; |
| 8216 | unsigned char *output = NULL; |
| 8217 | size_t output_size; |
| 8218 | size_t output_length = ~0; |
| 8219 | psa_status_t actual_status; |
| 8220 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8221 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8222 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8223 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 8224 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8225 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8226 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 8227 | psa_set_key_algorithm(&attributes, alg); |
| 8228 | psa_set_key_type(&attributes, key_type); |
| 8229 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8230 | &key)); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8231 | |
| 8232 | /* Determine the maximum output length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8233 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8234 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8235 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8236 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8237 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8238 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8239 | |
| 8240 | /* Encrypt the input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8241 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8242 | input_data->x, input_data->len, |
| 8243 | label->x, label->len, |
| 8244 | output, output_size, |
| 8245 | &output_length); |
| 8246 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8247 | if (actual_status == PSA_SUCCESS) { |
| 8248 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8249 | } else { |
| 8250 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8251 | } |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8252 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8253 | /* If the label is empty, the test framework puts a non-null pointer |
| 8254 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8255 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8256 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8257 | if (output_size != 0) { |
| 8258 | memset(output, 0, output_size); |
| 8259 | } |
| 8260 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8261 | input_data->x, input_data->len, |
| 8262 | NULL, label->len, |
| 8263 | output, output_size, |
| 8264 | &output_length); |
| 8265 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8266 | if (actual_status == PSA_SUCCESS) { |
| 8267 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8268 | } else { |
| 8269 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8270 | } |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8271 | } |
| 8272 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8273 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8274 | /* |
| 8275 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8276 | * thus reset them as required. |
| 8277 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8278 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8279 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8280 | psa_destroy_key(key); |
| 8281 | mbedtls_free(output); |
| 8282 | PSA_DONE(); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8283 | } |
| 8284 | /* END_CASE */ |
| 8285 | |
| 8286 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8287 | void asymmetric_encrypt_decrypt(int key_type_arg, |
| 8288 | data_t *key_data, |
| 8289 | int alg_arg, |
| 8290 | data_t *input_data, |
| 8291 | data_t *label) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8292 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8293 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8294 | psa_key_type_t key_type = key_type_arg; |
| 8295 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8296 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8297 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8298 | size_t output_size; |
| 8299 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8300 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8301 | size_t output2_size; |
| 8302 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8303 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8304 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8305 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8306 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8307 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 8308 | psa_set_key_algorithm(&attributes, alg); |
| 8309 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8310 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8311 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8312 | &key)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8313 | |
| 8314 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8315 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8316 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8317 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8318 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8319 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8320 | TEST_CALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8321 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8322 | output2_size = input_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8323 | TEST_LE_U(output2_size, |
| 8324 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)); |
| 8325 | TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8326 | TEST_CALLOC(output2, output2_size); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8327 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 8328 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 8329 | * the original plaintext because of the non-optional random |
| 8330 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8331 | PSA_ASSERT(psa_asymmetric_encrypt(key, alg, |
| 8332 | input_data->x, input_data->len, |
| 8333 | label->x, label->len, |
| 8334 | output, output_size, |
| 8335 | &output_length)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8336 | /* We don't know what ciphertext length to expect, but check that |
| 8337 | * it looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8338 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8339 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8340 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8341 | output, output_length, |
| 8342 | label->x, label->len, |
| 8343 | output2, output2_size, |
| 8344 | &output2_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8345 | TEST_MEMORY_COMPARE(input_data->x, input_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8346 | output2, output2_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8347 | |
| 8348 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8349 | /* |
| 8350 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8351 | * thus reset them as required. |
| 8352 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8353 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8354 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8355 | psa_destroy_key(key); |
| 8356 | mbedtls_free(output); |
| 8357 | mbedtls_free(output2); |
| 8358 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8359 | } |
| 8360 | /* END_CASE */ |
| 8361 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8362 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8363 | void asymmetric_decrypt(int key_type_arg, |
| 8364 | data_t *key_data, |
| 8365 | int alg_arg, |
| 8366 | data_t *input_data, |
| 8367 | data_t *label, |
| 8368 | data_t *expected_data) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8369 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8370 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8371 | psa_key_type_t key_type = key_type_arg; |
| 8372 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8373 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8374 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 8375 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8376 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8377 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8378 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8379 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8380 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8381 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8382 | psa_set_key_algorithm(&attributes, alg); |
| 8383 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8385 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8386 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8387 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8388 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8389 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8390 | |
| 8391 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8392 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8393 | TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8394 | TEST_CALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8395 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8396 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8397 | input_data->x, input_data->len, |
| 8398 | label->x, label->len, |
| 8399 | output, |
| 8400 | output_size, |
| 8401 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8402 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8403 | output, output_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8404 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8405 | /* If the label is empty, the test framework puts a non-null pointer |
| 8406 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8407 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8408 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8409 | if (output_size != 0) { |
| 8410 | memset(output, 0, output_size); |
| 8411 | } |
| 8412 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8413 | input_data->x, input_data->len, |
| 8414 | NULL, label->len, |
| 8415 | output, |
| 8416 | output_size, |
| 8417 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8418 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8419 | output, output_length); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8420 | } |
| 8421 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8422 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8423 | psa_reset_key_attributes(&attributes); |
| 8424 | psa_destroy_key(key); |
| 8425 | mbedtls_free(output); |
| 8426 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8427 | } |
| 8428 | /* END_CASE */ |
| 8429 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8430 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8431 | void asymmetric_decrypt_fail(int key_type_arg, |
| 8432 | data_t *key_data, |
| 8433 | int alg_arg, |
| 8434 | data_t *input_data, |
| 8435 | data_t *label, |
| 8436 | int output_size_arg, |
| 8437 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8438 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8439 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8440 | psa_key_type_t key_type = key_type_arg; |
| 8441 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8442 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 8443 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8444 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8445 | psa_status_t actual_status; |
| 8446 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8447 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8448 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8449 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8450 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8451 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8452 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8453 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8454 | psa_set_key_algorithm(&attributes, alg); |
| 8455 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8456 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8457 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8458 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8459 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8460 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8461 | input_data->x, input_data->len, |
| 8462 | label->x, label->len, |
| 8463 | output, output_size, |
| 8464 | &output_length); |
| 8465 | TEST_EQUAL(actual_status, expected_status); |
| 8466 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8467 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8468 | /* If the label is empty, the test framework puts a non-null pointer |
| 8469 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8470 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8471 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8472 | if (output_size != 0) { |
| 8473 | memset(output, 0, output_size); |
| 8474 | } |
| 8475 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8476 | input_data->x, input_data->len, |
| 8477 | NULL, label->len, |
| 8478 | output, output_size, |
| 8479 | &output_length); |
| 8480 | TEST_EQUAL(actual_status, expected_status); |
| 8481 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8482 | } |
| 8483 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8484 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8485 | psa_reset_key_attributes(&attributes); |
| 8486 | psa_destroy_key(key); |
| 8487 | mbedtls_free(output); |
| 8488 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8489 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8490 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8491 | |
| 8492 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8493 | void key_derivation_init() |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8494 | { |
| 8495 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 8496 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 8497 | * 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] | 8498 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8499 | size_t capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8500 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init(); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 8501 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8502 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8503 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8504 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8505 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8506 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8507 | TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity), |
| 8508 | PSA_ERROR_BAD_STATE); |
| 8509 | TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity), |
| 8510 | PSA_ERROR_BAD_STATE); |
| 8511 | TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity), |
| 8512 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8513 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8514 | /* A default operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8515 | PSA_ASSERT(psa_key_derivation_abort(&func)); |
| 8516 | PSA_ASSERT(psa_key_derivation_abort(&init)); |
| 8517 | PSA_ASSERT(psa_key_derivation_abort(&zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8518 | } |
| 8519 | /* END_CASE */ |
| 8520 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 8521 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8522 | void derive_setup(int alg_arg, int expected_status_arg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8523 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8524 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8525 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8526 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8527 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8528 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8529 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8530 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8531 | expected_status); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8532 | |
| 8533 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8534 | psa_key_derivation_abort(&operation); |
| 8535 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8536 | } |
| 8537 | /* END_CASE */ |
| 8538 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8539 | /* BEGIN_CASE */ |
Kusumit Ghoderao | 9ffd397 | 2023-12-01 16:40:13 +0530 | [diff] [blame] | 8540 | void derive_set_capacity(int alg_arg, int64_t capacity_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8541 | int expected_status_arg) |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8542 | { |
| 8543 | psa_algorithm_t alg = alg_arg; |
| 8544 | size_t capacity = capacity_arg; |
| 8545 | psa_status_t expected_status = expected_status_arg; |
| 8546 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8547 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8548 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8549 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8550 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8551 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8552 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 8553 | expected_status); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8554 | |
| 8555 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8556 | psa_key_derivation_abort(&operation); |
| 8557 | PSA_DONE(); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8558 | } |
| 8559 | /* END_CASE */ |
| 8560 | |
| 8561 | /* BEGIN_CASE */ |
Kusumit Ghoderao | d60dfc0 | 2023-05-01 17:39:27 +0530 | [diff] [blame] | 8562 | void parse_binary_string_test(data_t *input, int output) |
| 8563 | { |
| 8564 | uint64_t value; |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 8565 | value = mbedtls_test_parse_binary_string(input); |
Kusumit Ghoderao | d60dfc0 | 2023-05-01 17:39:27 +0530 | [diff] [blame] | 8566 | TEST_EQUAL(value, output); |
| 8567 | } |
| 8568 | /* END_CASE */ |
| 8569 | |
| 8570 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8571 | void derive_input(int alg_arg, |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8572 | int step_arg1, int key_type_arg1, data_t *input1, |
| 8573 | int expected_status_arg1, |
| 8574 | int step_arg2, int key_type_arg2, data_t *input2, |
| 8575 | int expected_status_arg2, |
| 8576 | int step_arg3, int key_type_arg3, data_t *input3, |
| 8577 | int expected_status_arg3, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8578 | int output_key_type_arg, int expected_output_status_arg) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8579 | { |
| 8580 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8581 | psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 }; |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8582 | 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] | 8583 | psa_status_t expected_statuses[] = { expected_status_arg1, |
| 8584 | expected_status_arg2, |
| 8585 | expected_status_arg3 }; |
| 8586 | data_t *inputs[] = { input1, input2, input3 }; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8587 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8588 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8589 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8590 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8591 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8592 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8593 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8594 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8595 | psa_status_t expected_output_status = expected_output_status_arg; |
| 8596 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8597 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8598 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8599 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8600 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8601 | psa_set_key_algorithm(&attributes, alg); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8602 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8603 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8604 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8605 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8606 | mbedtls_test_set_step(i); |
| 8607 | if (steps[i] == 0) { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 8608 | /* Skip this step */ |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8609 | } else if (((psa_key_type_t) key_types[i]) != PSA_KEY_TYPE_NONE && |
| 8610 | key_types[i] != INPUT_INTEGER) { |
| 8611 | psa_set_key_type(&attributes, ((psa_key_type_t) key_types[i])); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8612 | PSA_ASSERT(psa_import_key(&attributes, |
| 8613 | inputs[i]->x, inputs[i]->len, |
| 8614 | &keys[i])); |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8615 | 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] | 8616 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8617 | // When taking a private key as secret input, use key agreement |
| 8618 | // to add the shared secret to the derivation |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8619 | TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self( |
| 8620 | &operation, keys[i]), |
| 8621 | expected_statuses[i]); |
| 8622 | } else { |
| 8623 | TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i], |
| 8624 | keys[i]), |
| 8625 | expected_statuses[i]); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8626 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8627 | } else { |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8628 | if (key_types[i] == INPUT_INTEGER) { |
| 8629 | TEST_EQUAL(psa_key_derivation_input_integer( |
| 8630 | &operation, steps[i], |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 8631 | mbedtls_test_parse_binary_string(inputs[i])), |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8632 | expected_statuses[i]); |
| 8633 | } else { |
Kusumit Ghoderao | 02326d5 | 2023-04-06 17:47:59 +0530 | [diff] [blame] | 8634 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8635 | &operation, steps[i], |
| 8636 | inputs[i]->x, inputs[i]->len), |
| 8637 | expected_statuses[i]); |
Kusumit Ghoderao | 02326d5 | 2023-04-06 17:47:59 +0530 | [diff] [blame] | 8638 | } |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8639 | } |
| 8640 | } |
| 8641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8642 | if (output_key_type != PSA_KEY_TYPE_NONE) { |
| 8643 | psa_reset_key_attributes(&attributes); |
| 8644 | psa_set_key_type(&attributes, output_key_type); |
| 8645 | psa_set_key_bits(&attributes, 8); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8646 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8647 | psa_key_derivation_output_key(&attributes, &operation, |
| 8648 | &output_key); |
| 8649 | } else { |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8650 | uint8_t buffer[1]; |
| 8651 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8652 | psa_key_derivation_output_bytes(&operation, |
| 8653 | buffer, sizeof(buffer)); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8654 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8655 | TEST_EQUAL(actual_output_status, expected_output_status); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8656 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8657 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8658 | psa_key_derivation_abort(&operation); |
| 8659 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8660 | psa_destroy_key(keys[i]); |
| 8661 | } |
| 8662 | psa_destroy_key(output_key); |
| 8663 | PSA_DONE(); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8664 | } |
| 8665 | /* END_CASE */ |
| 8666 | |
Kusumit Ghoderao | 42b02b9 | 2023-06-06 16:48:46 +0530 | [diff] [blame] | 8667 | /* BEGIN_CASE*/ |
| 8668 | void derive_input_invalid_cost(int alg_arg, int64_t cost) |
| 8669 | { |
| 8670 | psa_algorithm_t alg = alg_arg; |
| 8671 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8672 | |
| 8673 | PSA_ASSERT(psa_crypto_init()); |
| 8674 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8675 | |
| 8676 | TEST_EQUAL(psa_key_derivation_input_integer(&operation, |
| 8677 | PSA_KEY_DERIVATION_INPUT_COST, |
| 8678 | cost), |
| 8679 | PSA_ERROR_NOT_SUPPORTED); |
| 8680 | |
| 8681 | exit: |
| 8682 | psa_key_derivation_abort(&operation); |
| 8683 | PSA_DONE(); |
| 8684 | } |
| 8685 | /* END_CASE*/ |
| 8686 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8687 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8688 | void derive_over_capacity(int alg_arg) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8689 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8690 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8691 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 8692 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8693 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8694 | unsigned char input1[] = "Input 1"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8695 | size_t input1_length = sizeof(input1); |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8696 | unsigned char input2[] = "Input 2"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8697 | size_t input2_length = sizeof(input2); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8698 | uint8_t buffer[42]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8699 | size_t capacity = sizeof(buffer); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 8700 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 8701 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8702 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8703 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8704 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8705 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8706 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8707 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8708 | psa_set_key_algorithm(&attributes, alg); |
| 8709 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8710 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8711 | PSA_ASSERT(psa_import_key(&attributes, |
| 8712 | key_data, sizeof(key_data), |
| 8713 | &key)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8714 | |
| 8715 | /* valid key derivation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8716 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8717 | input1, input1_length, |
| 8718 | input2, input2_length, |
| 8719 | capacity)) { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8720 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8721 | } |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8722 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8723 | /* state of operation shouldn't allow additional generation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8724 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8725 | PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8726 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8727 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8728 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8729 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity), |
| 8730 | PSA_ERROR_INSUFFICIENT_DATA); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8731 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8732 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8733 | psa_key_derivation_abort(&operation); |
| 8734 | psa_destroy_key(key); |
| 8735 | PSA_DONE(); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8736 | } |
| 8737 | /* END_CASE */ |
| 8738 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8739 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8740 | void derive_actions_without_setup() |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8741 | { |
| 8742 | uint8_t output_buffer[16]; |
| 8743 | size_t buffer_size = 16; |
| 8744 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8745 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8746 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8747 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8748 | output_buffer, buffer_size) |
| 8749 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8750 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8751 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8752 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8753 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8754 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8755 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8756 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8757 | output_buffer, buffer_size) |
| 8758 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8759 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8760 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8761 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8762 | |
| 8763 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8764 | psa_key_derivation_abort(&operation); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8765 | } |
| 8766 | /* END_CASE */ |
| 8767 | |
| 8768 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8769 | void derive_output(int alg_arg, |
| 8770 | int step1_arg, data_t *input1, int expected_status_arg1, |
| 8771 | int step2_arg, data_t *input2, int expected_status_arg2, |
| 8772 | int step3_arg, data_t *input3, int expected_status_arg3, |
| 8773 | int step4_arg, data_t *input4, int expected_status_arg4, |
| 8774 | data_t *key_agreement_peer_key, |
| 8775 | int requested_capacity_arg, |
| 8776 | data_t *expected_output1, |
| 8777 | data_t *expected_output2, |
| 8778 | int other_key_input_type, |
| 8779 | int key_input_type, |
| 8780 | int derive_type) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8781 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8782 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8783 | psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg }; |
| 8784 | data_t *inputs[] = { input1, input2, input3, input4 }; |
| 8785 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8786 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8787 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8788 | MBEDTLS_SVC_KEY_ID_INIT }; |
| 8789 | psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2, |
| 8790 | expected_status_arg3, expected_status_arg4 }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8791 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8792 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8793 | uint8_t *expected_outputs[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8794 | { expected_output1->x, expected_output2->x }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8795 | size_t output_sizes[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8796 | { expected_output1->len, expected_output2->len }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8797 | size_t output_buffer_size = 0; |
| 8798 | uint8_t *output_buffer = NULL; |
| 8799 | size_t expected_capacity; |
| 8800 | size_t current_capacity; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8801 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; |
| 8802 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; |
| 8803 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; |
| 8804 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8805 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8806 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8807 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8808 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8809 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
| 8810 | if (output_sizes[i] > output_buffer_size) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8811 | output_buffer_size = output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8812 | } |
| 8813 | if (output_sizes[i] == 0) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8814 | expected_outputs[i] = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8815 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8816 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8817 | TEST_CALLOC(output_buffer, output_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8818 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8819 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8820 | /* Extraction phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8821 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8822 | PSA_ASSERT(psa_key_derivation_set_capacity(&operation, |
| 8823 | requested_capacity)); |
| 8824 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8825 | switch (steps[i]) { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8826 | case 0: |
| 8827 | break; |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 8828 | case PSA_KEY_DERIVATION_INPUT_COST: |
| 8829 | TEST_EQUAL(psa_key_derivation_input_integer( |
Kusumit Ghoderao | f28e0f5 | 2023-06-06 15:03:22 +0530 | [diff] [blame] | 8830 | &operation, steps[i], |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 8831 | mbedtls_test_parse_binary_string(inputs[i])), |
Kusumit Ghoderao | f28e0f5 | 2023-06-06 15:03:22 +0530 | [diff] [blame] | 8832 | statuses[i]); |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 8833 | if (statuses[i] != PSA_SUCCESS) { |
| 8834 | goto exit; |
| 8835 | } |
| 8836 | break; |
| 8837 | case PSA_KEY_DERIVATION_INPUT_PASSWORD: |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8838 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8839 | switch (key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8840 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8841 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8842 | &operation, steps[i], |
| 8843 | inputs[i]->x, inputs[i]->len), |
| 8844 | statuses[i]); |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8845 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8846 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8847 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8848 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8849 | break; |
| 8850 | case 1: // input key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8851 | psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE); |
| 8852 | psa_set_key_algorithm(&attributes1, alg); |
| 8853 | psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8854 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8855 | PSA_ASSERT(psa_import_key(&attributes1, |
| 8856 | inputs[i]->x, inputs[i]->len, |
| 8857 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8858 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8859 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 8860 | PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1)); |
| 8861 | TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)), |
| 8862 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8863 | } |
| 8864 | |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 8865 | TEST_EQUAL(psa_key_derivation_input_key(&operation, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8866 | steps[i], |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 8867 | keys[i]), |
| 8868 | statuses[i]); |
| 8869 | |
| 8870 | if (statuses[i] != PSA_SUCCESS) { |
| 8871 | goto exit; |
| 8872 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8873 | break; |
| 8874 | default: |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 8875 | TEST_FAIL("default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8876 | break; |
| 8877 | } |
| 8878 | break; |
| 8879 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8880 | switch (other_key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8881 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8882 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 8883 | steps[i], |
| 8884 | inputs[i]->x, |
| 8885 | inputs[i]->len), |
| 8886 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8887 | break; |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 8888 | case 1: // input key, type DERIVE |
| 8889 | case 11: // input key, type RAW |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8890 | psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE); |
| 8891 | psa_set_key_algorithm(&attributes2, alg); |
| 8892 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8893 | |
| 8894 | // other secret of type RAW_DATA passed with input_key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8895 | if (other_key_input_type == 11) { |
| 8896 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA); |
| 8897 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8898 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8899 | PSA_ASSERT(psa_import_key(&attributes2, |
| 8900 | inputs[i]->x, inputs[i]->len, |
| 8901 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8902 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8903 | TEST_EQUAL(psa_key_derivation_input_key(&operation, |
| 8904 | steps[i], |
| 8905 | keys[i]), |
| 8906 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8907 | break; |
| 8908 | case 2: // key agreement |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8909 | psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE); |
| 8910 | psa_set_key_algorithm(&attributes3, alg); |
| 8911 | psa_set_key_type(&attributes3, |
| 8912 | PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8913 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8914 | PSA_ASSERT(psa_import_key(&attributes3, |
| 8915 | inputs[i]->x, inputs[i]->len, |
| 8916 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8917 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8918 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 8919 | &operation, |
| 8920 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 8921 | keys[i], key_agreement_peer_key->x, |
| 8922 | key_agreement_peer_key->len), statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8923 | break; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8924 | default: |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 8925 | TEST_FAIL("default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8926 | break; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8927 | } |
| 8928 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8929 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8930 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8931 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8932 | break; |
| 8933 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8934 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8935 | &operation, steps[i], |
| 8936 | inputs[i]->x, inputs[i]->len), statuses[i]); |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 8937 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8938 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 8939 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8940 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8941 | break; |
| 8942 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 8943 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8944 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8945 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8946 | ¤t_capacity)); |
| 8947 | TEST_EQUAL(current_capacity, requested_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8948 | expected_capacity = requested_capacity; |
| 8949 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8950 | if (derive_type == 1) { // output key |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8951 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; |
| 8952 | |
| 8953 | /* For output key derivation secret must be provided using |
| 8954 | input key, otherwise operation is not permitted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8955 | if (key_input_type == 1) { |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8956 | expected_status = PSA_SUCCESS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8957 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8958 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8959 | psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT); |
| 8960 | psa_set_key_algorithm(&attributes4, alg); |
| 8961 | psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE); |
| 8962 | psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8963 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8964 | TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation, |
| 8965 | &derived_key), expected_status); |
| 8966 | } else { // output bytes |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8967 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8968 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8969 | /* Read some bytes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8970 | status = psa_key_derivation_output_bytes(&operation, |
| 8971 | output_buffer, output_sizes[i]); |
| 8972 | if (expected_capacity == 0 && output_sizes[i] == 0) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8973 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8974 | TEST_ASSERT(status == PSA_SUCCESS || |
| 8975 | status == PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8976 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8977 | } else if (expected_capacity == 0 || |
| 8978 | output_sizes[i] > expected_capacity) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8979 | /* Capacity exceeded. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8980 | TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8981 | expected_capacity = 0; |
| 8982 | continue; |
| 8983 | } |
| 8984 | /* Success. Check the read data. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8985 | PSA_ASSERT(status); |
| 8986 | if (output_sizes[i] != 0) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8987 | TEST_MEMORY_COMPARE(output_buffer, output_sizes[i], |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8988 | expected_outputs[i], output_sizes[i]); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8989 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8990 | /* Check the operation status. */ |
| 8991 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8992 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8993 | ¤t_capacity)); |
| 8994 | TEST_EQUAL(expected_capacity, current_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8995 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8996 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8997 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8998 | |
| 8999 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9000 | mbedtls_free(output_buffer); |
| 9001 | psa_key_derivation_abort(&operation); |
| 9002 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 9003 | psa_destroy_key(keys[i]); |
| 9004 | } |
| 9005 | psa_destroy_key(derived_key); |
| 9006 | PSA_DONE(); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9007 | } |
| 9008 | /* END_CASE */ |
| 9009 | |
| 9010 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9011 | void derive_full(int alg_arg, |
| 9012 | data_t *key_data, |
| 9013 | data_t *input1, |
| 9014 | data_t *input2, |
| 9015 | int requested_capacity_arg) |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9016 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9017 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9018 | psa_algorithm_t alg = alg_arg; |
| 9019 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9020 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Kusumit Ghoderao | 9ffd397 | 2023-12-01 16:40:13 +0530 | [diff] [blame] | 9021 | unsigned char output_buffer[32]; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9022 | size_t expected_capacity = requested_capacity; |
| 9023 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9024 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9025 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9026 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9027 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9028 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9029 | psa_set_key_algorithm(&attributes, alg); |
| 9030 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9031 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9032 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 9033 | &key)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9035 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 9036 | input1->x, input1->len, |
| 9037 | input2->x, input2->len, |
| 9038 | requested_capacity)) { |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 9039 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9040 | } |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 9041 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9042 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 9043 | ¤t_capacity)); |
| 9044 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9045 | |
| 9046 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9047 | while (current_capacity > 0) { |
| 9048 | size_t read_size = sizeof(output_buffer); |
| 9049 | if (read_size > current_capacity) { |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9050 | read_size = current_capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9051 | } |
| 9052 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9053 | output_buffer, |
| 9054 | read_size)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9055 | expected_capacity -= read_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9056 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 9057 | ¤t_capacity)); |
| 9058 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9059 | } |
| 9060 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9061 | /* Check that the operation refuses to go over capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9062 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1), |
| 9063 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9064 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9065 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9066 | |
| 9067 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9068 | psa_key_derivation_abort(&operation); |
| 9069 | psa_destroy_key(key); |
| 9070 | PSA_DONE(); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9071 | } |
| 9072 | /* END_CASE */ |
| 9073 | |
Stephan Koch | 78109f5 | 2023-04-12 14:19:36 +0200 | [diff] [blame] | 9074 | /* 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] | 9075 | void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg, |
| 9076 | int derivation_step, |
| 9077 | int capacity, int expected_capacity_status_arg, |
| 9078 | data_t *expected_output, |
| 9079 | int expected_output_status_arg) |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9080 | { |
| 9081 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 9082 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | d378504 | 2022-09-16 06:45:44 -0400 | [diff] [blame] | 9083 | 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] | 9084 | uint8_t *output_buffer = NULL; |
| 9085 | psa_status_t status; |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 9086 | psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg; |
| 9087 | psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg; |
| 9088 | 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] | 9089 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9090 | TEST_CALLOC(output_buffer, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9091 | PSA_ASSERT(psa_crypto_init()); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9092 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9093 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9094 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 9095 | expected_capacity_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9096 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9097 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 9098 | step, input->x, input->len), |
| 9099 | expected_input_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9100 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9101 | if (((psa_status_t) expected_input_status) != PSA_SUCCESS) { |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9102 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9103 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9104 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9105 | status = psa_key_derivation_output_bytes(&operation, output_buffer, |
| 9106 | expected_output->len); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9107 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9108 | TEST_EQUAL(status, expected_output_status); |
| 9109 | if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9110 | TEST_MEMORY_COMPARE(output_buffer, expected_output->len, expected_output->x, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9111 | expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9112 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9113 | |
| 9114 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9115 | mbedtls_free(output_buffer); |
| 9116 | psa_key_derivation_abort(&operation); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9117 | PSA_DONE(); |
| 9118 | } |
| 9119 | /* END_CASE */ |
| 9120 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9121 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9122 | void derive_key_exercise(int alg_arg, |
| 9123 | data_t *key_data, |
| 9124 | data_t *input1, |
| 9125 | data_t *input2, |
| 9126 | int derived_type_arg, |
| 9127 | int derived_bits_arg, |
| 9128 | int derived_usage_arg, |
| 9129 | int derived_alg_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9130 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9131 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9132 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9133 | psa_algorithm_t alg = alg_arg; |
| 9134 | psa_key_type_t derived_type = derived_type_arg; |
| 9135 | size_t derived_bits = derived_bits_arg; |
| 9136 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 9137 | psa_algorithm_t derived_alg = derived_alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9138 | size_t capacity = PSA_BITS_TO_BYTES(derived_bits); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9139 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9140 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 9141 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9142 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9143 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9144 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9145 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9146 | psa_set_key_algorithm(&attributes, alg); |
| 9147 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
| 9148 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 9149 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9150 | |
| 9151 | /* Derive a key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9152 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9153 | input1->x, input1->len, |
| 9154 | input2->x, input2->len, |
| 9155 | capacity)) { |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9156 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9157 | } |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9159 | psa_set_key_usage_flags(&attributes, derived_usage); |
| 9160 | psa_set_key_algorithm(&attributes, derived_alg); |
| 9161 | psa_set_key_type(&attributes, derived_type); |
| 9162 | psa_set_key_bits(&attributes, derived_bits); |
| 9163 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation, |
| 9164 | &derived_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9165 | |
| 9166 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9167 | PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes)); |
| 9168 | TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type); |
| 9169 | TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9170 | |
| 9171 | /* Exercise the derived key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9172 | if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) { |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9173 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9174 | } |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9175 | |
| 9176 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9177 | /* |
| 9178 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9179 | * thus reset them as required. |
| 9180 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9181 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9183 | psa_key_derivation_abort(&operation); |
| 9184 | psa_destroy_key(base_key); |
| 9185 | psa_destroy_key(derived_key); |
| 9186 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9187 | } |
| 9188 | /* END_CASE */ |
| 9189 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9190 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9191 | void derive_key_export(int alg_arg, |
| 9192 | data_t *key_data, |
| 9193 | data_t *input1, |
| 9194 | data_t *input2, |
| 9195 | int bytes1_arg, |
| 9196 | int bytes2_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9197 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9198 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9199 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9200 | psa_algorithm_t alg = alg_arg; |
| 9201 | size_t bytes1 = bytes1_arg; |
| 9202 | size_t bytes2 = bytes2_arg; |
| 9203 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9204 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9205 | uint8_t *output_buffer = NULL; |
| 9206 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9207 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9208 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9209 | size_t length; |
| 9210 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9211 | TEST_CALLOC(output_buffer, capacity); |
| 9212 | TEST_CALLOC(export_buffer, capacity); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9213 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9215 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9216 | psa_set_key_algorithm(&base_attributes, alg); |
| 9217 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9218 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9219 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9220 | |
| 9221 | /* Derive some material and output it. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9222 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9223 | input1->x, input1->len, |
| 9224 | input2->x, input2->len, |
| 9225 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9226 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9227 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9228 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9229 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9230 | output_buffer, |
| 9231 | capacity)); |
| 9232 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9233 | |
| 9234 | /* 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] | 9235 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9236 | input1->x, input1->len, |
| 9237 | input2->x, input2->len, |
| 9238 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9239 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9240 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9241 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9242 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9243 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9244 | psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA); |
| 9245 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1)); |
| 9246 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9247 | &derived_key)); |
| 9248 | PSA_ASSERT(psa_export_key(derived_key, |
| 9249 | export_buffer, bytes1, |
| 9250 | &length)); |
| 9251 | TEST_EQUAL(length, bytes1); |
| 9252 | PSA_ASSERT(psa_destroy_key(derived_key)); |
| 9253 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2)); |
| 9254 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9255 | &derived_key)); |
| 9256 | PSA_ASSERT(psa_export_key(derived_key, |
| 9257 | export_buffer + bytes1, bytes2, |
| 9258 | &length)); |
| 9259 | TEST_EQUAL(length, bytes2); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9260 | |
| 9261 | /* Compare the outputs from the two runs. */ |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9262 | TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9263 | export_buffer, capacity); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9264 | |
| 9265 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9266 | mbedtls_free(output_buffer); |
| 9267 | mbedtls_free(export_buffer); |
| 9268 | psa_key_derivation_abort(&operation); |
| 9269 | psa_destroy_key(base_key); |
| 9270 | psa_destroy_key(derived_key); |
| 9271 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9272 | } |
| 9273 | /* END_CASE */ |
| 9274 | |
| 9275 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9276 | void derive_key_type(int alg_arg, |
| 9277 | data_t *key_data, |
| 9278 | data_t *input1, |
| 9279 | data_t *input2, |
| 9280 | int key_type_arg, int bits_arg, |
| 9281 | data_t *expected_export) |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9282 | { |
| 9283 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9284 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9285 | const psa_algorithm_t alg = alg_arg; |
| 9286 | const psa_key_type_t key_type = key_type_arg; |
| 9287 | const size_t bits = bits_arg; |
| 9288 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9289 | const size_t export_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9290 | PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9291 | uint8_t *export_buffer = NULL; |
| 9292 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9293 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9294 | size_t export_length; |
| 9295 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9296 | TEST_CALLOC(export_buffer, export_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9297 | PSA_ASSERT(psa_crypto_init()); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9298 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9299 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9300 | psa_set_key_algorithm(&base_attributes, alg); |
| 9301 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9302 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9303 | &base_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9304 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9305 | if (mbedtls_test_psa_setup_key_derivation_wrap( |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9306 | &operation, base_key, alg, |
| 9307 | input1->x, input1->len, |
| 9308 | input2->x, input2->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9309 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) { |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9310 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9311 | } |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9312 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9313 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9314 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9315 | psa_set_key_type(&derived_attributes, key_type); |
| 9316 | psa_set_key_bits(&derived_attributes, bits); |
| 9317 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9318 | &derived_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9319 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9320 | PSA_ASSERT(psa_export_key(derived_key, |
| 9321 | export_buffer, export_buffer_size, |
| 9322 | &export_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9323 | TEST_MEMORY_COMPARE(export_buffer, export_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9324 | expected_export->x, expected_export->len); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9325 | |
| 9326 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9327 | mbedtls_free(export_buffer); |
| 9328 | psa_key_derivation_abort(&operation); |
| 9329 | psa_destroy_key(base_key); |
| 9330 | psa_destroy_key(derived_key); |
| 9331 | PSA_DONE(); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9332 | } |
| 9333 | /* END_CASE */ |
| 9334 | |
| 9335 | /* BEGIN_CASE */ |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9336 | void derive_key_ext(int alg_arg, |
| 9337 | data_t *key_data, |
| 9338 | data_t *input1, |
| 9339 | data_t *input2, |
| 9340 | int key_type_arg, int bits_arg, |
Gilles Peskine | c81393b | 2024-02-14 20:51:28 +0100 | [diff] [blame] | 9341 | int flags_arg, |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame^] | 9342 | data_t *params_data, |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9343 | psa_status_t expected_status, |
| 9344 | data_t *expected_export) |
| 9345 | { |
| 9346 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9347 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9348 | const psa_algorithm_t alg = alg_arg; |
| 9349 | const psa_key_type_t key_type = key_type_arg; |
| 9350 | const size_t bits = bits_arg; |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame^] | 9351 | psa_key_production_parameters_t *params = NULL; |
| 9352 | size_t params_data_length = 0; |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9353 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9354 | const size_t export_buffer_size = |
| 9355 | PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits); |
| 9356 | uint8_t *export_buffer = NULL; |
| 9357 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9358 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9359 | size_t export_length; |
| 9360 | |
| 9361 | TEST_CALLOC(export_buffer, export_buffer_size); |
| 9362 | PSA_ASSERT(psa_crypto_init()); |
| 9363 | |
| 9364 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9365 | psa_set_key_algorithm(&base_attributes, alg); |
| 9366 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9367 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9368 | &base_key)); |
| 9369 | |
| 9370 | if (mbedtls_test_psa_setup_key_derivation_wrap( |
| 9371 | &operation, base_key, alg, |
| 9372 | input1->x, input1->len, |
| 9373 | input2->x, input2->len, |
| 9374 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) { |
| 9375 | goto exit; |
| 9376 | } |
| 9377 | |
| 9378 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9379 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9380 | psa_set_key_type(&derived_attributes, key_type); |
| 9381 | psa_set_key_bits(&derived_attributes, bits); |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame^] | 9382 | if (!setup_key_production_parameters(¶ms, ¶ms_data_length, |
| 9383 | flags_arg, params_data)) { |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9384 | goto exit; |
| 9385 | } |
| 9386 | |
| 9387 | TEST_EQUAL(psa_key_derivation_output_key_ext(&derived_attributes, &operation, |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame^] | 9388 | params, params_data_length, |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9389 | &derived_key), |
| 9390 | expected_status); |
| 9391 | |
| 9392 | if (expected_status == PSA_SUCCESS) { |
| 9393 | PSA_ASSERT(psa_export_key(derived_key, |
| 9394 | export_buffer, export_buffer_size, |
| 9395 | &export_length)); |
| 9396 | TEST_MEMORY_COMPARE(export_buffer, export_length, |
| 9397 | expected_export->x, expected_export->len); |
| 9398 | } |
| 9399 | |
| 9400 | exit: |
| 9401 | mbedtls_free(export_buffer); |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame^] | 9402 | mbedtls_free(params); |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9403 | psa_key_derivation_abort(&operation); |
| 9404 | psa_destroy_key(base_key); |
| 9405 | psa_destroy_key(derived_key); |
| 9406 | PSA_DONE(); |
| 9407 | } |
| 9408 | /* END_CASE */ |
| 9409 | |
| 9410 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9411 | void derive_key(int alg_arg, |
| 9412 | data_t *key_data, data_t *input1, data_t *input2, |
| 9413 | int type_arg, int bits_arg, |
| 9414 | int expected_status_arg, |
| 9415 | int is_large_output) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9416 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9417 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9418 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9419 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 9420 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9421 | size_t bits = bits_arg; |
| 9422 | psa_status_t expected_status = expected_status_arg; |
| 9423 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9424 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9425 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9426 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9427 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9428 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9429 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9430 | psa_set_key_algorithm(&base_attributes, alg); |
| 9431 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9432 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9433 | &base_key)); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9434 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9435 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9436 | input1->x, input1->len, |
| 9437 | input2->x, input2->len, |
| 9438 | SIZE_MAX)) { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9439 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9440 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9441 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9442 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9443 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9444 | psa_set_key_type(&derived_attributes, type); |
| 9445 | psa_set_key_bits(&derived_attributes, bits); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9446 | |
| 9447 | psa_status_t status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9448 | psa_key_derivation_output_key(&derived_attributes, |
| 9449 | &operation, |
| 9450 | &derived_key); |
| 9451 | if (is_large_output > 0) { |
| 9452 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9453 | } |
| 9454 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9455 | |
| 9456 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9457 | psa_key_derivation_abort(&operation); |
| 9458 | psa_destroy_key(base_key); |
| 9459 | psa_destroy_key(derived_key); |
| 9460 | PSA_DONE(); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9461 | } |
| 9462 | /* END_CASE */ |
| 9463 | |
| 9464 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9465 | void key_agreement_setup(int alg_arg, |
| 9466 | int our_key_type_arg, int our_key_alg_arg, |
| 9467 | data_t *our_key_data, data_t *peer_key_data, |
| 9468 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9469 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9470 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9471 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 9472 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9473 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9474 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9475 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9476 | psa_status_t expected_status = expected_status_arg; |
| 9477 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9478 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9479 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9480 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9481 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9482 | psa_set_key_algorithm(&attributes, our_key_alg); |
| 9483 | psa_set_key_type(&attributes, our_key_type); |
| 9484 | PSA_ASSERT(psa_import_key(&attributes, |
| 9485 | our_key_data->x, our_key_data->len, |
| 9486 | &our_key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9487 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9488 | /* The tests currently include inputs that should fail at either step. |
| 9489 | * Test cases that fail at the setup step should be changed to call |
| 9490 | * key_derivation_setup instead, and this function should be renamed |
| 9491 | * to key_agreement_fail. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9492 | status = psa_key_derivation_setup(&operation, alg); |
| 9493 | if (status == PSA_SUCCESS) { |
| 9494 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 9495 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 9496 | our_key, |
| 9497 | peer_key_data->x, peer_key_data->len), |
| 9498 | expected_status); |
| 9499 | } else { |
| 9500 | TEST_ASSERT(status == expected_status); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9501 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9502 | |
| 9503 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9504 | psa_key_derivation_abort(&operation); |
| 9505 | psa_destroy_key(our_key); |
| 9506 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9507 | } |
| 9508 | /* END_CASE */ |
| 9509 | |
| 9510 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9511 | void raw_key_agreement(int alg_arg, |
| 9512 | int our_key_type_arg, data_t *our_key_data, |
| 9513 | data_t *peer_key_data, |
| 9514 | data_t *expected_output) |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9515 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9516 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9517 | psa_algorithm_t alg = alg_arg; |
| 9518 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9519 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9520 | unsigned char *output = NULL; |
| 9521 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9522 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9523 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9524 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9525 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9526 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9527 | psa_set_key_algorithm(&attributes, alg); |
| 9528 | psa_set_key_type(&attributes, our_key_type); |
| 9529 | PSA_ASSERT(psa_import_key(&attributes, |
| 9530 | our_key_data->x, our_key_data->len, |
| 9531 | &our_key)); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9532 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9533 | PSA_ASSERT(psa_get_key_attributes(our_key, &attributes)); |
| 9534 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9535 | |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9536 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9537 | TEST_LE_U(expected_output->len, |
| 9538 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits)); |
| 9539 | TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits), |
| 9540 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9541 | |
| 9542 | /* Good case with exact output size */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9543 | TEST_CALLOC(output, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9544 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9545 | peer_key_data->x, peer_key_data->len, |
| 9546 | output, expected_output->len, |
| 9547 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9548 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9549 | expected_output->x, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9550 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9551 | output = NULL; |
| 9552 | output_length = ~0; |
| 9553 | |
| 9554 | /* Larger buffer */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9555 | TEST_CALLOC(output, expected_output->len + 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9556 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9557 | peer_key_data->x, peer_key_data->len, |
| 9558 | output, expected_output->len + 1, |
| 9559 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9560 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9561 | expected_output->x, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9562 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9563 | output = NULL; |
| 9564 | output_length = ~0; |
| 9565 | |
| 9566 | /* Buffer too small */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9567 | TEST_CALLOC(output, expected_output->len - 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9568 | TEST_EQUAL(psa_raw_key_agreement(alg, our_key, |
| 9569 | peer_key_data->x, peer_key_data->len, |
| 9570 | output, expected_output->len - 1, |
| 9571 | &output_length), |
| 9572 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9573 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9574 | TEST_LE_U(output_length, expected_output->len - 1); |
| 9575 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9576 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9577 | |
| 9578 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9579 | mbedtls_free(output); |
| 9580 | psa_destroy_key(our_key); |
| 9581 | PSA_DONE(); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9582 | } |
| 9583 | /* END_CASE */ |
| 9584 | |
| 9585 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9586 | void key_agreement_capacity(int alg_arg, |
| 9587 | int our_key_type_arg, data_t *our_key_data, |
| 9588 | data_t *peer_key_data, |
| 9589 | int expected_capacity_arg) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9590 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9591 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9592 | psa_algorithm_t alg = alg_arg; |
| 9593 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9594 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9595 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9596 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9597 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9598 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9599 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9600 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9601 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9602 | psa_set_key_algorithm(&attributes, alg); |
| 9603 | psa_set_key_type(&attributes, our_key_type); |
| 9604 | PSA_ASSERT(psa_import_key(&attributes, |
| 9605 | our_key_data->x, our_key_data->len, |
| 9606 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9607 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9608 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9609 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9610 | &operation, |
| 9611 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9612 | peer_key_data->x, peer_key_data->len)); |
| 9613 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9614 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9615 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9616 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9617 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9618 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9619 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 9620 | /* Test the advertised capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9621 | PSA_ASSERT(psa_key_derivation_get_capacity( |
| 9622 | &operation, &actual_capacity)); |
| 9623 | TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9624 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9625 | /* Test the actual capacity by reading the output. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9626 | while (actual_capacity > sizeof(output)) { |
| 9627 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9628 | output, sizeof(output))); |
| 9629 | actual_capacity -= sizeof(output); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9630 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9631 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9632 | output, actual_capacity)); |
| 9633 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1), |
| 9634 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9635 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9636 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9637 | psa_key_derivation_abort(&operation); |
| 9638 | psa_destroy_key(our_key); |
| 9639 | PSA_DONE(); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9640 | } |
| 9641 | /* END_CASE */ |
| 9642 | |
Valerio Setti | ad81967 | 2023-12-29 12:14:41 +0100 | [diff] [blame] | 9643 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */ |
| 9644 | 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] | 9645 | { |
| 9646 | mbedtls_ecp_group_id grp_id = grp_id_arg; |
Valerio Setti | ad81967 | 2023-12-29 12:14:41 +0100 | [diff] [blame] | 9647 | psa_ecc_family_t ecc_family = psa_family_arg; |
| 9648 | size_t bits = bits_arg; |
| 9649 | size_t bits_tmp; |
Valerio Setti | bf999cb | 2023-12-28 17:48:13 +0100 | [diff] [blame] | 9650 | |
Valerio Setti | ad81967 | 2023-12-29 12:14:41 +0100 | [diff] [blame] | 9651 | TEST_EQUAL(ecc_family, mbedtls_ecc_group_to_psa(grp_id, &bits_tmp)); |
| 9652 | TEST_EQUAL(bits, bits_tmp); |
Valerio Setti | ac73952 | 2024-01-04 10:22:01 +0100 | [diff] [blame] | 9653 | TEST_EQUAL(grp_id, mbedtls_ecc_group_from_psa(ecc_family, bits)); |
Valerio Setti | bf999cb | 2023-12-28 17:48:13 +0100 | [diff] [blame] | 9654 | } |
| 9655 | /* END_CASE */ |
| 9656 | |
Valerio Setti | ac73952 | 2024-01-04 10:22:01 +0100 | [diff] [blame] | 9657 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */ |
| 9658 | void ecc_conversion_functions_fail() |
| 9659 | { |
| 9660 | size_t bits; |
| 9661 | |
Valerio Setti | db6e029 | 2024-01-05 10:15:45 +0100 | [diff] [blame] | 9662 | /* Invalid legacy curve identifiers. */ |
| 9663 | TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_MAX, &bits)); |
| 9664 | TEST_EQUAL(0, bits); |
Valerio Setti | ac73952 | 2024-01-04 10:22:01 +0100 | [diff] [blame] | 9665 | TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_NONE, &bits)); |
| 9666 | TEST_EQUAL(0, bits); |
| 9667 | |
| 9668 | /* Invalid PSA EC family. */ |
| 9669 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(0, 192)); |
| 9670 | /* Invalid bit-size for a valid EC family. */ |
| 9671 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_SECP_R1, 512)); |
| 9672 | |
| 9673 | /* Twisted-Edward curves are not supported yet. */ |
| 9674 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, |
| 9675 | mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 255)); |
| 9676 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, |
| 9677 | mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 448)); |
| 9678 | } |
| 9679 | /* END_CASE */ |
| 9680 | |
| 9681 | |
Valerio Setti | bf999cb | 2023-12-28 17:48:13 +0100 | [diff] [blame] | 9682 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9683 | void key_agreement_output(int alg_arg, |
| 9684 | int our_key_type_arg, data_t *our_key_data, |
| 9685 | data_t *peer_key_data, |
| 9686 | data_t *expected_output1, data_t *expected_output2) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9687 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9688 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9689 | psa_algorithm_t alg = alg_arg; |
| 9690 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9691 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9692 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9693 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9694 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9695 | TEST_CALLOC(actual_output, MAX(expected_output1->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9696 | expected_output2->len)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9697 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9698 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9699 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9700 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9701 | psa_set_key_algorithm(&attributes, alg); |
| 9702 | psa_set_key_type(&attributes, our_key_type); |
| 9703 | PSA_ASSERT(psa_import_key(&attributes, |
| 9704 | our_key_data->x, our_key_data->len, |
| 9705 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9706 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9707 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9708 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9709 | &operation, |
| 9710 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9711 | peer_key_data->x, peer_key_data->len)); |
| 9712 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9713 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9714 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9715 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9716 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9717 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9718 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9719 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9720 | actual_output, |
| 9721 | expected_output1->len)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9722 | TEST_MEMORY_COMPARE(actual_output, expected_output1->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9723 | expected_output1->x, expected_output1->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9724 | if (expected_output2->len != 0) { |
| 9725 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9726 | actual_output, |
| 9727 | expected_output2->len)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9728 | TEST_MEMORY_COMPARE(actual_output, expected_output2->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9729 | expected_output2->x, expected_output2->len); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9730 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9731 | |
| 9732 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9733 | psa_key_derivation_abort(&operation); |
| 9734 | psa_destroy_key(our_key); |
| 9735 | PSA_DONE(); |
| 9736 | mbedtls_free(actual_output); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9737 | } |
| 9738 | /* END_CASE */ |
| 9739 | |
| 9740 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9741 | void generate_random(int bytes_arg) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9742 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9743 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9744 | unsigned char *output = NULL; |
| 9745 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9746 | size_t i; |
| 9747 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9748 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9749 | TEST_ASSERT(bytes_arg >= 0); |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 9750 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9751 | TEST_CALLOC(output, bytes); |
| 9752 | TEST_CALLOC(changed, bytes); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9753 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9754 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9755 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9756 | /* Run several times, to ensure that every output byte will be |
| 9757 | * nonzero at least once with overwhelming probability |
| 9758 | * (2^(-8*number_of_runs)). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9759 | for (run = 0; run < 10; run++) { |
| 9760 | if (bytes != 0) { |
| 9761 | memset(output, 0, bytes); |
| 9762 | } |
| 9763 | PSA_ASSERT(psa_generate_random(output, bytes)); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9764 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9765 | for (i = 0; i < bytes; i++) { |
| 9766 | if (output[i] != 0) { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9767 | ++changed[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9768 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9769 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9770 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9771 | |
| 9772 | /* Check that every byte was changed to nonzero at least once. This |
| 9773 | * validates that psa_generate_random is overwriting every byte of |
| 9774 | * the output buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9775 | for (i = 0; i < bytes; i++) { |
| 9776 | TEST_ASSERT(changed[i] != 0); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9777 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9778 | |
| 9779 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9780 | PSA_DONE(); |
| 9781 | mbedtls_free(output); |
| 9782 | mbedtls_free(changed); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9783 | } |
| 9784 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9785 | |
| 9786 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9787 | void generate_key(int type_arg, |
| 9788 | int bits_arg, |
| 9789 | int usage_arg, |
| 9790 | int alg_arg, |
| 9791 | int expected_status_arg, |
| 9792 | int is_large_key) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9793 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9794 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9795 | psa_key_type_t type = type_arg; |
| 9796 | psa_key_usage_t usage = usage_arg; |
| 9797 | size_t bits = bits_arg; |
| 9798 | psa_algorithm_t alg = alg_arg; |
| 9799 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9800 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 9801 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9802 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9803 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9804 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9805 | psa_set_key_usage_flags(&attributes, usage); |
| 9806 | psa_set_key_algorithm(&attributes, alg); |
| 9807 | psa_set_key_type(&attributes, type); |
| 9808 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9809 | |
| 9810 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9811 | psa_status_t status = psa_generate_key(&attributes, &key); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9812 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9813 | if (is_large_key > 0) { |
| 9814 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9815 | } |
| 9816 | TEST_EQUAL(status, expected_status); |
| 9817 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9818 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9819 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9820 | |
| 9821 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9822 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 9823 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 9824 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9825 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 9826 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9827 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 9828 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9829 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9830 | |
| 9831 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9832 | /* |
| 9833 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9834 | * thus reset them as required. |
| 9835 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9836 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9837 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9838 | psa_destroy_key(key); |
| 9839 | PSA_DONE(); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9840 | } |
| 9841 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 9842 | |
Valerio Setti | 19fec54 | 2023-07-25 12:31:50 +0200 | [diff] [blame] | 9843 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE:PSA_WANT_ALG_RSA_PKCS1V15_CRYPT:PSA_WANT_ALG_RSA_PKCS1V15_SIGN */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9844 | void generate_key_rsa(int bits_arg, |
| 9845 | data_t *e_arg, |
| 9846 | int expected_status_arg) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9847 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9848 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 9849 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9850 | size_t bits = bits_arg; |
| 9851 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 9852 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 9853 | psa_status_t expected_status = expected_status_arg; |
| 9854 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9855 | uint8_t *e_read_buffer = NULL; |
| 9856 | int is_default_public_exponent = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9857 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9858 | size_t e_read_length = SIZE_MAX; |
| 9859 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9860 | if (e_arg->len == 0 || |
| 9861 | (e_arg->len == 3 && |
| 9862 | e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9863 | is_default_public_exponent = 1; |
| 9864 | e_read_size = 0; |
| 9865 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9866 | TEST_CALLOC(e_read_buffer, e_read_size); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9867 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9868 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9869 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9870 | psa_set_key_usage_flags(&attributes, usage); |
| 9871 | psa_set_key_algorithm(&attributes, alg); |
| 9872 | PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type, |
| 9873 | e_arg->x, e_arg->len)); |
| 9874 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9875 | |
| 9876 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9877 | TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status); |
| 9878 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9879 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9880 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9881 | |
| 9882 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9883 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 9884 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 9885 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
Pengyu Lv | d90fbf7 | 2023-12-08 17:13:22 +0800 | [diff] [blame] | 9886 | psa_status_t status = psa_get_key_domain_parameters(&attributes, |
| 9887 | e_read_buffer, e_read_size, |
| 9888 | &e_read_length); |
| 9889 | |
| 9890 | |
| 9891 | #if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \ |
| 9892 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \ |
| 9893 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9894 | if (is_default_public_exponent) { |
| 9895 | TEST_EQUAL(e_read_length, 0); |
| 9896 | } else { |
Pengyu Lv | d90fbf7 | 2023-12-08 17:13:22 +0800 | [diff] [blame] | 9897 | TEST_EQUAL(status, PSA_SUCCESS); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9898 | TEST_MEMORY_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9899 | } |
Pengyu Lv | 9e976f3 | 2023-12-06 16:58:05 +0800 | [diff] [blame] | 9900 | #else |
Pengyu Lv | 9e976f3 | 2023-12-06 16:58:05 +0800 | [diff] [blame] | 9901 | (void) is_default_public_exponent; |
Pengyu Lv | d90fbf7 | 2023-12-08 17:13:22 +0800 | [diff] [blame] | 9902 | TEST_EQUAL(status, PSA_ERROR_NOT_SUPPORTED); |
Pengyu Lv | 9e976f3 | 2023-12-06 16:58:05 +0800 | [diff] [blame] | 9903 | #endif |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9904 | |
| 9905 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9906 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9907 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9908 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9909 | |
Gilles Peskine | 1d25a0a | 2024-02-12 16:40:04 +0100 | [diff] [blame] | 9910 | TEST_ASSERT(rsa_test_e(key, bits, e_arg)); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9911 | |
| 9912 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9913 | /* |
| 9914 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 9915 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 9916 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9917 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9918 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9919 | psa_destroy_key(key); |
| 9920 | PSA_DONE(); |
| 9921 | mbedtls_free(e_read_buffer); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9922 | } |
| 9923 | /* END_CASE */ |
| 9924 | |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9925 | /* BEGIN_CASE */ |
| 9926 | void generate_key_ext(int type_arg, |
| 9927 | int bits_arg, |
| 9928 | int usage_arg, |
| 9929 | int alg_arg, |
Gilles Peskine | c81393b | 2024-02-14 20:51:28 +0100 | [diff] [blame] | 9930 | int flags_arg, |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame^] | 9931 | data_t *params_data, |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9932 | int expected_status_arg) |
| 9933 | { |
| 9934 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9935 | psa_key_type_t type = type_arg; |
| 9936 | psa_key_usage_t usage = usage_arg; |
| 9937 | size_t bits = bits_arg; |
| 9938 | psa_algorithm_t alg = alg_arg; |
| 9939 | psa_status_t expected_status = expected_status_arg; |
| 9940 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame^] | 9941 | psa_key_production_parameters_t *params = NULL; |
| 9942 | size_t params_data_length = 0; |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9943 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9944 | |
| 9945 | PSA_ASSERT(psa_crypto_init()); |
| 9946 | |
| 9947 | psa_set_key_usage_flags(&attributes, usage); |
| 9948 | psa_set_key_algorithm(&attributes, alg); |
| 9949 | psa_set_key_type(&attributes, type); |
| 9950 | psa_set_key_bits(&attributes, bits); |
| 9951 | |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame^] | 9952 | if (!setup_key_production_parameters(¶ms, ¶ms_data_length, |
| 9953 | flags_arg, params_data)) { |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9954 | goto exit; |
| 9955 | } |
| 9956 | |
| 9957 | /* Generate a key */ |
| 9958 | psa_status_t status = psa_generate_key_ext(&attributes, |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame^] | 9959 | params, params_data_length, |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9960 | &key); |
| 9961 | |
| 9962 | TEST_EQUAL(status, expected_status); |
| 9963 | if (expected_status != PSA_SUCCESS) { |
| 9964 | goto exit; |
| 9965 | } |
| 9966 | |
| 9967 | /* Test the key information */ |
| 9968 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 9969 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 9970 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
| 9971 | |
Gilles Peskine | 7a18f96 | 2024-02-12 16:48:11 +0100 | [diff] [blame] | 9972 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) |
| 9973 | if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame^] | 9974 | TEST_ASSERT(rsa_test_e(key, bits, params_data)); |
Gilles Peskine | 7a18f96 | 2024-02-12 16:48:11 +0100 | [diff] [blame] | 9975 | } |
| 9976 | #endif |
| 9977 | |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9978 | /* Do something with the key according to its type and permitted usage. */ |
| 9979 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
| 9980 | goto exit; |
| 9981 | } |
| 9982 | |
| 9983 | exit: |
| 9984 | /* |
| 9985 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9986 | * thus reset them as required. |
| 9987 | */ |
| 9988 | psa_reset_key_attributes(&got_attributes); |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame^] | 9989 | mbedtls_free(params); |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9990 | psa_destroy_key(key); |
| 9991 | PSA_DONE(); |
| 9992 | } |
| 9993 | /* END_CASE */ |
| 9994 | |
| 9995 | /* BEGIN_CASE */ |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame^] | 9996 | void key_production_parameters_init() |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9997 | { |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame^] | 9998 | psa_key_production_parameters_t init = PSA_KEY_PRODUCTION_PARAMETERS_INIT; |
| 9999 | psa_key_production_parameters_t zero; |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10000 | memset(&zero, 0, sizeof(zero)); |
| 10001 | |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10002 | TEST_EQUAL(init.flags, 0); |
| 10003 | TEST_EQUAL(zero.flags, 0); |
| 10004 | } |
| 10005 | /* END_CASE */ |
| 10006 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10007 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10008 | void persistent_key_load_key_from_storage(data_t *data, |
| 10009 | int type_arg, int bits_arg, |
| 10010 | int usage_flags_arg, int alg_arg, |
| 10011 | int generation_method) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10012 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10013 | 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] | 10014 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 10015 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10016 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10017 | psa_key_type_t type = type_arg; |
| 10018 | size_t bits = bits_arg; |
| 10019 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 10020 | psa_algorithm_t alg = alg_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; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10022 | unsigned char *first_export = NULL; |
| 10023 | unsigned char *second_export = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10024 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits); |
Sergey | bef1f63 | 2023-03-06 15:25:06 -0700 | [diff] [blame] | 10025 | size_t first_exported_length = 0; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10026 | size_t second_exported_length; |
| 10027 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10028 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 10029 | TEST_CALLOC(first_export, export_size); |
| 10030 | TEST_CALLOC(second_export, export_size); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10031 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10032 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10033 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10035 | psa_set_key_id(&attributes, key_id); |
| 10036 | psa_set_key_usage_flags(&attributes, usage_flags); |
| 10037 | psa_set_key_algorithm(&attributes, alg); |
| 10038 | psa_set_key_type(&attributes, type); |
| 10039 | psa_set_key_bits(&attributes, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10040 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10041 | switch (generation_method) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10042 | case IMPORT_KEY: |
| 10043 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10044 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, |
| 10045 | &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10046 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10047 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10048 | case GENERATE_KEY: |
| 10049 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10050 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10051 | break; |
| 10052 | |
| 10053 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 10054 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10055 | { |
| 10056 | /* Create base key */ |
| 10057 | psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256); |
| 10058 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10059 | psa_set_key_usage_flags(&base_attributes, |
| 10060 | PSA_KEY_USAGE_DERIVE); |
| 10061 | psa_set_key_algorithm(&base_attributes, derive_alg); |
| 10062 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 10063 | PSA_ASSERT(psa_import_key(&base_attributes, |
| 10064 | data->x, data->len, |
| 10065 | &base_key)); |
| 10066 | /* Derive a key. */ |
| 10067 | PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg)); |
| 10068 | PSA_ASSERT(psa_key_derivation_input_key( |
| 10069 | &operation, |
| 10070 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key)); |
| 10071 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 10072 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
| 10073 | NULL, 0)); |
| 10074 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, |
| 10075 | &operation, |
| 10076 | &key)); |
| 10077 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
| 10078 | PSA_ASSERT(psa_destroy_key(base_key)); |
| 10079 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10080 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 10081 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10082 | TEST_ASSUME(!"KDF not supported in this configuration"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 10083 | #endif |
| 10084 | break; |
| 10085 | |
| 10086 | default: |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 10087 | TEST_FAIL("generation_method not implemented in test"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 10088 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10089 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10090 | psa_reset_key_attributes(&attributes); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10091 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10092 | /* Export the key if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10093 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 10094 | PSA_ASSERT(psa_export_key(key, |
| 10095 | first_export, export_size, |
| 10096 | &first_exported_length)); |
| 10097 | if (generation_method == IMPORT_KEY) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 10098 | TEST_MEMORY_COMPARE(data->x, data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 10099 | first_export, first_exported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10100 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10101 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10102 | |
| 10103 | /* Shutdown and restart */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10104 | PSA_ASSERT(psa_purge_key(key)); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 10105 | PSA_DONE(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10106 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10107 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10108 | /* Check key slot still contains key data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10109 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 10110 | TEST_ASSERT(mbedtls_svc_key_id_equal( |
| 10111 | psa_get_key_id(&attributes), key_id)); |
| 10112 | TEST_EQUAL(psa_get_key_lifetime(&attributes), |
| 10113 | PSA_KEY_LIFETIME_PERSISTENT); |
| 10114 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 10115 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 10116 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 10117 | mbedtls_test_update_key_usage_flags(usage_flags)); |
| 10118 | TEST_EQUAL(psa_get_key_algorithm(&attributes), alg); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10119 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10120 | /* Export the key again if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10121 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 10122 | PSA_ASSERT(psa_export_key(key, |
| 10123 | second_export, export_size, |
| 10124 | &second_exported_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 10125 | TEST_MEMORY_COMPARE(first_export, first_exported_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 10126 | second_export, second_exported_length); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10127 | } |
| 10128 | |
| 10129 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10130 | if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10131 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10132 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10133 | |
| 10134 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 10135 | /* |
| 10136 | * Key attributes may have been returned by psa_get_key_attributes() |
| 10137 | * thus reset them as required. |
| 10138 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10139 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 10140 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10141 | mbedtls_free(first_export); |
| 10142 | mbedtls_free(second_export); |
| 10143 | psa_key_derivation_abort(&operation); |
| 10144 | psa_destroy_key(base_key); |
| 10145 | psa_destroy_key(key); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 10146 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10147 | } |
| 10148 | /* END_CASE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10149 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 10150 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10151 | void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg, |
| 10152 | int primitive_arg, int hash_arg, int role_arg, |
| 10153 | int test_input, data_t *pw_data, |
| 10154 | int inj_err_type_arg, |
| 10155 | int expected_error_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10156 | { |
| 10157 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10158 | psa_pake_operation_t operation = psa_pake_operation_init(); |
| 10159 | psa_algorithm_t alg = alg_arg; |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 10160 | psa_pake_primitive_t primitive = primitive_arg; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 10161 | psa_key_type_t key_type_pw = key_type_pw_arg; |
| 10162 | psa_key_usage_t key_usage_pw = key_usage_pw_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10163 | psa_algorithm_t hash_alg = hash_arg; |
| 10164 | psa_pake_role_t role = role_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10165 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10166 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10167 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
| 10168 | psa_status_t expected_error = expected_error_arg; |
| 10169 | psa_status_t status; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10170 | unsigned char *output_buffer = NULL; |
| 10171 | size_t output_len = 0; |
| 10172 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10173 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10174 | |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 10175 | size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10176 | PSA_PAKE_STEP_KEY_SHARE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 10177 | TEST_CALLOC(output_buffer, buf_size); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10178 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10179 | if (pw_data->len > 0) { |
| 10180 | psa_set_key_usage_flags(&attributes, key_usage_pw); |
| 10181 | psa_set_key_algorithm(&attributes, alg); |
| 10182 | psa_set_key_type(&attributes, key_type_pw); |
| 10183 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10184 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10185 | } |
| 10186 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10187 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10188 | psa_pake_cs_set_primitive(&cipher_suite, primitive); |
| 10189 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10190 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10191 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 10192 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10193 | if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) { |
| 10194 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 10195 | expected_error); |
| 10196 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10197 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 10198 | expected_error); |
| 10199 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10200 | TEST_EQUAL(psa_pake_set_password_key(&operation, key), |
| 10201 | expected_error); |
| 10202 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10203 | TEST_EQUAL(psa_pake_set_role(&operation, role), |
| 10204 | expected_error); |
| 10205 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10206 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10207 | NULL, 0, NULL), |
| 10208 | expected_error); |
| 10209 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10210 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0), |
| 10211 | expected_error); |
| 10212 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10213 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10214 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10215 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10216 | status = psa_pake_setup(&operation, &cipher_suite); |
| 10217 | if (status != PSA_SUCCESS) { |
| 10218 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10219 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10220 | } |
| 10221 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10222 | if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) { |
| 10223 | TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite), |
| 10224 | expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10225 | goto exit; |
| 10226 | } |
| 10227 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10228 | status = psa_pake_set_role(&operation, role); |
| 10229 | if (status != PSA_SUCCESS) { |
| 10230 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10231 | goto exit; |
| 10232 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10233 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10234 | if (pw_data->len > 0) { |
| 10235 | status = psa_pake_set_password_key(&operation, key); |
| 10236 | if (status != PSA_SUCCESS) { |
| 10237 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10238 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10239 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10240 | } |
| 10241 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10242 | if (inj_err_type == INJECT_ERR_INVALID_USER) { |
| 10243 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 10244 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10245 | goto exit; |
| 10246 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 10247 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10248 | if (inj_err_type == INJECT_ERR_INVALID_PEER) { |
| 10249 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 10250 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10251 | goto exit; |
| 10252 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 10253 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10254 | if (inj_err_type == INJECT_ERR_SET_USER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10255 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10256 | TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4), |
| 10257 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10258 | goto exit; |
| 10259 | } |
| 10260 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10261 | if (inj_err_type == INJECT_ERR_SET_PEER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10262 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10263 | TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4), |
| 10264 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10265 | goto exit; |
| 10266 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 10267 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10268 | const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10269 | PSA_PAKE_STEP_KEY_SHARE); |
| 10270 | const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10271 | PSA_PAKE_STEP_ZK_PUBLIC); |
| 10272 | const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10273 | PSA_PAKE_STEP_ZK_PROOF); |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 10274 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10275 | if (test_input) { |
| 10276 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 10277 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0), |
| 10278 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10279 | goto exit; |
| 10280 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10282 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 10283 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 10284 | output_buffer, size_zk_proof), |
| 10285 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10286 | goto exit; |
| 10287 | } |
| 10288 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10289 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 10290 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10291 | output_buffer, size_zk_proof), |
| 10292 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10293 | goto exit; |
| 10294 | } |
| 10295 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10296 | status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10297 | output_buffer, size_key_share); |
| 10298 | if (status != PSA_SUCCESS) { |
| 10299 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10300 | goto exit; |
| 10301 | } |
| 10302 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10303 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 10304 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10305 | output_buffer, size_zk_public + 1), |
| 10306 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10307 | goto exit; |
| 10308 | } |
| 10309 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10310 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10311 | // 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] | 10312 | psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10313 | output_buffer, size_zk_public + 1); |
| 10314 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10315 | output_buffer, size_zk_public), |
| 10316 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10317 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10318 | } |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10319 | } else { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10320 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 10321 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10322 | NULL, 0, NULL), |
| 10323 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10324 | goto exit; |
| 10325 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10326 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10327 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 10328 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 10329 | output_buffer, buf_size, &output_len), |
| 10330 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10331 | goto exit; |
| 10332 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10333 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10334 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 10335 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10336 | output_buffer, buf_size, &output_len), |
| 10337 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10338 | goto exit; |
| 10339 | } |
| 10340 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10341 | status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10342 | output_buffer, buf_size, &output_len); |
| 10343 | if (status != PSA_SUCCESS) { |
| 10344 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10345 | goto exit; |
| 10346 | } |
| 10347 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10348 | TEST_ASSERT(output_len > 0); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10349 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10350 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 10351 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10352 | output_buffer, size_zk_public - 1, &output_len), |
| 10353 | PSA_ERROR_BUFFER_TOO_SMALL); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10354 | goto exit; |
| 10355 | } |
| 10356 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10357 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10358 | // 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] | 10359 | psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10360 | output_buffer, size_zk_public - 1, &output_len); |
| 10361 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10362 | output_buffer, buf_size, &output_len), |
| 10363 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10364 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10365 | } |
| 10366 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10367 | |
| 10368 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10369 | PSA_ASSERT(psa_destroy_key(key)); |
| 10370 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10371 | mbedtls_free(output_buffer); |
| 10372 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10373 | } |
| 10374 | /* END_CASE */ |
| 10375 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 10376 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10377 | void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg, |
| 10378 | int client_input_first, int inject_error, |
| 10379 | data_t *pw_data) |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10380 | { |
| 10381 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10382 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10383 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10384 | psa_algorithm_t alg = alg_arg; |
| 10385 | psa_algorithm_t hash_alg = hash_arg; |
| 10386 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10387 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10389 | PSA_INIT(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10390 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10391 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10392 | psa_set_key_algorithm(&attributes, alg); |
| 10393 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10394 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10395 | &key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10396 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10397 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10398 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10399 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10400 | |
| 10401 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10402 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10403 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10404 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10405 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10406 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10407 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10408 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10409 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10410 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10411 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10412 | client_input_first, 1, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10413 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10414 | if (inject_error == 1 || inject_error == 2) { |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10415 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10416 | } |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10417 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10418 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10419 | client_input_first, 2, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10420 | |
| 10421 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10422 | psa_destroy_key(key); |
| 10423 | psa_pake_abort(&server); |
| 10424 | psa_pake_abort(&client); |
| 10425 | PSA_DONE(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10426 | } |
| 10427 | /* END_CASE */ |
| 10428 | |
| 10429 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10430 | void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg, |
| 10431 | int derive_alg_arg, data_t *pw_data, |
| 10432 | int client_input_first, int inj_err_type_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10433 | { |
| 10434 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10435 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10436 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10437 | psa_algorithm_t alg = alg_arg; |
| 10438 | psa_algorithm_t hash_alg = hash_arg; |
| 10439 | psa_algorithm_t derive_alg = derive_alg_arg; |
| 10440 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10441 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10442 | psa_key_derivation_operation_t server_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10443 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10444 | psa_key_derivation_operation_t client_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10445 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10446 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10447 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10448 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10449 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10450 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10451 | psa_set_key_algorithm(&attributes, alg); |
| 10452 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10453 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10454 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10455 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10456 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10457 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10458 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10459 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10460 | /* Get shared key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10461 | PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg)); |
| 10462 | PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10463 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10464 | if (PSA_ALG_IS_TLS12_PRF(derive_alg) || |
| 10465 | PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) { |
| 10466 | PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive, |
| 10467 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10468 | (const uint8_t *) "", 0)); |
| 10469 | PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive, |
| 10470 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10471 | (const uint8_t *) "", 0)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10472 | } |
| 10473 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10474 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10475 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10476 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10477 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10478 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10479 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10480 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10481 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10482 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10483 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) { |
| 10484 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10485 | PSA_ERROR_BAD_STATE); |
| 10486 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10487 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10488 | goto exit; |
| 10489 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10490 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10491 | /* First round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10492 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10493 | client_input_first, 1, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10494 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10495 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) { |
| 10496 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10497 | PSA_ERROR_BAD_STATE); |
| 10498 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10499 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10500 | goto exit; |
| 10501 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10502 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10503 | /* Second round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10504 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10505 | client_input_first, 2, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10506 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10507 | PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive)); |
| 10508 | PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10509 | |
| 10510 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10511 | psa_key_derivation_abort(&server_derive); |
| 10512 | psa_key_derivation_abort(&client_derive); |
| 10513 | psa_destroy_key(key); |
| 10514 | psa_pake_abort(&server); |
| 10515 | psa_pake_abort(&client); |
| 10516 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10517 | } |
| 10518 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10519 | |
| 10520 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10521 | void ecjpake_size_macros() |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10522 | { |
| 10523 | const psa_algorithm_t alg = PSA_ALG_JPAKE; |
| 10524 | const size_t bits = 256; |
| 10525 | const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10526 | 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] | 10527 | 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] | 10528 | PSA_ECC_FAMILY_SECP_R1); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10529 | |
| 10530 | // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types |
| 10531 | /* 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] | 10532 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10533 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
| 10534 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10535 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10536 | /* The output for ZK_PROOF is the same bitsize as the curve */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10537 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10538 | PSA_BITS_TO_BYTES(bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10539 | |
| 10540 | /* Input sizes are the same as output sizes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10541 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10542 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE)); |
| 10543 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10544 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC)); |
| 10545 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10546 | 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] | 10547 | |
| 10548 | /* These inequalities will always hold even when other PAKEs are added */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10549 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10550 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10551 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10552 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10553 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10554 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10555 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10556 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10557 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10558 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10559 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10560 | PSA_PAKE_INPUT_MAX_SIZE); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10561 | } |
| 10562 | /* END_CASE */ |