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 | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1258 | /* END_HEADER */ |
| 1259 | |
| 1260 | /* BEGIN_DEPENDENCIES |
| 1261 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1262 | * END_DEPENDENCIES |
| 1263 | */ |
| 1264 | |
| 1265 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 7abdf7e | 2023-03-09 11:17:43 +0100 | [diff] [blame] | 1266 | void psa_can_do_hash() |
| 1267 | { |
| 1268 | /* We can't test that this is specific to drivers until partial init has |
| 1269 | * been implemented, but we can at least test before/after full init. */ |
| 1270 | TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE)); |
| 1271 | PSA_INIT(); |
| 1272 | TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE)); |
| 1273 | PSA_DONE(); |
| 1274 | } |
| 1275 | /* END_CASE */ |
| 1276 | |
| 1277 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1278 | void static_checks() |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1279 | { |
| 1280 | size_t max_truncated_mac_size = |
| 1281 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1282 | |
| 1283 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1284 | * encoding. The shifted mask is the maximum truncated value. The |
| 1285 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1286 | TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size); |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1287 | } |
| 1288 | /* END_CASE */ |
| 1289 | |
| 1290 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1291 | void import_with_policy(int type_arg, |
| 1292 | int usage_arg, int alg_arg, |
| 1293 | int expected_status_arg) |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1294 | { |
| 1295 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1296 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1297 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1298 | psa_key_type_t type = type_arg; |
| 1299 | psa_key_usage_t usage = usage_arg; |
| 1300 | psa_algorithm_t alg = alg_arg; |
| 1301 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1302 | const uint8_t key_material[16] = { 0 }; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1303 | psa_status_t status; |
| 1304 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1305 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1306 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1307 | psa_set_key_type(&attributes, type); |
| 1308 | psa_set_key_usage_flags(&attributes, usage); |
| 1309 | psa_set_key_algorithm(&attributes, alg); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1310 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1311 | status = psa_import_key(&attributes, |
| 1312 | key_material, sizeof(key_material), |
| 1313 | &key); |
| 1314 | TEST_EQUAL(status, expected_status); |
| 1315 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1316 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1317 | } |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1318 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1319 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1320 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1321 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), |
| 1322 | mbedtls_test_update_key_usage_flags(usage)); |
| 1323 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 1324 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1325 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1326 | PSA_ASSERT(psa_destroy_key(key)); |
| 1327 | test_operations_on_invalid_key(key); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1328 | |
| 1329 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1330 | /* |
| 1331 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1332 | * thus reset them as required. |
| 1333 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1334 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1335 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1336 | psa_destroy_key(key); |
| 1337 | PSA_DONE(); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1338 | } |
| 1339 | /* END_CASE */ |
| 1340 | |
| 1341 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1342 | void import_with_data(data_t *data, int type_arg, |
| 1343 | int attr_bits_arg, |
| 1344 | int expected_status_arg) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1345 | { |
| 1346 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1347 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1348 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1349 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1350 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1351 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1352 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1354 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1356 | psa_set_key_type(&attributes, type); |
| 1357 | psa_set_key_bits(&attributes, attr_bits); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1358 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1359 | status = psa_import_key(&attributes, data->x, data->len, &key); |
Manuel Pégourié-Gonnard | 0509b58 | 2023-08-08 12:47:56 +0200 | [diff] [blame] | 1360 | /* When expecting INVALID_ARGUMENT, also accept NOT_SUPPORTED. |
| 1361 | * |
| 1362 | * This can happen with a type supported only by a driver: |
| 1363 | * - the driver sees the invalid data (for example wrong size) and thinks |
| 1364 | * "well perhaps this is a key size I don't support" so it returns |
| 1365 | * NOT_SUPPORTED which is correct at this point; |
| 1366 | * - we fallback to built-ins, which don't support this type, so return |
| 1367 | * NOT_SUPPORTED which again is correct at this point. |
| 1368 | */ |
| 1369 | if (expected_status == PSA_ERROR_INVALID_ARGUMENT && |
| 1370 | status == PSA_ERROR_NOT_SUPPORTED) { |
| 1371 | ; // OK |
| 1372 | } else { |
| 1373 | TEST_EQUAL(status, expected_status); |
| 1374 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1375 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1376 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1377 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1378 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1379 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1380 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1381 | if (attr_bits != 0) { |
| 1382 | TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes)); |
| 1383 | } |
| 1384 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1386 | PSA_ASSERT(psa_destroy_key(key)); |
| 1387 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1388 | |
| 1389 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1390 | /* |
| 1391 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1392 | * thus reset them as required. |
| 1393 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1394 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1395 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1396 | psa_destroy_key(key); |
| 1397 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1398 | } |
| 1399 | /* END_CASE */ |
| 1400 | |
| 1401 | /* BEGIN_CASE */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1402 | /* Construct and attempt to import a large unstructured key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1403 | void import_large_key(int type_arg, int byte_size_arg, |
| 1404 | int expected_status_arg) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1405 | { |
| 1406 | psa_key_type_t type = type_arg; |
| 1407 | size_t byte_size = byte_size_arg; |
| 1408 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1409 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1410 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1411 | psa_status_t status; |
| 1412 | uint8_t *buffer = NULL; |
| 1413 | size_t buffer_size = byte_size + 1; |
| 1414 | size_t n; |
| 1415 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1416 | /* Skip the test case if the target running the test cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1417 | * accommodate large keys due to heap size constraints */ |
Tom Cosgrove | 412a813 | 2023-07-20 16:55:14 +0100 | [diff] [blame] | 1418 | TEST_CALLOC_OR_SKIP(buffer, buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1419 | memset(buffer, 'K', byte_size); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1420 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1421 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1422 | |
| 1423 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1424 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1425 | psa_set_key_type(&attributes, type); |
| 1426 | status = psa_import_key(&attributes, buffer, byte_size, &key); |
| 1427 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 1428 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1429 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1430 | if (status == PSA_SUCCESS) { |
| 1431 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1432 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 1433 | TEST_EQUAL(psa_get_key_bits(&attributes), |
| 1434 | PSA_BYTES_TO_BITS(byte_size)); |
| 1435 | ASSERT_NO_SLOT_NUMBER(&attributes); |
| 1436 | memset(buffer, 0, byte_size + 1); |
| 1437 | PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n)); |
| 1438 | for (n = 0; n < byte_size; n++) { |
| 1439 | TEST_EQUAL(buffer[n], 'K'); |
| 1440 | } |
| 1441 | for (n = byte_size; n < buffer_size; n++) { |
| 1442 | TEST_EQUAL(buffer[n], 0); |
| 1443 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1447 | /* |
| 1448 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1449 | * thus reset them as required. |
| 1450 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1451 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1452 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1453 | psa_destroy_key(key); |
| 1454 | PSA_DONE(); |
| 1455 | mbedtls_free(buffer); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1456 | } |
| 1457 | /* END_CASE */ |
| 1458 | |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 1459 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1460 | /* Import an RSA key with a valid structure (but not valid numbers |
| 1461 | * inside, beyond having sensible size and parity). This is expected to |
| 1462 | * fail for large keys. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1463 | 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] | 1464 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1465 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1466 | size_t bits = bits_arg; |
| 1467 | psa_status_t expected_status = expected_status_arg; |
| 1468 | psa_status_t status; |
| 1469 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1470 | 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] | 1471 | size_t buffer_size = /* Slight overapproximations */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1472 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1473 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1474 | unsigned char *p; |
| 1475 | int ret; |
| 1476 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1477 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1478 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1479 | PSA_ASSERT(psa_crypto_init()); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1480 | TEST_CALLOC(buffer, buffer_size); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1481 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1482 | TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p, |
| 1483 | bits, keypair)) >= 0); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1484 | length = ret; |
| 1485 | |
| 1486 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1487 | psa_set_key_type(&attributes, type); |
| 1488 | status = psa_import_key(&attributes, p, length, &key); |
| 1489 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1490 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1491 | if (status == PSA_SUCCESS) { |
| 1492 | PSA_ASSERT(psa_destroy_key(key)); |
| 1493 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1494 | |
| 1495 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1496 | mbedtls_free(buffer); |
| 1497 | PSA_DONE(); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1498 | } |
| 1499 | /* END_CASE */ |
| 1500 | |
| 1501 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1502 | void import_export(data_t *data, |
| 1503 | int type_arg, |
| 1504 | int usage_arg, int alg_arg, |
| 1505 | int lifetime_arg, |
| 1506 | int expected_bits, |
| 1507 | int export_size_delta, |
| 1508 | int expected_export_status_arg, |
| 1509 | /*whether reexport must give the original input exactly*/ |
| 1510 | int canonical_input) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1511 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1512 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1513 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1514 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1515 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1516 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1517 | psa_key_lifetime_t lifetime = lifetime_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1518 | unsigned char *exported = NULL; |
| 1519 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1520 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1521 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1522 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1523 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1524 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1525 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1526 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1527 | TEST_CALLOC(exported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1528 | if (!canonical_input) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1529 | TEST_CALLOC(reexported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1530 | } |
| 1531 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1532 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1533 | psa_set_key_lifetime(&attributes, lifetime); |
| 1534 | psa_set_key_usage_flags(&attributes, usage_arg); |
| 1535 | psa_set_key_algorithm(&attributes, alg); |
| 1536 | psa_set_key_type(&attributes, type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1537 | |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1538 | if (PSA_KEY_TYPE_IS_DH(type) && |
| 1539 | expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) { |
Przemek Stekiel | 654bef0 | 2022-12-15 13:28:02 +0100 | [diff] [blame] | 1540 | /* Simulate that buffer is too small, by decreasing its size by 1 byte. */ |
| 1541 | export_size -= 1; |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1542 | } |
| 1543 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1544 | /* Import the key */ |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1545 | TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key), |
Przemek Stekiel | 2e7c33d | 2023-04-27 12:29:45 +0200 | [diff] [blame] | 1546 | PSA_SUCCESS); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1547 | |
| 1548 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1549 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1550 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1551 | TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits); |
| 1552 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1553 | |
| 1554 | /* Export the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1555 | status = psa_export_key(key, exported, export_size, &exported_length); |
| 1556 | TEST_EQUAL(status, expected_export_status); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1557 | |
| 1558 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1559 | * and export_size. On errors, the exported length must be 0. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1560 | TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH); |
| 1561 | TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0); |
| 1562 | TEST_LE_U(exported_length, export_size); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1563 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1564 | TEST_ASSERT(mem_is_char(exported + exported_length, 0, |
| 1565 | export_size - exported_length)); |
| 1566 | if (status != PSA_SUCCESS) { |
| 1567 | TEST_EQUAL(exported_length, 0); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1568 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1569 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1570 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1571 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1572 | * this validates the canonical representations. For canonical inputs, |
| 1573 | * this doesn't directly validate the implementation, but it still helps |
| 1574 | * by cross-validating the test data with the sanity check code. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1575 | if (!psa_key_lifetime_is_external(lifetime)) { |
| 1576 | if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) { |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1577 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1578 | } |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1579 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1580 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1581 | if (canonical_input) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1582 | TEST_MEMORY_COMPARE(data->x, data->len, exported, exported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1583 | } else { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1584 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1585 | PSA_ASSERT(psa_import_key(&attributes, exported, exported_length, |
| 1586 | &key2)); |
| 1587 | PSA_ASSERT(psa_export_key(key2, |
| 1588 | reexported, |
| 1589 | export_size, |
| 1590 | &reexported_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1591 | TEST_MEMORY_COMPARE(exported, exported_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1592 | reexported, reexported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1593 | PSA_ASSERT(psa_destroy_key(key2)); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1594 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1595 | TEST_LE_U(exported_length, |
| 1596 | PSA_EXPORT_KEY_OUTPUT_SIZE(type, |
| 1597 | psa_get_key_bits(&got_attributes))); |
Valerio Setti | 1eacae8 | 2023-07-28 16:07:03 +0200 | [diff] [blame] | 1598 | if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 1599 | TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
| 1600 | } else if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) { |
| 1601 | TEST_LE_U(exported_length, PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
| 1602 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1603 | |
| 1604 | destroy: |
| 1605 | /* Destroy the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1606 | PSA_ASSERT(psa_destroy_key(key)); |
| 1607 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1608 | |
| 1609 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1610 | /* |
| 1611 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1612 | * thus reset them as required. |
| 1613 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1614 | psa_reset_key_attributes(&got_attributes); |
| 1615 | psa_destroy_key(key); |
| 1616 | mbedtls_free(exported); |
| 1617 | mbedtls_free(reexported); |
| 1618 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1619 | } |
| 1620 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1621 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1622 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1623 | void import_export_public_key(data_t *data, |
| 1624 | int type_arg, // key pair or public key |
| 1625 | int alg_arg, |
| 1626 | int lifetime_arg, |
| 1627 | int export_size_delta, |
| 1628 | int expected_export_status_arg, |
| 1629 | data_t *expected_public_key) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1630 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1631 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1632 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1633 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1634 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1635 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1636 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1637 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1638 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1639 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1640 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1642 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1643 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1644 | psa_set_key_lifetime(&attributes, lifetime); |
| 1645 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1646 | psa_set_key_algorithm(&attributes, alg); |
| 1647 | psa_set_key_type(&attributes, type); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1648 | |
| 1649 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1650 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1651 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1652 | /* Export the public key */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1653 | TEST_CALLOC(exported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1654 | status = psa_export_public_key(key, |
| 1655 | exported, export_size, |
| 1656 | &exported_length); |
| 1657 | TEST_EQUAL(status, expected_export_status); |
| 1658 | if (status == PSA_SUCCESS) { |
| 1659 | 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] | 1660 | size_t bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1661 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1662 | bits = psa_get_key_bits(&attributes); |
| 1663 | TEST_LE_U(expected_public_key->len, |
| 1664 | PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1665 | TEST_LE_U(expected_public_key->len, |
| 1666 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1667 | TEST_LE_U(expected_public_key->len, |
| 1668 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1669 | TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1670 | exported, exported_length); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1671 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1672 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1673 | /* |
| 1674 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1675 | * thus reset them as required. |
| 1676 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1677 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1678 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1679 | mbedtls_free(exported); |
| 1680 | psa_destroy_key(key); |
| 1681 | PSA_DONE(); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1682 | } |
| 1683 | /* END_CASE */ |
| 1684 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1685 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1686 | void import_and_exercise_key(data_t *data, |
| 1687 | int type_arg, |
| 1688 | int bits_arg, |
| 1689 | int alg_arg) |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1690 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1691 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1692 | psa_key_type_t type = type_arg; |
| 1693 | size_t bits = bits_arg; |
| 1694 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1695 | 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] | 1696 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1697 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1698 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1699 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1700 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1701 | psa_set_key_usage_flags(&attributes, usage); |
| 1702 | psa_set_key_algorithm(&attributes, alg); |
| 1703 | psa_set_key_type(&attributes, type); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1704 | |
| 1705 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1706 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1707 | |
| 1708 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1709 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1710 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1711 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1712 | |
| 1713 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1714 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1715 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1716 | } |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1717 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1718 | PSA_ASSERT(psa_destroy_key(key)); |
| 1719 | test_operations_on_invalid_key(key); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1720 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1721 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1722 | /* |
| 1723 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1724 | * thus reset them as required. |
| 1725 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1726 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1727 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1728 | psa_reset_key_attributes(&attributes); |
| 1729 | psa_destroy_key(key); |
| 1730 | PSA_DONE(); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1731 | } |
| 1732 | /* END_CASE */ |
| 1733 | |
| 1734 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1735 | void effective_key_attributes(int type_arg, int expected_type_arg, |
| 1736 | int bits_arg, int expected_bits_arg, |
| 1737 | int usage_arg, int expected_usage_arg, |
| 1738 | int alg_arg, int expected_alg_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1739 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1740 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1741 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1742 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1743 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1744 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1745 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1746 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1747 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1748 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1749 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1750 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1751 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1752 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1753 | psa_set_key_usage_flags(&attributes, usage); |
| 1754 | psa_set_key_algorithm(&attributes, alg); |
| 1755 | psa_set_key_type(&attributes, key_type); |
| 1756 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1757 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1758 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
| 1759 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1760 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1761 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1762 | TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type); |
| 1763 | TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits); |
| 1764 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
| 1765 | TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1766 | |
| 1767 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1768 | /* |
| 1769 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1770 | * thus reset them as required. |
| 1771 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1772 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1773 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1774 | psa_destroy_key(key); |
| 1775 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1776 | } |
| 1777 | /* END_CASE */ |
| 1778 | |
| 1779 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1780 | void check_key_policy(int type_arg, int bits_arg, |
| 1781 | int usage_arg, int alg_arg) |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1782 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1783 | test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg, |
| 1784 | usage_arg, |
| 1785 | mbedtls_test_update_key_usage_flags(usage_arg), |
| 1786 | alg_arg, alg_arg); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1787 | goto exit; |
| 1788 | } |
| 1789 | /* END_CASE */ |
| 1790 | |
| 1791 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1792 | void key_attributes_init() |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1793 | { |
| 1794 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1795 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1796 | * 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] | 1797 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1798 | psa_key_attributes_t func = psa_key_attributes_init(); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1799 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1800 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1801 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1802 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1803 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1804 | TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE); |
| 1805 | TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE); |
| 1806 | TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1807 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1808 | TEST_EQUAL(psa_get_key_type(&func), 0); |
| 1809 | TEST_EQUAL(psa_get_key_type(&init), 0); |
| 1810 | TEST_EQUAL(psa_get_key_type(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1811 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1812 | TEST_EQUAL(psa_get_key_bits(&func), 0); |
| 1813 | TEST_EQUAL(psa_get_key_bits(&init), 0); |
| 1814 | TEST_EQUAL(psa_get_key_bits(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1815 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1816 | TEST_EQUAL(psa_get_key_usage_flags(&func), 0); |
| 1817 | TEST_EQUAL(psa_get_key_usage_flags(&init), 0); |
| 1818 | TEST_EQUAL(psa_get_key_usage_flags(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1819 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1820 | TEST_EQUAL(psa_get_key_algorithm(&func), 0); |
| 1821 | TEST_EQUAL(psa_get_key_algorithm(&init), 0); |
| 1822 | TEST_EQUAL(psa_get_key_algorithm(&zero), 0); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1823 | } |
| 1824 | /* END_CASE */ |
| 1825 | |
| 1826 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1827 | void mac_key_policy(int policy_usage_arg, |
| 1828 | int policy_alg_arg, |
| 1829 | int key_type_arg, |
| 1830 | data_t *key_data, |
| 1831 | int exercise_alg_arg, |
| 1832 | int expected_status_sign_arg, |
| 1833 | int expected_status_verify_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1834 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1835 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1836 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1837 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1838 | psa_key_type_t key_type = key_type_arg; |
| 1839 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 1840 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 1841 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1842 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1843 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 1844 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1845 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1846 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1847 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1848 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1849 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1850 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1851 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1852 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1853 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1854 | &key)); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1855 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1856 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 1857 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1858 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1859 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 1860 | TEST_EQUAL(status, expected_status_sign); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1861 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1862 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1863 | uint8_t input[128] = { 0 }; |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1864 | size_t mac_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1865 | TEST_EQUAL(psa_mac_compute(key, exercise_alg, |
| 1866 | input, 128, |
| 1867 | mac, PSA_MAC_MAX_SIZE, &mac_len), |
| 1868 | expected_status_sign); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1869 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1870 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1871 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 1872 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 1873 | if (status == PSA_SUCCESS) { |
| 1874 | status = psa_mac_update(&operation, input, 128); |
| 1875 | if (status == PSA_SUCCESS) { |
| 1876 | TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE, |
| 1877 | &mac_len), |
| 1878 | expected_status_sign); |
| 1879 | } else { |
| 1880 | TEST_EQUAL(status, expected_status_sign); |
| 1881 | } |
| 1882 | } else { |
| 1883 | TEST_EQUAL(status, expected_status_sign); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1884 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1885 | PSA_ASSERT(psa_mac_abort(&operation)); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1886 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1887 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1888 | status = psa_mac_verify(key, exercise_alg, input, 128, |
| 1889 | mac, mac_len); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1890 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1891 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 1892 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1893 | } else { |
| 1894 | TEST_EQUAL(status, expected_status_verify); |
| 1895 | } |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1896 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1897 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1898 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 1899 | if (status == PSA_SUCCESS) { |
| 1900 | status = psa_mac_update(&operation, input, 128); |
| 1901 | if (status == PSA_SUCCESS) { |
| 1902 | status = psa_mac_verify_finish(&operation, mac, mac_len); |
| 1903 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 1904 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1905 | } else { |
| 1906 | TEST_EQUAL(status, expected_status_verify); |
| 1907 | } |
| 1908 | } else { |
| 1909 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1910 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1911 | } else { |
| 1912 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1913 | } |
| 1914 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1915 | psa_mac_abort(&operation); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1916 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1917 | memset(mac, 0, sizeof(mac)); |
| 1918 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 1919 | TEST_EQUAL(status, expected_status_verify); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1920 | |
| 1921 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1922 | psa_mac_abort(&operation); |
| 1923 | psa_destroy_key(key); |
| 1924 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1925 | } |
| 1926 | /* END_CASE */ |
| 1927 | |
| 1928 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1929 | void cipher_key_policy(int policy_usage_arg, |
| 1930 | int policy_alg, |
| 1931 | int key_type, |
| 1932 | data_t *key_data, |
| 1933 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1934 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1935 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1936 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1937 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1938 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1939 | size_t output_buffer_size = 0; |
| 1940 | size_t input_buffer_size = 0; |
| 1941 | size_t output_length = 0; |
| 1942 | uint8_t *output = NULL; |
| 1943 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1944 | psa_status_t status; |
| 1945 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1946 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg); |
| 1947 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg, |
| 1948 | input_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1949 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1950 | TEST_CALLOC(input, input_buffer_size); |
| 1951 | TEST_CALLOC(output, output_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1952 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1953 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1954 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1955 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1956 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1957 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1958 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1959 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1960 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1961 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1962 | /* Check if no key usage flag implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1963 | TEST_EQUAL(policy_usage, |
| 1964 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1965 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1966 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1967 | status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size, |
| 1968 | output, output_buffer_size, |
| 1969 | &output_length); |
| 1970 | if (policy_alg == exercise_alg && |
| 1971 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1972 | PSA_ASSERT(status); |
| 1973 | } else { |
| 1974 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1975 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1976 | |
| 1977 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1978 | status = psa_cipher_encrypt_setup(&operation, key, exercise_alg); |
| 1979 | if (policy_alg == exercise_alg && |
| 1980 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1981 | PSA_ASSERT(status); |
| 1982 | } else { |
| 1983 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1984 | } |
| 1985 | psa_cipher_abort(&operation); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1986 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1987 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1988 | status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size, |
| 1989 | input, input_buffer_size, |
| 1990 | &output_length); |
| 1991 | if (policy_alg == exercise_alg && |
| 1992 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 1993 | PSA_ASSERT(status); |
| 1994 | } else { |
| 1995 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1996 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1997 | |
| 1998 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1999 | status = psa_cipher_decrypt_setup(&operation, key, exercise_alg); |
| 2000 | if (policy_alg == exercise_alg && |
| 2001 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2002 | PSA_ASSERT(status); |
| 2003 | } else { |
| 2004 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2005 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2006 | |
| 2007 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2008 | psa_cipher_abort(&operation); |
| 2009 | mbedtls_free(input); |
| 2010 | mbedtls_free(output); |
| 2011 | psa_destroy_key(key); |
| 2012 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2013 | } |
| 2014 | /* END_CASE */ |
| 2015 | |
| 2016 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2017 | void aead_key_policy(int policy_usage_arg, |
| 2018 | int policy_alg, |
| 2019 | int key_type, |
| 2020 | data_t *key_data, |
| 2021 | int nonce_length_arg, |
| 2022 | int tag_length_arg, |
| 2023 | int exercise_alg, |
| 2024 | int expected_status_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2025 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2026 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2027 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2028 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2029 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2030 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2031 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2032 | unsigned char nonce[16] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2033 | size_t nonce_length = nonce_length_arg; |
| 2034 | unsigned char tag[16]; |
| 2035 | size_t tag_length = tag_length_arg; |
| 2036 | size_t output_length; |
| 2037 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2038 | TEST_LE_U(nonce_length, sizeof(nonce)); |
| 2039 | TEST_LE_U(tag_length, sizeof(tag)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2040 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2041 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2042 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2043 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2044 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2045 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2046 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2047 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2048 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2049 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2050 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2051 | TEST_EQUAL(policy_usage, |
| 2052 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2053 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2054 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2055 | status = psa_aead_encrypt(key, exercise_alg, |
| 2056 | nonce, nonce_length, |
| 2057 | NULL, 0, |
| 2058 | NULL, 0, |
| 2059 | tag, tag_length, |
| 2060 | &output_length); |
| 2061 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2062 | TEST_EQUAL(status, expected_status); |
| 2063 | } else { |
| 2064 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2065 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2066 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2067 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2068 | status = psa_aead_encrypt_setup(&operation, key, exercise_alg); |
| 2069 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2070 | TEST_EQUAL(status, expected_status); |
| 2071 | } else { |
| 2072 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2073 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2074 | |
| 2075 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2076 | memset(tag, 0, sizeof(tag)); |
| 2077 | status = psa_aead_decrypt(key, exercise_alg, |
| 2078 | nonce, nonce_length, |
| 2079 | NULL, 0, |
| 2080 | tag, tag_length, |
| 2081 | NULL, 0, |
| 2082 | &output_length); |
| 2083 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2084 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2085 | } else if (expected_status == PSA_SUCCESS) { |
| 2086 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2087 | } else { |
| 2088 | TEST_EQUAL(status, expected_status); |
| 2089 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2090 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2091 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2092 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2093 | status = psa_aead_decrypt_setup(&operation, key, exercise_alg); |
| 2094 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2095 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2096 | } else { |
| 2097 | TEST_EQUAL(status, expected_status); |
| 2098 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2099 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2100 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2101 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2102 | psa_destroy_key(key); |
| 2103 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2104 | } |
| 2105 | /* END_CASE */ |
| 2106 | |
| 2107 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2108 | void asymmetric_encryption_key_policy(int policy_usage_arg, |
| 2109 | int policy_alg, |
| 2110 | int key_type, |
| 2111 | data_t *key_data, |
Valerio Setti | f202c29 | 2024-01-15 10:42:37 +0100 | [diff] [blame] | 2112 | int exercise_alg, |
| 2113 | int use_opaque_key) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2114 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2115 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2116 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2117 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2118 | psa_status_t status; |
| 2119 | size_t key_bits; |
| 2120 | size_t buffer_length; |
| 2121 | unsigned char *buffer = NULL; |
| 2122 | size_t output_length; |
| 2123 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2124 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2125 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2126 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2127 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2128 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2129 | |
Valerio Setti | f202c29 | 2024-01-15 10:42:37 +0100 | [diff] [blame] | 2130 | if (use_opaque_key) { |
| 2131 | psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION( |
| 2132 | PSA_KEY_PERSISTENCE_VOLATILE, TEST_DRIVER_LOCATION)); |
| 2133 | } |
| 2134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2135 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2136 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2137 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2138 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2139 | TEST_EQUAL(policy_usage, |
| 2140 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2141 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2142 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 2143 | key_bits = psa_get_key_bits(&attributes); |
| 2144 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, |
| 2145 | exercise_alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2146 | TEST_CALLOC(buffer, buffer_length); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2147 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2148 | status = psa_asymmetric_encrypt(key, exercise_alg, |
| 2149 | NULL, 0, |
| 2150 | NULL, 0, |
| 2151 | buffer, buffer_length, |
| 2152 | &output_length); |
| 2153 | if (policy_alg == exercise_alg && |
| 2154 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2155 | PSA_ASSERT(status); |
| 2156 | } else { |
| 2157 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2158 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2159 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2160 | if (buffer_length != 0) { |
| 2161 | memset(buffer, 0, buffer_length); |
| 2162 | } |
| 2163 | status = psa_asymmetric_decrypt(key, exercise_alg, |
| 2164 | buffer, buffer_length, |
| 2165 | NULL, 0, |
| 2166 | buffer, buffer_length, |
| 2167 | &output_length); |
| 2168 | if (policy_alg == exercise_alg && |
| 2169 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2170 | TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING); |
| 2171 | } else { |
| 2172 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2173 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2174 | |
| 2175 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2176 | /* |
| 2177 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2178 | * thus reset them as required. |
| 2179 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2180 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2181 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2182 | psa_destroy_key(key); |
| 2183 | PSA_DONE(); |
| 2184 | mbedtls_free(buffer); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2185 | } |
| 2186 | /* END_CASE */ |
| 2187 | |
| 2188 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2189 | void asymmetric_signature_key_policy(int policy_usage_arg, |
| 2190 | int policy_alg, |
| 2191 | int key_type, |
| 2192 | data_t *key_data, |
| 2193 | int exercise_alg, |
| 2194 | int payload_length_arg, |
| 2195 | int expected_usage_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2196 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2197 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2198 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2199 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 2200 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2201 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2202 | unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 }; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2203 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2204 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2205 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2206 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2207 | int compatible_alg = payload_length_arg > 0; |
| 2208 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2209 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2210 | size_t signature_length; |
| 2211 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2212 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2213 | in the expected usage flags. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2214 | TEST_EQUAL(expected_usage, |
| 2215 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2216 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2217 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2218 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2219 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2220 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2221 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2222 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2223 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2224 | &key)); |
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 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2227 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2228 | status = psa_sign_hash(key, exercise_alg, |
| 2229 | payload, payload_length, |
| 2230 | signature, sizeof(signature), |
| 2231 | &signature_length); |
| 2232 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 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 | memset(signature, 0, sizeof(signature)); |
| 2239 | status = psa_verify_hash(key, exercise_alg, |
| 2240 | payload, payload_length, |
| 2241 | signature, sizeof(signature)); |
| 2242 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) { |
| 2243 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2244 | } else { |
| 2245 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2246 | } |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2247 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2248 | if (PSA_ALG_IS_SIGN_HASH(exercise_alg) && |
| 2249 | PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) { |
| 2250 | status = psa_sign_message(key, exercise_alg, |
| 2251 | payload, payload_length, |
| 2252 | signature, sizeof(signature), |
| 2253 | &signature_length); |
| 2254 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) { |
| 2255 | PSA_ASSERT(status); |
| 2256 | } else { |
| 2257 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2258 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2259 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2260 | memset(signature, 0, sizeof(signature)); |
| 2261 | status = psa_verify_message(key, exercise_alg, |
| 2262 | payload, payload_length, |
| 2263 | signature, sizeof(signature)); |
| 2264 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) { |
| 2265 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2266 | } else { |
| 2267 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2268 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2269 | } |
| 2270 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2271 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2272 | psa_destroy_key(key); |
| 2273 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2274 | } |
| 2275 | /* END_CASE */ |
| 2276 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2277 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2278 | void derive_key_policy(int policy_usage, |
| 2279 | int policy_alg, |
| 2280 | int key_type, |
| 2281 | data_t *key_data, |
| 2282 | int exercise_alg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2283 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2284 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2285 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2286 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2287 | psa_status_t status; |
| 2288 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2289 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2290 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2291 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2292 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2293 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2294 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2295 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2296 | &key)); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2297 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2298 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2299 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2300 | if (PSA_ALG_IS_TLS12_PRF(exercise_alg) || |
| 2301 | PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) { |
| 2302 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 2303 | &operation, |
| 2304 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2305 | (const uint8_t *) "", 0)); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2306 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2307 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2308 | status = psa_key_derivation_input_key(&operation, |
| 2309 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 2310 | key); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2311 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2312 | if (policy_alg == exercise_alg && |
| 2313 | (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) { |
| 2314 | PSA_ASSERT(status); |
| 2315 | } else { |
| 2316 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2317 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2318 | |
| 2319 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2320 | psa_key_derivation_abort(&operation); |
| 2321 | psa_destroy_key(key); |
| 2322 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2323 | } |
| 2324 | /* END_CASE */ |
| 2325 | |
| 2326 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2327 | void agreement_key_policy(int policy_usage, |
| 2328 | int policy_alg, |
| 2329 | int key_type_arg, |
| 2330 | data_t *key_data, |
| 2331 | int exercise_alg, |
| 2332 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2333 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2334 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2335 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2336 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2337 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2338 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2339 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2340 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2341 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2342 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2343 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2344 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2345 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2346 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2347 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2348 | &key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2349 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2350 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
| 2351 | status = mbedtls_test_psa_key_agreement_with_self(&operation, key); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2353 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2354 | |
| 2355 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2356 | psa_key_derivation_abort(&operation); |
| 2357 | psa_destroy_key(key); |
| 2358 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2359 | } |
| 2360 | /* END_CASE */ |
| 2361 | |
| 2362 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2363 | void key_policy_alg2(int key_type_arg, data_t *key_data, |
| 2364 | int usage_arg, int alg_arg, int alg2_arg) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2365 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2366 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2367 | psa_key_type_t key_type = key_type_arg; |
| 2368 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2369 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2370 | psa_key_usage_t usage = usage_arg; |
| 2371 | psa_algorithm_t alg = alg_arg; |
| 2372 | psa_algorithm_t alg2 = alg2_arg; |
| 2373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2374 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2375 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2376 | psa_set_key_usage_flags(&attributes, usage); |
| 2377 | psa_set_key_algorithm(&attributes, alg); |
| 2378 | psa_set_key_enrollment_algorithm(&attributes, alg2); |
| 2379 | psa_set_key_type(&attributes, key_type); |
| 2380 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2381 | &key)); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2382 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2383 | /* Update the usage flags to obtain implicit usage flags */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2384 | usage = mbedtls_test_update_key_usage_flags(usage); |
| 2385 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 2386 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage); |
| 2387 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 2388 | TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2389 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2390 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2391 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2392 | } |
| 2393 | if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2394 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2395 | } |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2396 | |
| 2397 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2398 | /* |
| 2399 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2400 | * thus reset them as required. |
| 2401 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2402 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2403 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2404 | psa_destroy_key(key); |
| 2405 | PSA_DONE(); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2406 | } |
| 2407 | /* END_CASE */ |
| 2408 | |
| 2409 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2410 | void raw_agreement_key_policy(int policy_usage, |
| 2411 | int policy_alg, |
| 2412 | int key_type_arg, |
| 2413 | data_t *key_data, |
| 2414 | int exercise_alg, |
| 2415 | int expected_status_arg) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2416 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2417 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2418 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2419 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2420 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2421 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2422 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2423 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2424 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2425 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2426 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2427 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2428 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2429 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2430 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2431 | &key)); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2433 | status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2434 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2435 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2436 | |
| 2437 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2438 | psa_key_derivation_abort(&operation); |
| 2439 | psa_destroy_key(key); |
| 2440 | PSA_DONE(); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2441 | } |
| 2442 | /* END_CASE */ |
| 2443 | |
| 2444 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2445 | void copy_success(int source_usage_arg, |
| 2446 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4ea4ad0 | 2022-12-04 15:11:00 +0100 | [diff] [blame] | 2447 | int source_lifetime_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2448 | int type_arg, data_t *material, |
| 2449 | int copy_attributes, |
| 2450 | int target_usage_arg, |
| 2451 | int target_alg_arg, int target_alg2_arg, |
Gilles Peskine | 4ea4ad0 | 2022-12-04 15:11:00 +0100 | [diff] [blame] | 2452 | int target_lifetime_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2453 | int expected_usage_arg, |
| 2454 | int expected_alg_arg, int expected_alg2_arg) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2455 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2456 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2457 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2458 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2459 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2460 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2461 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 2462 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2463 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2464 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2465 | uint8_t *export_buffer = NULL; |
| 2466 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2467 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2468 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2469 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2470 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2471 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2472 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2473 | psa_set_key_type(&source_attributes, type_arg); |
| 2474 | psa_set_key_lifetime(&source_attributes, source_lifetime); |
| 2475 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2476 | material->x, material->len, |
| 2477 | &source_key)); |
| 2478 | PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2479 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2480 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2481 | if (copy_attributes) { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2482 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2483 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2484 | psa_set_key_lifetime(&target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2485 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2486 | if (target_usage_arg != -1) { |
| 2487 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2488 | } |
| 2489 | if (target_alg_arg != -1) { |
| 2490 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2491 | } |
| 2492 | if (target_alg2_arg != -1) { |
| 2493 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
| 2494 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2495 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2496 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2497 | /* Copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2498 | PSA_ASSERT(psa_copy_key(source_key, |
| 2499 | &target_attributes, &target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2500 | |
| 2501 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2502 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2503 | |
| 2504 | /* Test that the target slot has the expected content and policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2505 | PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes)); |
| 2506 | TEST_EQUAL(psa_get_key_type(&source_attributes), |
| 2507 | psa_get_key_type(&target_attributes)); |
| 2508 | TEST_EQUAL(psa_get_key_bits(&source_attributes), |
| 2509 | psa_get_key_bits(&target_attributes)); |
| 2510 | TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes)); |
| 2511 | TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes)); |
| 2512 | TEST_EQUAL(expected_alg2, |
| 2513 | psa_get_key_enrollment_algorithm(&target_attributes)); |
| 2514 | if (expected_usage & PSA_KEY_USAGE_EXPORT) { |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2515 | size_t length; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2516 | TEST_CALLOC(export_buffer, material->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2517 | PSA_ASSERT(psa_export_key(target_key, export_buffer, |
| 2518 | material->len, &length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2519 | TEST_MEMORY_COMPARE(material->x, material->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2520 | export_buffer, length); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2521 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2522 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2523 | if (!psa_key_lifetime_is_external(target_lifetime)) { |
| 2524 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2525 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2526 | } |
| 2527 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2528 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2529 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2530 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2531 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2532 | PSA_ASSERT(psa_destroy_key(target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2533 | |
| 2534 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2535 | /* |
| 2536 | * Source and target key attributes may have been returned by |
| 2537 | * psa_get_key_attributes() thus reset them as required. |
| 2538 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2539 | psa_reset_key_attributes(&source_attributes); |
| 2540 | psa_reset_key_attributes(&target_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2541 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2542 | PSA_DONE(); |
| 2543 | mbedtls_free(export_buffer); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2544 | } |
| 2545 | /* END_CASE */ |
| 2546 | |
| 2547 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2548 | void copy_fail(int source_usage_arg, |
| 2549 | int source_alg_arg, int source_alg2_arg, |
| 2550 | int source_lifetime_arg, |
| 2551 | int type_arg, data_t *material, |
| 2552 | int target_type_arg, int target_bits_arg, |
| 2553 | int target_usage_arg, |
| 2554 | int target_alg_arg, int target_alg2_arg, |
| 2555 | int target_id_arg, int target_lifetime_arg, |
| 2556 | int expected_status_arg) |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2557 | { |
| 2558 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2559 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2560 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2561 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2562 | 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] | 2563 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2564 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2565 | |
| 2566 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2567 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2568 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2569 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2570 | psa_set_key_type(&source_attributes, type_arg); |
| 2571 | psa_set_key_lifetime(&source_attributes, source_lifetime_arg); |
| 2572 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2573 | material->x, material->len, |
| 2574 | &source_key)); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2575 | |
| 2576 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2577 | psa_set_key_id(&target_attributes, key_id); |
| 2578 | psa_set_key_lifetime(&target_attributes, target_lifetime_arg); |
| 2579 | psa_set_key_type(&target_attributes, target_type_arg); |
| 2580 | psa_set_key_bits(&target_attributes, target_bits_arg); |
| 2581 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2582 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2583 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2584 | |
| 2585 | /* Try to copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2586 | TEST_EQUAL(psa_copy_key(source_key, |
| 2587 | &target_attributes, &target_key), |
| 2588 | expected_status_arg); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2589 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2590 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2591 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2592 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2593 | psa_reset_key_attributes(&source_attributes); |
| 2594 | psa_reset_key_attributes(&target_attributes); |
| 2595 | PSA_DONE(); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2596 | } |
| 2597 | /* END_CASE */ |
| 2598 | |
| 2599 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2600 | void hash_operation_init() |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2601 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2602 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2603 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2604 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2605 | * 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] | 2606 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2607 | psa_hash_operation_t func = psa_hash_operation_init(); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2608 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2609 | psa_hash_operation_t zero; |
| 2610 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2611 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2612 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2613 | /* A freshly-initialized hash operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2614 | TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)), |
| 2615 | PSA_ERROR_BAD_STATE); |
| 2616 | TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)), |
| 2617 | PSA_ERROR_BAD_STATE); |
| 2618 | TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)), |
| 2619 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2620 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2621 | /* A default hash operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2622 | PSA_ASSERT(psa_hash_abort(&func)); |
| 2623 | PSA_ASSERT(psa_hash_abort(&init)); |
| 2624 | PSA_ASSERT(psa_hash_abort(&zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2625 | } |
| 2626 | /* END_CASE */ |
| 2627 | |
| 2628 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2629 | void hash_setup(int alg_arg, |
| 2630 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2631 | { |
| 2632 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2633 | uint8_t *output = NULL; |
| 2634 | size_t output_size = 0; |
| 2635 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2636 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2637 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2638 | psa_status_t status; |
| 2639 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2640 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2641 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2642 | /* Hash Setup, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2643 | output_size = PSA_HASH_LENGTH(alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2644 | TEST_CALLOC(output, output_size); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2645 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2646 | status = psa_hash_compute(alg, NULL, 0, |
| 2647 | output, output_size, &output_length); |
| 2648 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2649 | |
| 2650 | /* Hash Setup, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2651 | status = psa_hash_setup(&operation, alg); |
| 2652 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2653 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2654 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2655 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2656 | |
| 2657 | /* If setup failed, reproduce the failure, so as to |
| 2658 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2659 | if (status != PSA_SUCCESS) { |
| 2660 | TEST_EQUAL(psa_hash_setup(&operation, alg), status); |
| 2661 | } |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2662 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2663 | /* Now the operation object should be reusable. */ |
| 2664 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2665 | PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG)); |
| 2666 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2667 | #endif |
| 2668 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2669 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2670 | mbedtls_free(output); |
| 2671 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2672 | } |
| 2673 | /* END_CASE */ |
| 2674 | |
| 2675 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2676 | void hash_compute_fail(int alg_arg, data_t *input, |
| 2677 | int output_size_arg, int expected_status_arg) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2678 | { |
| 2679 | psa_algorithm_t alg = alg_arg; |
| 2680 | uint8_t *output = NULL; |
| 2681 | size_t output_size = output_size_arg; |
| 2682 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2683 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2684 | psa_status_t expected_status = expected_status_arg; |
| 2685 | psa_status_t status; |
| 2686 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2687 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2688 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2689 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2690 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2691 | /* Hash Compute, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2692 | status = psa_hash_compute(alg, input->x, input->len, |
| 2693 | output, output_size, &output_length); |
| 2694 | TEST_EQUAL(status, expected_status); |
| 2695 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2696 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2697 | /* Hash Compute, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2698 | status = psa_hash_setup(&operation, alg); |
| 2699 | if (status == PSA_SUCCESS) { |
| 2700 | status = psa_hash_update(&operation, input->x, input->len); |
| 2701 | if (status == PSA_SUCCESS) { |
| 2702 | status = psa_hash_finish(&operation, output, output_size, |
| 2703 | &output_length); |
| 2704 | if (status == PSA_SUCCESS) { |
| 2705 | TEST_LE_U(output_length, output_size); |
| 2706 | } else { |
| 2707 | TEST_EQUAL(status, expected_status); |
| 2708 | } |
| 2709 | } else { |
| 2710 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2711 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2712 | } else { |
| 2713 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2714 | } |
| 2715 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2716 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2717 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2718 | mbedtls_free(output); |
| 2719 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2720 | } |
| 2721 | /* END_CASE */ |
| 2722 | |
| 2723 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2724 | void hash_compare_fail(int alg_arg, data_t *input, |
| 2725 | data_t *reference_hash, |
| 2726 | int expected_status_arg) |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2727 | { |
| 2728 | psa_algorithm_t alg = alg_arg; |
| 2729 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2730 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2731 | psa_status_t status; |
| 2732 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2733 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2734 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2735 | /* Hash Compare, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2736 | status = psa_hash_compare(alg, input->x, input->len, |
| 2737 | reference_hash->x, reference_hash->len); |
| 2738 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2739 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2740 | /* Hash Compare, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2741 | status = psa_hash_setup(&operation, alg); |
| 2742 | if (status == PSA_SUCCESS) { |
| 2743 | status = psa_hash_update(&operation, input->x, input->len); |
| 2744 | if (status == PSA_SUCCESS) { |
| 2745 | status = psa_hash_verify(&operation, reference_hash->x, |
| 2746 | reference_hash->len); |
| 2747 | TEST_EQUAL(status, expected_status); |
| 2748 | } else { |
| 2749 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2750 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2751 | } else { |
| 2752 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2753 | } |
| 2754 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2755 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2756 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2757 | PSA_DONE(); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2758 | } |
| 2759 | /* END_CASE */ |
| 2760 | |
| 2761 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2762 | void hash_compute_compare(int alg_arg, data_t *input, |
| 2763 | data_t *expected_output) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2764 | { |
| 2765 | psa_algorithm_t alg = alg_arg; |
| 2766 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2767 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2768 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2769 | size_t i; |
| 2770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2771 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2772 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2773 | /* Compute with tight buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2774 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 2775 | output, PSA_HASH_LENGTH(alg), |
| 2776 | &output_length)); |
| 2777 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2778 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2779 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2780 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2781 | /* Compute with tight buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2782 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2783 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2784 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 2785 | PSA_HASH_LENGTH(alg), |
| 2786 | &output_length)); |
| 2787 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2788 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2789 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2790 | |
| 2791 | /* Compute with larger buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2792 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 2793 | output, sizeof(output), |
| 2794 | &output_length)); |
| 2795 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2796 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2797 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2798 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2799 | /* Compute with larger buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2800 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2801 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2802 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 2803 | sizeof(output), &output_length)); |
| 2804 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2805 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2806 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2807 | |
| 2808 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2809 | PSA_ASSERT(psa_hash_compare(alg, input->x, input->len, |
| 2810 | output, output_length)); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2811 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2812 | /* Compare with correct hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2813 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2814 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2815 | PSA_ASSERT(psa_hash_verify(&operation, output, |
| 2816 | output_length)); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2817 | |
| 2818 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2819 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2820 | output, output_length + 1), |
| 2821 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2822 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2823 | /* Compare with trailing garbage, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2824 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2825 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2826 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1), |
| 2827 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2828 | |
| 2829 | /* Compare with truncated hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2830 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2831 | output, output_length - 1), |
| 2832 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2833 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2834 | /* Compare with truncated hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2835 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2836 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2837 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1), |
| 2838 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2839 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2840 | /* Compare with corrupted value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2841 | for (i = 0; i < output_length; i++) { |
| 2842 | mbedtls_test_set_step(i); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2843 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2844 | |
| 2845 | /* One-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2846 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2847 | output, output_length), |
| 2848 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2849 | |
| 2850 | /* Multi-Part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2851 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2852 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2853 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length), |
| 2854 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2855 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2856 | output[i] ^= 1; |
| 2857 | } |
| 2858 | |
| 2859 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2860 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2861 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2862 | } |
| 2863 | /* END_CASE */ |
| 2864 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2865 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2866 | void hash_bad_order() |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2867 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2868 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2869 | unsigned char input[] = ""; |
| 2870 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2871 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2872 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2873 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2874 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 |
| 2875 | }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2876 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2877 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2878 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2879 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2880 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2881 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2882 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2883 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2884 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2885 | TEST_EQUAL(psa_hash_setup(&operation, alg), |
| 2886 | PSA_ERROR_BAD_STATE); |
| 2887 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2888 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2889 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2890 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2891 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2892 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2893 | PSA_ERROR_BAD_STATE); |
| 2894 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2895 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2896 | /* Check that update calls abort on error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2897 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 2898 | operation.id = UINT_MAX; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2899 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2900 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2901 | PSA_ERROR_BAD_STATE); |
| 2902 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2903 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2904 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2905 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2906 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2907 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2908 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2909 | hash, sizeof(hash), &hash_len)); |
| 2910 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2911 | PSA_ERROR_BAD_STATE); |
| 2912 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2913 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2914 | /* Call verify without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2915 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2916 | valid_hash, sizeof(valid_hash)), |
| 2917 | PSA_ERROR_BAD_STATE); |
| 2918 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2919 | |
| 2920 | /* Call verify after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2921 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2922 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2923 | hash, sizeof(hash), &hash_len)); |
| 2924 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2925 | valid_hash, sizeof(valid_hash)), |
| 2926 | PSA_ERROR_BAD_STATE); |
| 2927 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2928 | |
| 2929 | /* Call verify twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2930 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2931 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2932 | PSA_ASSERT(psa_hash_verify(&operation, |
| 2933 | valid_hash, sizeof(valid_hash))); |
| 2934 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2935 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2936 | valid_hash, sizeof(valid_hash)), |
| 2937 | PSA_ERROR_BAD_STATE); |
| 2938 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2939 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2940 | |
| 2941 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2942 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2943 | hash, sizeof(hash), &hash_len), |
| 2944 | PSA_ERROR_BAD_STATE); |
| 2945 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2946 | |
| 2947 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2948 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2949 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2950 | hash, sizeof(hash), &hash_len)); |
| 2951 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2952 | hash, sizeof(hash), &hash_len), |
| 2953 | PSA_ERROR_BAD_STATE); |
| 2954 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2955 | |
| 2956 | /* Call finish after calling verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2957 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2958 | PSA_ASSERT(psa_hash_verify(&operation, |
| 2959 | valid_hash, sizeof(valid_hash))); |
| 2960 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2961 | hash, sizeof(hash), &hash_len), |
| 2962 | PSA_ERROR_BAD_STATE); |
| 2963 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2964 | |
| 2965 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2966 | PSA_DONE(); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2967 | } |
| 2968 | /* END_CASE */ |
| 2969 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2970 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2971 | void hash_verify_bad_args() |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2972 | { |
| 2973 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2974 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2975 | * appended to it */ |
| 2976 | unsigned char hash[] = { |
| 2977 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2978 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2979 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb |
| 2980 | }; |
| 2981 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2982 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2983 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2984 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2985 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2986 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2987 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2988 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2989 | TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1), |
| 2990 | PSA_ERROR_INVALID_SIGNATURE); |
| 2991 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2992 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2993 | ASSERT_OPERATION_IS_INACTIVE(operation); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2994 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2995 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2996 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2997 | TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size), |
| 2998 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2999 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3000 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3001 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3002 | TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)), |
| 3003 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 3004 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3005 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3006 | PSA_DONE(); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3007 | } |
| 3008 | /* END_CASE */ |
| 3009 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3010 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3011 | void hash_finish_bad_args() |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3012 | { |
| 3013 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 3014 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3015 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 3016 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3017 | size_t hash_len; |
| 3018 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3019 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3020 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3021 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3022 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3023 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3024 | hash, expected_size - 1, &hash_len), |
| 3025 | PSA_ERROR_BUFFER_TOO_SMALL); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3026 | |
| 3027 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3028 | PSA_DONE(); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3029 | } |
| 3030 | /* END_CASE */ |
| 3031 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3032 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3033 | void hash_clone_source_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3034 | { |
| 3035 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3036 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3037 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 3038 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3039 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3040 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3041 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3042 | size_t hash_len; |
| 3043 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3044 | PSA_ASSERT(psa_crypto_init()); |
| 3045 | PSA_ASSERT(psa_hash_setup(&op_source, alg)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3046 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3047 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3048 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3049 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3050 | hash, sizeof(hash), &hash_len)); |
| 3051 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3052 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3053 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3054 | TEST_EQUAL(psa_hash_clone(&op_source, &op_setup), |
| 3055 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3056 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3057 | PSA_ASSERT(psa_hash_clone(&op_source, &op_init)); |
| 3058 | PSA_ASSERT(psa_hash_finish(&op_init, |
| 3059 | hash, sizeof(hash), &hash_len)); |
| 3060 | PSA_ASSERT(psa_hash_clone(&op_source, &op_finished)); |
| 3061 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3062 | hash, sizeof(hash), &hash_len)); |
| 3063 | PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted)); |
| 3064 | PSA_ASSERT(psa_hash_finish(&op_aborted, |
| 3065 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3066 | |
| 3067 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3068 | psa_hash_abort(&op_source); |
| 3069 | psa_hash_abort(&op_init); |
| 3070 | psa_hash_abort(&op_setup); |
| 3071 | psa_hash_abort(&op_finished); |
| 3072 | psa_hash_abort(&op_aborted); |
| 3073 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3074 | } |
| 3075 | /* END_CASE */ |
| 3076 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3077 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3078 | void hash_clone_target_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3079 | { |
| 3080 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3081 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3082 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3083 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3084 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3085 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3086 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 3087 | size_t hash_len; |
| 3088 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3089 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3090 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3091 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3092 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3093 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3094 | hash, sizeof(hash), &hash_len)); |
| 3095 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3096 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3097 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3098 | PSA_ASSERT(psa_hash_clone(&op_setup, &op_target)); |
| 3099 | PSA_ASSERT(psa_hash_finish(&op_target, |
| 3100 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3101 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3102 | TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE); |
| 3103 | TEST_EQUAL(psa_hash_clone(&op_finished, &op_target), |
| 3104 | PSA_ERROR_BAD_STATE); |
| 3105 | TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target), |
| 3106 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3107 | |
| 3108 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3109 | psa_hash_abort(&op_target); |
| 3110 | psa_hash_abort(&op_init); |
| 3111 | psa_hash_abort(&op_setup); |
| 3112 | psa_hash_abort(&op_finished); |
| 3113 | psa_hash_abort(&op_aborted); |
| 3114 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3115 | } |
| 3116 | /* END_CASE */ |
| 3117 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3118 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3119 | void mac_operation_init() |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3120 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3121 | const uint8_t input[1] = { 0 }; |
| 3122 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3123 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3124 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3125 | * 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] | 3126 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3127 | psa_mac_operation_t func = psa_mac_operation_init(); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3128 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 3129 | psa_mac_operation_t zero; |
| 3130 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3131 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3132 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3133 | /* A freshly-initialized MAC operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3134 | TEST_EQUAL(psa_mac_update(&func, |
| 3135 | input, sizeof(input)), |
| 3136 | PSA_ERROR_BAD_STATE); |
| 3137 | TEST_EQUAL(psa_mac_update(&init, |
| 3138 | input, sizeof(input)), |
| 3139 | PSA_ERROR_BAD_STATE); |
| 3140 | TEST_EQUAL(psa_mac_update(&zero, |
| 3141 | input, sizeof(input)), |
| 3142 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3143 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3144 | /* A default MAC operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3145 | PSA_ASSERT(psa_mac_abort(&func)); |
| 3146 | PSA_ASSERT(psa_mac_abort(&init)); |
| 3147 | PSA_ASSERT(psa_mac_abort(&zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3148 | } |
| 3149 | /* END_CASE */ |
| 3150 | |
| 3151 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3152 | void mac_setup(int key_type_arg, |
| 3153 | data_t *key, |
| 3154 | int alg_arg, |
| 3155 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3156 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3157 | psa_key_type_t key_type = key_type_arg; |
| 3158 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3159 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3160 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3161 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3162 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3163 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3164 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3165 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3166 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3167 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3168 | if (!exercise_mac_setup(key_type, key->x, key->len, alg, |
| 3169 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3170 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3171 | } |
| 3172 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3173 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3174 | /* The operation object should be reusable. */ |
| 3175 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3176 | if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 3177 | smoke_test_key_data, |
| 3178 | sizeof(smoke_test_key_data), |
| 3179 | KNOWN_SUPPORTED_MAC_ALG, |
| 3180 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3181 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3182 | } |
| 3183 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3184 | #endif |
| 3185 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3186 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3187 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3188 | } |
| 3189 | /* END_CASE */ |
| 3190 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3191 | /* 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] | 3192 | void mac_bad_order() |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3193 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3194 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3195 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 3196 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3197 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3198 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3199 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3200 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa |
| 3201 | }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3202 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3203 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3204 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 3205 | size_t sign_mac_length = 0; |
| 3206 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3207 | const uint8_t verify_mac[] = { |
| 3208 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 3209 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3210 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 |
| 3211 | }; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3212 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3213 | PSA_ASSERT(psa_crypto_init()); |
| 3214 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 3215 | psa_set_key_algorithm(&attributes, alg); |
| 3216 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3217 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3218 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3219 | &key)); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3220 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3221 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3222 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3223 | PSA_ERROR_BAD_STATE); |
| 3224 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3225 | |
| 3226 | /* Call sign finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3227 | TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac), |
| 3228 | &sign_mac_length), |
| 3229 | PSA_ERROR_BAD_STATE); |
| 3230 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3231 | |
| 3232 | /* Call verify finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3233 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3234 | verify_mac, sizeof(verify_mac)), |
| 3235 | PSA_ERROR_BAD_STATE); |
| 3236 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3237 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3238 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3239 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3240 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3241 | TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg), |
| 3242 | PSA_ERROR_BAD_STATE); |
| 3243 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3244 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3245 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3246 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3247 | /* Call update after sign finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3248 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3249 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3250 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3251 | sign_mac, sizeof(sign_mac), |
| 3252 | &sign_mac_length)); |
| 3253 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3254 | PSA_ERROR_BAD_STATE); |
| 3255 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3256 | |
| 3257 | /* Call update after verify finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3258 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3259 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3260 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3261 | verify_mac, sizeof(verify_mac))); |
| 3262 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3263 | PSA_ERROR_BAD_STATE); |
| 3264 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3265 | |
| 3266 | /* Call sign finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3267 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3268 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3269 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3270 | sign_mac, sizeof(sign_mac), |
| 3271 | &sign_mac_length)); |
| 3272 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3273 | sign_mac, sizeof(sign_mac), |
| 3274 | &sign_mac_length), |
| 3275 | PSA_ERROR_BAD_STATE); |
| 3276 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3277 | |
| 3278 | /* Call verify finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3279 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3280 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3281 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3282 | verify_mac, sizeof(verify_mac))); |
| 3283 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3284 | verify_mac, sizeof(verify_mac)), |
| 3285 | PSA_ERROR_BAD_STATE); |
| 3286 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3287 | |
| 3288 | /* Setup sign but try verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3289 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3290 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3291 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3292 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3293 | verify_mac, sizeof(verify_mac)), |
| 3294 | PSA_ERROR_BAD_STATE); |
| 3295 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3296 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3297 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3298 | |
| 3299 | /* Setup verify but try sign. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3300 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3301 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3302 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3303 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3304 | sign_mac, sizeof(sign_mac), |
| 3305 | &sign_mac_length), |
| 3306 | PSA_ERROR_BAD_STATE); |
| 3307 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3308 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3309 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3310 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3311 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3312 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3313 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3314 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3315 | } |
| 3316 | /* END_CASE */ |
| 3317 | |
| 3318 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3319 | void mac_sign_verify_multi(int key_type_arg, |
| 3320 | data_t *key_data, |
| 3321 | int alg_arg, |
| 3322 | data_t *input, |
| 3323 | int is_verify, |
| 3324 | data_t *expected_mac) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3325 | { |
| 3326 | size_t data_part_len = 0; |
| 3327 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3328 | 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] | 3329 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3330 | mbedtls_test_set_step(2000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3331 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3332 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3333 | alg_arg, |
| 3334 | input, data_part_len, |
| 3335 | expected_mac, |
| 3336 | is_verify, 0) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3337 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3338 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3339 | |
| 3340 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3341 | mbedtls_test_set_step(3000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3342 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3343 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3344 | alg_arg, |
| 3345 | input, data_part_len, |
| 3346 | expected_mac, |
| 3347 | is_verify, 1) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3348 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3349 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3350 | } |
| 3351 | |
| 3352 | /* Goto is required to silence warnings about unused labels, as we |
| 3353 | * don't actually do any test assertions in this function. */ |
| 3354 | goto exit; |
| 3355 | } |
| 3356 | /* END_CASE */ |
| 3357 | |
| 3358 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3359 | void mac_sign(int key_type_arg, |
| 3360 | data_t *key_data, |
| 3361 | int alg_arg, |
| 3362 | data_t *input, |
| 3363 | data_t *expected_mac) |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3364 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3365 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3366 | psa_key_type_t key_type = key_type_arg; |
| 3367 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3368 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3369 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3370 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3371 | size_t mac_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3372 | 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] | 3373 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3374 | const size_t output_sizes_to_test[] = { |
| 3375 | 0, |
| 3376 | 1, |
| 3377 | expected_mac->len - 1, |
| 3378 | expected_mac->len, |
| 3379 | expected_mac->len + 1, |
| 3380 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3381 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3382 | TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3383 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3384 | TEST_ASSERT(expected_mac->len == mac_buffer_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3386 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3387 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3388 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 3389 | psa_set_key_algorithm(&attributes, alg); |
| 3390 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3392 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3393 | &key)); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3394 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3395 | 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] | 3396 | const size_t output_size = output_sizes_to_test[i]; |
| 3397 | psa_status_t expected_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3398 | (output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3399 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3400 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3401 | mbedtls_test_set_step(output_size); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3402 | TEST_CALLOC(actual_mac, output_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3403 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3404 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3405 | TEST_EQUAL(psa_mac_compute(key, alg, |
| 3406 | input->x, input->len, |
| 3407 | actual_mac, output_size, &mac_length), |
| 3408 | expected_status); |
| 3409 | if (expected_status == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3410 | TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3411 | actual_mac, mac_length); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3412 | } |
| 3413 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3414 | if (output_size > 0) { |
| 3415 | memset(actual_mac, 0, output_size); |
| 3416 | } |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3417 | |
| 3418 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3419 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3420 | PSA_ASSERT(psa_mac_update(&operation, |
| 3421 | input->x, input->len)); |
| 3422 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3423 | actual_mac, output_size, |
| 3424 | &mac_length), |
| 3425 | expected_status); |
| 3426 | PSA_ASSERT(psa_mac_abort(&operation)); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3427 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3428 | if (expected_status == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3429 | TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3430 | actual_mac, mac_length); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3431 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3432 | mbedtls_free(actual_mac); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3433 | actual_mac = NULL; |
| 3434 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3435 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3436 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3437 | psa_mac_abort(&operation); |
| 3438 | psa_destroy_key(key); |
| 3439 | PSA_DONE(); |
| 3440 | mbedtls_free(actual_mac); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3441 | } |
| 3442 | /* END_CASE */ |
| 3443 | |
| 3444 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3445 | void mac_verify(int key_type_arg, |
| 3446 | data_t *key_data, |
| 3447 | int alg_arg, |
| 3448 | data_t *input, |
| 3449 | data_t *expected_mac) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3450 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3451 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3452 | psa_key_type_t key_type = key_type_arg; |
| 3453 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3454 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3455 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3456 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3457 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3458 | TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3459 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3460 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3461 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3462 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 3463 | psa_set_key_algorithm(&attributes, alg); |
| 3464 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3465 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3466 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3467 | &key)); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3468 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3469 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3470 | PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len, |
| 3471 | expected_mac->x, expected_mac->len)); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3472 | |
| 3473 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3474 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3475 | PSA_ASSERT(psa_mac_update(&operation, |
| 3476 | input->x, input->len)); |
| 3477 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3478 | expected_mac->x, |
| 3479 | expected_mac->len)); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3480 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3481 | /* Test a MAC that's too short, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3482 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3483 | input->x, input->len, |
| 3484 | expected_mac->x, |
| 3485 | expected_mac->len - 1), |
| 3486 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3487 | |
| 3488 | /* Test a MAC that's too short, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3489 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3490 | PSA_ASSERT(psa_mac_update(&operation, |
| 3491 | input->x, input->len)); |
| 3492 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3493 | expected_mac->x, |
| 3494 | expected_mac->len - 1), |
| 3495 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3496 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3497 | /* Test a MAC that's too long, one-shot case. */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3498 | TEST_CALLOC(perturbed_mac, expected_mac->len + 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3499 | memcpy(perturbed_mac, expected_mac->x, expected_mac->len); |
| 3500 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3501 | input->x, input->len, |
| 3502 | perturbed_mac, expected_mac->len + 1), |
| 3503 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3504 | |
| 3505 | /* Test a MAC that's too long, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3506 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3507 | PSA_ASSERT(psa_mac_update(&operation, |
| 3508 | input->x, input->len)); |
| 3509 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3510 | perturbed_mac, |
| 3511 | expected_mac->len + 1), |
| 3512 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3513 | |
| 3514 | /* Test changing one byte. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3515 | for (size_t i = 0; i < expected_mac->len; i++) { |
| 3516 | mbedtls_test_set_step(i); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3517 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3518 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3519 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3520 | input->x, input->len, |
| 3521 | perturbed_mac, expected_mac->len), |
| 3522 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3523 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3524 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3525 | PSA_ASSERT(psa_mac_update(&operation, |
| 3526 | input->x, input->len)); |
| 3527 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3528 | perturbed_mac, |
| 3529 | expected_mac->len), |
| 3530 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3531 | perturbed_mac[i] ^= 1; |
| 3532 | } |
| 3533 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3534 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3535 | psa_mac_abort(&operation); |
| 3536 | psa_destroy_key(key); |
| 3537 | PSA_DONE(); |
| 3538 | mbedtls_free(perturbed_mac); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3539 | } |
| 3540 | /* END_CASE */ |
| 3541 | |
| 3542 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3543 | void cipher_operation_init() |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3544 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3545 | const uint8_t input[1] = { 0 }; |
| 3546 | unsigned char output[1] = { 0 }; |
| 3547 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3548 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3549 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3550 | * 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] | 3551 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3552 | psa_cipher_operation_t func = psa_cipher_operation_init(); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3553 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3554 | psa_cipher_operation_t zero; |
| 3555 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3556 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3557 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3558 | /* A freshly-initialized cipher operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3559 | TEST_EQUAL(psa_cipher_update(&func, |
| 3560 | input, sizeof(input), |
| 3561 | output, sizeof(output), |
| 3562 | &output_length), |
| 3563 | PSA_ERROR_BAD_STATE); |
| 3564 | TEST_EQUAL(psa_cipher_update(&init, |
| 3565 | input, sizeof(input), |
| 3566 | output, sizeof(output), |
| 3567 | &output_length), |
| 3568 | PSA_ERROR_BAD_STATE); |
| 3569 | TEST_EQUAL(psa_cipher_update(&zero, |
| 3570 | input, sizeof(input), |
| 3571 | output, sizeof(output), |
| 3572 | &output_length), |
| 3573 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3574 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3575 | /* A default cipher operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3576 | PSA_ASSERT(psa_cipher_abort(&func)); |
| 3577 | PSA_ASSERT(psa_cipher_abort(&init)); |
| 3578 | PSA_ASSERT(psa_cipher_abort(&zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3579 | } |
| 3580 | /* END_CASE */ |
| 3581 | |
| 3582 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3583 | void cipher_setup(int key_type_arg, |
| 3584 | data_t *key, |
| 3585 | int alg_arg, |
| 3586 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3587 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3588 | psa_key_type_t key_type = key_type_arg; |
| 3589 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3590 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3591 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3592 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3593 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3594 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3595 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3596 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3597 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3598 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3599 | if (!exercise_cipher_setup(key_type, key->x, key->len, alg, |
| 3600 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3601 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3602 | } |
| 3603 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3604 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3605 | /* The operation object should be reusable. */ |
| 3606 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3607 | if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3608 | smoke_test_key_data, |
| 3609 | sizeof(smoke_test_key_data), |
| 3610 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3611 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3612 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3613 | } |
| 3614 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3615 | #endif |
| 3616 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3617 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3618 | psa_cipher_abort(&operation); |
| 3619 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3620 | } |
| 3621 | /* END_CASE */ |
| 3622 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3623 | /* 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] | 3624 | void cipher_bad_order() |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3625 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3626 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3627 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3628 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3629 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3630 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3631 | 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] | 3632 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3633 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3634 | 0xaa, 0xaa, 0xaa, 0xaa |
| 3635 | }; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3636 | const uint8_t text[] = { |
| 3637 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3638 | 0xbb, 0xbb, 0xbb, 0xbb |
| 3639 | }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3640 | 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] | 3641 | size_t length = 0; |
| 3642 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3643 | PSA_ASSERT(psa_crypto_init()); |
| 3644 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3645 | psa_set_key_algorithm(&attributes, alg); |
| 3646 | psa_set_key_type(&attributes, key_type); |
| 3647 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3648 | &key)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3649 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3650 | /* Call encrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3651 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3652 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3653 | TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg), |
| 3654 | PSA_ERROR_BAD_STATE); |
| 3655 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3656 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3657 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3658 | |
| 3659 | /* Call decrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3660 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3661 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3662 | TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg), |
| 3663 | PSA_ERROR_BAD_STATE); |
| 3664 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3665 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3666 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3667 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3668 | /* Generate an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3669 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3670 | buffer, sizeof(buffer), |
| 3671 | &length), |
| 3672 | PSA_ERROR_BAD_STATE); |
| 3673 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3674 | |
| 3675 | /* Generate an IV twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3676 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3677 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3678 | buffer, sizeof(buffer), |
| 3679 | &length)); |
| 3680 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3681 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3682 | buffer, sizeof(buffer), |
| 3683 | &length), |
| 3684 | PSA_ERROR_BAD_STATE); |
| 3685 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3686 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3687 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3688 | |
| 3689 | /* Generate an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3690 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3691 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3692 | iv, sizeof(iv))); |
| 3693 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3694 | buffer, sizeof(buffer), |
| 3695 | &length), |
| 3696 | PSA_ERROR_BAD_STATE); |
| 3697 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3698 | |
| 3699 | /* Set an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3700 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3701 | iv, sizeof(iv)), |
| 3702 | PSA_ERROR_BAD_STATE); |
| 3703 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3704 | |
| 3705 | /* Set an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3706 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3707 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3708 | iv, sizeof(iv))); |
| 3709 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3710 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3711 | iv, sizeof(iv)), |
| 3712 | PSA_ERROR_BAD_STATE); |
| 3713 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3714 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3715 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3716 | |
| 3717 | /* Set an IV after it's already generated. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3718 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3719 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3720 | buffer, sizeof(buffer), |
| 3721 | &length)); |
| 3722 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3723 | iv, sizeof(iv)), |
| 3724 | PSA_ERROR_BAD_STATE); |
| 3725 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3726 | |
| 3727 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3728 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3729 | text, sizeof(text), |
| 3730 | buffer, sizeof(buffer), |
| 3731 | &length), |
| 3732 | PSA_ERROR_BAD_STATE); |
| 3733 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3734 | |
| 3735 | /* Call update without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3736 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3737 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3738 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3739 | text, sizeof(text), |
| 3740 | buffer, sizeof(buffer), |
| 3741 | &length), |
| 3742 | PSA_ERROR_BAD_STATE); |
| 3743 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3744 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3745 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3746 | |
| 3747 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3748 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3749 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3750 | iv, sizeof(iv))); |
| 3751 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3752 | buffer, sizeof(buffer), &length)); |
| 3753 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3754 | text, sizeof(text), |
| 3755 | buffer, sizeof(buffer), |
| 3756 | &length), |
| 3757 | PSA_ERROR_BAD_STATE); |
| 3758 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3759 | |
| 3760 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3761 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3762 | buffer, sizeof(buffer), &length), |
| 3763 | PSA_ERROR_BAD_STATE); |
| 3764 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3765 | |
| 3766 | /* Call finish without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3767 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3768 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3769 | * for cipher modes with padding. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3770 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3771 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3772 | buffer, sizeof(buffer), &length), |
| 3773 | PSA_ERROR_BAD_STATE); |
| 3774 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3775 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3776 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3777 | |
| 3778 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3779 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3780 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3781 | iv, sizeof(iv))); |
| 3782 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3783 | buffer, sizeof(buffer), &length)); |
| 3784 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3785 | buffer, sizeof(buffer), &length), |
| 3786 | PSA_ERROR_BAD_STATE); |
| 3787 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3788 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3789 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3790 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3791 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3792 | psa_cipher_abort(&operation); |
| 3793 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3794 | } |
| 3795 | /* END_CASE */ |
| 3796 | |
| 3797 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3798 | void cipher_encrypt_fail(int alg_arg, |
| 3799 | int key_type_arg, |
| 3800 | data_t *key_data, |
| 3801 | data_t *input, |
| 3802 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3803 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3804 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3805 | psa_status_t status; |
| 3806 | psa_key_type_t key_type = key_type_arg; |
| 3807 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3808 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3809 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 }; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3810 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; |
| 3811 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3812 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3813 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3814 | size_t output_length = 0; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3815 | size_t function_output_length; |
| 3816 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3817 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3818 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3819 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 3820 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3821 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3822 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3823 | psa_set_key_algorithm(&attributes, alg); |
| 3824 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3825 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3826 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 3827 | input->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3828 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3829 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3830 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3831 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3832 | } |
| 3833 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3834 | /* Encrypt, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3835 | status = psa_cipher_encrypt(key, alg, input->x, input->len, output, |
| 3836 | output_buffer_size, &output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3837 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3838 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3839 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3840 | /* Encrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3841 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
| 3842 | if (status == PSA_SUCCESS) { |
| 3843 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 3844 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3845 | iv, iv_size, |
| 3846 | &iv_length)); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3847 | } |
| 3848 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3849 | status = psa_cipher_update(&operation, input->x, input->len, |
| 3850 | output, output_buffer_size, |
| 3851 | &function_output_length); |
| 3852 | if (status == PSA_SUCCESS) { |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3853 | output_length += function_output_length; |
| 3854 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3855 | status = psa_cipher_finish(&operation, output + output_length, |
| 3856 | output_buffer_size - output_length, |
| 3857 | &function_output_length); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3858 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3859 | TEST_EQUAL(status, expected_status); |
| 3860 | } else { |
| 3861 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3862 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3863 | } else { |
| 3864 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3865 | } |
| 3866 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3867 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3868 | psa_cipher_abort(&operation); |
| 3869 | mbedtls_free(output); |
| 3870 | psa_destroy_key(key); |
| 3871 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3872 | } |
| 3873 | /* END_CASE */ |
| 3874 | |
| 3875 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3876 | void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data, |
| 3877 | data_t *input, int iv_length, |
| 3878 | int expected_result) |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3879 | { |
| 3880 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3881 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3882 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3883 | size_t output_buffer_size = 0; |
| 3884 | unsigned char *output = NULL; |
| 3885 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3886 | 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] | 3887 | TEST_CALLOC(output, output_buffer_size); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3888 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3889 | PSA_ASSERT(psa_crypto_init()); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3890 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3891 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3892 | psa_set_key_algorithm(&attributes, alg); |
| 3893 | psa_set_key_type(&attributes, key_type); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3894 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3895 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3896 | &key)); |
| 3897 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3898 | TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output, |
| 3899 | iv_length)); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3900 | |
| 3901 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3902 | psa_cipher_abort(&operation); |
| 3903 | mbedtls_free(output); |
| 3904 | psa_destroy_key(key); |
| 3905 | PSA_DONE(); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3906 | } |
| 3907 | /* END_CASE */ |
| 3908 | |
| 3909 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3910 | void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data, |
| 3911 | data_t *plaintext, data_t *ciphertext) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3912 | { |
| 3913 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3914 | psa_key_type_t key_type = key_type_arg; |
| 3915 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3916 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3917 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3918 | unsigned char *output = NULL; |
| 3919 | size_t output_buffer_size = 0; |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3920 | size_t output_length, length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3921 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3922 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3923 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3924 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3925 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3926 | TEST_LE_U(ciphertext->len, |
| 3927 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len)); |
| 3928 | TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len), |
| 3929 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len)); |
| 3930 | TEST_LE_U(plaintext->len, |
| 3931 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len)); |
| 3932 | TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len), |
| 3933 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len)); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3934 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3935 | |
| 3936 | /* Set up key and output buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3937 | psa_set_key_usage_flags(&attributes, |
| 3938 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3939 | psa_set_key_algorithm(&attributes, alg); |
| 3940 | psa_set_key_type(&attributes, key_type); |
| 3941 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3942 | &key)); |
| 3943 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 3944 | plaintext->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3945 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3946 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3947 | /* set_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3948 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3949 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 3950 | PSA_ERROR_BAD_STATE); |
| 3951 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3952 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 3953 | PSA_ERROR_BAD_STATE); |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3954 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3955 | /* generate_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3956 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3957 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 3958 | &length), |
| 3959 | PSA_ERROR_BAD_STATE); |
| 3960 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3961 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 3962 | &length), |
| 3963 | PSA_ERROR_BAD_STATE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3964 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3965 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3966 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3967 | output_length = 0; |
| 3968 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3969 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3970 | plaintext->x, plaintext->len, |
| 3971 | output, output_buffer_size, |
| 3972 | &length)); |
| 3973 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3974 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3975 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3976 | mbedtls_buffer_offset(output, output_length), |
| 3977 | output_buffer_size - output_length, |
| 3978 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3979 | output_length += length; |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3980 | TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3981 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3982 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3983 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3984 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3985 | output_length = 0; |
| 3986 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3987 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3988 | ciphertext->x, ciphertext->len, |
| 3989 | output, output_buffer_size, |
| 3990 | &length)); |
| 3991 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3992 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3993 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3994 | mbedtls_buffer_offset(output, output_length), |
| 3995 | output_buffer_size - output_length, |
| 3996 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3997 | output_length += length; |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3998 | TEST_MEMORY_COMPARE(plaintext->x, plaintext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3999 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4000 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4001 | /* One-shot encryption */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4002 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4003 | PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len, |
| 4004 | output, output_buffer_size, |
| 4005 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4006 | TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4007 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4008 | |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4009 | /* One-shot decryption */ |
| 4010 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4011 | PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len, |
| 4012 | output, output_buffer_size, |
| 4013 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4014 | TEST_MEMORY_COMPARE(plaintext->x, plaintext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4015 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4016 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4017 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4018 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4019 | mbedtls_free(output); |
| 4020 | psa_cipher_abort(&operation); |
| 4021 | psa_destroy_key(key); |
| 4022 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4023 | } |
| 4024 | /* END_CASE */ |
| 4025 | |
| 4026 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4027 | 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] | 4028 | { |
| 4029 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4030 | psa_algorithm_t alg = alg_arg; |
| 4031 | psa_key_type_t key_type = key_type_arg; |
| 4032 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4033 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4034 | psa_status_t status; |
| 4035 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4036 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4037 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4038 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4039 | psa_set_key_algorithm(&attributes, alg); |
| 4040 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4041 | |
| 4042 | /* Usage of either of these two size macros would cause divide by zero |
| 4043 | * with incorrect key types previously. Input length should be irrelevant |
| 4044 | * here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4045 | TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16), |
| 4046 | 0); |
| 4047 | TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4048 | |
| 4049 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4050 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4051 | &key)); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4052 | |
| 4053 | /* Should fail due to invalid alg type (to support invalid key type). |
| 4054 | * Encrypt or decrypt will end up in the same place. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4055 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4056 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4057 | TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4058 | |
| 4059 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4060 | psa_cipher_abort(&operation); |
| 4061 | psa_destroy_key(key); |
| 4062 | PSA_DONE(); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4063 | } |
| 4064 | /* END_CASE */ |
| 4065 | |
| 4066 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4067 | void cipher_encrypt_validation(int alg_arg, |
| 4068 | int key_type_arg, |
| 4069 | data_t *key_data, |
| 4070 | data_t *input) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4071 | { |
| 4072 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4073 | psa_key_type_t key_type = key_type_arg; |
| 4074 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4075 | size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4076 | unsigned char *output1 = NULL; |
| 4077 | size_t output1_buffer_size = 0; |
| 4078 | size_t output1_length = 0; |
| 4079 | unsigned char *output2 = NULL; |
| 4080 | size_t output2_buffer_size = 0; |
| 4081 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4082 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4083 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4084 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4085 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4086 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4087 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4088 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4089 | psa_set_key_algorithm(&attributes, alg); |
| 4090 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4091 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4092 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4093 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4094 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4095 | TEST_CALLOC(output1, output1_buffer_size); |
| 4096 | TEST_CALLOC(output2, output2_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4097 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4098 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4099 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4100 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 4101 | /* The one-shot cipher encryption uses generated iv so validating |
| 4102 | the output is not possible. Validating with multipart encryption. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4103 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1, |
| 4104 | output1_buffer_size, &output1_length)); |
| 4105 | TEST_LE_U(output1_length, |
| 4106 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4107 | TEST_LE_U(output1_length, |
| 4108 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4109 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4110 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4111 | PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4112 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4113 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4114 | input->x, input->len, |
| 4115 | output2, output2_buffer_size, |
| 4116 | &function_output_length)); |
| 4117 | TEST_LE_U(function_output_length, |
| 4118 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4119 | TEST_LE_U(function_output_length, |
| 4120 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4121 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4122 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4123 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4124 | output2 + output2_length, |
| 4125 | output2_buffer_size - output2_length, |
| 4126 | &function_output_length)); |
| 4127 | TEST_LE_U(function_output_length, |
| 4128 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4129 | TEST_LE_U(function_output_length, |
| 4130 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4131 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4132 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4133 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4134 | TEST_MEMORY_COMPARE(output1 + iv_size, output1_length - iv_size, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4135 | output2, output2_length); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4136 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4137 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4138 | psa_cipher_abort(&operation); |
| 4139 | mbedtls_free(output1); |
| 4140 | mbedtls_free(output2); |
| 4141 | psa_destroy_key(key); |
| 4142 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4143 | } |
| 4144 | /* END_CASE */ |
| 4145 | |
| 4146 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4147 | void cipher_encrypt_multipart(int alg_arg, int key_type_arg, |
| 4148 | data_t *key_data, data_t *iv, |
| 4149 | data_t *input, |
| 4150 | int first_part_size_arg, |
| 4151 | int output1_length_arg, int output2_length_arg, |
| 4152 | data_t *expected_output, |
| 4153 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4154 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4155 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4156 | psa_key_type_t key_type = key_type_arg; |
| 4157 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4158 | psa_status_t status; |
| 4159 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4160 | size_t first_part_size = first_part_size_arg; |
| 4161 | size_t output1_length = output1_length_arg; |
| 4162 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4163 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4164 | size_t output_buffer_size = 0; |
| 4165 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4166 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4167 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4168 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4169 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4170 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4172 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4173 | psa_set_key_algorithm(&attributes, alg); |
| 4174 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [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 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4179 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4181 | if (iv->len > 0) { |
| 4182 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4183 | } |
| 4184 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4185 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4186 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4187 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4188 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4189 | TEST_LE_U(first_part_size, input->len); |
| 4190 | PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size, |
| 4191 | output, output_buffer_size, |
| 4192 | &function_output_length)); |
| 4193 | TEST_ASSERT(function_output_length == output1_length); |
| 4194 | TEST_LE_U(function_output_length, |
| 4195 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4196 | TEST_LE_U(function_output_length, |
| 4197 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4198 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4199 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4200 | if (first_part_size < input->len) { |
| 4201 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4202 | input->x + first_part_size, |
| 4203 | input->len - first_part_size, |
| 4204 | (output_buffer_size == 0 ? NULL : |
| 4205 | output + total_output_length), |
| 4206 | output_buffer_size - total_output_length, |
| 4207 | &function_output_length)); |
| 4208 | TEST_ASSERT(function_output_length == output2_length); |
| 4209 | TEST_LE_U(function_output_length, |
| 4210 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4211 | alg, |
| 4212 | input->len - first_part_size)); |
| 4213 | TEST_LE_U(function_output_length, |
| 4214 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4215 | total_output_length += function_output_length; |
| 4216 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4217 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4218 | status = psa_cipher_finish(&operation, |
| 4219 | (output_buffer_size == 0 ? NULL : |
| 4220 | output + total_output_length), |
| 4221 | output_buffer_size - total_output_length, |
| 4222 | &function_output_length); |
| 4223 | TEST_LE_U(function_output_length, |
| 4224 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4225 | TEST_LE_U(function_output_length, |
| 4226 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4227 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4228 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4230 | if (expected_status == PSA_SUCCESS) { |
| 4231 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4232 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4233 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4234 | output, total_output_length); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4235 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4236 | |
| 4237 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4238 | psa_cipher_abort(&operation); |
| 4239 | mbedtls_free(output); |
| 4240 | psa_destroy_key(key); |
| 4241 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4242 | } |
| 4243 | /* END_CASE */ |
| 4244 | |
| 4245 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4246 | void cipher_decrypt_multipart(int alg_arg, int key_type_arg, |
| 4247 | data_t *key_data, data_t *iv, |
| 4248 | data_t *input, |
| 4249 | int first_part_size_arg, |
| 4250 | int output1_length_arg, int output2_length_arg, |
| 4251 | data_t *expected_output, |
| 4252 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4253 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4254 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4255 | psa_key_type_t key_type = key_type_arg; |
| 4256 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4257 | psa_status_t status; |
| 4258 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4259 | size_t first_part_size = first_part_size_arg; |
| 4260 | size_t output1_length = output1_length_arg; |
| 4261 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4262 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4263 | size_t output_buffer_size = 0; |
| 4264 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4265 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4266 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4267 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4268 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4269 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4270 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4271 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4272 | psa_set_key_algorithm(&attributes, alg); |
| 4273 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4274 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4275 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4276 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4277 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4278 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4279 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4280 | if (iv->len > 0) { |
| 4281 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4282 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4283 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4284 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4285 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4286 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4287 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4288 | TEST_LE_U(first_part_size, input->len); |
| 4289 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4290 | input->x, first_part_size, |
| 4291 | output, output_buffer_size, |
| 4292 | &function_output_length)); |
| 4293 | TEST_ASSERT(function_output_length == output1_length); |
| 4294 | TEST_LE_U(function_output_length, |
| 4295 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4296 | TEST_LE_U(function_output_length, |
| 4297 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4298 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4299 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4300 | if (first_part_size < input->len) { |
| 4301 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4302 | input->x + first_part_size, |
| 4303 | input->len - first_part_size, |
| 4304 | (output_buffer_size == 0 ? NULL : |
| 4305 | output + total_output_length), |
| 4306 | output_buffer_size - total_output_length, |
| 4307 | &function_output_length)); |
| 4308 | TEST_ASSERT(function_output_length == output2_length); |
| 4309 | TEST_LE_U(function_output_length, |
| 4310 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4311 | alg, |
| 4312 | input->len - first_part_size)); |
| 4313 | TEST_LE_U(function_output_length, |
| 4314 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4315 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4316 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4317 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4318 | status = psa_cipher_finish(&operation, |
| 4319 | (output_buffer_size == 0 ? NULL : |
| 4320 | output + total_output_length), |
| 4321 | output_buffer_size - total_output_length, |
| 4322 | &function_output_length); |
| 4323 | TEST_LE_U(function_output_length, |
| 4324 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4325 | TEST_LE_U(function_output_length, |
| 4326 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4327 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4328 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4329 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4330 | if (expected_status == PSA_SUCCESS) { |
| 4331 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4332 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4333 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4334 | output, total_output_length); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4335 | } |
| 4336 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4337 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4338 | psa_cipher_abort(&operation); |
| 4339 | mbedtls_free(output); |
| 4340 | psa_destroy_key(key); |
| 4341 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4342 | } |
| 4343 | /* END_CASE */ |
| 4344 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4345 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4346 | void cipher_decrypt_fail(int alg_arg, |
| 4347 | int key_type_arg, |
| 4348 | data_t *key_data, |
| 4349 | data_t *iv, |
| 4350 | data_t *input_arg, |
| 4351 | int expected_status_arg) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4352 | { |
| 4353 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4354 | psa_status_t status; |
| 4355 | psa_key_type_t key_type = key_type_arg; |
| 4356 | psa_algorithm_t alg = alg_arg; |
| 4357 | psa_status_t expected_status = expected_status_arg; |
| 4358 | unsigned char *input = NULL; |
| 4359 | size_t input_buffer_size = 0; |
| 4360 | unsigned char *output = NULL; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4361 | unsigned char *output_multi = NULL; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4362 | size_t output_buffer_size = 0; |
| 4363 | size_t output_length = 0; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4364 | size_t function_output_length; |
| 4365 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4366 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4367 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4368 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 4369 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4371 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4372 | psa_set_key_algorithm(&attributes, alg); |
| 4373 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4374 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4375 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4376 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4377 | } |
| 4378 | |
| 4379 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4380 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4381 | if (input_buffer_size > 0) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4382 | TEST_CALLOC(input, input_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4383 | memcpy(input, iv->x, iv->len); |
| 4384 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4385 | } |
| 4386 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4387 | 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] | 4388 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4389 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4390 | /* Decrypt, one-short */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4391 | status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4392 | output_buffer_size, &output_length); |
| 4393 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4394 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4395 | /* Decrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4396 | status = psa_cipher_decrypt_setup(&operation, key, alg); |
| 4397 | if (status == PSA_SUCCESS) { |
| 4398 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 4399 | input_arg->len) + |
| 4400 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4401 | TEST_CALLOC(output_multi, output_buffer_size); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4402 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4403 | if (iv->len > 0) { |
| 4404 | status = psa_cipher_set_iv(&operation, iv->x, iv->len); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4405 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4406 | if (status != PSA_SUCCESS) { |
| 4407 | TEST_EQUAL(status, expected_status); |
| 4408 | } |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4409 | } |
| 4410 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4411 | if (status == PSA_SUCCESS) { |
| 4412 | status = psa_cipher_update(&operation, |
| 4413 | input_arg->x, input_arg->len, |
| 4414 | output_multi, output_buffer_size, |
| 4415 | &function_output_length); |
| 4416 | if (status == PSA_SUCCESS) { |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4417 | output_length = function_output_length; |
| 4418 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4419 | status = psa_cipher_finish(&operation, |
| 4420 | output_multi + output_length, |
| 4421 | output_buffer_size - output_length, |
| 4422 | &function_output_length); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4423 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4424 | TEST_EQUAL(status, expected_status); |
| 4425 | } else { |
| 4426 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4427 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4428 | } else { |
| 4429 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4430 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4431 | } else { |
| 4432 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4433 | } |
| 4434 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4435 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4436 | psa_cipher_abort(&operation); |
| 4437 | mbedtls_free(input); |
| 4438 | mbedtls_free(output); |
| 4439 | mbedtls_free(output_multi); |
| 4440 | psa_destroy_key(key); |
| 4441 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4442 | } |
| 4443 | /* END_CASE */ |
| 4444 | |
| 4445 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4446 | void cipher_decrypt(int alg_arg, |
| 4447 | int key_type_arg, |
| 4448 | data_t *key_data, |
| 4449 | data_t *iv, |
| 4450 | data_t *input_arg, |
| 4451 | data_t *expected_output) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4452 | { |
| 4453 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4454 | psa_key_type_t key_type = key_type_arg; |
| 4455 | psa_algorithm_t alg = alg_arg; |
| 4456 | unsigned char *input = NULL; |
| 4457 | size_t input_buffer_size = 0; |
| 4458 | unsigned char *output = NULL; |
| 4459 | size_t output_buffer_size = 0; |
| 4460 | size_t output_length = 0; |
| 4461 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4462 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4463 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4464 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4465 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4466 | psa_set_key_algorithm(&attributes, alg); |
| 4467 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4468 | |
| 4469 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4470 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4471 | if (input_buffer_size > 0) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4472 | TEST_CALLOC(input, input_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4473 | memcpy(input, iv->x, iv->len); |
| 4474 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4475 | } |
| 4476 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4477 | 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] | 4478 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4479 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4480 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4481 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4482 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4483 | PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4484 | output_buffer_size, &output_length)); |
| 4485 | TEST_LE_U(output_length, |
| 4486 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size)); |
| 4487 | TEST_LE_U(output_length, |
| 4488 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4489 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4490 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4491 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4492 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4493 | mbedtls_free(input); |
| 4494 | mbedtls_free(output); |
| 4495 | psa_destroy_key(key); |
| 4496 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4497 | } |
| 4498 | /* END_CASE */ |
| 4499 | |
| 4500 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4501 | void cipher_verify_output(int alg_arg, |
| 4502 | int key_type_arg, |
| 4503 | data_t *key_data, |
| 4504 | data_t *input) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4505 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4506 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4507 | psa_key_type_t key_type = key_type_arg; |
| 4508 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4509 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4510 | size_t output1_size = 0; |
| 4511 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4512 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4513 | size_t output2_size = 0; |
| 4514 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4515 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4516 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4517 | PSA_ASSERT(psa_crypto_init()); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4518 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4519 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4520 | psa_set_key_algorithm(&attributes, alg); |
| 4521 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4522 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4523 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4524 | &key)); |
| 4525 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4526 | TEST_CALLOC(output1, output1_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4527 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4528 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, |
| 4529 | output1, output1_size, |
| 4530 | &output1_length)); |
| 4531 | TEST_LE_U(output1_length, |
| 4532 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4533 | TEST_LE_U(output1_length, |
| 4534 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4535 | |
| 4536 | output2_size = output1_length; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4537 | TEST_CALLOC(output2, output2_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4538 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4539 | PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length, |
| 4540 | output2, output2_size, |
| 4541 | &output2_length)); |
| 4542 | TEST_LE_U(output2_length, |
| 4543 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4544 | TEST_LE_U(output2_length, |
| 4545 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4546 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4547 | TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4548 | |
| 4549 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4550 | mbedtls_free(output1); |
| 4551 | mbedtls_free(output2); |
| 4552 | psa_destroy_key(key); |
| 4553 | PSA_DONE(); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4554 | } |
| 4555 | /* END_CASE */ |
| 4556 | |
| 4557 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4558 | void cipher_verify_output_multipart(int alg_arg, |
| 4559 | int key_type_arg, |
| 4560 | data_t *key_data, |
| 4561 | data_t *input, |
| 4562 | int first_part_size_arg) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4563 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4564 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4565 | psa_key_type_t key_type = key_type_arg; |
| 4566 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4567 | size_t first_part_size = first_part_size_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4568 | unsigned char iv[16] = { 0 }; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4569 | size_t iv_size = 16; |
| 4570 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4571 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4572 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4573 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4574 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4575 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4576 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4577 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4578 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 4579 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4580 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4581 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4582 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4583 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4584 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4585 | psa_set_key_algorithm(&attributes, alg); |
| 4586 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4587 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4588 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4589 | &key)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4590 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4591 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg)); |
| 4592 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4593 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4594 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 4595 | PSA_ASSERT(psa_cipher_generate_iv(&operation1, |
| 4596 | iv, iv_size, |
| 4597 | &iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4598 | } |
| 4599 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4600 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4601 | TEST_LE_U(output1_buffer_size, |
| 4602 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4603 | TEST_CALLOC(output1, output1_buffer_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4604 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4605 | TEST_LE_U(first_part_size, input->len); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4606 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4607 | PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size, |
| 4608 | output1, output1_buffer_size, |
| 4609 | &function_output_length)); |
| 4610 | TEST_LE_U(function_output_length, |
| 4611 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4612 | TEST_LE_U(function_output_length, |
| 4613 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4614 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4615 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4616 | PSA_ASSERT(psa_cipher_update(&operation1, |
| 4617 | input->x + first_part_size, |
| 4618 | input->len - first_part_size, |
| 4619 | output1, output1_buffer_size, |
| 4620 | &function_output_length)); |
| 4621 | TEST_LE_U(function_output_length, |
| 4622 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4623 | alg, |
| 4624 | input->len - first_part_size)); |
| 4625 | TEST_LE_U(function_output_length, |
| 4626 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4627 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4628 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4629 | PSA_ASSERT(psa_cipher_finish(&operation1, |
| 4630 | output1 + output1_length, |
| 4631 | output1_buffer_size - output1_length, |
| 4632 | &function_output_length)); |
| 4633 | TEST_LE_U(function_output_length, |
| 4634 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4635 | TEST_LE_U(function_output_length, |
| 4636 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4637 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4638 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4639 | PSA_ASSERT(psa_cipher_abort(&operation1)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4640 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4641 | output2_buffer_size = output1_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4642 | TEST_LE_U(output2_buffer_size, |
| 4643 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4644 | TEST_LE_U(output2_buffer_size, |
| 4645 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4646 | TEST_CALLOC(output2, output2_buffer_size); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4647 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4648 | if (iv_length > 0) { |
| 4649 | PSA_ASSERT(psa_cipher_set_iv(&operation2, |
| 4650 | iv, iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4651 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4652 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4653 | PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size, |
| 4654 | output2, output2_buffer_size, |
| 4655 | &function_output_length)); |
| 4656 | TEST_LE_U(function_output_length, |
| 4657 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4658 | TEST_LE_U(function_output_length, |
| 4659 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4660 | output2_length += function_output_length; |
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_ASSERT(psa_cipher_update(&operation2, |
| 4663 | output1 + first_part_size, |
| 4664 | output1_length - first_part_size, |
| 4665 | output2, output2_buffer_size, |
| 4666 | &function_output_length)); |
| 4667 | TEST_LE_U(function_output_length, |
| 4668 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4669 | alg, |
| 4670 | output1_length - first_part_size)); |
| 4671 | TEST_LE_U(function_output_length, |
| 4672 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4673 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4674 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4675 | PSA_ASSERT(psa_cipher_finish(&operation2, |
| 4676 | output2 + output2_length, |
| 4677 | output2_buffer_size - output2_length, |
| 4678 | &function_output_length)); |
| 4679 | TEST_LE_U(function_output_length, |
| 4680 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4681 | TEST_LE_U(function_output_length, |
| 4682 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4683 | output2_length += function_output_length; |
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_abort(&operation2)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4686 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4687 | TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4688 | |
| 4689 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4690 | psa_cipher_abort(&operation1); |
| 4691 | psa_cipher_abort(&operation2); |
| 4692 | mbedtls_free(output1); |
| 4693 | mbedtls_free(output2); |
| 4694 | psa_destroy_key(key); |
| 4695 | PSA_DONE(); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4696 | } |
| 4697 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 4698 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4699 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4700 | void aead_encrypt_decrypt(int key_type_arg, data_t *key_data, |
| 4701 | int alg_arg, |
| 4702 | data_t *nonce, |
| 4703 | data_t *additional_data, |
| 4704 | data_t *input_data, |
| 4705 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4706 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4707 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4708 | psa_key_type_t key_type = key_type_arg; |
| 4709 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4710 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4711 | unsigned char *output_data = NULL; |
| 4712 | size_t output_size = 0; |
| 4713 | size_t output_length = 0; |
| 4714 | unsigned char *output_data2 = NULL; |
| 4715 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4716 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4717 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4718 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4719 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4720 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4721 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4722 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4723 | psa_set_key_algorithm(&attributes, alg); |
| 4724 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4725 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4726 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4727 | &key)); |
| 4728 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4729 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4730 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4731 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4732 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4733 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4734 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4735 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4736 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
| 4737 | TEST_EQUAL(output_size, |
| 4738 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4739 | TEST_LE_U(output_size, |
| 4740 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4741 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4742 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4743 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4744 | status = psa_aead_encrypt(key, alg, |
| 4745 | nonce->x, nonce->len, |
| 4746 | additional_data->x, |
| 4747 | additional_data->len, |
| 4748 | input_data->x, input_data->len, |
| 4749 | output_data, output_size, |
| 4750 | &output_length); |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4751 | |
| 4752 | /* If the operation is not supported, just skip and not fail in case the |
| 4753 | * encryption involves a common limitation of cryptography hardwares and |
| 4754 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4755 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4756 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4757 | 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] | 4758 | } |
| 4759 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4760 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4761 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4762 | if (PSA_SUCCESS == expected_result) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4763 | TEST_CALLOC(output_data2, output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4764 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4765 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4766 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4767 | TEST_EQUAL(input_data->len, |
| 4768 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length)); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4769 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4770 | TEST_LE_U(input_data->len, |
| 4771 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length)); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4772 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4773 | TEST_EQUAL(psa_aead_decrypt(key, alg, |
| 4774 | nonce->x, nonce->len, |
| 4775 | additional_data->x, |
| 4776 | additional_data->len, |
| 4777 | output_data, output_length, |
| 4778 | output_data2, output_length, |
| 4779 | &output_length2), |
| 4780 | expected_result); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4781 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4782 | TEST_MEMORY_COMPARE(input_data->x, input_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4783 | output_data2, output_length2); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4784 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4785 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4786 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4787 | psa_destroy_key(key); |
| 4788 | mbedtls_free(output_data); |
| 4789 | mbedtls_free(output_data2); |
| 4790 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4791 | } |
| 4792 | /* END_CASE */ |
| 4793 | |
| 4794 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4795 | void aead_encrypt(int key_type_arg, data_t *key_data, |
| 4796 | int alg_arg, |
| 4797 | data_t *nonce, |
| 4798 | data_t *additional_data, |
| 4799 | data_t *input_data, |
| 4800 | data_t *expected_result) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4801 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4802 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4803 | psa_key_type_t key_type = key_type_arg; |
| 4804 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4805 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4806 | unsigned char *output_data = NULL; |
| 4807 | size_t output_size = 0; |
| 4808 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4809 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4810 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4811 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4812 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4813 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4814 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4815 | psa_set_key_algorithm(&attributes, alg); |
| 4816 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4817 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4818 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4819 | &key)); |
| 4820 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4821 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4822 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4823 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4824 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4825 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4826 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4827 | TEST_EQUAL(output_size, |
| 4828 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4829 | TEST_LE_U(output_size, |
| 4830 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4831 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4832 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4833 | status = psa_aead_encrypt(key, alg, |
| 4834 | nonce->x, nonce->len, |
| 4835 | additional_data->x, additional_data->len, |
| 4836 | input_data->x, input_data->len, |
| 4837 | output_data, output_size, |
| 4838 | &output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4839 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4840 | /* If the operation is not supported, just skip and not fail in case the |
| 4841 | * encryption involves a common limitation of cryptography hardwares and |
| 4842 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4843 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4844 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4845 | 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] | 4846 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4847 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4848 | PSA_ASSERT(status); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4849 | TEST_MEMORY_COMPARE(expected_result->x, expected_result->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4850 | output_data, output_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4851 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4852 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4853 | psa_destroy_key(key); |
| 4854 | mbedtls_free(output_data); |
| 4855 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4856 | } |
| 4857 | /* END_CASE */ |
| 4858 | |
| 4859 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4860 | void aead_decrypt(int key_type_arg, data_t *key_data, |
| 4861 | int alg_arg, |
| 4862 | data_t *nonce, |
| 4863 | data_t *additional_data, |
| 4864 | data_t *input_data, |
| 4865 | data_t *expected_data, |
| 4866 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4867 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4868 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4869 | psa_key_type_t key_type = key_type_arg; |
| 4870 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4871 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4872 | unsigned char *output_data = NULL; |
| 4873 | size_t output_size = 0; |
| 4874 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4875 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4876 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4877 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4878 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4879 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4880 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4881 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4882 | psa_set_key_algorithm(&attributes, alg); |
| 4883 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4884 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4885 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4886 | &key)); |
| 4887 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4888 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4889 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4890 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4891 | alg); |
| 4892 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4893 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4894 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4895 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4896 | TEST_EQUAL(output_size, |
| 4897 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4898 | TEST_LE_U(output_size, |
| 4899 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4900 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4901 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4902 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4903 | status = psa_aead_decrypt(key, alg, |
| 4904 | nonce->x, nonce->len, |
| 4905 | additional_data->x, |
| 4906 | additional_data->len, |
| 4907 | input_data->x, input_data->len, |
| 4908 | output_data, output_size, |
| 4909 | &output_length); |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4910 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4911 | /* If the operation is not supported, just skip and not fail in case the |
| 4912 | * decryption involves a common limitation of cryptography hardwares and |
| 4913 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4914 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4915 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4916 | 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] | 4917 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4918 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4919 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4920 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4921 | if (expected_result == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4922 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4923 | output_data, output_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4924 | } |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4925 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4926 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4927 | psa_destroy_key(key); |
| 4928 | mbedtls_free(output_data); |
| 4929 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4930 | } |
| 4931 | /* END_CASE */ |
| 4932 | |
| 4933 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4934 | void aead_multipart_encrypt(int key_type_arg, data_t *key_data, |
| 4935 | int alg_arg, |
| 4936 | data_t *nonce, |
| 4937 | data_t *additional_data, |
| 4938 | data_t *input_data, |
| 4939 | int do_set_lengths, |
| 4940 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4941 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4942 | size_t ad_part_len = 0; |
| 4943 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4944 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4945 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4946 | for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) { |
| 4947 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4948 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4949 | if (do_set_lengths) { |
| 4950 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4951 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4952 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4953 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4954 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4955 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4956 | |
| 4957 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4958 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4959 | alg_arg, nonce, |
| 4960 | additional_data, |
| 4961 | ad_part_len, |
| 4962 | input_data, -1, |
| 4963 | set_lengths_method, |
| 4964 | expected_output, |
| 4965 | 1, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4966 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4967 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4968 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4969 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 4970 | mbedtls_test_set_step(1000 + ad_part_len); |
| 4971 | |
| 4972 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4973 | alg_arg, nonce, |
| 4974 | additional_data, |
| 4975 | ad_part_len, |
| 4976 | input_data, -1, |
| 4977 | set_lengths_method, |
| 4978 | expected_output, |
| 4979 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4980 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4981 | } |
| 4982 | } |
| 4983 | |
| 4984 | for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) { |
| 4985 | /* Split data into length(data_part_len) parts. */ |
| 4986 | mbedtls_test_set_step(2000 + data_part_len); |
| 4987 | |
| 4988 | if (do_set_lengths) { |
| 4989 | if (data_part_len & 0x01) { |
| 4990 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4991 | } else { |
| 4992 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
| 4993 | } |
| 4994 | } |
| 4995 | |
| 4996 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4997 | alg_arg, nonce, |
| 4998 | additional_data, -1, |
| 4999 | input_data, data_part_len, |
| 5000 | set_lengths_method, |
| 5001 | expected_output, |
| 5002 | 1, 0)) { |
| 5003 | break; |
| 5004 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5005 | |
| 5006 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5007 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5008 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5009 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5010 | alg_arg, nonce, |
| 5011 | additional_data, -1, |
| 5012 | input_data, data_part_len, |
| 5013 | set_lengths_method, |
| 5014 | expected_output, |
| 5015 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5016 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5017 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5018 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5019 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5020 | /* Goto is required to silence warnings about unused labels, as we |
| 5021 | * don't actually do any test assertions in this function. */ |
| 5022 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5023 | } |
| 5024 | /* END_CASE */ |
| 5025 | |
| 5026 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5027 | void aead_multipart_decrypt(int key_type_arg, data_t *key_data, |
| 5028 | int alg_arg, |
| 5029 | data_t *nonce, |
| 5030 | data_t *additional_data, |
| 5031 | data_t *input_data, |
| 5032 | int do_set_lengths, |
| 5033 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5034 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5035 | size_t ad_part_len = 0; |
| 5036 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5037 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5038 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5039 | 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] | 5040 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5041 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5042 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5043 | if (do_set_lengths) { |
| 5044 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5045 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5046 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5047 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5048 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5049 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5050 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5051 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5052 | alg_arg, nonce, |
| 5053 | additional_data, |
| 5054 | ad_part_len, |
| 5055 | input_data, -1, |
| 5056 | set_lengths_method, |
| 5057 | expected_output, |
| 5058 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5059 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5060 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5061 | |
| 5062 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5063 | mbedtls_test_set_step(1000 + ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5064 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5065 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5066 | alg_arg, nonce, |
| 5067 | additional_data, |
| 5068 | ad_part_len, |
| 5069 | input_data, -1, |
| 5070 | set_lengths_method, |
| 5071 | expected_output, |
| 5072 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5073 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5074 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5075 | } |
| 5076 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5077 | 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] | 5078 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5079 | mbedtls_test_set_step(2000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5080 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5081 | if (do_set_lengths) { |
| 5082 | if (data_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5083 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5084 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5085 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5086 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5087 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5088 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5089 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5090 | alg_arg, nonce, |
| 5091 | additional_data, -1, |
| 5092 | input_data, data_part_len, |
| 5093 | set_lengths_method, |
| 5094 | expected_output, |
| 5095 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5096 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5097 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5098 | |
| 5099 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5100 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5101 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5102 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5103 | alg_arg, nonce, |
| 5104 | additional_data, -1, |
| 5105 | input_data, data_part_len, |
| 5106 | set_lengths_method, |
| 5107 | expected_output, |
| 5108 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5109 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5110 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5111 | } |
| 5112 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5113 | /* Goto is required to silence warnings about unused labels, as we |
| 5114 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5115 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5116 | } |
| 5117 | /* END_CASE */ |
| 5118 | |
| 5119 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5120 | void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data, |
| 5121 | int alg_arg, |
| 5122 | int nonce_length, |
| 5123 | int expected_nonce_length_arg, |
| 5124 | data_t *additional_data, |
| 5125 | data_t *input_data, |
| 5126 | int expected_status_arg) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5127 | { |
| 5128 | |
| 5129 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5130 | psa_key_type_t key_type = key_type_arg; |
| 5131 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5132 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5133 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5134 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5135 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5136 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5137 | size_t actual_nonce_length = 0; |
| 5138 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 5139 | unsigned char *output = NULL; |
| 5140 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5141 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5142 | size_t ciphertext_size = 0; |
| 5143 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5144 | size_t tag_length = 0; |
| 5145 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5146 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5147 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5149 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5150 | psa_set_key_algorithm(&attributes, alg); |
| 5151 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5153 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5154 | &key)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5155 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5156 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5157 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5158 | 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] | 5159 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5160 | TEST_CALLOC(output, output_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5161 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5162 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5164 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5165 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5166 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5167 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5168 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5169 | |
| 5170 | /* If the operation is not supported, just skip and not fail in case the |
| 5171 | * encryption involves a common limitation of cryptography hardwares and |
| 5172 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5173 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5174 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5175 | 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] | 5176 | } |
| 5177 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5178 | PSA_ASSERT(status); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5179 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5180 | status = psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5181 | nonce_length, |
| 5182 | &actual_nonce_length); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5183 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5184 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5185 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5186 | TEST_EQUAL(actual_nonce_length, expected_nonce_length); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 5187 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5188 | if (expected_status == PSA_SUCCESS) { |
| 5189 | TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type, |
| 5190 | alg)); |
| 5191 | } |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 5192 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5193 | TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 5194 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5195 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5196 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5197 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5198 | input_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5199 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5200 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5201 | additional_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5202 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5203 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5204 | output, output_size, |
| 5205 | &ciphertext_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5206 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5207 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5208 | &ciphertext_length, tag_buffer, |
| 5209 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5210 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5211 | |
| 5212 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5213 | psa_destroy_key(key); |
| 5214 | mbedtls_free(output); |
| 5215 | mbedtls_free(ciphertext); |
| 5216 | psa_aead_abort(&operation); |
| 5217 | PSA_DONE(); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5218 | } |
| 5219 | /* END_CASE */ |
| 5220 | |
| 5221 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5222 | void aead_multipart_set_nonce(int key_type_arg, data_t *key_data, |
| 5223 | int alg_arg, |
| 5224 | int nonce_length_arg, |
| 5225 | int set_lengths_method_arg, |
| 5226 | data_t *additional_data, |
| 5227 | data_t *input_data, |
| 5228 | int expected_status_arg) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5229 | { |
| 5230 | |
| 5231 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5232 | psa_key_type_t key_type = key_type_arg; |
| 5233 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5234 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5235 | uint8_t *nonce_buffer = NULL; |
| 5236 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5237 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5238 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5239 | unsigned char *output = NULL; |
| 5240 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5241 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5242 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5243 | size_t ciphertext_size = 0; |
| 5244 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5245 | size_t tag_length = 0; |
| 5246 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5247 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5248 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5249 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5250 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5252 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5253 | psa_set_key_algorithm(&attributes, alg); |
| 5254 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5256 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5257 | &key)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5258 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5259 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5260 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5261 | 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] | 5262 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5263 | TEST_CALLOC(output, output_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5264 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5265 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5266 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5267 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5268 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5269 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5270 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5271 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5272 | |
| 5273 | /* If the operation is not supported, just skip and not fail in case the |
| 5274 | * encryption involves a common limitation of cryptography hardwares and |
| 5275 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5276 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5277 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5278 | 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] | 5279 | } |
| 5280 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5281 | PSA_ASSERT(status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5282 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5283 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5284 | if (nonce_length_arg == -1) { |
| 5285 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5286 | TEST_CALLOC(nonce_buffer, 4); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5287 | nonce_length = 0; |
| 5288 | } else { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5289 | /* If length is zero, then this will return NULL. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5290 | nonce_length = (size_t) nonce_length_arg; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5291 | TEST_CALLOC(nonce_buffer, nonce_length); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5292 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5293 | if (nonce_buffer) { |
| 5294 | for (index = 0; index < nonce_length - 1; ++index) { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5295 | nonce_buffer[index] = 'a' + index; |
| 5296 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5297 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5298 | } |
| 5299 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5300 | if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { |
| 5301 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5302 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5303 | } |
| 5304 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5305 | status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5306 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5307 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5308 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5309 | if (expected_status == PSA_SUCCESS) { |
| 5310 | if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) { |
| 5311 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5312 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5313 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5314 | 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] | 5315 | expected_status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5316 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5317 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5318 | /* 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] | 5319 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5320 | additional_data->len), |
| 5321 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5322 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5323 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5324 | output, output_size, |
| 5325 | &ciphertext_length), |
| 5326 | expected_status); |
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 | TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5329 | &ciphertext_length, tag_buffer, |
| 5330 | PSA_AEAD_TAG_MAX_SIZE, &tag_length), |
| 5331 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5332 | } |
| 5333 | |
| 5334 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5335 | psa_destroy_key(key); |
| 5336 | mbedtls_free(output); |
| 5337 | mbedtls_free(ciphertext); |
| 5338 | mbedtls_free(nonce_buffer); |
| 5339 | psa_aead_abort(&operation); |
| 5340 | PSA_DONE(); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5341 | } |
| 5342 | /* END_CASE */ |
| 5343 | |
| 5344 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5345 | 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] | 5346 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5347 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5348 | data_t *nonce, |
| 5349 | data_t *additional_data, |
| 5350 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5351 | int expected_status_arg) |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5352 | { |
| 5353 | |
| 5354 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5355 | psa_key_type_t key_type = key_type_arg; |
| 5356 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5357 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5358 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5359 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5360 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5361 | unsigned char *output = NULL; |
| 5362 | unsigned char *ciphertext = NULL; |
| 5363 | size_t output_size = output_size_arg; |
| 5364 | size_t ciphertext_size = 0; |
| 5365 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5366 | size_t tag_length = 0; |
| 5367 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5368 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5369 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5371 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5372 | psa_set_key_algorithm(&attributes, alg); |
| 5373 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5374 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5375 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5376 | &key)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5377 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5378 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5379 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5380 | TEST_CALLOC(output, output_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5381 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5382 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5383 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5384 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5386 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5387 | |
| 5388 | /* If the operation is not supported, just skip and not fail in case the |
| 5389 | * encryption involves a common limitation of cryptography hardwares and |
| 5390 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5391 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5392 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5393 | 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] | 5394 | } |
| 5395 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5396 | PSA_ASSERT(status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5397 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5398 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5399 | input_data->len)); |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5400 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5401 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5402 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5403 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5404 | additional_data->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5405 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5406 | status = psa_aead_update(&operation, input_data->x, input_data->len, |
| 5407 | output, output_size, &ciphertext_length); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5408 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5409 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5410 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5411 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5412 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5413 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5414 | &ciphertext_length, tag_buffer, |
| 5415 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5416 | } |
| 5417 | |
| 5418 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5419 | psa_destroy_key(key); |
| 5420 | mbedtls_free(output); |
| 5421 | mbedtls_free(ciphertext); |
| 5422 | psa_aead_abort(&operation); |
| 5423 | PSA_DONE(); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5424 | } |
| 5425 | /* END_CASE */ |
| 5426 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5427 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5428 | void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data, |
| 5429 | int alg_arg, |
| 5430 | int finish_ciphertext_size_arg, |
| 5431 | int tag_size_arg, |
| 5432 | data_t *nonce, |
| 5433 | data_t *additional_data, |
| 5434 | data_t *input_data, |
| 5435 | int expected_status_arg) |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5436 | { |
| 5437 | |
| 5438 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5439 | psa_key_type_t key_type = key_type_arg; |
| 5440 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5441 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5442 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5443 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5444 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5445 | unsigned char *ciphertext = NULL; |
| 5446 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5447 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5448 | size_t ciphertext_size = 0; |
| 5449 | size_t ciphertext_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5450 | size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg; |
| 5451 | size_t tag_size = (size_t) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5452 | size_t tag_length = 0; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5453 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5454 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5455 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5456 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5457 | psa_set_key_algorithm(&attributes, alg); |
| 5458 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5459 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5460 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5461 | &key)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5462 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5463 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5464 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5465 | 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] | 5466 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5467 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5468 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5469 | TEST_CALLOC(finish_ciphertext, finish_ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5470 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5471 | TEST_CALLOC(tag_buffer, tag_size); |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5472 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5473 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5474 | |
| 5475 | /* If the operation is not supported, just skip and not fail in case the |
| 5476 | * encryption involves a common limitation of cryptography hardwares and |
| 5477 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5478 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5479 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5480 | 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] | 5481 | } |
| 5482 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5483 | PSA_ASSERT(status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5484 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5485 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5486 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5487 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5488 | input_data->len)); |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5489 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5490 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5491 | additional_data->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5492 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5493 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5494 | ciphertext, ciphertext_size, &ciphertext_length)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5495 | |
| 5496 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5497 | status = psa_aead_finish(&operation, finish_ciphertext, |
| 5498 | finish_ciphertext_size, |
| 5499 | &ciphertext_length, tag_buffer, |
| 5500 | tag_size, &tag_length); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5501 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5502 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5503 | |
| 5504 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5505 | psa_destroy_key(key); |
| 5506 | mbedtls_free(ciphertext); |
| 5507 | mbedtls_free(finish_ciphertext); |
| 5508 | mbedtls_free(tag_buffer); |
| 5509 | psa_aead_abort(&operation); |
| 5510 | PSA_DONE(); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5511 | } |
| 5512 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5513 | |
| 5514 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5515 | void aead_multipart_verify(int key_type_arg, data_t *key_data, |
| 5516 | int alg_arg, |
| 5517 | data_t *nonce, |
| 5518 | data_t *additional_data, |
| 5519 | data_t *input_data, |
| 5520 | data_t *tag, |
| 5521 | int tag_usage_arg, |
| 5522 | int expected_setup_status_arg, |
| 5523 | int expected_status_arg) |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5524 | { |
| 5525 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5526 | psa_key_type_t key_type = key_type_arg; |
| 5527 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5528 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5529 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5530 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5531 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5532 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5533 | unsigned char *plaintext = NULL; |
| 5534 | unsigned char *finish_plaintext = NULL; |
| 5535 | size_t plaintext_size = 0; |
| 5536 | size_t plaintext_length = 0; |
| 5537 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5538 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5539 | unsigned char *tag_buffer = NULL; |
| 5540 | size_t tag_size = 0; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5541 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5542 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5543 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5544 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 5545 | psa_set_key_algorithm(&attributes, alg); |
| 5546 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5547 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5548 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5549 | &key)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5550 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5551 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5552 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5553 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 5554 | input_data->len); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5555 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5556 | TEST_CALLOC(plaintext, plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5557 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5558 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5559 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5560 | TEST_CALLOC(finish_plaintext, verify_plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5562 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5563 | |
| 5564 | /* If the operation is not supported, just skip and not fail in case the |
| 5565 | * encryption involves a common limitation of cryptography hardwares and |
| 5566 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5567 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5568 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5569 | 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] | 5570 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5571 | TEST_EQUAL(status, expected_setup_status); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5572 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5573 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5574 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5575 | } |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5576 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5577 | PSA_ASSERT(status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5578 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5579 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5580 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5581 | status = psa_aead_set_lengths(&operation, additional_data->len, |
| 5582 | input_data->len); |
| 5583 | PSA_ASSERT(status); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5584 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5585 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5586 | additional_data->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5587 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5588 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 5589 | input_data->len, |
| 5590 | plaintext, plaintext_size, |
| 5591 | &plaintext_length)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5592 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5593 | if (tag_usage == USE_GIVEN_TAG) { |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5594 | tag_buffer = tag->x; |
| 5595 | tag_size = tag->len; |
| 5596 | } |
| 5597 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5598 | status = psa_aead_verify(&operation, finish_plaintext, |
| 5599 | verify_plaintext_size, |
| 5600 | &plaintext_length, |
| 5601 | tag_buffer, tag_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5602 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5603 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5604 | |
| 5605 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5606 | psa_destroy_key(key); |
| 5607 | mbedtls_free(plaintext); |
| 5608 | mbedtls_free(finish_plaintext); |
| 5609 | psa_aead_abort(&operation); |
| 5610 | PSA_DONE(); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5611 | } |
| 5612 | /* END_CASE */ |
| 5613 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5614 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5615 | void aead_multipart_setup(int key_type_arg, data_t *key_data, |
| 5616 | int alg_arg, int expected_status_arg) |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5617 | { |
| 5618 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5619 | psa_key_type_t key_type = key_type_arg; |
| 5620 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5621 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5622 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5623 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5624 | psa_status_t expected_status = expected_status_arg; |
| 5625 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5626 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5627 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5628 | psa_set_key_usage_flags(&attributes, |
| 5629 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5630 | psa_set_key_algorithm(&attributes, alg); |
| 5631 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5632 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5633 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5634 | &key)); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5635 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5636 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5637 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5638 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5639 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5640 | psa_aead_abort(&operation); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5642 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5643 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5644 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5645 | |
| 5646 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5647 | psa_destroy_key(key); |
| 5648 | psa_aead_abort(&operation); |
| 5649 | PSA_DONE(); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5650 | } |
| 5651 | /* END_CASE */ |
| 5652 | |
| 5653 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5654 | void aead_multipart_state_test(int key_type_arg, data_t *key_data, |
| 5655 | int alg_arg, |
| 5656 | data_t *nonce, |
| 5657 | data_t *additional_data, |
| 5658 | data_t *input_data) |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5659 | { |
| 5660 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5661 | psa_key_type_t key_type = key_type_arg; |
| 5662 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5663 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5664 | unsigned char *output_data = NULL; |
| 5665 | unsigned char *final_data = NULL; |
| 5666 | size_t output_size = 0; |
| 5667 | size_t finish_output_size = 0; |
| 5668 | size_t output_length = 0; |
| 5669 | size_t key_bits = 0; |
| 5670 | size_t tag_length = 0; |
| 5671 | size_t tag_size = 0; |
| 5672 | size_t nonce_length = 0; |
| 5673 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5674 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5675 | size_t output_part_length = 0; |
| 5676 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5677 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5678 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5679 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5680 | psa_set_key_usage_flags(&attributes, |
| 5681 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5682 | psa_set_key_algorithm(&attributes, alg); |
| 5683 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5684 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5685 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5686 | &key)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5687 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5688 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5689 | key_bits = psa_get_key_bits(&attributes); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5690 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5691 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5692 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5693 | TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5694 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5695 | 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] | 5696 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5697 | TEST_CALLOC(output_data, output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5698 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5699 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5700 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5701 | TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5702 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5703 | TEST_CALLOC(final_data, finish_output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5704 | |
| 5705 | /* Test all operations error without calling setup first. */ |
| 5706 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5707 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5708 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5709 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5710 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5711 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5712 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5713 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5714 | &nonce_length), |
| 5715 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5716 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5717 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5718 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5719 | /* ------------------------------------------------------- */ |
| 5720 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5721 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 5722 | input_data->len), |
| 5723 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5724 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5725 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5726 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5727 | /* ------------------------------------------------------- */ |
| 5728 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5729 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5730 | additional_data->len), |
| 5731 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5732 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5733 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5734 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5735 | /* ------------------------------------------------------- */ |
| 5736 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5737 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 5738 | input_data->len, output_data, |
| 5739 | output_size, &output_length), |
| 5740 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5741 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5742 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5743 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5744 | /* ------------------------------------------------------- */ |
| 5745 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5746 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 5747 | finish_output_size, |
| 5748 | &output_part_length, |
| 5749 | tag_buffer, tag_length, |
| 5750 | &tag_size), |
| 5751 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5752 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5753 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5754 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5755 | /* ------------------------------------------------------- */ |
| 5756 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5757 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 5758 | finish_output_size, |
| 5759 | &output_part_length, |
| 5760 | tag_buffer, |
| 5761 | tag_length), |
| 5762 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5763 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5764 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5765 | |
| 5766 | /* Test for double setups. */ |
| 5767 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5768 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5769 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5770 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 5771 | PSA_ERROR_BAD_STATE); |
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 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5774 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5775 | /* ------------------------------------------------------- */ |
| 5776 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5777 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, 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_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 5780 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5781 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5782 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5783 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5784 | /* ------------------------------------------------------- */ |
| 5785 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5786 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5787 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5788 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 5789 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5790 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5791 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5792 | |
| 5793 | /* ------------------------------------------------------- */ |
| 5794 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5795 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5796 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5797 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 5798 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5799 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5800 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5801 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5802 | /* Test for not setting a nonce. */ |
| 5803 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5804 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5805 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5806 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5807 | additional_data->len), |
| 5808 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5809 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5810 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5811 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5812 | /* ------------------------------------------------------- */ |
| 5813 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5814 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5815 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5816 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 5817 | input_data->len, output_data, |
| 5818 | output_size, &output_length), |
| 5819 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5820 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5821 | psa_aead_abort(&operation); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5822 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5823 | /* ------------------------------------------------------- */ |
| 5824 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5825 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5826 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5827 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 5828 | finish_output_size, |
| 5829 | &output_part_length, |
| 5830 | tag_buffer, tag_length, |
| 5831 | &tag_size), |
| 5832 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5833 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5834 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5835 | |
| 5836 | /* ------------------------------------------------------- */ |
| 5837 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5838 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5839 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5840 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 5841 | finish_output_size, |
| 5842 | &output_part_length, |
| 5843 | tag_buffer, |
| 5844 | tag_length), |
| 5845 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5846 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5847 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5848 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5849 | /* Test for double setting nonce. */ |
| 5850 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5851 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5852 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5853 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5854 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5855 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5856 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5857 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5858 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5859 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5860 | /* Test for double generating nonce. */ |
| 5861 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5862 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5863 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5864 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5865 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5866 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5867 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5868 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5869 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5870 | &nonce_length), |
| 5871 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5872 | |
| 5873 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5874 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5875 | |
| 5876 | /* Test for generate nonce then set and vice versa */ |
| 5877 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5878 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5879 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5880 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5881 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5882 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5883 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5884 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5885 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5886 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5887 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5888 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5889 | /* Test for generating nonce after calling set lengths */ |
| 5890 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5891 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5892 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5893 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5894 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5895 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5896 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5897 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5898 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5899 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5900 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5901 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5902 | /* 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] | 5903 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5904 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5905 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5906 | if (operation.alg == PSA_ALG_CCM) { |
| 5907 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5908 | input_data->len), |
| 5909 | PSA_ERROR_INVALID_ARGUMENT); |
| 5910 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5911 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5912 | &nonce_length), |
| 5913 | PSA_ERROR_BAD_STATE); |
| 5914 | } else { |
| 5915 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5916 | input_data->len)); |
| 5917 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5918 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5919 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5920 | } |
| 5921 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5922 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5923 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5924 | /* 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] | 5925 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5926 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5927 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5928 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 5929 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5930 | input_data->len), |
| 5931 | PSA_ERROR_INVALID_ARGUMENT); |
| 5932 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5933 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5934 | &nonce_length), |
| 5935 | PSA_ERROR_BAD_STATE); |
| 5936 | } else { |
| 5937 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5938 | input_data->len)); |
| 5939 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5940 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5941 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5942 | } |
| 5943 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5944 | psa_aead_abort(&operation); |
Dave Rodgman | d26d744 | 2023-02-11 17:14:54 +0000 | [diff] [blame] | 5945 | #endif |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5946 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5947 | /* 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] | 5948 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5949 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5950 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5951 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5952 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5953 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5954 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5955 | if (operation.alg == PSA_ALG_CCM) { |
| 5956 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5957 | input_data->len), |
| 5958 | PSA_ERROR_INVALID_ARGUMENT); |
| 5959 | } else { |
| 5960 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5961 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5962 | } |
| 5963 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5964 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5965 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5966 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5967 | /* Test for setting 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 | 1e8e174 | 2021-12-25 23:50:53 +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 | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5973 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5974 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5975 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5976 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5977 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5978 | /* 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] | 5979 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5980 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5981 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5982 | if (operation.alg == PSA_ALG_CCM) { |
| 5983 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5984 | input_data->len), |
| 5985 | PSA_ERROR_INVALID_ARGUMENT); |
| 5986 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5987 | PSA_ERROR_BAD_STATE); |
| 5988 | } else { |
| 5989 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5990 | input_data->len)); |
| 5991 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5992 | } |
| 5993 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5994 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5995 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5996 | /* 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] | 5997 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5998 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5999 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6000 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 6001 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 6002 | input_data->len), |
| 6003 | PSA_ERROR_INVALID_ARGUMENT); |
| 6004 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6005 | PSA_ERROR_BAD_STATE); |
| 6006 | } else { |
| 6007 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 6008 | input_data->len)); |
| 6009 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6010 | } |
| 6011 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6012 | psa_aead_abort(&operation); |
Dave Rodgman | a476363 | 2023-02-11 18:36:23 +0000 | [diff] [blame] | 6013 | #endif |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6014 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6015 | /* 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] | 6016 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6017 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6018 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6019 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6020 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6021 | if (operation.alg == PSA_ALG_CCM) { |
| 6022 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6023 | input_data->len), |
| 6024 | PSA_ERROR_INVALID_ARGUMENT); |
| 6025 | } else { |
| 6026 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6027 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6028 | } |
| 6029 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6030 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6031 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6032 | /* 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] | 6033 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6034 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6035 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6036 | if (operation.alg == PSA_ALG_GCM) { |
| 6037 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6038 | SIZE_MAX), |
| 6039 | PSA_ERROR_INVALID_ARGUMENT); |
| 6040 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6041 | PSA_ERROR_BAD_STATE); |
| 6042 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6043 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6044 | SIZE_MAX)); |
| 6045 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6046 | } |
| 6047 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6048 | psa_aead_abort(&operation); |
Dave Rodgman | 91e8321 | 2023-02-11 20:07:43 +0000 | [diff] [blame] | 6049 | #endif |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6050 | |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 6051 | /* 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] | 6052 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6053 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6054 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6055 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6056 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6057 | if (operation.alg == PSA_ALG_GCM) { |
| 6058 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6059 | SIZE_MAX), |
| 6060 | PSA_ERROR_INVALID_ARGUMENT); |
| 6061 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6062 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6063 | SIZE_MAX)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6064 | } |
| 6065 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6066 | psa_aead_abort(&operation); |
Dave Rodgman | 641288b | 2023-02-11 22:02:04 +0000 | [diff] [blame] | 6067 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6068 | |
| 6069 | /* ------------------------------------------------------- */ |
| 6070 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6071 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6072 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6073 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6074 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6075 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6076 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6077 | &nonce_length), |
| 6078 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6079 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6080 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6081 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6082 | /* Test for generating nonce in decrypt setup. */ |
| 6083 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6084 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6085 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6086 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6087 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6088 | &nonce_length), |
| 6089 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6090 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6091 | psa_aead_abort(&operation); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6092 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6093 | /* Test for setting lengths twice. */ |
| 6094 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6095 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +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)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6098 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6099 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6100 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6101 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6102 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6103 | input_data->len), |
| 6104 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6105 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6106 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6107 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6108 | /* Test for setting lengths after setting nonce + already starting data. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6109 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6110 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6111 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6112 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6114 | if (operation.alg == PSA_ALG_CCM) { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6115 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6116 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6117 | additional_data->len), |
| 6118 | PSA_ERROR_BAD_STATE); |
| 6119 | } else { |
| 6120 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6121 | additional_data->len)); |
| 6122 | |
| 6123 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6124 | input_data->len), |
| 6125 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6126 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6127 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6128 | |
| 6129 | /* ------------------------------------------------------- */ |
| 6130 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6131 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +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)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6135 | if (operation.alg == PSA_ALG_CCM) { |
| 6136 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6137 | input_data->len, output_data, |
| 6138 | output_size, &output_length), |
| 6139 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6140 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6141 | } else { |
| 6142 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6143 | input_data->len, output_data, |
| 6144 | output_size, &output_length)); |
| 6145 | |
| 6146 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6147 | input_data->len), |
| 6148 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6149 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6150 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6151 | |
| 6152 | /* ------------------------------------------------------- */ |
| 6153 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6154 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6155 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6156 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6157 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6158 | if (operation.alg == PSA_ALG_CCM) { |
| 6159 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6160 | finish_output_size, |
| 6161 | &output_part_length, |
| 6162 | tag_buffer, tag_length, |
| 6163 | &tag_size)); |
| 6164 | } else { |
| 6165 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6166 | finish_output_size, |
| 6167 | &output_part_length, |
| 6168 | tag_buffer, tag_length, |
| 6169 | &tag_size)); |
| 6170 | |
| 6171 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6172 | input_data->len), |
| 6173 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6174 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6175 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6176 | |
| 6177 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 6178 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6179 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6181 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6182 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6183 | &nonce_length)); |
| 6184 | if (operation.alg == PSA_ALG_CCM) { |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6185 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6186 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6187 | additional_data->len), |
| 6188 | PSA_ERROR_BAD_STATE); |
| 6189 | } else { |
| 6190 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6191 | additional_data->len)); |
| 6192 | |
| 6193 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6194 | input_data->len), |
| 6195 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6196 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6197 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6198 | |
| 6199 | /* ------------------------------------------------------- */ |
| 6200 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6201 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6202 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6203 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6204 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6205 | &nonce_length)); |
| 6206 | if (operation.alg == PSA_ALG_CCM) { |
| 6207 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6208 | input_data->len, output_data, |
| 6209 | output_size, &output_length), |
| 6210 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6211 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6212 | } else { |
| 6213 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6214 | input_data->len, output_data, |
| 6215 | output_size, &output_length)); |
| 6216 | |
| 6217 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6218 | input_data->len), |
| 6219 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6220 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6221 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6222 | |
| 6223 | /* ------------------------------------------------------- */ |
| 6224 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6225 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6226 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6227 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6228 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6229 | &nonce_length)); |
| 6230 | if (operation.alg == PSA_ALG_CCM) { |
| 6231 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6232 | finish_output_size, |
| 6233 | &output_part_length, |
| 6234 | tag_buffer, tag_length, |
| 6235 | &tag_size)); |
| 6236 | } else { |
| 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)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6242 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6243 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6244 | input_data->len), |
| 6245 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6246 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6247 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6248 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6249 | /* Test for not sending any additional data or data after setting non zero |
| 6250 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6252 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6253 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6254 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6256 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6257 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6258 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6259 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6260 | finish_output_size, |
| 6261 | &output_part_length, |
| 6262 | tag_buffer, tag_length, |
| 6263 | &tag_size), |
| 6264 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6266 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6267 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6268 | /* Test for not sending any additional data or data after setting non-zero |
| 6269 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6270 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6271 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6272 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6273 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6274 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6275 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6276 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6277 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6278 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6279 | finish_output_size, |
| 6280 | &output_part_length, |
| 6281 | tag_buffer, |
| 6282 | tag_length), |
| 6283 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6284 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6285 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6286 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6287 | /* Test for not sending any additional data after setting a non-zero length |
| 6288 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6289 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6290 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6291 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6292 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6293 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6294 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6295 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6296 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6297 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6298 | input_data->len, output_data, |
| 6299 | output_size, &output_length), |
| 6300 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6301 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6302 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6303 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6304 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 6305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6306 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6307 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6308 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6309 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6310 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6311 | input_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6312 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6313 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6314 | additional_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6315 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6316 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6317 | finish_output_size, |
| 6318 | &output_part_length, |
| 6319 | tag_buffer, tag_length, |
| 6320 | &tag_size), |
| 6321 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6322 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6323 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6324 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6325 | /* Test for sending too much additional data after setting lengths. */ |
| 6326 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6327 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6328 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6329 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6330 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6331 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6332 | |
| 6333 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6334 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6335 | additional_data->len), |
| 6336 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6337 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6338 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6339 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6340 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6341 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6342 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6343 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6344 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6345 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6346 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6347 | input_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6349 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6350 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6352 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6353 | 1), |
| 6354 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6356 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6357 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6358 | /* Test for sending too much data after setting lengths. */ |
| 6359 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6360 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6361 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6362 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6363 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6364 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6365 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6366 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6367 | input_data->len, output_data, |
| 6368 | output_size, &output_length), |
| 6369 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6371 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6372 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6373 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6374 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6375 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6377 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6378 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6379 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6380 | input_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6381 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6382 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6383 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6385 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6386 | input_data->len, output_data, |
| 6387 | output_size, &output_length)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6389 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6390 | 1, output_data, |
| 6391 | output_size, &output_length), |
| 6392 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6393 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6394 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6395 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6396 | /* Test sending additional data after data. */ |
| 6397 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6398 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6399 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6400 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6401 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6402 | if (operation.alg != PSA_ALG_CCM) { |
| 6403 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6404 | input_data->len, output_data, |
| 6405 | output_size, &output_length)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6406 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6407 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6408 | additional_data->len), |
| 6409 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6410 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6411 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6412 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6413 | /* Test calling finish on decryption. */ |
| 6414 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6415 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6416 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6417 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6418 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6419 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6420 | finish_output_size, |
| 6421 | &output_part_length, |
| 6422 | tag_buffer, tag_length, |
| 6423 | &tag_size), |
| 6424 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6425 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6426 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6427 | |
| 6428 | /* Test calling verify on encryption. */ |
| 6429 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6430 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6431 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6432 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6433 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6434 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6435 | finish_output_size, |
| 6436 | &output_part_length, |
| 6437 | tag_buffer, |
| 6438 | tag_length), |
| 6439 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6440 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6441 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6442 | |
| 6443 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6444 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6445 | psa_destroy_key(key); |
| 6446 | psa_aead_abort(&operation); |
| 6447 | mbedtls_free(output_data); |
| 6448 | mbedtls_free(final_data); |
| 6449 | PSA_DONE(); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6450 | } |
| 6451 | /* END_CASE */ |
| 6452 | |
| 6453 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6454 | void signature_size(int type_arg, |
| 6455 | int bits, |
| 6456 | int alg_arg, |
| 6457 | int expected_size_arg) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6458 | { |
| 6459 | psa_key_type_t type = type_arg; |
| 6460 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6461 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6462 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6463 | TEST_EQUAL(actual_size, (size_t) expected_size_arg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6464 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6465 | exit: |
| 6466 | ; |
| 6467 | } |
| 6468 | /* END_CASE */ |
| 6469 | |
| 6470 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6471 | void sign_hash_deterministic(int key_type_arg, data_t *key_data, |
| 6472 | int alg_arg, data_t *input_data, |
| 6473 | data_t *output_data) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6474 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6475 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6476 | psa_key_type_t key_type = key_type_arg; |
| 6477 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6478 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6479 | unsigned char *signature = NULL; |
| 6480 | size_t signature_size; |
| 6481 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6482 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6483 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6484 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6485 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6486 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6487 | psa_set_key_algorithm(&attributes, alg); |
| 6488 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6489 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6490 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6491 | &key)); |
| 6492 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6493 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6494 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6495 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6496 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6497 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6498 | key_bits, alg); |
| 6499 | TEST_ASSERT(signature_size != 0); |
| 6500 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6501 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6502 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6503 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6504 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6505 | input_data->x, input_data->len, |
| 6506 | signature, signature_size, |
| 6507 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6508 | /* Verify that the signature is what is expected. */ |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 6509 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 6510 | signature, signature_length); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6511 | |
| 6512 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6513 | /* |
| 6514 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6515 | * thus reset them as required. |
| 6516 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6517 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6518 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6519 | psa_destroy_key(key); |
| 6520 | mbedtls_free(signature); |
| 6521 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6522 | } |
| 6523 | /* END_CASE */ |
| 6524 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6525 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6526 | /** |
| 6527 | * sign_hash_interruptible() test intentions: |
| 6528 | * |
| 6529 | * Note: This test can currently only handle ECDSA. |
| 6530 | * |
| 6531 | * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 6532 | * and private keys / keypairs only). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6533 | * |
| 6534 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 6535 | * expected for different max_ops values. |
| 6536 | * |
| 6537 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 6538 | * and that each successful stage completes some ops (this is not mandated by |
| 6539 | * the PSA specification, but is currently the case). |
| 6540 | * |
| 6541 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 6542 | * complete() calls does not alter the number of ops returned. |
| 6543 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6544 | void sign_hash_interruptible(int key_type_arg, data_t *key_data, |
| 6545 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6546 | data_t *output_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6547 | { |
| 6548 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6549 | psa_key_type_t key_type = key_type_arg; |
| 6550 | psa_algorithm_t alg = alg_arg; |
| 6551 | size_t key_bits; |
| 6552 | unsigned char *signature = NULL; |
| 6553 | size_t signature_size; |
| 6554 | size_t signature_length = 0xdeadbeef; |
| 6555 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6556 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6557 | uint32_t num_ops = 0; |
| 6558 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6559 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6560 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6561 | size_t min_completes = 0; |
| 6562 | size_t max_completes = 0; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6563 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6564 | psa_sign_hash_interruptible_operation_t operation = |
| 6565 | psa_sign_hash_interruptible_operation_init(); |
| 6566 | |
| 6567 | PSA_ASSERT(psa_crypto_init()); |
| 6568 | |
| 6569 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6570 | psa_set_key_algorithm(&attributes, alg); |
| 6571 | psa_set_key_type(&attributes, key_type); |
| 6572 | |
| 6573 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6574 | &key)); |
| 6575 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6576 | key_bits = psa_get_key_bits(&attributes); |
| 6577 | |
| 6578 | /* Allocate a buffer which has the size advertised by the |
| 6579 | * library. */ |
| 6580 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6581 | key_bits, alg); |
| 6582 | TEST_ASSERT(signature_size != 0); |
| 6583 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6584 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6585 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6586 | psa_interruptible_set_max_ops(max_ops); |
| 6587 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6588 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6589 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6590 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6591 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6592 | TEST_ASSERT(num_ops_prior == 0); |
| 6593 | |
| 6594 | /* Start performing the signature. */ |
| 6595 | PSA_ASSERT(psa_sign_hash_start(&operation, key, alg, |
| 6596 | input_data->x, input_data->len)); |
| 6597 | |
| 6598 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6599 | TEST_ASSERT(num_ops_prior == 0); |
| 6600 | |
| 6601 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6602 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6603 | status = psa_sign_hash_complete(&operation, signature, signature_size, |
| 6604 | &signature_length); |
| 6605 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6606 | num_completes++; |
| 6607 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6608 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 6609 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6610 | /* We are asserting here that every complete makes progress |
| 6611 | * (completes some ops), which is true of the internal |
| 6612 | * implementation and probably any implementation, however this is |
| 6613 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6614 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6615 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6616 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 6617 | |
| 6618 | /* Ensure calling get_num_ops() twice still returns the same |
| 6619 | * number of ops as previously reported. */ |
| 6620 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6621 | |
| 6622 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6623 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6624 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6625 | |
| 6626 | TEST_ASSERT(status == PSA_SUCCESS); |
| 6627 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6628 | TEST_LE_U(min_completes, num_completes); |
| 6629 | TEST_LE_U(num_completes, max_completes); |
| 6630 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6631 | /* Verify that the signature is what is expected. */ |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 6632 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 6633 | signature, signature_length); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6634 | |
| 6635 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6636 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6637 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6638 | TEST_ASSERT(num_ops == 0); |
| 6639 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6640 | exit: |
| 6641 | |
| 6642 | /* |
| 6643 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6644 | * thus reset them as required. |
| 6645 | */ |
| 6646 | psa_reset_key_attributes(&attributes); |
| 6647 | |
| 6648 | psa_destroy_key(key); |
| 6649 | mbedtls_free(signature); |
| 6650 | PSA_DONE(); |
| 6651 | } |
| 6652 | /* END_CASE */ |
| 6653 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6654 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6655 | void sign_hash_fail(int key_type_arg, data_t *key_data, |
| 6656 | int alg_arg, data_t *input_data, |
| 6657 | int signature_size_arg, int expected_status_arg) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6658 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6659 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6660 | psa_key_type_t key_type = key_type_arg; |
| 6661 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6662 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6663 | psa_status_t actual_status; |
| 6664 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 6665 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 6666 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6667 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6668 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6669 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6670 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6671 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6672 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6673 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6674 | psa_set_key_algorithm(&attributes, alg); |
| 6675 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6676 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6677 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6678 | &key)); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6679 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6680 | actual_status = psa_sign_hash(key, alg, |
| 6681 | input_data->x, input_data->len, |
| 6682 | signature, signature_size, |
| 6683 | &signature_length); |
| 6684 | TEST_EQUAL(actual_status, expected_status); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6685 | /* The value of *signature_length is unspecified on error, but |
| 6686 | * whatever it is, it should be less than signature_size, so that |
| 6687 | * if the caller tries to read *signature_length bytes without |
| 6688 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6689 | TEST_LE_U(signature_length, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6690 | |
| 6691 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6692 | psa_reset_key_attributes(&attributes); |
| 6693 | psa_destroy_key(key); |
| 6694 | mbedtls_free(signature); |
| 6695 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6696 | } |
| 6697 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6698 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6699 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6700 | /** |
| 6701 | * sign_hash_fail_interruptible() test intentions: |
| 6702 | * |
| 6703 | * Note: This test can currently only handle ECDSA. |
| 6704 | * |
| 6705 | * 1. Test that various failure cases for interruptible sign hash fail with the |
| 6706 | * correct error codes, and at the correct point (at start or during |
| 6707 | * complete). |
| 6708 | * |
| 6709 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 6710 | * expected for different max_ops values. |
| 6711 | * |
| 6712 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 6713 | * and that each successful stage completes some ops (this is not mandated by |
| 6714 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 6715 | * |
| 6716 | * 4. Check that calling complete() when start() fails and complete() |
| 6717 | * after completion results in a BAD_STATE error. |
| 6718 | * |
| 6719 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 6720 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6721 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6722 | void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 6723 | int alg_arg, data_t *input_data, |
| 6724 | int signature_size_arg, |
| 6725 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6726 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6727 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6728 | { |
| 6729 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6730 | psa_key_type_t key_type = key_type_arg; |
| 6731 | psa_algorithm_t alg = alg_arg; |
| 6732 | size_t signature_size = signature_size_arg; |
| 6733 | psa_status_t actual_status; |
| 6734 | psa_status_t expected_start_status = expected_start_status_arg; |
| 6735 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 6736 | unsigned char *signature = NULL; |
| 6737 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6738 | uint32_t num_ops = 0; |
| 6739 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6740 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6741 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6742 | size_t min_completes = 0; |
| 6743 | size_t max_completes = 0; |
| 6744 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6745 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6746 | psa_sign_hash_interruptible_operation_t operation = |
| 6747 | psa_sign_hash_interruptible_operation_init(); |
| 6748 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6749 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6750 | |
| 6751 | PSA_ASSERT(psa_crypto_init()); |
| 6752 | |
| 6753 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6754 | psa_set_key_algorithm(&attributes, alg); |
| 6755 | psa_set_key_type(&attributes, key_type); |
| 6756 | |
| 6757 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6758 | &key)); |
| 6759 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6760 | psa_interruptible_set_max_ops(max_ops); |
| 6761 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6762 | interruptible_signverify_get_minmax_completes(max_ops, |
| 6763 | expected_complete_status, |
| 6764 | &min_completes, |
| 6765 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6766 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6767 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6768 | TEST_ASSERT(num_ops_prior == 0); |
| 6769 | |
| 6770 | /* Start performing the signature. */ |
| 6771 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 6772 | input_data->x, input_data->len); |
| 6773 | |
| 6774 | TEST_EQUAL(actual_status, expected_start_status); |
| 6775 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6776 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 6777 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 6778 | * start failed. */ |
| 6779 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6780 | signature_size, |
| 6781 | &signature_length); |
| 6782 | |
| 6783 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 6784 | |
| 6785 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6786 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 6787 | input_data->x, input_data->len); |
| 6788 | |
| 6789 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 6790 | } |
| 6791 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6792 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6793 | TEST_ASSERT(num_ops_prior == 0); |
| 6794 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6795 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6796 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6797 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6798 | signature_size, |
| 6799 | &signature_length); |
| 6800 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6801 | num_completes++; |
| 6802 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6803 | if (actual_status == PSA_SUCCESS || |
| 6804 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6805 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6806 | /* We are asserting here that every complete makes progress |
| 6807 | * (completes some ops), which is true of the internal |
| 6808 | * implementation and probably any implementation, however this is |
| 6809 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6810 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6811 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6812 | num_ops_prior = num_ops; |
| 6813 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6814 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6815 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6816 | TEST_EQUAL(actual_status, expected_complete_status); |
| 6817 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 6818 | /* Check that another complete returns BAD_STATE. */ |
| 6819 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6820 | signature_size, |
| 6821 | &signature_length); |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6822 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 6823 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6824 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6825 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6826 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6827 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6828 | TEST_ASSERT(num_ops == 0); |
| 6829 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6830 | /* The value of *signature_length is unspecified on error, but |
| 6831 | * whatever it is, it should be less than signature_size, so that |
| 6832 | * if the caller tries to read *signature_length bytes without |
| 6833 | * checking the error code then they don't overflow a buffer. */ |
| 6834 | TEST_LE_U(signature_length, signature_size); |
| 6835 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6836 | TEST_LE_U(min_completes, num_completes); |
| 6837 | TEST_LE_U(num_completes, max_completes); |
| 6838 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6839 | exit: |
| 6840 | psa_reset_key_attributes(&attributes); |
| 6841 | psa_destroy_key(key); |
| 6842 | mbedtls_free(signature); |
| 6843 | PSA_DONE(); |
| 6844 | } |
| 6845 | /* END_CASE */ |
| 6846 | |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6847 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6848 | void sign_verify_hash(int key_type_arg, data_t *key_data, |
| 6849 | int alg_arg, data_t *input_data) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6850 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6851 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6852 | psa_key_type_t key_type = key_type_arg; |
| 6853 | psa_algorithm_t alg = alg_arg; |
| 6854 | size_t key_bits; |
| 6855 | unsigned char *signature = NULL; |
| 6856 | size_t signature_size; |
| 6857 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6858 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6859 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6860 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6861 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6862 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 6863 | psa_set_key_algorithm(&attributes, alg); |
| 6864 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6865 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6866 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6867 | &key)); |
| 6868 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6869 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6870 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6871 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6872 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6873 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6874 | key_bits, alg); |
| 6875 | TEST_ASSERT(signature_size != 0); |
| 6876 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6877 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6878 | |
| 6879 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6880 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6881 | input_data->x, input_data->len, |
| 6882 | signature, signature_size, |
| 6883 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6884 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6885 | TEST_LE_U(signature_length, signature_size); |
| 6886 | TEST_ASSERT(signature_length > 0); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6887 | |
| 6888 | /* Use the library to verify that the signature is correct. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6889 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 6890 | input_data->x, input_data->len, |
| 6891 | signature, signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6892 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6893 | if (input_data->len != 0) { |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6894 | /* Flip a bit in the input and verify that the signature is now |
| 6895 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6896 | * because ECDSA may ignore the last few bits of the input. */ |
| 6897 | input_data->x[0] ^= 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6898 | TEST_EQUAL(psa_verify_hash(key, alg, |
| 6899 | input_data->x, input_data->len, |
| 6900 | signature, signature_length), |
| 6901 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6902 | } |
| 6903 | |
| 6904 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6905 | /* |
| 6906 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6907 | * thus reset them as required. |
| 6908 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6909 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6910 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6911 | psa_destroy_key(key); |
| 6912 | mbedtls_free(signature); |
| 6913 | PSA_DONE(); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6914 | } |
| 6915 | /* END_CASE */ |
| 6916 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6917 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6918 | /** |
| 6919 | * sign_verify_hash_interruptible() test intentions: |
| 6920 | * |
| 6921 | * Note: This test can currently only handle ECDSA. |
| 6922 | * |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 6923 | * 1. Test that we can sign an input hash with the given keypair and then |
| 6924 | * afterwards verify that signature. This is currently the only way to test |
| 6925 | * non deterministic ECDSA, but this test can also handle deterministic. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6926 | * |
| 6927 | * 2. Test that after corrupting the hash, the verification detects an invalid |
| 6928 | * signature. |
| 6929 | * |
| 6930 | * 3. Test the number of calls to psa_sign_hash_complete() required are as |
| 6931 | * expected for different max_ops values. |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6932 | * |
| 6933 | * 4. Test that the number of ops done prior to starting signing and after abort |
| 6934 | * is zero and that each successful signing stage completes some ops (this is |
| 6935 | * not mandated by the PSA specification, but is currently the case). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6936 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6937 | 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] | 6938 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6939 | int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6940 | { |
| 6941 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6942 | psa_key_type_t key_type = key_type_arg; |
| 6943 | psa_algorithm_t alg = alg_arg; |
| 6944 | size_t key_bits; |
| 6945 | unsigned char *signature = NULL; |
| 6946 | size_t signature_size; |
| 6947 | size_t signature_length = 0xdeadbeef; |
| 6948 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6949 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6950 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6951 | uint32_t num_ops = 0; |
| 6952 | uint32_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6953 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6954 | size_t min_completes = 0; |
| 6955 | size_t max_completes = 0; |
| 6956 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6957 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 6958 | psa_sign_hash_interruptible_operation_init(); |
| 6959 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 6960 | psa_verify_hash_interruptible_operation_init(); |
| 6961 | |
| 6962 | PSA_ASSERT(psa_crypto_init()); |
| 6963 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6964 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 6965 | PSA_KEY_USAGE_VERIFY_HASH); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6966 | psa_set_key_algorithm(&attributes, alg); |
| 6967 | psa_set_key_type(&attributes, key_type); |
| 6968 | |
| 6969 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6970 | &key)); |
| 6971 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6972 | key_bits = psa_get_key_bits(&attributes); |
| 6973 | |
| 6974 | /* Allocate a buffer which has the size advertised by the |
| 6975 | * library. */ |
| 6976 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6977 | key_bits, alg); |
| 6978 | TEST_ASSERT(signature_size != 0); |
| 6979 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6980 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6981 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6982 | psa_interruptible_set_max_ops(max_ops); |
| 6983 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6984 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6985 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6986 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6987 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); |
| 6988 | TEST_ASSERT(num_ops_prior == 0); |
| 6989 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6990 | /* Start performing the signature. */ |
| 6991 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 6992 | input_data->x, input_data->len)); |
| 6993 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6994 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); |
| 6995 | TEST_ASSERT(num_ops_prior == 0); |
| 6996 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6997 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6998 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6999 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7000 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7001 | signature_size, |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7002 | &signature_length); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7003 | |
| 7004 | num_completes++; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7005 | |
| 7006 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 7007 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7008 | /* We are asserting here that every complete makes progress |
| 7009 | * (completes some ops), which is true of the internal |
| 7010 | * implementation and probably any implementation, however this is |
| 7011 | * not mandated by the PSA specification. */ |
| 7012 | TEST_ASSERT(num_ops > num_ops_prior); |
| 7013 | |
| 7014 | num_ops_prior = num_ops; |
| 7015 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7016 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7017 | |
| 7018 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7019 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7020 | TEST_LE_U(min_completes, num_completes); |
| 7021 | TEST_LE_U(num_completes, max_completes); |
| 7022 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7023 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7024 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7025 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7026 | TEST_ASSERT(num_ops == 0); |
| 7027 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7028 | /* Check that the signature length looks sensible. */ |
| 7029 | TEST_LE_U(signature_length, signature_size); |
| 7030 | TEST_ASSERT(signature_length > 0); |
| 7031 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7032 | num_completes = 0; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7033 | |
| 7034 | /* Start verification. */ |
| 7035 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7036 | input_data->x, input_data->len, |
| 7037 | signature, signature_length)); |
| 7038 | |
| 7039 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7040 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7041 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7042 | |
| 7043 | num_completes++; |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7044 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7045 | |
| 7046 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7047 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7048 | TEST_LE_U(min_completes, num_completes); |
| 7049 | TEST_LE_U(num_completes, max_completes); |
| 7050 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7051 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7052 | |
| 7053 | verify_operation = psa_verify_hash_interruptible_operation_init(); |
| 7054 | |
| 7055 | if (input_data->len != 0) { |
| 7056 | /* Flip a bit in the input and verify that the signature is now |
| 7057 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 7058 | * because ECDSA may ignore the last few bits of the input. */ |
| 7059 | input_data->x[0] ^= 1; |
| 7060 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7061 | /* Start verification. */ |
| 7062 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7063 | input_data->x, input_data->len, |
| 7064 | signature, signature_length)); |
| 7065 | |
| 7066 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7067 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7068 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7069 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7070 | |
| 7071 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7072 | } |
| 7073 | |
| 7074 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7075 | |
| 7076 | exit: |
| 7077 | /* |
| 7078 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7079 | * thus reset them as required. |
| 7080 | */ |
| 7081 | psa_reset_key_attributes(&attributes); |
| 7082 | |
| 7083 | psa_destroy_key(key); |
| 7084 | mbedtls_free(signature); |
| 7085 | PSA_DONE(); |
| 7086 | } |
| 7087 | /* END_CASE */ |
| 7088 | |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7089 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7090 | void verify_hash(int key_type_arg, data_t *key_data, |
| 7091 | int alg_arg, data_t *hash_data, |
| 7092 | data_t *signature_data) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7093 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7094 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7095 | psa_key_type_t key_type = key_type_arg; |
| 7096 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7097 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7098 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7099 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 7100 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7101 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7102 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7103 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7104 | psa_set_key_algorithm(&attributes, alg); |
| 7105 | psa_set_key_type(&attributes, key_type); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7106 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7107 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7108 | &key)); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7109 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7110 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 7111 | hash_data->x, hash_data->len, |
| 7112 | signature_data->x, signature_data->len)); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 7113 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7114 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7115 | psa_reset_key_attributes(&attributes); |
| 7116 | psa_destroy_key(key); |
| 7117 | PSA_DONE(); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7118 | } |
| 7119 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7120 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7121 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7122 | /** |
| 7123 | * verify_hash_interruptible() test intentions: |
| 7124 | * |
| 7125 | * Note: This test can currently only handle ECDSA. |
| 7126 | * |
| 7127 | * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 7128 | * only). Given this test only does verification it can accept public keys as |
| 7129 | * well as private keys / keypairs. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7130 | * |
| 7131 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7132 | * expected for different max_ops values. |
| 7133 | * |
| 7134 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7135 | * and that each successful stage completes some ops (this is not mandated by |
| 7136 | * the PSA specification, but is currently the case). |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7137 | * |
| 7138 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 7139 | * complete() calls does not alter the number of ops returned. |
| 7140 | * |
| 7141 | * 5. Test that after corrupting the hash, the verification detects an invalid |
| 7142 | * signature. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7143 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7144 | void verify_hash_interruptible(int key_type_arg, data_t *key_data, |
| 7145 | int alg_arg, data_t *hash_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7146 | data_t *signature_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7147 | { |
| 7148 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7149 | psa_key_type_t key_type = key_type_arg; |
| 7150 | psa_algorithm_t alg = alg_arg; |
| 7151 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7152 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7153 | uint32_t num_ops = 0; |
| 7154 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7155 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7156 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7157 | size_t min_completes = 0; |
| 7158 | size_t max_completes = 0; |
| 7159 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7160 | psa_verify_hash_interruptible_operation_t operation = |
| 7161 | psa_verify_hash_interruptible_operation_init(); |
| 7162 | |
| 7163 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 7164 | |
| 7165 | PSA_ASSERT(psa_crypto_init()); |
| 7166 | |
| 7167 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7168 | psa_set_key_algorithm(&attributes, alg); |
| 7169 | psa_set_key_type(&attributes, key_type); |
| 7170 | |
| 7171 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7172 | &key)); |
| 7173 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7174 | psa_interruptible_set_max_ops(max_ops); |
| 7175 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7176 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 7177 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7178 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7179 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7180 | |
| 7181 | TEST_ASSERT(num_ops_prior == 0); |
| 7182 | |
| 7183 | /* Start verification. */ |
| 7184 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7185 | hash_data->x, hash_data->len, |
| 7186 | signature_data->x, signature_data->len) |
| 7187 | ); |
| 7188 | |
| 7189 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7190 | |
| 7191 | TEST_ASSERT(num_ops_prior == 0); |
| 7192 | |
| 7193 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7194 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7195 | status = psa_verify_hash_complete(&operation); |
| 7196 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7197 | num_completes++; |
| 7198 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7199 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 7200 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7201 | /* We are asserting here that every complete makes progress |
| 7202 | * (completes some ops), which is true of the internal |
| 7203 | * implementation and probably any implementation, however this is |
| 7204 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7205 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7206 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7207 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 7208 | |
| 7209 | /* Ensure calling get_num_ops() twice still returns the same |
| 7210 | * number of ops as previously reported. */ |
| 7211 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7212 | |
| 7213 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7214 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7215 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7216 | |
| 7217 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7218 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7219 | TEST_LE_U(min_completes, num_completes); |
| 7220 | TEST_LE_U(num_completes, max_completes); |
| 7221 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7222 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7223 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7224 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7225 | TEST_ASSERT(num_ops == 0); |
| 7226 | |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7227 | if (hash_data->len != 0) { |
| 7228 | /* Flip a bit in the hash and verify that the signature is now detected |
| 7229 | * as invalid. Flip a bit at the beginning, not at the end, because |
| 7230 | * ECDSA may ignore the last few bits of the input. */ |
| 7231 | hash_data->x[0] ^= 1; |
| 7232 | |
| 7233 | /* Start verification. */ |
| 7234 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7235 | hash_data->x, hash_data->len, |
| 7236 | signature_data->x, signature_data->len)); |
| 7237 | |
| 7238 | /* Continue performing the signature until complete. */ |
| 7239 | do { |
| 7240 | status = psa_verify_hash_complete(&operation); |
| 7241 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7242 | |
| 7243 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7244 | } |
| 7245 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7246 | exit: |
| 7247 | psa_reset_key_attributes(&attributes); |
| 7248 | psa_destroy_key(key); |
| 7249 | PSA_DONE(); |
| 7250 | } |
| 7251 | /* END_CASE */ |
| 7252 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7253 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7254 | void verify_hash_fail(int key_type_arg, data_t *key_data, |
| 7255 | int alg_arg, data_t *hash_data, |
| 7256 | data_t *signature_data, |
| 7257 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7258 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7259 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7260 | psa_key_type_t key_type = key_type_arg; |
| 7261 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7262 | psa_status_t actual_status; |
| 7263 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7264 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7266 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7267 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7268 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7269 | psa_set_key_algorithm(&attributes, alg); |
| 7270 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7271 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7272 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7273 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7274 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7275 | actual_status = psa_verify_hash(key, alg, |
| 7276 | hash_data->x, hash_data->len, |
| 7277 | signature_data->x, signature_data->len); |
| 7278 | TEST_EQUAL(actual_status, expected_status); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7279 | |
| 7280 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7281 | psa_reset_key_attributes(&attributes); |
| 7282 | psa_destroy_key(key); |
| 7283 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7284 | } |
| 7285 | /* END_CASE */ |
| 7286 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7287 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7288 | /** |
| 7289 | * verify_hash_fail_interruptible() test intentions: |
| 7290 | * |
| 7291 | * Note: This test can currently only handle ECDSA. |
| 7292 | * |
| 7293 | * 1. Test that various failure cases for interruptible verify hash fail with |
| 7294 | * the correct error codes, and at the correct point (at start or during |
| 7295 | * complete). |
| 7296 | * |
| 7297 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7298 | * expected for different max_ops values. |
| 7299 | * |
| 7300 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7301 | * and that each successful stage completes some ops (this is not mandated by |
| 7302 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7303 | * |
| 7304 | * 4. Check that calling complete() when start() fails and complete() |
| 7305 | * after completion results in a BAD_STATE error. |
| 7306 | * |
| 7307 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 7308 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7309 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7310 | void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 7311 | int alg_arg, data_t *hash_data, |
| 7312 | data_t *signature_data, |
| 7313 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7314 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7315 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7316 | { |
| 7317 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7318 | psa_key_type_t key_type = key_type_arg; |
| 7319 | psa_algorithm_t alg = alg_arg; |
| 7320 | psa_status_t actual_status; |
| 7321 | psa_status_t expected_start_status = expected_start_status_arg; |
| 7322 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 7323 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7324 | uint32_t num_ops = 0; |
| 7325 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7326 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7327 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7328 | size_t min_completes = 0; |
| 7329 | size_t max_completes = 0; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7330 | psa_verify_hash_interruptible_operation_t operation = |
| 7331 | psa_verify_hash_interruptible_operation_init(); |
| 7332 | |
| 7333 | PSA_ASSERT(psa_crypto_init()); |
| 7334 | |
| 7335 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7336 | psa_set_key_algorithm(&attributes, alg); |
| 7337 | psa_set_key_type(&attributes, key_type); |
| 7338 | |
| 7339 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7340 | &key)); |
| 7341 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7342 | psa_interruptible_set_max_ops(max_ops); |
| 7343 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7344 | interruptible_signverify_get_minmax_completes(max_ops, |
| 7345 | expected_complete_status, |
| 7346 | &min_completes, |
| 7347 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7348 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7349 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7350 | TEST_ASSERT(num_ops_prior == 0); |
| 7351 | |
| 7352 | /* Start verification. */ |
| 7353 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7354 | hash_data->x, hash_data->len, |
| 7355 | signature_data->x, |
| 7356 | signature_data->len); |
| 7357 | |
| 7358 | TEST_EQUAL(actual_status, expected_start_status); |
| 7359 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7360 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 7361 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7362 | * start failed. */ |
| 7363 | actual_status = psa_verify_hash_complete(&operation); |
| 7364 | |
| 7365 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7366 | |
| 7367 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7368 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7369 | hash_data->x, hash_data->len, |
| 7370 | signature_data->x, |
| 7371 | signature_data->len); |
| 7372 | |
| 7373 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7374 | } |
| 7375 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7376 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7377 | TEST_ASSERT(num_ops_prior == 0); |
| 7378 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7379 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7380 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7381 | actual_status = psa_verify_hash_complete(&operation); |
| 7382 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7383 | num_completes++; |
| 7384 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7385 | if (actual_status == PSA_SUCCESS || |
| 7386 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7387 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7388 | /* We are asserting here that every complete makes progress |
| 7389 | * (completes some ops), which is true of the internal |
| 7390 | * implementation and probably any implementation, however this is |
| 7391 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7392 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7393 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7394 | num_ops_prior = num_ops; |
| 7395 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7396 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7397 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7398 | TEST_EQUAL(actual_status, expected_complete_status); |
| 7399 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 7400 | /* Check that another complete returns BAD_STATE. */ |
| 7401 | actual_status = psa_verify_hash_complete(&operation); |
| 7402 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7403 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7404 | TEST_LE_U(min_completes, num_completes); |
| 7405 | TEST_LE_U(num_completes, max_completes); |
| 7406 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7407 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7408 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7409 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7410 | TEST_ASSERT(num_ops == 0); |
| 7411 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7412 | exit: |
| 7413 | psa_reset_key_attributes(&attributes); |
| 7414 | psa_destroy_key(key); |
| 7415 | PSA_DONE(); |
| 7416 | } |
| 7417 | /* END_CASE */ |
| 7418 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7419 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7420 | /** |
| 7421 | * interruptible_signverify_hash_state_test() test intentions: |
| 7422 | * |
| 7423 | * Note: This test can currently only handle ECDSA. |
| 7424 | * |
| 7425 | * 1. Test that calling the various interruptible sign and verify hash functions |
| 7426 | * in incorrect orders returns BAD_STATE errors. |
| 7427 | */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7428 | void interruptible_signverify_hash_state_test(int key_type_arg, |
| 7429 | data_t *key_data, int alg_arg, data_t *input_data) |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7430 | { |
| 7431 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7432 | psa_key_type_t key_type = key_type_arg; |
| 7433 | psa_algorithm_t alg = alg_arg; |
| 7434 | size_t key_bits; |
| 7435 | unsigned char *signature = NULL; |
| 7436 | size_t signature_size; |
| 7437 | size_t signature_length = 0xdeadbeef; |
| 7438 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7439 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7440 | psa_sign_hash_interruptible_operation_init(); |
| 7441 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7442 | psa_verify_hash_interruptible_operation_init(); |
| 7443 | |
| 7444 | PSA_ASSERT(psa_crypto_init()); |
| 7445 | |
| 7446 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7447 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7448 | psa_set_key_algorithm(&attributes, alg); |
| 7449 | psa_set_key_type(&attributes, key_type); |
| 7450 | |
| 7451 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7452 | &key)); |
| 7453 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7454 | key_bits = psa_get_key_bits(&attributes); |
| 7455 | |
| 7456 | /* Allocate a buffer which has the size advertised by the |
| 7457 | * library. */ |
| 7458 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7459 | key_bits, alg); |
| 7460 | TEST_ASSERT(signature_size != 0); |
| 7461 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7462 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7463 | |
| 7464 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7465 | |
| 7466 | /* --- Attempt completes prior to starts --- */ |
| 7467 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7468 | signature_size, |
| 7469 | &signature_length), |
| 7470 | PSA_ERROR_BAD_STATE); |
| 7471 | |
| 7472 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7473 | |
| 7474 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7475 | PSA_ERROR_BAD_STATE); |
| 7476 | |
| 7477 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7478 | |
| 7479 | /* --- Aborts in all other places. --- */ |
| 7480 | psa_sign_hash_abort(&sign_operation); |
| 7481 | |
| 7482 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7483 | input_data->x, input_data->len)); |
| 7484 | |
| 7485 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7486 | |
| 7487 | psa_interruptible_set_max_ops(1); |
| 7488 | |
| 7489 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7490 | input_data->x, input_data->len)); |
| 7491 | |
| 7492 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7493 | signature_size, |
| 7494 | &signature_length), |
| 7495 | PSA_OPERATION_INCOMPLETE); |
| 7496 | |
| 7497 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7498 | |
| 7499 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7500 | |
| 7501 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7502 | input_data->x, input_data->len)); |
| 7503 | |
| 7504 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7505 | signature_size, |
| 7506 | &signature_length)); |
| 7507 | |
| 7508 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7509 | |
| 7510 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7511 | |
| 7512 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7513 | input_data->x, input_data->len, |
| 7514 | signature, signature_length)); |
| 7515 | |
| 7516 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7517 | |
| 7518 | psa_interruptible_set_max_ops(1); |
| 7519 | |
| 7520 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7521 | input_data->x, input_data->len, |
| 7522 | signature, signature_length)); |
| 7523 | |
| 7524 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7525 | PSA_OPERATION_INCOMPLETE); |
| 7526 | |
| 7527 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7528 | |
| 7529 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7530 | |
| 7531 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7532 | input_data->x, input_data->len, |
| 7533 | signature, signature_length)); |
| 7534 | |
| 7535 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7536 | |
| 7537 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7538 | |
| 7539 | /* --- Attempt double starts. --- */ |
| 7540 | |
| 7541 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7542 | input_data->x, input_data->len)); |
| 7543 | |
| 7544 | TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg, |
| 7545 | input_data->x, input_data->len), |
| 7546 | PSA_ERROR_BAD_STATE); |
| 7547 | |
| 7548 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7549 | |
| 7550 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7551 | input_data->x, input_data->len, |
| 7552 | signature, signature_length)); |
| 7553 | |
| 7554 | TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg, |
| 7555 | input_data->x, input_data->len, |
| 7556 | signature, signature_length), |
| 7557 | PSA_ERROR_BAD_STATE); |
| 7558 | |
| 7559 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7560 | |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7561 | exit: |
| 7562 | /* |
| 7563 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7564 | * thus reset them as required. |
| 7565 | */ |
| 7566 | psa_reset_key_attributes(&attributes); |
| 7567 | |
| 7568 | psa_destroy_key(key); |
| 7569 | mbedtls_free(signature); |
| 7570 | PSA_DONE(); |
| 7571 | } |
| 7572 | /* END_CASE */ |
| 7573 | |
| 7574 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7575 | /** |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7576 | * interruptible_signverify_hash_edgecase_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7577 | * |
| 7578 | * Note: This test can currently only handle ECDSA. |
| 7579 | * |
| 7580 | * 1. Test various edge cases in the interruptible sign and verify hash |
| 7581 | * interfaces. |
| 7582 | */ |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7583 | void interruptible_signverify_hash_edgecase_tests(int key_type_arg, |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7584 | data_t *key_data, int alg_arg, data_t *input_data) |
| 7585 | { |
| 7586 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7587 | psa_key_type_t key_type = key_type_arg; |
| 7588 | psa_algorithm_t alg = alg_arg; |
| 7589 | size_t key_bits; |
| 7590 | unsigned char *signature = NULL; |
| 7591 | size_t signature_size; |
| 7592 | size_t signature_length = 0xdeadbeef; |
| 7593 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7594 | uint8_t *input_buffer = NULL; |
| 7595 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7596 | psa_sign_hash_interruptible_operation_init(); |
| 7597 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7598 | psa_verify_hash_interruptible_operation_init(); |
| 7599 | |
| 7600 | PSA_ASSERT(psa_crypto_init()); |
| 7601 | |
| 7602 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7603 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7604 | psa_set_key_algorithm(&attributes, alg); |
| 7605 | psa_set_key_type(&attributes, key_type); |
| 7606 | |
| 7607 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7608 | &key)); |
| 7609 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7610 | key_bits = psa_get_key_bits(&attributes); |
| 7611 | |
| 7612 | /* Allocate a buffer which has the size advertised by the |
| 7613 | * library. */ |
| 7614 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7615 | key_bits, alg); |
| 7616 | TEST_ASSERT(signature_size != 0); |
| 7617 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7618 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7619 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7620 | /* --- Change function inputs mid run, to cause an error (sign only, |
| 7621 | * verify passes all inputs to start. --- */ |
| 7622 | |
| 7623 | psa_interruptible_set_max_ops(1); |
| 7624 | |
| 7625 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7626 | input_data->x, input_data->len)); |
| 7627 | |
| 7628 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7629 | signature_size, |
| 7630 | &signature_length), |
| 7631 | PSA_OPERATION_INCOMPLETE); |
| 7632 | |
| 7633 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7634 | 0, |
| 7635 | &signature_length), |
| 7636 | PSA_ERROR_BUFFER_TOO_SMALL); |
| 7637 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7638 | /* And test that this invalidates the operation. */ |
| 7639 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7640 | 0, |
| 7641 | &signature_length), |
| 7642 | PSA_ERROR_BAD_STATE); |
| 7643 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7644 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7645 | |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7646 | /* Trash the hash buffer in between start and complete, to ensure |
| 7647 | * no reliance on external buffers. */ |
| 7648 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7649 | |
Paul Elliott | 6c68df4 | 2023-10-23 15:33:37 +0100 | [diff] [blame] | 7650 | TEST_CALLOC(input_buffer, input_data->len); |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7651 | |
| 7652 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7653 | |
| 7654 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7655 | input_buffer, input_data->len)); |
| 7656 | |
| 7657 | memset(input_buffer, '!', input_data->len); |
| 7658 | mbedtls_free(input_buffer); |
| 7659 | input_buffer = NULL; |
| 7660 | |
| 7661 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7662 | signature_size, |
| 7663 | &signature_length)); |
| 7664 | |
| 7665 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7666 | |
Paul Elliott | 6c68df4 | 2023-10-23 15:33:37 +0100 | [diff] [blame] | 7667 | TEST_CALLOC(input_buffer, input_data->len); |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7668 | |
| 7669 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7670 | |
| 7671 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7672 | input_buffer, input_data->len, |
| 7673 | signature, signature_length)); |
| 7674 | |
| 7675 | memset(input_buffer, '!', input_data->len); |
| 7676 | mbedtls_free(input_buffer); |
| 7677 | input_buffer = NULL; |
| 7678 | |
| 7679 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7680 | |
| 7681 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7682 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7683 | exit: |
| 7684 | /* |
| 7685 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7686 | * thus reset them as required. |
| 7687 | */ |
| 7688 | psa_reset_key_attributes(&attributes); |
| 7689 | |
| 7690 | psa_destroy_key(key); |
| 7691 | mbedtls_free(signature); |
Paul Elliott | 6c68df4 | 2023-10-23 15:33:37 +0100 | [diff] [blame] | 7692 | mbedtls_free(input_buffer); |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7693 | PSA_DONE(); |
| 7694 | } |
| 7695 | /* END_CASE */ |
| 7696 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7697 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7698 | /** |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 7699 | * interruptible_signverify_hash_ops_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7700 | * |
| 7701 | * Note: This test can currently only handle ECDSA. |
| 7702 | * |
| 7703 | * 1. Test that setting max ops is reflected in both interruptible sign and |
| 7704 | * verify hash |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7705 | * 2. Test that changing the value of max_ops to unlimited during an operation |
| 7706 | * causes that operation to complete in the next call. |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7707 | * |
| 7708 | * 3. Test that calling get_num_ops() between complete calls gives the same |
| 7709 | * 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] | 7710 | */ |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 7711 | void interruptible_signverify_hash_ops_tests(int key_type_arg, |
| 7712 | data_t *key_data, int alg_arg, |
| 7713 | data_t *input_data) |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7714 | { |
| 7715 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7716 | psa_key_type_t key_type = key_type_arg; |
| 7717 | psa_algorithm_t alg = alg_arg; |
| 7718 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7719 | size_t key_bits; |
| 7720 | unsigned char *signature = NULL; |
| 7721 | size_t signature_size; |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7722 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7723 | uint32_t num_ops = 0; |
| 7724 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 7725 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7726 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7727 | psa_sign_hash_interruptible_operation_init(); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7728 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7729 | psa_verify_hash_interruptible_operation_init(); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7730 | |
| 7731 | PSA_ASSERT(psa_crypto_init()); |
| 7732 | |
| 7733 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7734 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7735 | psa_set_key_algorithm(&attributes, alg); |
| 7736 | psa_set_key_type(&attributes, key_type); |
| 7737 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7738 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key)); |
| 7739 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7740 | key_bits = psa_get_key_bits(&attributes); |
| 7741 | |
| 7742 | /* Allocate a buffer which has the size advertised by the |
| 7743 | * library. */ |
| 7744 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7745 | |
| 7746 | TEST_ASSERT(signature_size != 0); |
| 7747 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7748 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7749 | |
| 7750 | /* Check that default max ops gets set if we don't set it. */ |
| 7751 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7752 | input_data->x, input_data->len)); |
| 7753 | |
| 7754 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 7755 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7756 | |
| 7757 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7758 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7759 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7760 | input_data->x, input_data->len, |
| 7761 | signature, signature_size)); |
| 7762 | |
| 7763 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 7764 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7765 | |
| 7766 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7767 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7768 | /* Check that max ops gets set properly. */ |
| 7769 | |
| 7770 | psa_interruptible_set_max_ops(0xbeef); |
| 7771 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7772 | TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7773 | |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7774 | /* --- Ensure changing the max ops mid operation works (operation should |
| 7775 | * complete successfully after setting max ops to unlimited --- */ |
| 7776 | psa_interruptible_set_max_ops(1); |
| 7777 | |
| 7778 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7779 | input_data->x, input_data->len)); |
| 7780 | |
| 7781 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7782 | signature_size, |
| 7783 | &signature_length), |
| 7784 | PSA_OPERATION_INCOMPLETE); |
| 7785 | |
| 7786 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7787 | |
| 7788 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7789 | signature_size, |
| 7790 | &signature_length)); |
| 7791 | |
| 7792 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7793 | |
| 7794 | psa_interruptible_set_max_ops(1); |
| 7795 | |
| 7796 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7797 | input_data->x, input_data->len, |
| 7798 | signature, signature_length)); |
| 7799 | |
| 7800 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7801 | PSA_OPERATION_INCOMPLETE); |
| 7802 | |
| 7803 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7804 | |
| 7805 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7806 | |
| 7807 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7808 | |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7809 | /* --- Test that not calling get_num_ops inbetween complete calls does not |
| 7810 | * result in lost ops. ---*/ |
| 7811 | |
| 7812 | psa_interruptible_set_max_ops(1); |
| 7813 | |
| 7814 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7815 | input_data->x, input_data->len)); |
| 7816 | |
| 7817 | /* Continue performing the signature until complete. */ |
| 7818 | do { |
| 7819 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7820 | signature_size, |
| 7821 | &signature_length); |
| 7822 | |
| 7823 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7824 | |
| 7825 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7826 | |
| 7827 | PSA_ASSERT(status); |
| 7828 | |
| 7829 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7830 | |
| 7831 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7832 | input_data->x, input_data->len)); |
| 7833 | |
| 7834 | /* Continue performing the signature until complete. */ |
| 7835 | do { |
| 7836 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7837 | signature_size, |
| 7838 | &signature_length); |
| 7839 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7840 | |
| 7841 | PSA_ASSERT(status); |
| 7842 | |
| 7843 | TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation)); |
| 7844 | |
| 7845 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7846 | |
| 7847 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7848 | input_data->x, input_data->len, |
| 7849 | signature, signature_length)); |
| 7850 | |
| 7851 | /* Continue performing the verification until complete. */ |
| 7852 | do { |
| 7853 | status = psa_verify_hash_complete(&verify_operation); |
| 7854 | |
| 7855 | num_ops = psa_verify_hash_get_num_ops(&verify_operation); |
| 7856 | |
| 7857 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7858 | |
| 7859 | PSA_ASSERT(status); |
| 7860 | |
| 7861 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7862 | |
| 7863 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7864 | input_data->x, input_data->len, |
| 7865 | signature, signature_length)); |
| 7866 | |
| 7867 | /* Continue performing the verification until complete. */ |
| 7868 | do { |
| 7869 | status = psa_verify_hash_complete(&verify_operation); |
| 7870 | |
| 7871 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7872 | |
| 7873 | PSA_ASSERT(status); |
| 7874 | |
| 7875 | TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation)); |
| 7876 | |
| 7877 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7878 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7879 | exit: |
| 7880 | /* |
| 7881 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7882 | * thus reset them as required. |
| 7883 | */ |
| 7884 | psa_reset_key_attributes(&attributes); |
| 7885 | |
| 7886 | psa_destroy_key(key); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7887 | mbedtls_free(signature); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7888 | PSA_DONE(); |
| 7889 | } |
| 7890 | /* END_CASE */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7891 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7892 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7893 | void sign_message_deterministic(int key_type_arg, |
| 7894 | data_t *key_data, |
| 7895 | int alg_arg, |
| 7896 | data_t *input_data, |
| 7897 | data_t *output_data) |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7898 | { |
| 7899 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7900 | psa_key_type_t key_type = key_type_arg; |
| 7901 | psa_algorithm_t alg = alg_arg; |
| 7902 | size_t key_bits; |
| 7903 | unsigned char *signature = NULL; |
| 7904 | size_t signature_size; |
| 7905 | size_t signature_length = 0xdeadbeef; |
| 7906 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7907 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7908 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7909 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7910 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 7911 | psa_set_key_algorithm(&attributes, alg); |
| 7912 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7913 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7914 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7915 | &key)); |
| 7916 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7917 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7918 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7919 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7920 | TEST_ASSERT(signature_size != 0); |
| 7921 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7922 | TEST_CALLOC(signature, signature_size); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7923 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7924 | PSA_ASSERT(psa_sign_message(key, alg, |
| 7925 | input_data->x, input_data->len, |
| 7926 | signature, signature_size, |
| 7927 | &signature_length)); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7928 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 7929 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 7930 | signature, signature_length); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7931 | |
| 7932 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7933 | psa_reset_key_attributes(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7934 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7935 | psa_destroy_key(key); |
| 7936 | mbedtls_free(signature); |
| 7937 | PSA_DONE(); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7938 | |
| 7939 | } |
| 7940 | /* END_CASE */ |
| 7941 | |
| 7942 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7943 | void sign_message_fail(int key_type_arg, |
| 7944 | data_t *key_data, |
| 7945 | int alg_arg, |
| 7946 | data_t *input_data, |
| 7947 | int signature_size_arg, |
| 7948 | int expected_status_arg) |
| 7949 | { |
| 7950 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7951 | psa_key_type_t key_type = key_type_arg; |
| 7952 | psa_algorithm_t alg = alg_arg; |
| 7953 | size_t signature_size = signature_size_arg; |
| 7954 | psa_status_t actual_status; |
| 7955 | psa_status_t expected_status = expected_status_arg; |
| 7956 | unsigned char *signature = NULL; |
| 7957 | size_t signature_length = 0xdeadbeef; |
| 7958 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7959 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7960 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7961 | |
| 7962 | PSA_ASSERT(psa_crypto_init()); |
| 7963 | |
| 7964 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 7965 | psa_set_key_algorithm(&attributes, alg); |
| 7966 | psa_set_key_type(&attributes, key_type); |
| 7967 | |
| 7968 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7969 | &key)); |
| 7970 | |
| 7971 | actual_status = psa_sign_message(key, alg, |
| 7972 | input_data->x, input_data->len, |
| 7973 | signature, signature_size, |
| 7974 | &signature_length); |
| 7975 | TEST_EQUAL(actual_status, expected_status); |
| 7976 | /* The value of *signature_length is unspecified on error, but |
| 7977 | * whatever it is, it should be less than signature_size, so that |
| 7978 | * if the caller tries to read *signature_length bytes without |
| 7979 | * checking the error code then they don't overflow a buffer. */ |
| 7980 | TEST_LE_U(signature_length, signature_size); |
| 7981 | |
| 7982 | exit: |
| 7983 | psa_reset_key_attributes(&attributes); |
| 7984 | psa_destroy_key(key); |
| 7985 | mbedtls_free(signature); |
| 7986 | PSA_DONE(); |
| 7987 | } |
| 7988 | /* END_CASE */ |
| 7989 | |
| 7990 | /* BEGIN_CASE */ |
| 7991 | void sign_verify_message(int key_type_arg, |
| 7992 | data_t *key_data, |
| 7993 | int alg_arg, |
| 7994 | data_t *input_data) |
| 7995 | { |
| 7996 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7997 | psa_key_type_t key_type = key_type_arg; |
| 7998 | psa_algorithm_t alg = alg_arg; |
| 7999 | size_t key_bits; |
| 8000 | unsigned char *signature = NULL; |
| 8001 | size_t signature_size; |
| 8002 | size_t signature_length = 0xdeadbeef; |
| 8003 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8004 | |
| 8005 | PSA_ASSERT(psa_crypto_init()); |
| 8006 | |
| 8007 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 8008 | PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8009 | psa_set_key_algorithm(&attributes, alg); |
| 8010 | psa_set_key_type(&attributes, key_type); |
| 8011 | |
| 8012 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8013 | &key)); |
| 8014 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8015 | key_bits = psa_get_key_bits(&attributes); |
| 8016 | |
| 8017 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8018 | TEST_ASSERT(signature_size != 0); |
| 8019 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8020 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8021 | |
| 8022 | PSA_ASSERT(psa_sign_message(key, alg, |
| 8023 | input_data->x, input_data->len, |
| 8024 | signature, signature_size, |
| 8025 | &signature_length)); |
| 8026 | TEST_LE_U(signature_length, signature_size); |
| 8027 | TEST_ASSERT(signature_length > 0); |
| 8028 | |
| 8029 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8030 | input_data->x, input_data->len, |
| 8031 | signature, signature_length)); |
| 8032 | |
| 8033 | if (input_data->len != 0) { |
| 8034 | /* Flip a bit in the input and verify that the signature is now |
| 8035 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 8036 | * because ECDSA may ignore the last few bits of the input. */ |
| 8037 | input_data->x[0] ^= 1; |
| 8038 | TEST_EQUAL(psa_verify_message(key, alg, |
| 8039 | input_data->x, input_data->len, |
| 8040 | signature, signature_length), |
| 8041 | PSA_ERROR_INVALID_SIGNATURE); |
| 8042 | } |
| 8043 | |
| 8044 | exit: |
| 8045 | psa_reset_key_attributes(&attributes); |
| 8046 | |
| 8047 | psa_destroy_key(key); |
| 8048 | mbedtls_free(signature); |
| 8049 | PSA_DONE(); |
| 8050 | } |
| 8051 | /* END_CASE */ |
| 8052 | |
| 8053 | /* BEGIN_CASE */ |
| 8054 | void verify_message(int key_type_arg, |
| 8055 | data_t *key_data, |
| 8056 | int alg_arg, |
| 8057 | data_t *input_data, |
| 8058 | data_t *signature_data) |
| 8059 | { |
| 8060 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8061 | psa_key_type_t key_type = key_type_arg; |
| 8062 | psa_algorithm_t alg = alg_arg; |
| 8063 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8064 | |
| 8065 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 8066 | |
| 8067 | PSA_ASSERT(psa_crypto_init()); |
| 8068 | |
| 8069 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8070 | psa_set_key_algorithm(&attributes, alg); |
| 8071 | psa_set_key_type(&attributes, key_type); |
| 8072 | |
| 8073 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8074 | &key)); |
| 8075 | |
| 8076 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8077 | input_data->x, input_data->len, |
| 8078 | signature_data->x, signature_data->len)); |
| 8079 | |
| 8080 | exit: |
| 8081 | psa_reset_key_attributes(&attributes); |
| 8082 | psa_destroy_key(key); |
| 8083 | PSA_DONE(); |
| 8084 | } |
| 8085 | /* END_CASE */ |
| 8086 | |
| 8087 | /* BEGIN_CASE */ |
| 8088 | void verify_message_fail(int key_type_arg, |
| 8089 | data_t *key_data, |
| 8090 | int alg_arg, |
| 8091 | data_t *hash_data, |
| 8092 | data_t *signature_data, |
| 8093 | int expected_status_arg) |
| 8094 | { |
| 8095 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8096 | psa_key_type_t key_type = key_type_arg; |
| 8097 | psa_algorithm_t alg = alg_arg; |
| 8098 | psa_status_t actual_status; |
| 8099 | psa_status_t expected_status = expected_status_arg; |
| 8100 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8101 | |
| 8102 | PSA_ASSERT(psa_crypto_init()); |
| 8103 | |
| 8104 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8105 | psa_set_key_algorithm(&attributes, alg); |
| 8106 | psa_set_key_type(&attributes, key_type); |
| 8107 | |
| 8108 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8109 | &key)); |
| 8110 | |
| 8111 | actual_status = psa_verify_message(key, alg, |
| 8112 | hash_data->x, hash_data->len, |
| 8113 | signature_data->x, |
| 8114 | signature_data->len); |
| 8115 | TEST_EQUAL(actual_status, expected_status); |
| 8116 | |
| 8117 | exit: |
| 8118 | psa_reset_key_attributes(&attributes); |
| 8119 | psa_destroy_key(key); |
| 8120 | PSA_DONE(); |
| 8121 | } |
| 8122 | /* END_CASE */ |
| 8123 | |
| 8124 | /* BEGIN_CASE */ |
| 8125 | void asymmetric_encrypt(int key_type_arg, |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8126 | data_t *key_data, |
| 8127 | int alg_arg, |
| 8128 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8129 | data_t *label, |
| 8130 | int expected_output_length_arg, |
| 8131 | int expected_status_arg) |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8132 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8133 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8134 | psa_key_type_t key_type = key_type_arg; |
| 8135 | psa_algorithm_t alg = alg_arg; |
| 8136 | size_t expected_output_length = expected_output_length_arg; |
| 8137 | size_t key_bits; |
| 8138 | unsigned char *output = NULL; |
| 8139 | size_t output_size; |
| 8140 | size_t output_length = ~0; |
| 8141 | psa_status_t actual_status; |
| 8142 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8143 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8144 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8145 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 8146 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8147 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8148 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 8149 | psa_set_key_algorithm(&attributes, alg); |
| 8150 | psa_set_key_type(&attributes, key_type); |
| 8151 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8152 | &key)); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8153 | |
| 8154 | /* Determine the maximum output length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8155 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8156 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8157 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8158 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8159 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8160 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8161 | |
| 8162 | /* Encrypt the input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8163 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8164 | input_data->x, input_data->len, |
| 8165 | label->x, label->len, |
| 8166 | output, output_size, |
| 8167 | &output_length); |
| 8168 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8169 | if (actual_status == PSA_SUCCESS) { |
| 8170 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8171 | } else { |
| 8172 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8173 | } |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8174 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8175 | /* If the label is empty, the test framework puts a non-null pointer |
| 8176 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8177 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8178 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8179 | if (output_size != 0) { |
| 8180 | memset(output, 0, output_size); |
| 8181 | } |
| 8182 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8183 | input_data->x, input_data->len, |
| 8184 | NULL, label->len, |
| 8185 | output, output_size, |
| 8186 | &output_length); |
| 8187 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8188 | if (actual_status == PSA_SUCCESS) { |
| 8189 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8190 | } else { |
| 8191 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8192 | } |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8193 | } |
| 8194 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8195 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8196 | /* |
| 8197 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8198 | * thus reset them as required. |
| 8199 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8200 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8201 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8202 | psa_destroy_key(key); |
| 8203 | mbedtls_free(output); |
| 8204 | PSA_DONE(); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8205 | } |
| 8206 | /* END_CASE */ |
| 8207 | |
| 8208 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8209 | void asymmetric_encrypt_decrypt(int key_type_arg, |
| 8210 | data_t *key_data, |
| 8211 | int alg_arg, |
| 8212 | data_t *input_data, |
| 8213 | data_t *label) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8214 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8215 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8216 | psa_key_type_t key_type = key_type_arg; |
| 8217 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8218 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8219 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8220 | size_t output_size; |
| 8221 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8222 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8223 | size_t output2_size; |
| 8224 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8225 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8226 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8227 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8228 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8229 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 8230 | psa_set_key_algorithm(&attributes, alg); |
| 8231 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8232 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8233 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8234 | &key)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8235 | |
| 8236 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8237 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8238 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8239 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8240 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8241 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8242 | TEST_CALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8243 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8244 | output2_size = input_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8245 | TEST_LE_U(output2_size, |
| 8246 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)); |
| 8247 | TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8248 | TEST_CALLOC(output2, output2_size); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8249 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 8250 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 8251 | * the original plaintext because of the non-optional random |
| 8252 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8253 | PSA_ASSERT(psa_asymmetric_encrypt(key, alg, |
| 8254 | input_data->x, input_data->len, |
| 8255 | label->x, label->len, |
| 8256 | output, output_size, |
| 8257 | &output_length)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8258 | /* We don't know what ciphertext length to expect, but check that |
| 8259 | * it looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8260 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8261 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8262 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8263 | output, output_length, |
| 8264 | label->x, label->len, |
| 8265 | output2, output2_size, |
| 8266 | &output2_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8267 | TEST_MEMORY_COMPARE(input_data->x, input_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8268 | output2, output2_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8269 | |
| 8270 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8271 | /* |
| 8272 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8273 | * thus reset them as required. |
| 8274 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8275 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8276 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8277 | psa_destroy_key(key); |
| 8278 | mbedtls_free(output); |
| 8279 | mbedtls_free(output2); |
| 8280 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8281 | } |
| 8282 | /* END_CASE */ |
| 8283 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8284 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8285 | void asymmetric_decrypt(int key_type_arg, |
| 8286 | data_t *key_data, |
| 8287 | int alg_arg, |
| 8288 | data_t *input_data, |
| 8289 | data_t *label, |
| 8290 | data_t *expected_data) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8291 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8292 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8293 | psa_key_type_t key_type = key_type_arg; |
| 8294 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8295 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8296 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 8297 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8298 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8299 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8300 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8301 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8302 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8303 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8304 | psa_set_key_algorithm(&attributes, alg); |
| 8305 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8306 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8307 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8308 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8309 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8310 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8311 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8312 | |
| 8313 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8314 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8315 | TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8316 | TEST_CALLOC(output, output_size); |
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 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8319 | input_data->x, input_data->len, |
| 8320 | label->x, label->len, |
| 8321 | output, |
| 8322 | output_size, |
| 8323 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8324 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8325 | output, output_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8326 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8327 | /* If the label is empty, the test framework puts a non-null pointer |
| 8328 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8329 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8330 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8331 | if (output_size != 0) { |
| 8332 | memset(output, 0, output_size); |
| 8333 | } |
| 8334 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8335 | input_data->x, input_data->len, |
| 8336 | NULL, label->len, |
| 8337 | output, |
| 8338 | output_size, |
| 8339 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8340 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8341 | output, output_length); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8342 | } |
| 8343 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8344 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8345 | psa_reset_key_attributes(&attributes); |
| 8346 | psa_destroy_key(key); |
| 8347 | mbedtls_free(output); |
| 8348 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8349 | } |
| 8350 | /* END_CASE */ |
| 8351 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8352 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8353 | void asymmetric_decrypt_fail(int key_type_arg, |
| 8354 | data_t *key_data, |
| 8355 | int alg_arg, |
| 8356 | data_t *input_data, |
| 8357 | data_t *label, |
| 8358 | int output_size_arg, |
| 8359 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8360 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8361 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8362 | psa_key_type_t key_type = key_type_arg; |
| 8363 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8364 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 8365 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8366 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8367 | psa_status_t actual_status; |
| 8368 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8369 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8370 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8371 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8372 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8373 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8374 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8375 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8376 | psa_set_key_algorithm(&attributes, alg); |
| 8377 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8378 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8379 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8380 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8381 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8382 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8383 | input_data->x, input_data->len, |
| 8384 | label->x, label->len, |
| 8385 | output, output_size, |
| 8386 | &output_length); |
| 8387 | TEST_EQUAL(actual_status, expected_status); |
| 8388 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8389 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8390 | /* If the label is empty, the test framework puts a non-null pointer |
| 8391 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8392 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8393 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8394 | if (output_size != 0) { |
| 8395 | memset(output, 0, output_size); |
| 8396 | } |
| 8397 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8398 | input_data->x, input_data->len, |
| 8399 | NULL, label->len, |
| 8400 | output, output_size, |
| 8401 | &output_length); |
| 8402 | TEST_EQUAL(actual_status, expected_status); |
| 8403 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8404 | } |
| 8405 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8406 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8407 | psa_reset_key_attributes(&attributes); |
| 8408 | psa_destroy_key(key); |
| 8409 | mbedtls_free(output); |
| 8410 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8411 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8412 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8413 | |
| 8414 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8415 | void key_derivation_init() |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8416 | { |
| 8417 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 8418 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 8419 | * 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] | 8420 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8421 | size_t capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8422 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init(); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 8423 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8424 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8425 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8426 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8427 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8428 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8429 | TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity), |
| 8430 | PSA_ERROR_BAD_STATE); |
| 8431 | TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity), |
| 8432 | PSA_ERROR_BAD_STATE); |
| 8433 | TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity), |
| 8434 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8435 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8436 | /* A default operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8437 | PSA_ASSERT(psa_key_derivation_abort(&func)); |
| 8438 | PSA_ASSERT(psa_key_derivation_abort(&init)); |
| 8439 | PSA_ASSERT(psa_key_derivation_abort(&zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8440 | } |
| 8441 | /* END_CASE */ |
| 8442 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 8443 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8444 | void derive_setup(int alg_arg, int expected_status_arg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8445 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8446 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8447 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8448 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8449 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8450 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8451 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8452 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8453 | expected_status); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8454 | |
| 8455 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8456 | psa_key_derivation_abort(&operation); |
| 8457 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8458 | } |
| 8459 | /* END_CASE */ |
| 8460 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8461 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8462 | void derive_set_capacity(int alg_arg, int capacity_arg, |
| 8463 | int expected_status_arg) |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8464 | { |
| 8465 | psa_algorithm_t alg = alg_arg; |
| 8466 | size_t capacity = capacity_arg; |
| 8467 | psa_status_t expected_status = expected_status_arg; |
| 8468 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8469 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8470 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8471 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8472 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8473 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8474 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 8475 | expected_status); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8476 | |
| 8477 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8478 | psa_key_derivation_abort(&operation); |
| 8479 | PSA_DONE(); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8480 | } |
| 8481 | /* END_CASE */ |
| 8482 | |
| 8483 | /* BEGIN_CASE */ |
Kusumit Ghoderao | d60dfc0 | 2023-05-01 17:39:27 +0530 | [diff] [blame] | 8484 | void parse_binary_string_test(data_t *input, int output) |
| 8485 | { |
| 8486 | uint64_t value; |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 8487 | value = mbedtls_test_parse_binary_string(input); |
Kusumit Ghoderao | d60dfc0 | 2023-05-01 17:39:27 +0530 | [diff] [blame] | 8488 | TEST_EQUAL(value, output); |
| 8489 | } |
| 8490 | /* END_CASE */ |
| 8491 | |
| 8492 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8493 | void derive_input(int alg_arg, |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8494 | int step_arg1, int key_type_arg1, data_t *input1, |
| 8495 | int expected_status_arg1, |
| 8496 | int step_arg2, int key_type_arg2, data_t *input2, |
| 8497 | int expected_status_arg2, |
| 8498 | int step_arg3, int key_type_arg3, data_t *input3, |
| 8499 | int expected_status_arg3, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8500 | int output_key_type_arg, int expected_output_status_arg) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8501 | { |
| 8502 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8503 | psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 }; |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8504 | 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] | 8505 | psa_status_t expected_statuses[] = { expected_status_arg1, |
| 8506 | expected_status_arg2, |
| 8507 | expected_status_arg3 }; |
| 8508 | data_t *inputs[] = { input1, input2, input3 }; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8509 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8510 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8511 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8512 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8513 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8514 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8515 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8516 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8517 | psa_status_t expected_output_status = expected_output_status_arg; |
| 8518 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8519 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8520 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8521 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8522 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8523 | psa_set_key_algorithm(&attributes, alg); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8524 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8525 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8526 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8527 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8528 | mbedtls_test_set_step(i); |
| 8529 | if (steps[i] == 0) { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 8530 | /* Skip this step */ |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8531 | } else if (((psa_key_type_t) key_types[i]) != PSA_KEY_TYPE_NONE && |
| 8532 | key_types[i] != INPUT_INTEGER) { |
| 8533 | psa_set_key_type(&attributes, ((psa_key_type_t) key_types[i])); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8534 | PSA_ASSERT(psa_import_key(&attributes, |
| 8535 | inputs[i]->x, inputs[i]->len, |
| 8536 | &keys[i])); |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8537 | 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] | 8538 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8539 | // When taking a private key as secret input, use key agreement |
| 8540 | // to add the shared secret to the derivation |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8541 | TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self( |
| 8542 | &operation, keys[i]), |
| 8543 | expected_statuses[i]); |
| 8544 | } else { |
| 8545 | TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i], |
| 8546 | keys[i]), |
| 8547 | expected_statuses[i]); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8548 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8549 | } else { |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8550 | if (key_types[i] == INPUT_INTEGER) { |
| 8551 | TEST_EQUAL(psa_key_derivation_input_integer( |
| 8552 | &operation, steps[i], |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 8553 | mbedtls_test_parse_binary_string(inputs[i])), |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8554 | expected_statuses[i]); |
| 8555 | } else { |
Kusumit Ghoderao | 02326d5 | 2023-04-06 17:47:59 +0530 | [diff] [blame] | 8556 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8557 | &operation, steps[i], |
| 8558 | inputs[i]->x, inputs[i]->len), |
| 8559 | expected_statuses[i]); |
Kusumit Ghoderao | 02326d5 | 2023-04-06 17:47:59 +0530 | [diff] [blame] | 8560 | } |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8561 | } |
| 8562 | } |
| 8563 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8564 | if (output_key_type != PSA_KEY_TYPE_NONE) { |
| 8565 | psa_reset_key_attributes(&attributes); |
| 8566 | psa_set_key_type(&attributes, output_key_type); |
| 8567 | psa_set_key_bits(&attributes, 8); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8568 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8569 | psa_key_derivation_output_key(&attributes, &operation, |
| 8570 | &output_key); |
| 8571 | } else { |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8572 | uint8_t buffer[1]; |
| 8573 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8574 | psa_key_derivation_output_bytes(&operation, |
| 8575 | buffer, sizeof(buffer)); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8576 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8577 | TEST_EQUAL(actual_output_status, expected_output_status); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8578 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8579 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8580 | psa_key_derivation_abort(&operation); |
| 8581 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8582 | psa_destroy_key(keys[i]); |
| 8583 | } |
| 8584 | psa_destroy_key(output_key); |
| 8585 | PSA_DONE(); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8586 | } |
| 8587 | /* END_CASE */ |
| 8588 | |
Kusumit Ghoderao | 42b02b9 | 2023-06-06 16:48:46 +0530 | [diff] [blame] | 8589 | /* BEGIN_CASE*/ |
| 8590 | void derive_input_invalid_cost(int alg_arg, int64_t cost) |
| 8591 | { |
| 8592 | psa_algorithm_t alg = alg_arg; |
| 8593 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8594 | |
| 8595 | PSA_ASSERT(psa_crypto_init()); |
| 8596 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8597 | |
| 8598 | TEST_EQUAL(psa_key_derivation_input_integer(&operation, |
| 8599 | PSA_KEY_DERIVATION_INPUT_COST, |
| 8600 | cost), |
| 8601 | PSA_ERROR_NOT_SUPPORTED); |
| 8602 | |
| 8603 | exit: |
| 8604 | psa_key_derivation_abort(&operation); |
| 8605 | PSA_DONE(); |
| 8606 | } |
| 8607 | /* END_CASE*/ |
| 8608 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8609 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8610 | void derive_over_capacity(int alg_arg) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8611 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8612 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8613 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 8614 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8615 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8616 | unsigned char input1[] = "Input 1"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8617 | size_t input1_length = sizeof(input1); |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8618 | unsigned char input2[] = "Input 2"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8619 | size_t input2_length = sizeof(input2); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8620 | uint8_t buffer[42]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8621 | size_t capacity = sizeof(buffer); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 8622 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 8623 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8624 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8625 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8626 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8627 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8628 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8629 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8630 | psa_set_key_algorithm(&attributes, alg); |
| 8631 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8632 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8633 | PSA_ASSERT(psa_import_key(&attributes, |
| 8634 | key_data, sizeof(key_data), |
| 8635 | &key)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8636 | |
| 8637 | /* valid key derivation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8638 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8639 | input1, input1_length, |
| 8640 | input2, input2_length, |
| 8641 | capacity)) { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8642 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8643 | } |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8644 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8645 | /* state of operation shouldn't allow additional generation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8646 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8647 | PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8648 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8649 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8650 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8651 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity), |
| 8652 | PSA_ERROR_INSUFFICIENT_DATA); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8653 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8654 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8655 | psa_key_derivation_abort(&operation); |
| 8656 | psa_destroy_key(key); |
| 8657 | PSA_DONE(); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8658 | } |
| 8659 | /* END_CASE */ |
| 8660 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8661 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8662 | void derive_actions_without_setup() |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8663 | { |
| 8664 | uint8_t output_buffer[16]; |
| 8665 | size_t buffer_size = 16; |
| 8666 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8667 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8668 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8669 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8670 | output_buffer, buffer_size) |
| 8671 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8672 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8673 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8674 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8676 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8677 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8678 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8679 | output_buffer, buffer_size) |
| 8680 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8681 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8682 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8683 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8684 | |
| 8685 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8686 | psa_key_derivation_abort(&operation); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8687 | } |
| 8688 | /* END_CASE */ |
| 8689 | |
| 8690 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8691 | void derive_output(int alg_arg, |
| 8692 | int step1_arg, data_t *input1, int expected_status_arg1, |
| 8693 | int step2_arg, data_t *input2, int expected_status_arg2, |
| 8694 | int step3_arg, data_t *input3, int expected_status_arg3, |
| 8695 | int step4_arg, data_t *input4, int expected_status_arg4, |
| 8696 | data_t *key_agreement_peer_key, |
| 8697 | int requested_capacity_arg, |
| 8698 | data_t *expected_output1, |
| 8699 | data_t *expected_output2, |
| 8700 | int other_key_input_type, |
| 8701 | int key_input_type, |
| 8702 | int derive_type) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8703 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8704 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8705 | psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg }; |
| 8706 | data_t *inputs[] = { input1, input2, input3, input4 }; |
| 8707 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8708 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8709 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8710 | MBEDTLS_SVC_KEY_ID_INIT }; |
| 8711 | psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2, |
| 8712 | expected_status_arg3, expected_status_arg4 }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8713 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8714 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8715 | uint8_t *expected_outputs[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8716 | { expected_output1->x, expected_output2->x }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8717 | size_t output_sizes[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8718 | { expected_output1->len, expected_output2->len }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8719 | size_t output_buffer_size = 0; |
| 8720 | uint8_t *output_buffer = NULL; |
| 8721 | size_t expected_capacity; |
| 8722 | size_t current_capacity; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8723 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; |
| 8724 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; |
| 8725 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; |
| 8726 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8727 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8728 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8729 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8730 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8731 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
| 8732 | if (output_sizes[i] > output_buffer_size) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8733 | output_buffer_size = output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8734 | } |
| 8735 | if (output_sizes[i] == 0) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8736 | expected_outputs[i] = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8737 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8738 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8739 | TEST_CALLOC(output_buffer, output_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8740 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8741 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8742 | /* Extraction phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8743 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8744 | PSA_ASSERT(psa_key_derivation_set_capacity(&operation, |
| 8745 | requested_capacity)); |
| 8746 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8747 | switch (steps[i]) { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8748 | case 0: |
| 8749 | break; |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 8750 | case PSA_KEY_DERIVATION_INPUT_COST: |
| 8751 | TEST_EQUAL(psa_key_derivation_input_integer( |
Kusumit Ghoderao | f28e0f5 | 2023-06-06 15:03:22 +0530 | [diff] [blame] | 8752 | &operation, steps[i], |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 8753 | mbedtls_test_parse_binary_string(inputs[i])), |
Kusumit Ghoderao | f28e0f5 | 2023-06-06 15:03:22 +0530 | [diff] [blame] | 8754 | statuses[i]); |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 8755 | if (statuses[i] != PSA_SUCCESS) { |
| 8756 | goto exit; |
| 8757 | } |
| 8758 | break; |
| 8759 | case PSA_KEY_DERIVATION_INPUT_PASSWORD: |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8760 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8761 | switch (key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8762 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8763 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8764 | &operation, steps[i], |
| 8765 | inputs[i]->x, inputs[i]->len), |
| 8766 | statuses[i]); |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8767 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8768 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8769 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8770 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8771 | break; |
| 8772 | case 1: // input key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8773 | psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE); |
| 8774 | psa_set_key_algorithm(&attributes1, alg); |
| 8775 | psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8776 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8777 | PSA_ASSERT(psa_import_key(&attributes1, |
| 8778 | inputs[i]->x, inputs[i]->len, |
| 8779 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8780 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8781 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 8782 | PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1)); |
| 8783 | TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)), |
| 8784 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8785 | } |
| 8786 | |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 8787 | TEST_EQUAL(psa_key_derivation_input_key(&operation, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8788 | steps[i], |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 8789 | keys[i]), |
| 8790 | statuses[i]); |
| 8791 | |
| 8792 | if (statuses[i] != PSA_SUCCESS) { |
| 8793 | goto exit; |
| 8794 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8795 | break; |
| 8796 | default: |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 8797 | TEST_FAIL("default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8798 | break; |
| 8799 | } |
| 8800 | break; |
| 8801 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8802 | switch (other_key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8803 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8804 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 8805 | steps[i], |
| 8806 | inputs[i]->x, |
| 8807 | inputs[i]->len), |
| 8808 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8809 | break; |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 8810 | case 1: // input key, type DERIVE |
| 8811 | case 11: // input key, type RAW |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8812 | psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE); |
| 8813 | psa_set_key_algorithm(&attributes2, alg); |
| 8814 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8815 | |
| 8816 | // other secret of type RAW_DATA passed with input_key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8817 | if (other_key_input_type == 11) { |
| 8818 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA); |
| 8819 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8820 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8821 | PSA_ASSERT(psa_import_key(&attributes2, |
| 8822 | inputs[i]->x, inputs[i]->len, |
| 8823 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8824 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8825 | TEST_EQUAL(psa_key_derivation_input_key(&operation, |
| 8826 | steps[i], |
| 8827 | keys[i]), |
| 8828 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8829 | break; |
| 8830 | case 2: // key agreement |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8831 | psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE); |
| 8832 | psa_set_key_algorithm(&attributes3, alg); |
| 8833 | psa_set_key_type(&attributes3, |
| 8834 | PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8835 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8836 | PSA_ASSERT(psa_import_key(&attributes3, |
| 8837 | inputs[i]->x, inputs[i]->len, |
| 8838 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8839 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8840 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 8841 | &operation, |
| 8842 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 8843 | keys[i], key_agreement_peer_key->x, |
| 8844 | key_agreement_peer_key->len), statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8845 | break; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8846 | default: |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 8847 | TEST_FAIL("default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8848 | break; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8849 | } |
| 8850 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8851 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8852 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8853 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8854 | break; |
| 8855 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8856 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8857 | &operation, steps[i], |
| 8858 | inputs[i]->x, inputs[i]->len), statuses[i]); |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 8859 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8860 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 8861 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8862 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8863 | break; |
| 8864 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 8865 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8866 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8867 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8868 | ¤t_capacity)); |
| 8869 | TEST_EQUAL(current_capacity, requested_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8870 | expected_capacity = requested_capacity; |
| 8871 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8872 | if (derive_type == 1) { // output key |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8873 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; |
| 8874 | |
| 8875 | /* For output key derivation secret must be provided using |
| 8876 | input key, otherwise operation is not permitted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8877 | if (key_input_type == 1) { |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8878 | expected_status = PSA_SUCCESS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8879 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8880 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8881 | psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT); |
| 8882 | psa_set_key_algorithm(&attributes4, alg); |
| 8883 | psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE); |
| 8884 | psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8885 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8886 | TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation, |
| 8887 | &derived_key), expected_status); |
| 8888 | } else { // output bytes |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8889 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8890 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8891 | /* Read some bytes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8892 | status = psa_key_derivation_output_bytes(&operation, |
| 8893 | output_buffer, output_sizes[i]); |
| 8894 | if (expected_capacity == 0 && output_sizes[i] == 0) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8895 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8896 | TEST_ASSERT(status == PSA_SUCCESS || |
| 8897 | status == PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8898 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8899 | } else if (expected_capacity == 0 || |
| 8900 | output_sizes[i] > expected_capacity) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8901 | /* Capacity exceeded. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8902 | TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8903 | expected_capacity = 0; |
| 8904 | continue; |
| 8905 | } |
| 8906 | /* Success. Check the read data. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8907 | PSA_ASSERT(status); |
| 8908 | if (output_sizes[i] != 0) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8909 | TEST_MEMORY_COMPARE(output_buffer, output_sizes[i], |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8910 | expected_outputs[i], output_sizes[i]); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8911 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8912 | /* Check the operation status. */ |
| 8913 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8914 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8915 | ¤t_capacity)); |
| 8916 | TEST_EQUAL(expected_capacity, current_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8917 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8918 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8919 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8920 | |
| 8921 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8922 | mbedtls_free(output_buffer); |
| 8923 | psa_key_derivation_abort(&operation); |
| 8924 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8925 | psa_destroy_key(keys[i]); |
| 8926 | } |
| 8927 | psa_destroy_key(derived_key); |
| 8928 | PSA_DONE(); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8929 | } |
| 8930 | /* END_CASE */ |
| 8931 | |
| 8932 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8933 | void derive_full(int alg_arg, |
| 8934 | data_t *key_data, |
| 8935 | data_t *input1, |
| 8936 | data_t *input2, |
| 8937 | int requested_capacity_arg) |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8938 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8939 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8940 | psa_algorithm_t alg = alg_arg; |
| 8941 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8942 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8943 | unsigned char output_buffer[16]; |
| 8944 | size_t expected_capacity = requested_capacity; |
| 8945 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8946 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8947 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8948 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8949 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8950 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8951 | psa_set_key_algorithm(&attributes, alg); |
| 8952 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8953 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8954 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8955 | &key)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8956 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8957 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8958 | input1->x, input1->len, |
| 8959 | input2->x, input2->len, |
| 8960 | requested_capacity)) { |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 8961 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8962 | } |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 8963 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8964 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8965 | ¤t_capacity)); |
| 8966 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8967 | |
| 8968 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8969 | while (current_capacity > 0) { |
| 8970 | size_t read_size = sizeof(output_buffer); |
| 8971 | if (read_size > current_capacity) { |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8972 | read_size = current_capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8973 | } |
| 8974 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8975 | output_buffer, |
| 8976 | read_size)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8977 | expected_capacity -= read_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8978 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8979 | ¤t_capacity)); |
| 8980 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8981 | } |
| 8982 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8983 | /* Check that the operation refuses to go over capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8984 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1), |
| 8985 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8986 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8987 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8988 | |
| 8989 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8990 | psa_key_derivation_abort(&operation); |
| 8991 | psa_destroy_key(key); |
| 8992 | PSA_DONE(); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8993 | } |
| 8994 | /* END_CASE */ |
| 8995 | |
Stephan Koch | 78109f5 | 2023-04-12 14:19:36 +0200 | [diff] [blame] | 8996 | /* 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] | 8997 | void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg, |
| 8998 | int derivation_step, |
| 8999 | int capacity, int expected_capacity_status_arg, |
| 9000 | data_t *expected_output, |
| 9001 | int expected_output_status_arg) |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9002 | { |
| 9003 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 9004 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | d378504 | 2022-09-16 06:45:44 -0400 | [diff] [blame] | 9005 | 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] | 9006 | uint8_t *output_buffer = NULL; |
| 9007 | psa_status_t status; |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 9008 | psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg; |
| 9009 | psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg; |
| 9010 | 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] | 9011 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9012 | TEST_CALLOC(output_buffer, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9013 | PSA_ASSERT(psa_crypto_init()); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9014 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9015 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9016 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 9017 | expected_capacity_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9018 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9019 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 9020 | step, input->x, input->len), |
| 9021 | expected_input_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9022 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9023 | if (((psa_status_t) expected_input_status) != PSA_SUCCESS) { |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9024 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9025 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9026 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9027 | status = psa_key_derivation_output_bytes(&operation, output_buffer, |
| 9028 | expected_output->len); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9029 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9030 | TEST_EQUAL(status, expected_output_status); |
| 9031 | if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9032 | TEST_MEMORY_COMPARE(output_buffer, expected_output->len, expected_output->x, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9033 | expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9034 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9035 | |
| 9036 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9037 | mbedtls_free(output_buffer); |
| 9038 | psa_key_derivation_abort(&operation); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9039 | PSA_DONE(); |
| 9040 | } |
| 9041 | /* END_CASE */ |
| 9042 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9043 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9044 | void derive_key_exercise(int alg_arg, |
| 9045 | data_t *key_data, |
| 9046 | data_t *input1, |
| 9047 | data_t *input2, |
| 9048 | int derived_type_arg, |
| 9049 | int derived_bits_arg, |
| 9050 | int derived_usage_arg, |
| 9051 | int derived_alg_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9052 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9053 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9054 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9055 | psa_algorithm_t alg = alg_arg; |
| 9056 | psa_key_type_t derived_type = derived_type_arg; |
| 9057 | size_t derived_bits = derived_bits_arg; |
| 9058 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 9059 | psa_algorithm_t derived_alg = derived_alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9060 | size_t capacity = PSA_BITS_TO_BYTES(derived_bits); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9061 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9062 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 9063 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9064 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9065 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9066 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9067 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9068 | psa_set_key_algorithm(&attributes, alg); |
| 9069 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
| 9070 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 9071 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9072 | |
| 9073 | /* Derive a key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9074 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9075 | input1->x, input1->len, |
| 9076 | input2->x, input2->len, |
| 9077 | capacity)) { |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9078 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9079 | } |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9080 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9081 | psa_set_key_usage_flags(&attributes, derived_usage); |
| 9082 | psa_set_key_algorithm(&attributes, derived_alg); |
| 9083 | psa_set_key_type(&attributes, derived_type); |
| 9084 | psa_set_key_bits(&attributes, derived_bits); |
| 9085 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation, |
| 9086 | &derived_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9087 | |
| 9088 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9089 | PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes)); |
| 9090 | TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type); |
| 9091 | TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9092 | |
| 9093 | /* Exercise the derived key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9094 | if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) { |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9095 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9096 | } |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9097 | |
| 9098 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9099 | /* |
| 9100 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9101 | * thus reset them as required. |
| 9102 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9103 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9104 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9105 | psa_key_derivation_abort(&operation); |
| 9106 | psa_destroy_key(base_key); |
| 9107 | psa_destroy_key(derived_key); |
| 9108 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9109 | } |
| 9110 | /* END_CASE */ |
| 9111 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9112 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9113 | void derive_key_export(int alg_arg, |
| 9114 | data_t *key_data, |
| 9115 | data_t *input1, |
| 9116 | data_t *input2, |
| 9117 | int bytes1_arg, |
| 9118 | int bytes2_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9119 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9120 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9121 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9122 | psa_algorithm_t alg = alg_arg; |
| 9123 | size_t bytes1 = bytes1_arg; |
| 9124 | size_t bytes2 = bytes2_arg; |
| 9125 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9126 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9127 | uint8_t *output_buffer = NULL; |
| 9128 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9129 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9130 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9131 | size_t length; |
| 9132 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9133 | TEST_CALLOC(output_buffer, capacity); |
| 9134 | TEST_CALLOC(export_buffer, capacity); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9135 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9136 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9137 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9138 | psa_set_key_algorithm(&base_attributes, alg); |
| 9139 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9140 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9141 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9142 | |
| 9143 | /* Derive some material and output it. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9144 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9145 | input1->x, input1->len, |
| 9146 | input2->x, input2->len, |
| 9147 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9148 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9149 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9151 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9152 | output_buffer, |
| 9153 | capacity)); |
| 9154 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9155 | |
| 9156 | /* 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] | 9157 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9158 | input1->x, input1->len, |
| 9159 | input2->x, input2->len, |
| 9160 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9161 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9162 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9164 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9165 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9166 | psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA); |
| 9167 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1)); |
| 9168 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9169 | &derived_key)); |
| 9170 | PSA_ASSERT(psa_export_key(derived_key, |
| 9171 | export_buffer, bytes1, |
| 9172 | &length)); |
| 9173 | TEST_EQUAL(length, bytes1); |
| 9174 | PSA_ASSERT(psa_destroy_key(derived_key)); |
| 9175 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2)); |
| 9176 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9177 | &derived_key)); |
| 9178 | PSA_ASSERT(psa_export_key(derived_key, |
| 9179 | export_buffer + bytes1, bytes2, |
| 9180 | &length)); |
| 9181 | TEST_EQUAL(length, bytes2); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9182 | |
| 9183 | /* Compare the outputs from the two runs. */ |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9184 | TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9185 | export_buffer, capacity); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9186 | |
| 9187 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9188 | mbedtls_free(output_buffer); |
| 9189 | mbedtls_free(export_buffer); |
| 9190 | psa_key_derivation_abort(&operation); |
| 9191 | psa_destroy_key(base_key); |
| 9192 | psa_destroy_key(derived_key); |
| 9193 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9194 | } |
| 9195 | /* END_CASE */ |
| 9196 | |
| 9197 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9198 | void derive_key_type(int alg_arg, |
| 9199 | data_t *key_data, |
| 9200 | data_t *input1, |
| 9201 | data_t *input2, |
| 9202 | int key_type_arg, int bits_arg, |
| 9203 | data_t *expected_export) |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9204 | { |
| 9205 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9206 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9207 | const psa_algorithm_t alg = alg_arg; |
| 9208 | const psa_key_type_t key_type = key_type_arg; |
| 9209 | const size_t bits = bits_arg; |
| 9210 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9211 | const size_t export_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9212 | PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9213 | uint8_t *export_buffer = NULL; |
| 9214 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9215 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9216 | size_t export_length; |
| 9217 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9218 | TEST_CALLOC(export_buffer, export_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9219 | PSA_ASSERT(psa_crypto_init()); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9221 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9222 | psa_set_key_algorithm(&base_attributes, alg); |
| 9223 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9224 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9225 | &base_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9226 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9227 | if (mbedtls_test_psa_setup_key_derivation_wrap( |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9228 | &operation, base_key, alg, |
| 9229 | input1->x, input1->len, |
| 9230 | input2->x, input2->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9231 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) { |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9232 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9233 | } |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9234 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9235 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9236 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9237 | psa_set_key_type(&derived_attributes, key_type); |
| 9238 | psa_set_key_bits(&derived_attributes, bits); |
| 9239 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9240 | &derived_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9241 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9242 | PSA_ASSERT(psa_export_key(derived_key, |
| 9243 | export_buffer, export_buffer_size, |
| 9244 | &export_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9245 | TEST_MEMORY_COMPARE(export_buffer, export_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9246 | expected_export->x, expected_export->len); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9247 | |
| 9248 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9249 | mbedtls_free(export_buffer); |
| 9250 | psa_key_derivation_abort(&operation); |
| 9251 | psa_destroy_key(base_key); |
| 9252 | psa_destroy_key(derived_key); |
| 9253 | PSA_DONE(); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9254 | } |
| 9255 | /* END_CASE */ |
| 9256 | |
| 9257 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9258 | void derive_key(int alg_arg, |
| 9259 | data_t *key_data, data_t *input1, data_t *input2, |
| 9260 | int type_arg, int bits_arg, |
| 9261 | int expected_status_arg, |
| 9262 | int is_large_output) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9263 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9264 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9265 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9266 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 9267 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9268 | size_t bits = bits_arg; |
| 9269 | psa_status_t expected_status = expected_status_arg; |
| 9270 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9271 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9272 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9273 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9274 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9275 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9276 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9277 | psa_set_key_algorithm(&base_attributes, alg); |
| 9278 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9279 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9280 | &base_key)); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9282 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9283 | input1->x, input1->len, |
| 9284 | input2->x, input2->len, |
| 9285 | SIZE_MAX)) { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9286 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9287 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9288 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9289 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9290 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9291 | psa_set_key_type(&derived_attributes, type); |
| 9292 | psa_set_key_bits(&derived_attributes, bits); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9293 | |
| 9294 | psa_status_t status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9295 | psa_key_derivation_output_key(&derived_attributes, |
| 9296 | &operation, |
| 9297 | &derived_key); |
| 9298 | if (is_large_output > 0) { |
| 9299 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9300 | } |
| 9301 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9302 | |
| 9303 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9304 | psa_key_derivation_abort(&operation); |
| 9305 | psa_destroy_key(base_key); |
| 9306 | psa_destroy_key(derived_key); |
| 9307 | PSA_DONE(); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9308 | } |
| 9309 | /* END_CASE */ |
| 9310 | |
| 9311 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9312 | void key_agreement_setup(int alg_arg, |
| 9313 | int our_key_type_arg, int our_key_alg_arg, |
| 9314 | data_t *our_key_data, data_t *peer_key_data, |
| 9315 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9316 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9317 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9318 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 9319 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9320 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9321 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9322 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9323 | psa_status_t expected_status = expected_status_arg; |
| 9324 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9325 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9326 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9327 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9328 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9329 | psa_set_key_algorithm(&attributes, our_key_alg); |
| 9330 | psa_set_key_type(&attributes, our_key_type); |
| 9331 | PSA_ASSERT(psa_import_key(&attributes, |
| 9332 | our_key_data->x, our_key_data->len, |
| 9333 | &our_key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9334 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9335 | /* The tests currently include inputs that should fail at either step. |
| 9336 | * Test cases that fail at the setup step should be changed to call |
| 9337 | * key_derivation_setup instead, and this function should be renamed |
| 9338 | * to key_agreement_fail. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9339 | status = psa_key_derivation_setup(&operation, alg); |
| 9340 | if (status == PSA_SUCCESS) { |
| 9341 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 9342 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 9343 | our_key, |
| 9344 | peer_key_data->x, peer_key_data->len), |
| 9345 | expected_status); |
| 9346 | } else { |
| 9347 | TEST_ASSERT(status == expected_status); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9348 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9349 | |
| 9350 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9351 | psa_key_derivation_abort(&operation); |
| 9352 | psa_destroy_key(our_key); |
| 9353 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9354 | } |
| 9355 | /* END_CASE */ |
| 9356 | |
| 9357 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9358 | void raw_key_agreement(int alg_arg, |
| 9359 | int our_key_type_arg, data_t *our_key_data, |
| 9360 | data_t *peer_key_data, |
| 9361 | data_t *expected_output) |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9362 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9363 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9364 | psa_algorithm_t alg = alg_arg; |
| 9365 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9366 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9367 | unsigned char *output = NULL; |
| 9368 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9369 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9371 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9372 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9373 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9374 | psa_set_key_algorithm(&attributes, alg); |
| 9375 | psa_set_key_type(&attributes, our_key_type); |
| 9376 | PSA_ASSERT(psa_import_key(&attributes, |
| 9377 | our_key_data->x, our_key_data->len, |
| 9378 | &our_key)); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9379 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9380 | PSA_ASSERT(psa_get_key_attributes(our_key, &attributes)); |
| 9381 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9382 | |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9383 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9384 | TEST_LE_U(expected_output->len, |
| 9385 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits)); |
| 9386 | TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits), |
| 9387 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9388 | |
| 9389 | /* Good case with exact output size */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9390 | TEST_CALLOC(output, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9391 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9392 | peer_key_data->x, peer_key_data->len, |
| 9393 | output, expected_output->len, |
| 9394 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9395 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9396 | expected_output->x, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9397 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9398 | output = NULL; |
| 9399 | output_length = ~0; |
| 9400 | |
| 9401 | /* Larger buffer */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9402 | TEST_CALLOC(output, expected_output->len + 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9403 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9404 | peer_key_data->x, peer_key_data->len, |
| 9405 | output, expected_output->len + 1, |
| 9406 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9407 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9408 | expected_output->x, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9409 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9410 | output = NULL; |
| 9411 | output_length = ~0; |
| 9412 | |
| 9413 | /* Buffer too small */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9414 | TEST_CALLOC(output, expected_output->len - 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9415 | TEST_EQUAL(psa_raw_key_agreement(alg, our_key, |
| 9416 | peer_key_data->x, peer_key_data->len, |
| 9417 | output, expected_output->len - 1, |
| 9418 | &output_length), |
| 9419 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9420 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9421 | TEST_LE_U(output_length, expected_output->len - 1); |
| 9422 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9423 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9424 | |
| 9425 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9426 | mbedtls_free(output); |
| 9427 | psa_destroy_key(our_key); |
| 9428 | PSA_DONE(); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9429 | } |
| 9430 | /* END_CASE */ |
| 9431 | |
| 9432 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9433 | void key_agreement_capacity(int alg_arg, |
| 9434 | int our_key_type_arg, data_t *our_key_data, |
| 9435 | data_t *peer_key_data, |
| 9436 | int expected_capacity_arg) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9437 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9438 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9439 | psa_algorithm_t alg = alg_arg; |
| 9440 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9441 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9442 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9443 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9444 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9445 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9446 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9447 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9448 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9449 | psa_set_key_algorithm(&attributes, alg); |
| 9450 | psa_set_key_type(&attributes, our_key_type); |
| 9451 | PSA_ASSERT(psa_import_key(&attributes, |
| 9452 | our_key_data->x, our_key_data->len, |
| 9453 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9454 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9455 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9456 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9457 | &operation, |
| 9458 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9459 | peer_key_data->x, peer_key_data->len)); |
| 9460 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9461 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9462 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9463 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9464 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9465 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9466 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 9467 | /* Test the advertised capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9468 | PSA_ASSERT(psa_key_derivation_get_capacity( |
| 9469 | &operation, &actual_capacity)); |
| 9470 | TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9471 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9472 | /* Test the actual capacity by reading the output. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9473 | while (actual_capacity > sizeof(output)) { |
| 9474 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9475 | output, sizeof(output))); |
| 9476 | actual_capacity -= sizeof(output); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9477 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9478 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9479 | output, actual_capacity)); |
| 9480 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1), |
| 9481 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9482 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9483 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9484 | psa_key_derivation_abort(&operation); |
| 9485 | psa_destroy_key(our_key); |
| 9486 | PSA_DONE(); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9487 | } |
| 9488 | /* END_CASE */ |
| 9489 | |
Valerio Setti | ad81967 | 2023-12-29 12:14:41 +0100 | [diff] [blame] | 9490 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */ |
| 9491 | 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] | 9492 | { |
| 9493 | mbedtls_ecp_group_id grp_id = grp_id_arg; |
Valerio Setti | ad81967 | 2023-12-29 12:14:41 +0100 | [diff] [blame] | 9494 | psa_ecc_family_t ecc_family = psa_family_arg; |
| 9495 | size_t bits = bits_arg; |
| 9496 | size_t bits_tmp; |
Valerio Setti | bf999cb | 2023-12-28 17:48:13 +0100 | [diff] [blame] | 9497 | |
Valerio Setti | ad81967 | 2023-12-29 12:14:41 +0100 | [diff] [blame] | 9498 | TEST_EQUAL(ecc_family, mbedtls_ecc_group_to_psa(grp_id, &bits_tmp)); |
| 9499 | TEST_EQUAL(bits, bits_tmp); |
Valerio Setti | ac73952 | 2024-01-04 10:22:01 +0100 | [diff] [blame] | 9500 | TEST_EQUAL(grp_id, mbedtls_ecc_group_from_psa(ecc_family, bits)); |
Valerio Setti | bf999cb | 2023-12-28 17:48:13 +0100 | [diff] [blame] | 9501 | } |
| 9502 | /* END_CASE */ |
| 9503 | |
Valerio Setti | ac73952 | 2024-01-04 10:22:01 +0100 | [diff] [blame] | 9504 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */ |
| 9505 | void ecc_conversion_functions_fail() |
| 9506 | { |
| 9507 | size_t bits; |
| 9508 | |
Valerio Setti | db6e029 | 2024-01-05 10:15:45 +0100 | [diff] [blame] | 9509 | /* Invalid legacy curve identifiers. */ |
| 9510 | TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_MAX, &bits)); |
| 9511 | TEST_EQUAL(0, bits); |
Valerio Setti | ac73952 | 2024-01-04 10:22:01 +0100 | [diff] [blame] | 9512 | TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_NONE, &bits)); |
| 9513 | TEST_EQUAL(0, bits); |
| 9514 | |
| 9515 | /* Invalid PSA EC family. */ |
| 9516 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(0, 192)); |
| 9517 | /* Invalid bit-size for a valid EC family. */ |
| 9518 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_SECP_R1, 512)); |
| 9519 | |
| 9520 | /* Twisted-Edward curves are not supported yet. */ |
| 9521 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, |
| 9522 | mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 255)); |
| 9523 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, |
| 9524 | mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 448)); |
| 9525 | } |
| 9526 | /* END_CASE */ |
| 9527 | |
| 9528 | |
Valerio Setti | bf999cb | 2023-12-28 17:48:13 +0100 | [diff] [blame] | 9529 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9530 | void key_agreement_output(int alg_arg, |
| 9531 | int our_key_type_arg, data_t *our_key_data, |
| 9532 | data_t *peer_key_data, |
| 9533 | data_t *expected_output1, data_t *expected_output2) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9534 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9535 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9536 | psa_algorithm_t alg = alg_arg; |
| 9537 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9538 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9539 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9540 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9541 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9542 | TEST_CALLOC(actual_output, MAX(expected_output1->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9543 | expected_output2->len)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9544 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9545 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9546 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9547 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9548 | psa_set_key_algorithm(&attributes, alg); |
| 9549 | psa_set_key_type(&attributes, our_key_type); |
| 9550 | PSA_ASSERT(psa_import_key(&attributes, |
| 9551 | our_key_data->x, our_key_data->len, |
| 9552 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9553 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9554 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9555 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9556 | &operation, |
| 9557 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9558 | peer_key_data->x, peer_key_data->len)); |
| 9559 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9560 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9561 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9562 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9563 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9564 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9565 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9566 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9567 | actual_output, |
| 9568 | expected_output1->len)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9569 | TEST_MEMORY_COMPARE(actual_output, expected_output1->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9570 | expected_output1->x, expected_output1->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9571 | if (expected_output2->len != 0) { |
| 9572 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9573 | actual_output, |
| 9574 | expected_output2->len)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9575 | TEST_MEMORY_COMPARE(actual_output, expected_output2->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9576 | expected_output2->x, expected_output2->len); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9577 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9578 | |
| 9579 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9580 | psa_key_derivation_abort(&operation); |
| 9581 | psa_destroy_key(our_key); |
| 9582 | PSA_DONE(); |
| 9583 | mbedtls_free(actual_output); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9584 | } |
| 9585 | /* END_CASE */ |
| 9586 | |
| 9587 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9588 | void generate_random(int bytes_arg) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9589 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9590 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9591 | unsigned char *output = NULL; |
| 9592 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9593 | size_t i; |
| 9594 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9595 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9596 | TEST_ASSERT(bytes_arg >= 0); |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 9597 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9598 | TEST_CALLOC(output, bytes); |
| 9599 | TEST_CALLOC(changed, bytes); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9600 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9601 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9602 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9603 | /* Run several times, to ensure that every output byte will be |
| 9604 | * nonzero at least once with overwhelming probability |
| 9605 | * (2^(-8*number_of_runs)). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9606 | for (run = 0; run < 10; run++) { |
| 9607 | if (bytes != 0) { |
| 9608 | memset(output, 0, bytes); |
| 9609 | } |
| 9610 | PSA_ASSERT(psa_generate_random(output, bytes)); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9611 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9612 | for (i = 0; i < bytes; i++) { |
| 9613 | if (output[i] != 0) { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9614 | ++changed[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9615 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9616 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9617 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9618 | |
| 9619 | /* Check that every byte was changed to nonzero at least once. This |
| 9620 | * validates that psa_generate_random is overwriting every byte of |
| 9621 | * the output buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9622 | for (i = 0; i < bytes; i++) { |
| 9623 | TEST_ASSERT(changed[i] != 0); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9624 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9625 | |
| 9626 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9627 | PSA_DONE(); |
| 9628 | mbedtls_free(output); |
| 9629 | mbedtls_free(changed); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9630 | } |
| 9631 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9632 | |
| 9633 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9634 | void generate_key(int type_arg, |
| 9635 | int bits_arg, |
| 9636 | int usage_arg, |
| 9637 | int alg_arg, |
| 9638 | int expected_status_arg, |
| 9639 | int is_large_key) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9640 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9641 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9642 | psa_key_type_t type = type_arg; |
| 9643 | psa_key_usage_t usage = usage_arg; |
| 9644 | size_t bits = bits_arg; |
| 9645 | psa_algorithm_t alg = alg_arg; |
| 9646 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9647 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 9648 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9649 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9650 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9651 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9652 | psa_set_key_usage_flags(&attributes, usage); |
| 9653 | psa_set_key_algorithm(&attributes, alg); |
| 9654 | psa_set_key_type(&attributes, type); |
| 9655 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9656 | |
| 9657 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9658 | psa_status_t status = psa_generate_key(&attributes, &key); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9659 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9660 | if (is_large_key > 0) { |
| 9661 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9662 | } |
| 9663 | TEST_EQUAL(status, expected_status); |
| 9664 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9665 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9666 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9667 | |
| 9668 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9669 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 9670 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 9671 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9672 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 9673 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9674 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 9675 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9676 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9677 | |
| 9678 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9679 | /* |
| 9680 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9681 | * thus reset them as required. |
| 9682 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9683 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9684 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9685 | psa_destroy_key(key); |
| 9686 | PSA_DONE(); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9687 | } |
| 9688 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 9689 | |
Valerio Setti | 19fec54 | 2023-07-25 12:31:50 +0200 | [diff] [blame] | 9690 | /* 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] | 9691 | void generate_key_rsa(int bits_arg, |
| 9692 | data_t *e_arg, |
| 9693 | int expected_status_arg) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9694 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9695 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 9696 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9697 | size_t bits = bits_arg; |
| 9698 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 9699 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 9700 | psa_status_t expected_status = expected_status_arg; |
| 9701 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9702 | uint8_t *exported = NULL; |
| 9703 | size_t exported_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9704 | PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9705 | size_t exported_length = SIZE_MAX; |
| 9706 | uint8_t *e_read_buffer = NULL; |
| 9707 | int is_default_public_exponent = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9708 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9709 | size_t e_read_length = SIZE_MAX; |
| 9710 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9711 | if (e_arg->len == 0 || |
| 9712 | (e_arg->len == 3 && |
| 9713 | 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] | 9714 | is_default_public_exponent = 1; |
| 9715 | e_read_size = 0; |
| 9716 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9717 | TEST_CALLOC(e_read_buffer, e_read_size); |
| 9718 | TEST_CALLOC(exported, exported_size); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9719 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9720 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9721 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9722 | psa_set_key_usage_flags(&attributes, usage); |
| 9723 | psa_set_key_algorithm(&attributes, alg); |
| 9724 | PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type, |
| 9725 | e_arg->x, e_arg->len)); |
| 9726 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9727 | |
| 9728 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9729 | TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status); |
| 9730 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9731 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9732 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9733 | |
| 9734 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9735 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 9736 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 9737 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
Pengyu Lv | d90fbf7 | 2023-12-08 17:13:22 +0800 | [diff] [blame] | 9738 | psa_status_t status = psa_get_key_domain_parameters(&attributes, |
| 9739 | e_read_buffer, e_read_size, |
| 9740 | &e_read_length); |
| 9741 | |
| 9742 | |
| 9743 | #if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \ |
| 9744 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \ |
| 9745 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9746 | if (is_default_public_exponent) { |
| 9747 | TEST_EQUAL(e_read_length, 0); |
| 9748 | } else { |
Pengyu Lv | d90fbf7 | 2023-12-08 17:13:22 +0800 | [diff] [blame] | 9749 | TEST_EQUAL(status, PSA_SUCCESS); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9750 | 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] | 9751 | } |
Pengyu Lv | 9e976f3 | 2023-12-06 16:58:05 +0800 | [diff] [blame] | 9752 | #else |
Pengyu Lv | 9e976f3 | 2023-12-06 16:58:05 +0800 | [diff] [blame] | 9753 | (void) is_default_public_exponent; |
Pengyu Lv | d90fbf7 | 2023-12-08 17:13:22 +0800 | [diff] [blame] | 9754 | TEST_EQUAL(status, PSA_ERROR_NOT_SUPPORTED); |
Pengyu Lv | 9e976f3 | 2023-12-06 16:58:05 +0800 | [diff] [blame] | 9755 | #endif |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9756 | |
| 9757 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9758 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9759 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9760 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9761 | |
| 9762 | /* Export the key and check the public exponent. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9763 | PSA_ASSERT(psa_export_public_key(key, |
| 9764 | exported, exported_size, |
| 9765 | &exported_length)); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9766 | { |
| 9767 | uint8_t *p = exported; |
| 9768 | uint8_t *end = exported + exported_length; |
| 9769 | size_t len; |
| 9770 | /* RSAPublicKey ::= SEQUENCE { |
| 9771 | * modulus INTEGER, -- n |
| 9772 | * publicExponent INTEGER } -- e |
| 9773 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9774 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 9775 | MBEDTLS_ASN1_SEQUENCE | |
| 9776 | MBEDTLS_ASN1_CONSTRUCTED)); |
| 9777 | TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1)); |
| 9778 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 9779 | MBEDTLS_ASN1_INTEGER)); |
| 9780 | if (len >= 1 && p[0] == 0) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9781 | ++p; |
| 9782 | --len; |
| 9783 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9784 | if (e_arg->len == 0) { |
| 9785 | TEST_EQUAL(len, 3); |
| 9786 | TEST_EQUAL(p[0], 1); |
| 9787 | TEST_EQUAL(p[1], 0); |
| 9788 | TEST_EQUAL(p[2], 1); |
| 9789 | } else { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9790 | TEST_MEMORY_COMPARE(p, len, e_arg->x, e_arg->len); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9791 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9792 | } |
| 9793 | |
| 9794 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9795 | /* |
| 9796 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 9797 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 9798 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9799 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9800 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9801 | psa_destroy_key(key); |
| 9802 | PSA_DONE(); |
| 9803 | mbedtls_free(e_read_buffer); |
| 9804 | mbedtls_free(exported); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9805 | } |
| 9806 | /* END_CASE */ |
| 9807 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9808 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9809 | void persistent_key_load_key_from_storage(data_t *data, |
| 9810 | int type_arg, int bits_arg, |
| 9811 | int usage_flags_arg, int alg_arg, |
| 9812 | int generation_method) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9813 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9814 | 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] | 9815 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9816 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9817 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9818 | psa_key_type_t type = type_arg; |
| 9819 | size_t bits = bits_arg; |
| 9820 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 9821 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9822 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9823 | unsigned char *first_export = NULL; |
| 9824 | unsigned char *second_export = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9825 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits); |
Sergey | bef1f63 | 2023-03-06 15:25:06 -0700 | [diff] [blame] | 9826 | size_t first_exported_length = 0; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9827 | size_t second_exported_length; |
| 9828 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9829 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9830 | TEST_CALLOC(first_export, export_size); |
| 9831 | TEST_CALLOC(second_export, export_size); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9832 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9833 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9834 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9835 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9836 | psa_set_key_id(&attributes, key_id); |
| 9837 | psa_set_key_usage_flags(&attributes, usage_flags); |
| 9838 | psa_set_key_algorithm(&attributes, alg); |
| 9839 | psa_set_key_type(&attributes, type); |
| 9840 | psa_set_key_bits(&attributes, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9841 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9842 | switch (generation_method) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9843 | case IMPORT_KEY: |
| 9844 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9845 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, |
| 9846 | &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9847 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9848 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9849 | case GENERATE_KEY: |
| 9850 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9851 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9852 | break; |
| 9853 | |
| 9854 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 9855 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9856 | { |
| 9857 | /* Create base key */ |
| 9858 | psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256); |
| 9859 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9860 | psa_set_key_usage_flags(&base_attributes, |
| 9861 | PSA_KEY_USAGE_DERIVE); |
| 9862 | psa_set_key_algorithm(&base_attributes, derive_alg); |
| 9863 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9864 | PSA_ASSERT(psa_import_key(&base_attributes, |
| 9865 | data->x, data->len, |
| 9866 | &base_key)); |
| 9867 | /* Derive a key. */ |
| 9868 | PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg)); |
| 9869 | PSA_ASSERT(psa_key_derivation_input_key( |
| 9870 | &operation, |
| 9871 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key)); |
| 9872 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 9873 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
| 9874 | NULL, 0)); |
| 9875 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, |
| 9876 | &operation, |
| 9877 | &key)); |
| 9878 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
| 9879 | PSA_ASSERT(psa_destroy_key(base_key)); |
| 9880 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9881 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9882 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9883 | TEST_ASSUME(!"KDF not supported in this configuration"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9884 | #endif |
| 9885 | break; |
| 9886 | |
| 9887 | default: |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 9888 | TEST_FAIL("generation_method not implemented in test"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9889 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9890 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9891 | psa_reset_key_attributes(&attributes); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9892 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9893 | /* Export the key if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9894 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9895 | PSA_ASSERT(psa_export_key(key, |
| 9896 | first_export, export_size, |
| 9897 | &first_exported_length)); |
| 9898 | if (generation_method == IMPORT_KEY) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9899 | TEST_MEMORY_COMPARE(data->x, data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9900 | first_export, first_exported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9901 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9902 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9903 | |
| 9904 | /* Shutdown and restart */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9905 | PSA_ASSERT(psa_purge_key(key)); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 9906 | PSA_DONE(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9907 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9908 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9909 | /* Check key slot still contains key data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9910 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 9911 | TEST_ASSERT(mbedtls_svc_key_id_equal( |
| 9912 | psa_get_key_id(&attributes), key_id)); |
| 9913 | TEST_EQUAL(psa_get_key_lifetime(&attributes), |
| 9914 | PSA_KEY_LIFETIME_PERSISTENT); |
| 9915 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 9916 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 9917 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 9918 | mbedtls_test_update_key_usage_flags(usage_flags)); |
| 9919 | TEST_EQUAL(psa_get_key_algorithm(&attributes), alg); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9920 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9921 | /* Export the key again if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9922 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9923 | PSA_ASSERT(psa_export_key(key, |
| 9924 | second_export, export_size, |
| 9925 | &second_exported_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9926 | TEST_MEMORY_COMPARE(first_export, first_exported_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9927 | second_export, second_exported_length); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9928 | } |
| 9929 | |
| 9930 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9931 | if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9932 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9933 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9934 | |
| 9935 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9936 | /* |
| 9937 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9938 | * thus reset them as required. |
| 9939 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9940 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9941 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9942 | mbedtls_free(first_export); |
| 9943 | mbedtls_free(second_export); |
| 9944 | psa_key_derivation_abort(&operation); |
| 9945 | psa_destroy_key(base_key); |
| 9946 | psa_destroy_key(key); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 9947 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9948 | } |
| 9949 | /* END_CASE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9950 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 9951 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9952 | void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg, |
| 9953 | int primitive_arg, int hash_arg, int role_arg, |
| 9954 | int test_input, data_t *pw_data, |
| 9955 | int inj_err_type_arg, |
| 9956 | int expected_error_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9957 | { |
| 9958 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 9959 | psa_pake_operation_t operation = psa_pake_operation_init(); |
| 9960 | psa_algorithm_t alg = alg_arg; |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9961 | psa_pake_primitive_t primitive = primitive_arg; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 9962 | psa_key_type_t key_type_pw = key_type_pw_arg; |
| 9963 | psa_key_usage_t key_usage_pw = key_usage_pw_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9964 | psa_algorithm_t hash_alg = hash_arg; |
| 9965 | psa_pake_role_t role = role_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9966 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9967 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9968 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
| 9969 | psa_status_t expected_error = expected_error_arg; |
| 9970 | psa_status_t status; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9971 | unsigned char *output_buffer = NULL; |
| 9972 | size_t output_len = 0; |
| 9973 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9974 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9975 | |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9976 | size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9977 | PSA_PAKE_STEP_KEY_SHARE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9978 | TEST_CALLOC(output_buffer, buf_size); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9979 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9980 | if (pw_data->len > 0) { |
| 9981 | psa_set_key_usage_flags(&attributes, key_usage_pw); |
| 9982 | psa_set_key_algorithm(&attributes, alg); |
| 9983 | psa_set_key_type(&attributes, key_type_pw); |
| 9984 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 9985 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9986 | } |
| 9987 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9988 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 9989 | psa_pake_cs_set_primitive(&cipher_suite, primitive); |
| 9990 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9991 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9992 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 9993 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9994 | if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) { |
| 9995 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 9996 | expected_error); |
| 9997 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9998 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 9999 | expected_error); |
| 10000 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10001 | TEST_EQUAL(psa_pake_set_password_key(&operation, key), |
| 10002 | expected_error); |
| 10003 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10004 | TEST_EQUAL(psa_pake_set_role(&operation, role), |
| 10005 | expected_error); |
| 10006 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10007 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10008 | NULL, 0, NULL), |
| 10009 | expected_error); |
| 10010 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10011 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0), |
| 10012 | expected_error); |
| 10013 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10014 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10015 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10016 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10017 | status = psa_pake_setup(&operation, &cipher_suite); |
| 10018 | if (status != PSA_SUCCESS) { |
| 10019 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10020 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10021 | } |
| 10022 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10023 | if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) { |
| 10024 | TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite), |
| 10025 | expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10026 | goto exit; |
| 10027 | } |
| 10028 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10029 | status = psa_pake_set_role(&operation, role); |
| 10030 | if (status != PSA_SUCCESS) { |
| 10031 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10032 | goto exit; |
| 10033 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10035 | if (pw_data->len > 0) { |
| 10036 | status = psa_pake_set_password_key(&operation, key); |
| 10037 | if (status != PSA_SUCCESS) { |
| 10038 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10039 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10040 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10041 | } |
| 10042 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10043 | if (inj_err_type == INJECT_ERR_INVALID_USER) { |
| 10044 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 10045 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10046 | goto exit; |
| 10047 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 10048 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10049 | if (inj_err_type == INJECT_ERR_INVALID_PEER) { |
| 10050 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 10051 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10052 | goto exit; |
| 10053 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 10054 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10055 | if (inj_err_type == INJECT_ERR_SET_USER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10056 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10057 | TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4), |
| 10058 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10059 | goto exit; |
| 10060 | } |
| 10061 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10062 | if (inj_err_type == INJECT_ERR_SET_PEER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10063 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10064 | TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4), |
| 10065 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10066 | goto exit; |
| 10067 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 10068 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10069 | const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10070 | PSA_PAKE_STEP_KEY_SHARE); |
| 10071 | const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10072 | PSA_PAKE_STEP_ZK_PUBLIC); |
| 10073 | const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10074 | PSA_PAKE_STEP_ZK_PROOF); |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 10075 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10076 | if (test_input) { |
| 10077 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 10078 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0), |
| 10079 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10080 | goto exit; |
| 10081 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10082 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10083 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 10084 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 10085 | output_buffer, size_zk_proof), |
| 10086 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10087 | goto exit; |
| 10088 | } |
| 10089 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10090 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 10091 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10092 | output_buffer, size_zk_proof), |
| 10093 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10094 | goto exit; |
| 10095 | } |
| 10096 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10097 | status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10098 | output_buffer, size_key_share); |
| 10099 | if (status != PSA_SUCCESS) { |
| 10100 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10101 | goto exit; |
| 10102 | } |
| 10103 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10104 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 10105 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10106 | output_buffer, size_zk_public + 1), |
| 10107 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10108 | goto exit; |
| 10109 | } |
| 10110 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10111 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10112 | // 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] | 10113 | psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10114 | output_buffer, size_zk_public + 1); |
| 10115 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10116 | output_buffer, size_zk_public), |
| 10117 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10118 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10119 | } |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10120 | } else { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10121 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 10122 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10123 | NULL, 0, NULL), |
| 10124 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10125 | goto exit; |
| 10126 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10127 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10128 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 10129 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 10130 | output_buffer, buf_size, &output_len), |
| 10131 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10132 | goto exit; |
| 10133 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10135 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 10136 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10137 | output_buffer, buf_size, &output_len), |
| 10138 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10139 | goto exit; |
| 10140 | } |
| 10141 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10142 | status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10143 | output_buffer, buf_size, &output_len); |
| 10144 | if (status != PSA_SUCCESS) { |
| 10145 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10146 | goto exit; |
| 10147 | } |
| 10148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10149 | TEST_ASSERT(output_len > 0); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10151 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 10152 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10153 | output_buffer, size_zk_public - 1, &output_len), |
| 10154 | PSA_ERROR_BUFFER_TOO_SMALL); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10155 | goto exit; |
| 10156 | } |
| 10157 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10158 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10159 | // 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] | 10160 | psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10161 | output_buffer, size_zk_public - 1, &output_len); |
| 10162 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10163 | output_buffer, buf_size, &output_len), |
| 10164 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10165 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10166 | } |
| 10167 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10168 | |
| 10169 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10170 | PSA_ASSERT(psa_destroy_key(key)); |
| 10171 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10172 | mbedtls_free(output_buffer); |
| 10173 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10174 | } |
| 10175 | /* END_CASE */ |
| 10176 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 10177 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10178 | void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg, |
| 10179 | int client_input_first, int inject_error, |
| 10180 | data_t *pw_data) |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10181 | { |
| 10182 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10183 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10184 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10185 | psa_algorithm_t alg = alg_arg; |
| 10186 | psa_algorithm_t hash_alg = hash_arg; |
| 10187 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10188 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10189 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10190 | PSA_INIT(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10191 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10192 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10193 | psa_set_key_algorithm(&attributes, alg); |
| 10194 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10195 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10196 | &key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10197 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10198 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10199 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10200 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10201 | |
| 10202 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10203 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10204 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10205 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10206 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10207 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10208 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10209 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10210 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10211 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10212 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10213 | client_input_first, 1, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10215 | if (inject_error == 1 || inject_error == 2) { |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10216 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10217 | } |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10218 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10219 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10220 | client_input_first, 2, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10221 | |
| 10222 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10223 | psa_destroy_key(key); |
| 10224 | psa_pake_abort(&server); |
| 10225 | psa_pake_abort(&client); |
| 10226 | PSA_DONE(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10227 | } |
| 10228 | /* END_CASE */ |
| 10229 | |
| 10230 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10231 | void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg, |
| 10232 | int derive_alg_arg, data_t *pw_data, |
| 10233 | int client_input_first, int inj_err_type_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10234 | { |
| 10235 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10236 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10237 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10238 | psa_algorithm_t alg = alg_arg; |
| 10239 | psa_algorithm_t hash_alg = hash_arg; |
| 10240 | psa_algorithm_t derive_alg = derive_alg_arg; |
| 10241 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10242 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10243 | psa_key_derivation_operation_t server_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10244 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10245 | psa_key_derivation_operation_t client_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10246 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10247 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10248 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10249 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10250 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10251 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10252 | psa_set_key_algorithm(&attributes, alg); |
| 10253 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10254 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10255 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10256 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10257 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10258 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10259 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10260 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10261 | /* Get shared key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10262 | PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg)); |
| 10263 | PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10264 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10265 | if (PSA_ALG_IS_TLS12_PRF(derive_alg) || |
| 10266 | PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) { |
| 10267 | PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive, |
| 10268 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10269 | (const uint8_t *) "", 0)); |
| 10270 | PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive, |
| 10271 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10272 | (const uint8_t *) "", 0)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10273 | } |
| 10274 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10275 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10276 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10277 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10278 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10279 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10280 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10281 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10282 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10283 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10284 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) { |
| 10285 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10286 | PSA_ERROR_BAD_STATE); |
| 10287 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10288 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10289 | goto exit; |
| 10290 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10291 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10292 | /* First round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10293 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10294 | client_input_first, 1, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10295 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10296 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) { |
| 10297 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10298 | PSA_ERROR_BAD_STATE); |
| 10299 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10300 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10301 | goto exit; |
| 10302 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10303 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10304 | /* Second round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10305 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10306 | client_input_first, 2, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10307 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10308 | PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive)); |
| 10309 | PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10310 | |
| 10311 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10312 | psa_key_derivation_abort(&server_derive); |
| 10313 | psa_key_derivation_abort(&client_derive); |
| 10314 | psa_destroy_key(key); |
| 10315 | psa_pake_abort(&server); |
| 10316 | psa_pake_abort(&client); |
| 10317 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10318 | } |
| 10319 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10320 | |
| 10321 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10322 | void ecjpake_size_macros() |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10323 | { |
| 10324 | const psa_algorithm_t alg = PSA_ALG_JPAKE; |
| 10325 | const size_t bits = 256; |
| 10326 | const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10327 | 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] | 10328 | 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] | 10329 | PSA_ECC_FAMILY_SECP_R1); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10330 | |
| 10331 | // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types |
| 10332 | /* 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] | 10333 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10334 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
| 10335 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10336 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10337 | /* The output for ZK_PROOF is the same bitsize as the curve */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10338 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10339 | PSA_BITS_TO_BYTES(bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10340 | |
| 10341 | /* Input sizes are the same as output sizes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10342 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10343 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE)); |
| 10344 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10345 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC)); |
| 10346 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10347 | 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] | 10348 | |
| 10349 | /* These inequalities will always hold even when other PAKEs are added */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10350 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10351 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10352 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10353 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10354 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10355 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10356 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10357 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10358 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10359 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10360 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10361 | PSA_PAKE_INPUT_MAX_SIZE); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10362 | } |
| 10363 | /* END_CASE */ |