Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2 | #include <stdint.h> |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 3 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 4 | #include "mbedtls/asn1.h" |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 5 | #include "mbedtls/asn1write.h" |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 6 | #include "mbedtls/oid.h" |
Gilles Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 7 | #include "common.h" |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 8 | |
Valerio Setti | bf999cb | 2023-12-28 17:48:13 +0100 | [diff] [blame] | 9 | #include "mbedtls/psa_util.h" |
| 10 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 11 | /* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random() |
| 12 | * uses mbedtls_ctr_drbg internally. */ |
| 13 | #include "mbedtls/ctr_drbg.h" |
| 14 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 15 | #include "psa/crypto.h" |
Ronald Cron | 4184107 | 2020-09-17 15:28:26 +0200 | [diff] [blame] | 16 | #include "psa_crypto_slot_management.h" |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 17 | |
Manuel Pégourié-Gonnard | 7abdf7e | 2023-03-09 11:17:43 +0100 | [diff] [blame] | 18 | #include "psa_crypto_core.h" |
| 19 | |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 20 | #include "test/asn1_helpers.h" |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 21 | #include "test/psa_crypto_helpers.h" |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 22 | #include "test/psa_exercise_key.h" |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 23 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 24 | #include "test/drivers/test_driver.h" |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 25 | #define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION |
| 26 | #else |
| 27 | #define TEST_DRIVER_LOCATION 0x7fffff |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 28 | #endif |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 29 | |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 30 | /* If this comes up, it's a bug in the test code or in the test data. */ |
| 31 | #define UNUSED 0xdeadbeef |
| 32 | |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 33 | /* Assert that an operation is (not) active. |
| 34 | * This serves as a proxy for checking if the operation is aborted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 35 | #define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0) |
| 36 | #define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 37 | |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 38 | #if defined(PSA_WANT_ALG_JPAKE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 39 | int ecjpake_operation_setup(psa_pake_operation_t *operation, |
| 40 | psa_pake_cipher_suite_t *cipher_suite, |
| 41 | psa_pake_role_t role, |
| 42 | mbedtls_svc_key_id_t key, |
| 43 | size_t key_available) |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 44 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 45 | PSA_ASSERT(psa_pake_abort(operation)); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 46 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 47 | PSA_ASSERT(psa_pake_setup(operation, cipher_suite)); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 48 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 49 | PSA_ASSERT(psa_pake_set_role(operation, role)); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 50 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 51 | if (key_available) { |
| 52 | PSA_ASSERT(psa_pake_set_password_key(operation, key)); |
| 53 | } |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 54 | return 0; |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 55 | exit: |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 56 | return 1; |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 57 | } |
| 58 | #endif |
| 59 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 60 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 61 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 62 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 63 | /** Test if a buffer contains a constant byte value. |
| 64 | * |
| 65 | * `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] | 66 | * |
| 67 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 68 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 69 | * \param size Size of the buffer in bytes. |
| 70 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 71 | * \return 1 if the buffer is all-bits-zero. |
| 72 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 73 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 74 | 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] | 75 | { |
| 76 | size_t i; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 77 | for (i = 0; i < size; i++) { |
| 78 | if (((unsigned char *) buffer)[i] != c) { |
| 79 | return 0; |
| 80 | } |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 81 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 82 | return 1; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 83 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 84 | #if defined(MBEDTLS_ASN1_WRITE_C) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 85 | /* 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] | 86 | static int asn1_write_10x(unsigned char **p, |
| 87 | unsigned char *start, |
| 88 | size_t bits, |
| 89 | unsigned char x) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 90 | { |
| 91 | int ret; |
| 92 | int len = bits / 8 + 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 93 | if (bits == 0) { |
| 94 | return MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 95 | } |
| 96 | if (bits <= 8 && x >= 1 << (bits - 1)) { |
| 97 | return MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 98 | } |
| 99 | if (*p < start || *p - start < (ptrdiff_t) len) { |
| 100 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 101 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 102 | *p -= len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 103 | (*p)[len-1] = x; |
| 104 | if (bits % 8 == 0) { |
| 105 | (*p)[1] |= 1; |
| 106 | } else { |
| 107 | (*p)[0] |= 1 << (bits % 8); |
| 108 | } |
| 109 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 110 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, |
| 111 | MBEDTLS_ASN1_INTEGER)); |
| 112 | return len; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 113 | } |
| 114 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 115 | static int construct_fake_rsa_key(unsigned char *buffer, |
| 116 | size_t buffer_size, |
| 117 | unsigned char **p, |
| 118 | size_t bits, |
| 119 | int keypair) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 120 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 121 | size_t half_bits = (bits + 1) / 2; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 122 | int ret; |
| 123 | int len = 0; |
| 124 | /* Construct something that looks like a DER encoding of |
| 125 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 126 | * RSAPrivateKey ::= SEQUENCE { |
| 127 | * version Version, |
| 128 | * modulus INTEGER, -- n |
| 129 | * publicExponent INTEGER, -- e |
| 130 | * privateExponent INTEGER, -- d |
| 131 | * prime1 INTEGER, -- p |
| 132 | * prime2 INTEGER, -- q |
| 133 | * exponent1 INTEGER, -- d mod (p-1) |
| 134 | * exponent2 INTEGER, -- d mod (q-1) |
| 135 | * coefficient INTEGER, -- (inverse of q) mod p |
| 136 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 137 | * } |
| 138 | * Or, for a public key, the same structure with only |
| 139 | * version, modulus and publicExponent. |
| 140 | */ |
| 141 | *p = buffer + buffer_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 142 | if (keypair) { |
| 143 | MBEDTLS_ASN1_CHK_ADD(len, /* pq */ |
| 144 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 145 | MBEDTLS_ASN1_CHK_ADD(len, /* dq */ |
| 146 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 147 | MBEDTLS_ASN1_CHK_ADD(len, /* dp */ |
| 148 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 149 | MBEDTLS_ASN1_CHK_ADD(len, /* q */ |
| 150 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 151 | MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */ |
| 152 | asn1_write_10x(p, buffer, half_bits, 3)); |
| 153 | MBEDTLS_ASN1_CHK_ADD(len, /* d */ |
| 154 | asn1_write_10x(p, buffer, bits, 1)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 155 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 156 | MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */ |
| 157 | asn1_write_10x(p, buffer, 17, 1)); |
| 158 | MBEDTLS_ASN1_CHK_ADD(len, /* n */ |
| 159 | asn1_write_10x(p, buffer, bits, 1)); |
| 160 | if (keypair) { |
| 161 | MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */ |
| 162 | mbedtls_asn1_write_int(p, buffer, 0)); |
| 163 | } |
| 164 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 165 | { |
| 166 | const unsigned char tag = |
| 167 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 168 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 169 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 170 | return len; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 171 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 172 | #endif /* MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 173 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 174 | int exercise_mac_setup(psa_key_type_t key_type, |
| 175 | const unsigned char *key_bytes, |
| 176 | size_t key_length, |
| 177 | psa_algorithm_t alg, |
| 178 | psa_mac_operation_t *operation, |
| 179 | psa_status_t *status) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 180 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 181 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 182 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 183 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 184 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 185 | psa_set_key_algorithm(&attributes, alg); |
| 186 | psa_set_key_type(&attributes, key_type); |
| 187 | PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 188 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 189 | *status = psa_mac_sign_setup(operation, key, alg); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 190 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 191 | PSA_ASSERT(psa_mac_abort(operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 192 | /* If setup failed, reproduce the failure, so that the caller can |
| 193 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 194 | if (*status != PSA_SUCCESS) { |
| 195 | TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 196 | } |
| 197 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 198 | psa_destroy_key(key); |
| 199 | return 1; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 200 | |
| 201 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 202 | psa_destroy_key(key); |
| 203 | return 0; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 204 | } |
| 205 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 206 | int exercise_cipher_setup(psa_key_type_t key_type, |
| 207 | const unsigned char *key_bytes, |
| 208 | size_t key_length, |
| 209 | psa_algorithm_t alg, |
| 210 | psa_cipher_operation_t *operation, |
| 211 | psa_status_t *status) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 212 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 213 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 214 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 215 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 216 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 217 | psa_set_key_algorithm(&attributes, alg); |
| 218 | psa_set_key_type(&attributes, key_type); |
| 219 | PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 221 | *status = psa_cipher_encrypt_setup(operation, key, alg); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 222 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 223 | PSA_ASSERT(psa_cipher_abort(operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 224 | /* If setup failed, reproduce the failure, so that the caller can |
| 225 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 226 | if (*status != PSA_SUCCESS) { |
| 227 | TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg), |
| 228 | *status); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 229 | } |
| 230 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 231 | psa_destroy_key(key); |
| 232 | return 1; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 233 | |
| 234 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 235 | psa_destroy_key(key); |
| 236 | return 0; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 237 | } |
| 238 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 239 | 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] | 240 | { |
| 241 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 242 | 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] | 243 | uint8_t buffer[1]; |
| 244 | size_t length; |
| 245 | int ok = 0; |
| 246 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 247 | psa_set_key_id(&attributes, key_id); |
| 248 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 249 | psa_set_key_algorithm(&attributes, PSA_ALG_CTR); |
| 250 | psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); |
| 251 | TEST_EQUAL(psa_get_key_attributes(key, &attributes), |
| 252 | PSA_ERROR_INVALID_HANDLE); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 253 | TEST_EQUAL( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 254 | 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] | 255 | TEST_EQUAL( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 256 | MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0); |
| 257 | TEST_EQUAL(psa_get_key_lifetime(&attributes), 0); |
| 258 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0); |
| 259 | TEST_EQUAL(psa_get_key_algorithm(&attributes), 0); |
| 260 | TEST_EQUAL(psa_get_key_type(&attributes), 0); |
| 261 | TEST_EQUAL(psa_get_key_bits(&attributes), 0); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 263 | TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length), |
| 264 | PSA_ERROR_INVALID_HANDLE); |
| 265 | TEST_EQUAL(psa_export_public_key(key, |
| 266 | buffer, sizeof(buffer), &length), |
| 267 | PSA_ERROR_INVALID_HANDLE); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 268 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 269 | ok = 1; |
| 270 | |
| 271 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 272 | /* |
| 273 | * Key attributes may have been returned by psa_get_key_attributes() |
| 274 | * thus reset them as required. |
| 275 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 276 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 277 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 278 | return ok; |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 279 | } |
| 280 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 281 | /* Assert that a key isn't reported as having a slot number. */ |
| 282 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 283 | #define ASSERT_NO_SLOT_NUMBER(attributes) \ |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 284 | do \ |
| 285 | { \ |
| 286 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 287 | TEST_EQUAL(psa_get_key_slot_number( \ |
| 288 | attributes, \ |
| 289 | &ASSERT_NO_SLOT_NUMBER_slot_number), \ |
| 290 | PSA_ERROR_INVALID_ARGUMENT); \ |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 291 | } \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 292 | while (0) |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 293 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 294 | #define ASSERT_NO_SLOT_NUMBER(attributes) \ |
| 295 | ((void) 0) |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 296 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 297 | |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 298 | #define INPUT_INTEGER 0x10000 /* Out of range of psa_key_type_t */ |
| 299 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 300 | /* An overapproximation of the amount of storage needed for a key of the |
| 301 | * given type and with the given content. The API doesn't make it easy |
| 302 | * to find a good value for the size. The current implementation doesn't |
| 303 | * care about the value anyway. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 304 | #define KEY_BITS_FROM_DATA(type, data) \ |
| 305 | (data)->len |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 306 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 307 | typedef enum { |
| 308 | IMPORT_KEY = 0, |
| 309 | GENERATE_KEY = 1, |
| 310 | DERIVE_KEY = 2 |
| 311 | } generate_method; |
| 312 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 313 | typedef enum { |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 314 | DO_NOT_SET_LENGTHS = 0, |
| 315 | SET_LENGTHS_BEFORE_NONCE = 1, |
| 316 | SET_LENGTHS_AFTER_NONCE = 2 |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 317 | } set_lengths_method_t; |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 318 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 319 | typedef enum { |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 320 | USE_NULL_TAG = 0, |
| 321 | USE_GIVEN_TAG = 1, |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 322 | } tag_usage_method_t; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 323 | |
Kusumit Ghoderao | a14ae5a | 2023-04-19 14:16:26 +0530 | [diff] [blame] | 324 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 325 | /*! |
| 326 | * \brief Internal Function for AEAD multipart tests. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 327 | * \param key_type_arg Type of key passed in |
| 328 | * \param key_data The encryption / decryption key data |
| 329 | * \param alg_arg The type of algorithm used |
| 330 | * \param nonce Nonce data |
| 331 | * \param additional_data Additional data |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 332 | * \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] | 333 | * feed additional data in to be encrypted / |
| 334 | * decrypted. If -1, no chunking. |
| 335 | * \param input_data Data to encrypt / decrypt |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 336 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 337 | * the data in to be encrypted / decrypted. If |
| 338 | * -1, no chunking |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 339 | * \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] | 340 | * expected here, this controls whether or not |
| 341 | * to set lengths, and in what order with |
| 342 | * respect to set nonce. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 343 | * \param expected_output Expected output |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 344 | * \param is_encrypt If non-zero this is an encryption operation. |
Paul Elliott | 41ffae1 | 2021-07-22 21:52:01 +0100 | [diff] [blame] | 345 | * \param do_zero_parts If non-zero, interleave zero length chunks |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 346 | * with normal length chunks. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 347 | * \return int Zero on failure, non-zero on success. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 348 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 349 | static int aead_multipart_internal_func(int key_type_arg, data_t *key_data, |
| 350 | int alg_arg, |
| 351 | data_t *nonce, |
| 352 | data_t *additional_data, |
| 353 | int ad_part_len_arg, |
| 354 | data_t *input_data, |
| 355 | int data_part_len_arg, |
| 356 | set_lengths_method_t set_lengths_method, |
| 357 | data_t *expected_output, |
| 358 | int is_encrypt, |
| 359 | int do_zero_parts) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 360 | { |
| 361 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 362 | psa_key_type_t key_type = key_type_arg; |
| 363 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 364 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 365 | unsigned char *output_data = NULL; |
| 366 | unsigned char *part_data = NULL; |
| 367 | unsigned char *final_data = NULL; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 368 | size_t data_true_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 369 | size_t part_data_size = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 370 | size_t output_size = 0; |
| 371 | size_t final_output_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 372 | size_t output_length = 0; |
| 373 | size_t key_bits = 0; |
| 374 | size_t tag_length = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 375 | size_t part_offset = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 376 | size_t part_length = 0; |
| 377 | size_t output_part_length = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 378 | size_t tag_size = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 379 | size_t ad_part_len = 0; |
| 380 | size_t data_part_len = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 381 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 382 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 383 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 384 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 385 | int test_ok = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 386 | size_t part_count = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 387 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 388 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 389 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 390 | if (is_encrypt) { |
| 391 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 392 | } else { |
| 393 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 394 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 395 | |
| 396 | psa_set_key_algorithm(&attributes, alg); |
| 397 | psa_set_key_type(&attributes, key_type); |
| 398 | |
| 399 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 400 | &key)); |
| 401 | |
| 402 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 403 | key_bits = psa_get_key_bits(&attributes); |
| 404 | |
| 405 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); |
| 406 | |
| 407 | if (is_encrypt) { |
| 408 | /* Tag gets written at end of buffer. */ |
| 409 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 410 | (input_data->len + |
| 411 | tag_length)); |
| 412 | data_true_size = input_data->len; |
| 413 | } else { |
| 414 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 415 | (input_data->len - |
| 416 | tag_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 417 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 418 | /* Do not want to attempt to decrypt tag. */ |
| 419 | data_true_size = input_data->len - tag_length; |
| 420 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 421 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 422 | TEST_CALLOC(output_data, output_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 423 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 424 | if (is_encrypt) { |
| 425 | final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
| 426 | TEST_LE_U(final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
| 427 | } else { |
| 428 | final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); |
| 429 | TEST_LE_U(final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 430 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 431 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 432 | TEST_CALLOC(final_data, final_output_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 433 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 434 | if (is_encrypt) { |
| 435 | status = psa_aead_encrypt_setup(&operation, key, alg); |
| 436 | } else { |
| 437 | status = psa_aead_decrypt_setup(&operation, key, alg); |
| 438 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 439 | |
| 440 | /* If the operation is not supported, just skip and not fail in case the |
| 441 | * encryption involves a common limitation of cryptography hardwares and |
| 442 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 443 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 444 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 445 | 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] | 446 | } |
| 447 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 448 | PSA_ASSERT(status); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 449 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 450 | if (set_lengths_method == DO_NOT_SET_LENGTHS) { |
| 451 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
| 452 | } else if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { |
| 453 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 454 | data_true_size)); |
| 455 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
| 456 | } else if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) { |
| 457 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 458 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 459 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 460 | data_true_size)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 461 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 462 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 463 | if (ad_part_len_arg != -1) { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 464 | /* Pass additional data in parts */ |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 465 | ad_part_len = (size_t) ad_part_len_arg; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 466 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 467 | for (part_offset = 0, part_count = 0; |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 468 | part_offset < additional_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 469 | part_offset += part_length, part_count++) { |
| 470 | if (do_zero_parts && (part_count & 0x01)) { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 471 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 472 | } else if (additional_data->len - part_offset < ad_part_len) { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 473 | part_length = additional_data->len - part_offset; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 474 | } else { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 475 | part_length = ad_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 476 | } |
| 477 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 478 | PSA_ASSERT(psa_aead_update_ad(&operation, |
| 479 | additional_data->x + part_offset, |
| 480 | part_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 481 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 482 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 483 | } else { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 484 | /* Pass additional data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 485 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 486 | additional_data->len)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 487 | } |
| 488 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 489 | if (data_part_len_arg != -1) { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 490 | /* Pass data in parts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 491 | data_part_len = (size_t) data_part_len_arg; |
| 492 | part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 493 | (size_t) data_part_len); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 494 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 495 | TEST_CALLOC(part_data, part_data_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 496 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 497 | for (part_offset = 0, part_count = 0; |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 498 | part_offset < data_true_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 499 | part_offset += part_length, part_count++) { |
| 500 | if (do_zero_parts && (part_count & 0x01)) { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 501 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 502 | } else if ((data_true_size - part_offset) < data_part_len) { |
| 503 | part_length = (data_true_size - part_offset); |
| 504 | } else { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 505 | part_length = data_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 506 | } |
| 507 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 508 | PSA_ASSERT(psa_aead_update(&operation, |
| 509 | (input_data->x + part_offset), |
| 510 | part_length, part_data, |
| 511 | part_data_size, |
| 512 | &output_part_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 513 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 514 | if (output_data && output_part_length) { |
| 515 | memcpy((output_data + output_length), part_data, |
| 516 | output_part_length); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 517 | } |
| 518 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 519 | output_length += output_part_length; |
| 520 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 521 | } else { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 522 | /* Pass all data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 523 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 524 | data_true_size, output_data, |
| 525 | output_size, &output_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 526 | } |
| 527 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 528 | if (is_encrypt) { |
| 529 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 530 | final_output_size, |
| 531 | &output_part_length, |
| 532 | tag_buffer, tag_length, |
| 533 | &tag_size)); |
| 534 | } else { |
| 535 | PSA_ASSERT(psa_aead_verify(&operation, final_data, |
| 536 | final_output_size, |
| 537 | &output_part_length, |
| 538 | (input_data->x + data_true_size), |
| 539 | tag_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 540 | } |
| 541 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 542 | if (output_data && output_part_length) { |
| 543 | memcpy((output_data + output_length), final_data, |
| 544 | output_part_length); |
| 545 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 546 | |
| 547 | output_length += output_part_length; |
| 548 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 549 | |
| 550 | /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE |
| 551 | * should be exact.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 552 | if (is_encrypt) { |
| 553 | TEST_EQUAL(tag_length, tag_size); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 554 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 555 | if (output_data && tag_length) { |
| 556 | memcpy((output_data + output_length), tag_buffer, |
| 557 | tag_length); |
| 558 | } |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 559 | |
| 560 | output_length += tag_length; |
| 561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 562 | TEST_EQUAL(output_length, |
| 563 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 564 | input_data->len)); |
| 565 | TEST_LE_U(output_length, |
| 566 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
| 567 | } else { |
| 568 | TEST_EQUAL(output_length, |
| 569 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, |
| 570 | input_data->len)); |
| 571 | TEST_LE_U(output_length, |
| 572 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 573 | } |
| 574 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 575 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 576 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 577 | output_data, output_length); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 578 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 579 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 580 | test_ok = 1; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 581 | |
| 582 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 583 | psa_destroy_key(key); |
| 584 | psa_aead_abort(&operation); |
| 585 | mbedtls_free(output_data); |
| 586 | mbedtls_free(part_data); |
| 587 | mbedtls_free(final_data); |
| 588 | PSA_DONE(); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 589 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 590 | return test_ok; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 591 | } |
| 592 | |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 593 | /*! |
| 594 | * \brief Internal Function for MAC multipart tests. |
| 595 | * \param key_type_arg Type of key passed in |
| 596 | * \param key_data The encryption / decryption key data |
| 597 | * \param alg_arg The type of algorithm used |
| 598 | * \param input_data Data to encrypt / decrypt |
| 599 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 600 | * the data in to be encrypted / decrypted. If |
| 601 | * -1, no chunking |
| 602 | * \param expected_output Expected output |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 603 | * \param is_verify If non-zero this is a verify operation. |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 604 | * \param do_zero_parts If non-zero, interleave zero length chunks |
| 605 | * with normal length chunks. |
| 606 | * \return int Zero on failure, non-zero on success. |
| 607 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 608 | static int mac_multipart_internal_func(int key_type_arg, data_t *key_data, |
| 609 | int alg_arg, |
| 610 | data_t *input_data, |
| 611 | int data_part_len_arg, |
| 612 | data_t *expected_output, |
| 613 | int is_verify, |
| 614 | int do_zero_parts) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 615 | { |
| 616 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 617 | psa_key_type_t key_type = key_type_arg; |
| 618 | psa_algorithm_t alg = alg_arg; |
| 619 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 620 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
| 621 | size_t part_offset = 0; |
| 622 | size_t part_length = 0; |
| 623 | size_t data_part_len = 0; |
| 624 | size_t mac_len = 0; |
| 625 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 626 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 627 | |
| 628 | int test_ok = 0; |
| 629 | size_t part_count = 0; |
| 630 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 631 | PSA_INIT(); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 632 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 633 | if (is_verify) { |
| 634 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 635 | } else { |
| 636 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 637 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 638 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 639 | psa_set_key_algorithm(&attributes, alg); |
| 640 | psa_set_key_type(&attributes, key_type); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 642 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 643 | &key)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 644 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 645 | if (is_verify) { |
| 646 | status = psa_mac_verify_setup(&operation, key, alg); |
| 647 | } else { |
| 648 | status = psa_mac_sign_setup(&operation, key, alg); |
| 649 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 650 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 651 | PSA_ASSERT(status); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 652 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 653 | if (data_part_len_arg != -1) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 654 | /* Pass data in parts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 655 | data_part_len = (size_t) data_part_len_arg; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 656 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 657 | for (part_offset = 0, part_count = 0; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 658 | part_offset < input_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 659 | part_offset += part_length, part_count++) { |
| 660 | if (do_zero_parts && (part_count & 0x01)) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 661 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 662 | } else if ((input_data->len - part_offset) < data_part_len) { |
| 663 | part_length = (input_data->len - part_offset); |
| 664 | } else { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 665 | part_length = data_part_len; |
| 666 | } |
| 667 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 668 | PSA_ASSERT(psa_mac_update(&operation, |
| 669 | (input_data->x + part_offset), |
| 670 | part_length)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 671 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 672 | } else { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 673 | /* Pass all data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 674 | PSA_ASSERT(psa_mac_update(&operation, input_data->x, |
| 675 | input_data->len)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 676 | } |
| 677 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 678 | if (is_verify) { |
| 679 | PSA_ASSERT(psa_mac_verify_finish(&operation, expected_output->x, |
| 680 | expected_output->len)); |
| 681 | } else { |
| 682 | PSA_ASSERT(psa_mac_sign_finish(&operation, mac, |
| 683 | PSA_MAC_MAX_SIZE, &mac_len)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 684 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 685 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 686 | mac, mac_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | test_ok = 1; |
| 690 | |
| 691 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 692 | psa_destroy_key(key); |
| 693 | psa_mac_abort(&operation); |
| 694 | PSA_DONE(); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 695 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 696 | return test_ok; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 697 | } |
| 698 | |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 699 | #if defined(PSA_WANT_ALG_JPAKE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 700 | static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive, |
| 701 | psa_pake_operation_t *server, |
| 702 | psa_pake_operation_t *client, |
| 703 | int client_input_first, |
| 704 | int round, int inject_error) |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 705 | { |
| 706 | unsigned char *buffer0 = NULL, *buffer1 = NULL; |
| 707 | size_t buffer_length = ( |
| 708 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) + |
| 709 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) + |
| 710 | 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] | 711 | /* The output should be exactly this size according to the spec */ |
| 712 | const size_t expected_size_key_share = |
| 713 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE); |
| 714 | /* The output should be exactly this size according to the spec */ |
| 715 | const size_t expected_size_zk_public = |
| 716 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC); |
| 717 | /* The output can be smaller: the spec allows stripping leading zeroes */ |
| 718 | const size_t max_expected_size_zk_proof = |
| 719 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 720 | size_t buffer0_off = 0; |
| 721 | size_t buffer1_off = 0; |
| 722 | size_t s_g1_len, s_g2_len, s_a_len; |
| 723 | size_t s_g1_off, s_g2_off, s_a_off; |
| 724 | size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len; |
| 725 | size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off; |
| 726 | size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len; |
| 727 | size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off; |
| 728 | size_t c_g1_len, c_g2_len, c_a_len; |
| 729 | size_t c_g1_off, c_g2_off, c_a_off; |
| 730 | size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len; |
| 731 | size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off; |
| 732 | size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len; |
| 733 | size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off; |
| 734 | psa_status_t expected_status = PSA_SUCCESS; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 735 | psa_status_t status; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 736 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 737 | TEST_CALLOC(buffer0, buffer_length); |
| 738 | TEST_CALLOC(buffer1, buffer_length); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 739 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 740 | switch (round) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 741 | case 1: |
| 742 | /* Server first round Output */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 743 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 744 | buffer0 + buffer0_off, |
| 745 | 512 - buffer0_off, &s_g1_len)); |
| 746 | TEST_EQUAL(s_g1_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 747 | s_g1_off = buffer0_off; |
| 748 | buffer0_off += s_g1_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 749 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 750 | buffer0 + buffer0_off, |
| 751 | 512 - buffer0_off, &s_x1_pk_len)); |
| 752 | TEST_EQUAL(s_x1_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 753 | s_x1_pk_off = buffer0_off; |
| 754 | buffer0_off += s_x1_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 755 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 756 | buffer0 + buffer0_off, |
| 757 | 512 - buffer0_off, &s_x1_pr_len)); |
| 758 | TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 759 | s_x1_pr_off = buffer0_off; |
| 760 | buffer0_off += s_x1_pr_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 761 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 762 | buffer0 + buffer0_off, |
| 763 | 512 - buffer0_off, &s_g2_len)); |
| 764 | TEST_EQUAL(s_g2_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 765 | s_g2_off = buffer0_off; |
| 766 | buffer0_off += s_g2_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 767 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 768 | buffer0 + buffer0_off, |
| 769 | 512 - buffer0_off, &s_x2_pk_len)); |
| 770 | TEST_EQUAL(s_x2_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 771 | s_x2_pk_off = buffer0_off; |
| 772 | buffer0_off += s_x2_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 773 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 774 | buffer0 + buffer0_off, |
| 775 | 512 - buffer0_off, &s_x2_pr_len)); |
| 776 | TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 777 | s_x2_pr_off = buffer0_off; |
| 778 | buffer0_off += s_x2_pr_len; |
| 779 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 780 | if (inject_error == 1) { |
Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 781 | buffer0[s_x1_pr_off + 8] ^= 1; |
| 782 | buffer0[s_x2_pr_off + 7] ^= 1; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 783 | expected_status = PSA_ERROR_DATA_INVALID; |
| 784 | } |
| 785 | |
Neil Armstrong | 51009d7 | 2022-09-05 17:59:54 +0200 | [diff] [blame] | 786 | /* |
| 787 | * When injecting errors in inputs, the implementation is |
| 788 | * free to detect it right away of with a delay. |
| 789 | * This permits delaying the error until the end of the input |
| 790 | * sequence, if no error appears then, this will be treated |
| 791 | * as an error. |
| 792 | */ |
| 793 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 794 | if (client_input_first == 1) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 795 | /* Client first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 796 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 797 | buffer0 + s_g1_off, s_g1_len); |
| 798 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 799 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 800 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 801 | } else { |
| 802 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 803 | } |
| 804 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 805 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 806 | buffer0 + s_x1_pk_off, |
| 807 | s_x1_pk_len); |
| 808 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 809 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 810 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 811 | } else { |
| 812 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 813 | } |
| 814 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 815 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 816 | buffer0 + s_x1_pr_off, |
| 817 | s_x1_pr_len); |
| 818 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 819 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 820 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 821 | } else { |
| 822 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 823 | } |
| 824 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 825 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 826 | buffer0 + s_g2_off, |
| 827 | s_g2_len); |
| 828 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 829 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 830 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 831 | } else { |
| 832 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 833 | } |
| 834 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 835 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 836 | buffer0 + s_x2_pk_off, |
| 837 | s_x2_pk_len); |
| 838 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 839 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 840 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 841 | } else { |
| 842 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 843 | } |
| 844 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 845 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 846 | buffer0 + s_x2_pr_off, |
| 847 | s_x2_pr_len); |
| 848 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 849 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 850 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 851 | } else { |
| 852 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 853 | } |
| 854 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 855 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 856 | if (inject_error == 1) { |
| 857 | TEST_ASSERT( |
| 858 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 859 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | /* Client first round Output */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 863 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 864 | buffer1 + buffer1_off, |
| 865 | 512 - buffer1_off, &c_g1_len)); |
| 866 | TEST_EQUAL(c_g1_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 867 | c_g1_off = buffer1_off; |
| 868 | buffer1_off += c_g1_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 869 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 870 | buffer1 + buffer1_off, |
| 871 | 512 - buffer1_off, &c_x1_pk_len)); |
| 872 | TEST_EQUAL(c_x1_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 873 | c_x1_pk_off = buffer1_off; |
| 874 | buffer1_off += c_x1_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 875 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 876 | buffer1 + buffer1_off, |
| 877 | 512 - buffer1_off, &c_x1_pr_len)); |
| 878 | TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 879 | c_x1_pr_off = buffer1_off; |
| 880 | buffer1_off += c_x1_pr_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 881 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 882 | buffer1 + buffer1_off, |
| 883 | 512 - buffer1_off, &c_g2_len)); |
| 884 | TEST_EQUAL(c_g2_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 885 | c_g2_off = buffer1_off; |
| 886 | buffer1_off += c_g2_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 887 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 888 | buffer1 + buffer1_off, |
| 889 | 512 - buffer1_off, &c_x2_pk_len)); |
| 890 | TEST_EQUAL(c_x2_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 891 | c_x2_pk_off = buffer1_off; |
| 892 | buffer1_off += c_x2_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 893 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 894 | buffer1 + buffer1_off, |
| 895 | 512 - buffer1_off, &c_x2_pr_len)); |
| 896 | TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 897 | c_x2_pr_off = buffer1_off; |
| 898 | buffer1_off += c_x2_pr_len; |
| 899 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 900 | if (client_input_first == 0) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 901 | /* Client first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 902 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 903 | buffer0 + s_g1_off, s_g1_len); |
| 904 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 905 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 906 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 907 | } else { |
| 908 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 909 | } |
| 910 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 911 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 912 | buffer0 + s_x1_pk_off, |
| 913 | s_x1_pk_len); |
| 914 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 915 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 916 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 917 | } else { |
| 918 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 919 | } |
| 920 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 921 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 922 | buffer0 + s_x1_pr_off, |
| 923 | s_x1_pr_len); |
| 924 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 925 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 926 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 927 | } else { |
| 928 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 929 | } |
| 930 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 931 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 932 | buffer0 + s_g2_off, |
| 933 | s_g2_len); |
| 934 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 935 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 936 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 937 | } else { |
| 938 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 939 | } |
| 940 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 941 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 942 | buffer0 + s_x2_pk_off, |
| 943 | s_x2_pk_len); |
| 944 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 945 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 946 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 947 | } else { |
| 948 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 949 | } |
| 950 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 951 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 952 | buffer0 + s_x2_pr_off, |
| 953 | s_x2_pr_len); |
| 954 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 955 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 956 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 957 | } else { |
| 958 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 959 | } |
| 960 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 961 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 962 | if (inject_error == 1) { |
| 963 | TEST_ASSERT( |
| 964 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 965 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 966 | } |
| 967 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 968 | if (inject_error == 2) { |
Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 969 | buffer1[c_x1_pr_off + 12] ^= 1; |
| 970 | buffer1[c_x2_pr_off + 7] ^= 1; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 971 | expected_status = PSA_ERROR_DATA_INVALID; |
| 972 | } |
| 973 | |
| 974 | /* Server first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 975 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 976 | buffer1 + c_g1_off, c_g1_len); |
| 977 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 978 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 979 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 980 | } else { |
| 981 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 982 | } |
| 983 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 984 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 985 | buffer1 + c_x1_pk_off, c_x1_pk_len); |
| 986 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 987 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 988 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 989 | } else { |
| 990 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 991 | } |
| 992 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 993 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 994 | buffer1 + c_x1_pr_off, c_x1_pr_len); |
| 995 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 996 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 997 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 998 | } else { |
| 999 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1000 | } |
| 1001 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1002 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 1003 | buffer1 + c_g2_off, c_g2_len); |
| 1004 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1005 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1006 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1007 | } else { |
| 1008 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1009 | } |
| 1010 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1011 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1012 | buffer1 + c_x2_pk_off, c_x2_pk_len); |
| 1013 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1014 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1015 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1016 | } else { |
| 1017 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1018 | } |
| 1019 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1020 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1021 | buffer1 + c_x2_pr_off, c_x2_pr_len); |
| 1022 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1023 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1024 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1025 | } else { |
| 1026 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1027 | } |
| 1028 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1029 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1030 | if (inject_error == 2) { |
| 1031 | TEST_ASSERT( |
| 1032 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1033 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1034 | |
| 1035 | break; |
| 1036 | |
| 1037 | case 2: |
| 1038 | /* Server second round Output */ |
| 1039 | buffer0_off = 0; |
| 1040 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1041 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 1042 | buffer0 + buffer0_off, |
| 1043 | 512 - buffer0_off, &s_a_len)); |
| 1044 | TEST_EQUAL(s_a_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1045 | s_a_off = buffer0_off; |
| 1046 | buffer0_off += s_a_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1047 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1048 | buffer0 + buffer0_off, |
| 1049 | 512 - buffer0_off, &s_x2s_pk_len)); |
| 1050 | TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1051 | s_x2s_pk_off = buffer0_off; |
| 1052 | buffer0_off += s_x2s_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1053 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1054 | buffer0 + buffer0_off, |
| 1055 | 512 - buffer0_off, &s_x2s_pr_len)); |
| 1056 | TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1057 | s_x2s_pr_off = buffer0_off; |
| 1058 | buffer0_off += s_x2s_pr_len; |
| 1059 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1060 | if (inject_error == 3) { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1061 | buffer0[s_x2s_pk_off + 12] += 0x33; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1062 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1063 | } |
| 1064 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1065 | if (client_input_first == 1) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1066 | /* Client second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1067 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1068 | buffer0 + s_a_off, s_a_len); |
| 1069 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1070 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1071 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1072 | } else { |
| 1073 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1074 | } |
| 1075 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1076 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1077 | buffer0 + s_x2s_pk_off, |
| 1078 | s_x2s_pk_len); |
| 1079 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1080 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1081 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1082 | } else { |
| 1083 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1084 | } |
| 1085 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1086 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1087 | buffer0 + s_x2s_pr_off, |
| 1088 | s_x2s_pr_len); |
| 1089 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1090 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1091 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1092 | } else { |
| 1093 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1094 | } |
| 1095 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1096 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1097 | if (inject_error == 3) { |
| 1098 | TEST_ASSERT( |
| 1099 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1100 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | /* Client second round Output */ |
| 1104 | buffer1_off = 0; |
| 1105 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1106 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1107 | buffer1 + buffer1_off, |
| 1108 | 512 - buffer1_off, &c_a_len)); |
| 1109 | TEST_EQUAL(c_a_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1110 | c_a_off = buffer1_off; |
| 1111 | buffer1_off += c_a_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1112 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1113 | buffer1 + buffer1_off, |
| 1114 | 512 - buffer1_off, &c_x2s_pk_len)); |
| 1115 | TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1116 | c_x2s_pk_off = buffer1_off; |
| 1117 | buffer1_off += c_x2s_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1118 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1119 | buffer1 + buffer1_off, |
| 1120 | 512 - buffer1_off, &c_x2s_pr_len)); |
| 1121 | TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1122 | c_x2s_pr_off = buffer1_off; |
| 1123 | buffer1_off += c_x2s_pr_len; |
| 1124 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1125 | if (client_input_first == 0) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1126 | /* Client second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1127 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1128 | buffer0 + s_a_off, s_a_len); |
| 1129 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1130 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1131 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1132 | } else { |
| 1133 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1134 | } |
| 1135 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1136 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1137 | buffer0 + s_x2s_pk_off, |
| 1138 | s_x2s_pk_len); |
| 1139 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1140 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1141 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1142 | } else { |
| 1143 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1144 | } |
| 1145 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1146 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1147 | buffer0 + s_x2s_pr_off, |
| 1148 | s_x2s_pr_len); |
| 1149 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1150 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1151 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1152 | } else { |
| 1153 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1154 | } |
| 1155 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1156 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1157 | if (inject_error == 3) { |
| 1158 | TEST_ASSERT( |
| 1159 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1160 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1161 | } |
| 1162 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1163 | if (inject_error == 4) { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1164 | buffer1[c_x2s_pk_off + 7] += 0x28; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1165 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1166 | } |
| 1167 | |
| 1168 | /* Server second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1169 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 1170 | buffer1 + c_a_off, c_a_len); |
| 1171 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1172 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1173 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1174 | } else { |
| 1175 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1176 | } |
| 1177 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1178 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1179 | buffer1 + c_x2s_pk_off, c_x2s_pk_len); |
| 1180 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1181 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1182 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1183 | } else { |
| 1184 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1185 | } |
| 1186 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1187 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1188 | buffer1 + c_x2s_pr_off, c_x2s_pr_len); |
| 1189 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1190 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1191 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1192 | } else { |
| 1193 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1194 | } |
| 1195 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1196 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1197 | if (inject_error == 4) { |
| 1198 | TEST_ASSERT( |
| 1199 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1200 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1201 | |
| 1202 | break; |
| 1203 | |
| 1204 | } |
| 1205 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1206 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1207 | mbedtls_free(buffer0); |
| 1208 | mbedtls_free(buffer1); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1209 | } |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 1210 | #endif /* PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1211 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1212 | typedef enum { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 1213 | INJECT_ERR_NONE = 0, |
| 1214 | INJECT_ERR_UNINITIALIZED_ACCESS, |
| 1215 | INJECT_ERR_DUPLICATE_SETUP, |
| 1216 | INJECT_ERR_INVALID_USER, |
| 1217 | INJECT_ERR_INVALID_PEER, |
| 1218 | INJECT_ERR_SET_USER, |
| 1219 | INJECT_ERR_SET_PEER, |
| 1220 | INJECT_EMPTY_IO_BUFFER, |
| 1221 | INJECT_UNKNOWN_STEP, |
| 1222 | INJECT_INVALID_FIRST_STEP, |
| 1223 | INJECT_WRONG_BUFFER_SIZE, |
| 1224 | INJECT_VALID_OPERATION_AFTER_FAILURE, |
| 1225 | INJECT_ANTICIPATE_KEY_DERIVATION_1, |
| 1226 | INJECT_ANTICIPATE_KEY_DERIVATION_2, |
| 1227 | } ecjpake_injected_failure_t; |
| 1228 | |
Paul Elliott | 01885fa | 2023-02-09 12:07:30 +0000 | [diff] [blame] | 1229 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Paul Elliott | 1243f93 | 2023-02-07 11:21:10 +0000 | [diff] [blame] | 1230 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1231 | static void interruptible_signverify_get_minmax_completes(uint32_t max_ops, |
| 1232 | psa_status_t expected_status, |
| 1233 | size_t *min_completes, |
| 1234 | size_t *max_completes) |
| 1235 | { |
| 1236 | |
| 1237 | /* This is slightly contrived, but we only really know that with a minimum |
| 1238 | value of max_ops that a successful operation should take more than one op |
| 1239 | to complete, and likewise that with a max_ops of |
| 1240 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */ |
| 1241 | if (max_ops == 0 || max_ops == 1) { |
Paul Elliott | c86d45e | 2023-02-15 17:38:05 +0000 | [diff] [blame] | 1242 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1243 | if (expected_status == PSA_SUCCESS) { |
| 1244 | *min_completes = 2; |
| 1245 | } else { |
| 1246 | *min_completes = 1; |
| 1247 | } |
| 1248 | |
| 1249 | *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED; |
| 1250 | } else { |
| 1251 | *min_completes = 1; |
| 1252 | *max_completes = 1; |
| 1253 | } |
| 1254 | } |
Paul Elliott | 01885fa | 2023-02-09 12:07:30 +0000 | [diff] [blame] | 1255 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1256 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1257 | /* END_HEADER */ |
| 1258 | |
| 1259 | /* BEGIN_DEPENDENCIES |
| 1260 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1261 | * END_DEPENDENCIES |
| 1262 | */ |
| 1263 | |
| 1264 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 7abdf7e | 2023-03-09 11:17:43 +0100 | [diff] [blame] | 1265 | void psa_can_do_hash() |
| 1266 | { |
| 1267 | /* We can't test that this is specific to drivers until partial init has |
| 1268 | * been implemented, but we can at least test before/after full init. */ |
| 1269 | TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE)); |
| 1270 | PSA_INIT(); |
| 1271 | TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE)); |
| 1272 | PSA_DONE(); |
| 1273 | } |
| 1274 | /* END_CASE */ |
| 1275 | |
| 1276 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1277 | void static_checks() |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1278 | { |
| 1279 | size_t max_truncated_mac_size = |
| 1280 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1281 | |
| 1282 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1283 | * encoding. The shifted mask is the maximum truncated value. The |
| 1284 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1285 | TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size); |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1286 | } |
| 1287 | /* END_CASE */ |
| 1288 | |
| 1289 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1290 | void import_with_policy(int type_arg, |
| 1291 | int usage_arg, int alg_arg, |
| 1292 | int expected_status_arg) |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1293 | { |
| 1294 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1295 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1296 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1297 | psa_key_type_t type = type_arg; |
| 1298 | psa_key_usage_t usage = usage_arg; |
| 1299 | psa_algorithm_t alg = alg_arg; |
| 1300 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1301 | const uint8_t key_material[16] = { 0 }; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1302 | psa_status_t status; |
| 1303 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1304 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1306 | psa_set_key_type(&attributes, type); |
| 1307 | psa_set_key_usage_flags(&attributes, usage); |
| 1308 | psa_set_key_algorithm(&attributes, alg); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1309 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1310 | status = psa_import_key(&attributes, |
| 1311 | key_material, sizeof(key_material), |
| 1312 | &key); |
| 1313 | TEST_EQUAL(status, expected_status); |
| 1314 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1315 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1316 | } |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1317 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1318 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1319 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1320 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), |
| 1321 | mbedtls_test_update_key_usage_flags(usage)); |
| 1322 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 1323 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1324 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1325 | PSA_ASSERT(psa_destroy_key(key)); |
| 1326 | test_operations_on_invalid_key(key); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1327 | |
| 1328 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1329 | /* |
| 1330 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1331 | * thus reset them as required. |
| 1332 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1333 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1334 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1335 | psa_destroy_key(key); |
| 1336 | PSA_DONE(); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1337 | } |
| 1338 | /* END_CASE */ |
| 1339 | |
| 1340 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1341 | void import_with_data(data_t *data, int type_arg, |
| 1342 | int attr_bits_arg, |
| 1343 | int expected_status_arg) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1344 | { |
| 1345 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1346 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1347 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1348 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1349 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1350 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1351 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1353 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1354 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1355 | psa_set_key_type(&attributes, type); |
| 1356 | psa_set_key_bits(&attributes, attr_bits); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1358 | status = psa_import_key(&attributes, data->x, data->len, &key); |
Manuel Pégourié-Gonnard | 0509b58 | 2023-08-08 12:47:56 +0200 | [diff] [blame] | 1359 | /* When expecting INVALID_ARGUMENT, also accept NOT_SUPPORTED. |
| 1360 | * |
| 1361 | * This can happen with a type supported only by a driver: |
| 1362 | * - the driver sees the invalid data (for example wrong size) and thinks |
| 1363 | * "well perhaps this is a key size I don't support" so it returns |
| 1364 | * NOT_SUPPORTED which is correct at this point; |
| 1365 | * - we fallback to built-ins, which don't support this type, so return |
| 1366 | * NOT_SUPPORTED which again is correct at this point. |
| 1367 | */ |
| 1368 | if (expected_status == PSA_ERROR_INVALID_ARGUMENT && |
| 1369 | status == PSA_ERROR_NOT_SUPPORTED) { |
| 1370 | ; // OK |
| 1371 | } else { |
| 1372 | TEST_EQUAL(status, expected_status); |
| 1373 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1374 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1375 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1376 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1377 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1378 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1379 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1380 | if (attr_bits != 0) { |
| 1381 | TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes)); |
| 1382 | } |
| 1383 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1385 | PSA_ASSERT(psa_destroy_key(key)); |
| 1386 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1387 | |
| 1388 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1389 | /* |
| 1390 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1391 | * thus reset them as required. |
| 1392 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1393 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1394 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1395 | psa_destroy_key(key); |
| 1396 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1397 | } |
| 1398 | /* END_CASE */ |
| 1399 | |
| 1400 | /* BEGIN_CASE */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1401 | /* Construct and attempt to import a large unstructured key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1402 | void import_large_key(int type_arg, int byte_size_arg, |
| 1403 | int expected_status_arg) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1404 | { |
| 1405 | psa_key_type_t type = type_arg; |
| 1406 | size_t byte_size = byte_size_arg; |
| 1407 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1408 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1409 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1410 | psa_status_t status; |
| 1411 | uint8_t *buffer = NULL; |
| 1412 | size_t buffer_size = byte_size + 1; |
| 1413 | size_t n; |
| 1414 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1415 | /* Skip the test case if the target running the test cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1416 | * accommodate large keys due to heap size constraints */ |
Tom Cosgrove | 412a813 | 2023-07-20 16:55:14 +0100 | [diff] [blame] | 1417 | TEST_CALLOC_OR_SKIP(buffer, buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1418 | memset(buffer, 'K', byte_size); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1419 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1420 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1421 | |
| 1422 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1423 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1424 | psa_set_key_type(&attributes, type); |
| 1425 | status = psa_import_key(&attributes, buffer, byte_size, &key); |
| 1426 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 1427 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1428 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1429 | if (status == PSA_SUCCESS) { |
| 1430 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1431 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 1432 | TEST_EQUAL(psa_get_key_bits(&attributes), |
| 1433 | PSA_BYTES_TO_BITS(byte_size)); |
| 1434 | ASSERT_NO_SLOT_NUMBER(&attributes); |
| 1435 | memset(buffer, 0, byte_size + 1); |
| 1436 | PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n)); |
| 1437 | for (n = 0; n < byte_size; n++) { |
| 1438 | TEST_EQUAL(buffer[n], 'K'); |
| 1439 | } |
| 1440 | for (n = byte_size; n < buffer_size; n++) { |
| 1441 | TEST_EQUAL(buffer[n], 0); |
| 1442 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1446 | /* |
| 1447 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1448 | * thus reset them as required. |
| 1449 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1450 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1451 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1452 | psa_destroy_key(key); |
| 1453 | PSA_DONE(); |
| 1454 | mbedtls_free(buffer); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1455 | } |
| 1456 | /* END_CASE */ |
| 1457 | |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 1458 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1459 | /* Import an RSA key with a valid structure (but not valid numbers |
| 1460 | * inside, beyond having sensible size and parity). This is expected to |
| 1461 | * fail for large keys. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1462 | 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] | 1463 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1464 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1465 | size_t bits = bits_arg; |
| 1466 | psa_status_t expected_status = expected_status_arg; |
| 1467 | psa_status_t status; |
| 1468 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1469 | 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] | 1470 | size_t buffer_size = /* Slight overapproximations */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1471 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1472 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1473 | unsigned char *p; |
| 1474 | int ret; |
| 1475 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1476 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1477 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1478 | PSA_ASSERT(psa_crypto_init()); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1479 | TEST_CALLOC(buffer, buffer_size); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1480 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1481 | TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p, |
| 1482 | bits, keypair)) >= 0); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1483 | length = ret; |
| 1484 | |
| 1485 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1486 | psa_set_key_type(&attributes, type); |
| 1487 | status = psa_import_key(&attributes, p, length, &key); |
| 1488 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1489 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1490 | if (status == PSA_SUCCESS) { |
| 1491 | PSA_ASSERT(psa_destroy_key(key)); |
| 1492 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1493 | |
| 1494 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1495 | mbedtls_free(buffer); |
| 1496 | PSA_DONE(); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1497 | } |
| 1498 | /* END_CASE */ |
| 1499 | |
| 1500 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1501 | void import_export(data_t *data, |
| 1502 | int type_arg, |
| 1503 | int usage_arg, int alg_arg, |
| 1504 | int lifetime_arg, |
| 1505 | int expected_bits, |
| 1506 | int export_size_delta, |
| 1507 | int expected_export_status_arg, |
| 1508 | /*whether reexport must give the original input exactly*/ |
| 1509 | int canonical_input) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1510 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1511 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1512 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1513 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1514 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1515 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1516 | psa_key_lifetime_t lifetime = lifetime_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1517 | unsigned char *exported = NULL; |
| 1518 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1519 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1520 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1521 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1522 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1523 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1524 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1525 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1526 | TEST_CALLOC(exported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1527 | if (!canonical_input) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1528 | TEST_CALLOC(reexported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1529 | } |
| 1530 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1531 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1532 | psa_set_key_lifetime(&attributes, lifetime); |
| 1533 | psa_set_key_usage_flags(&attributes, usage_arg); |
| 1534 | psa_set_key_algorithm(&attributes, alg); |
| 1535 | psa_set_key_type(&attributes, type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1536 | |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1537 | if (PSA_KEY_TYPE_IS_DH(type) && |
| 1538 | expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) { |
Przemek Stekiel | 654bef0 | 2022-12-15 13:28:02 +0100 | [diff] [blame] | 1539 | /* Simulate that buffer is too small, by decreasing its size by 1 byte. */ |
| 1540 | export_size -= 1; |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1541 | } |
| 1542 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1543 | /* Import the key */ |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1544 | TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key), |
Przemek Stekiel | 2e7c33d | 2023-04-27 12:29:45 +0200 | [diff] [blame] | 1545 | PSA_SUCCESS); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1546 | |
| 1547 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1548 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1549 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1550 | TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits); |
| 1551 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1552 | |
| 1553 | /* Export the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1554 | status = psa_export_key(key, exported, export_size, &exported_length); |
| 1555 | TEST_EQUAL(status, expected_export_status); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1556 | |
| 1557 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1558 | * and export_size. On errors, the exported length must be 0. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1559 | TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH); |
| 1560 | TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0); |
| 1561 | TEST_LE_U(exported_length, export_size); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1562 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1563 | TEST_ASSERT(mem_is_char(exported + exported_length, 0, |
| 1564 | export_size - exported_length)); |
| 1565 | if (status != PSA_SUCCESS) { |
| 1566 | TEST_EQUAL(exported_length, 0); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1567 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1568 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1569 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1570 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1571 | * this validates the canonical representations. For canonical inputs, |
| 1572 | * this doesn't directly validate the implementation, but it still helps |
| 1573 | * by cross-validating the test data with the sanity check code. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1574 | if (!psa_key_lifetime_is_external(lifetime)) { |
| 1575 | if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) { |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1576 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1577 | } |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1578 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1579 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1580 | if (canonical_input) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1581 | TEST_MEMORY_COMPARE(data->x, data->len, exported, exported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1582 | } else { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1583 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1584 | PSA_ASSERT(psa_import_key(&attributes, exported, exported_length, |
| 1585 | &key2)); |
| 1586 | PSA_ASSERT(psa_export_key(key2, |
| 1587 | reexported, |
| 1588 | export_size, |
| 1589 | &reexported_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1590 | TEST_MEMORY_COMPARE(exported, exported_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1591 | reexported, reexported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1592 | PSA_ASSERT(psa_destroy_key(key2)); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1593 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1594 | TEST_LE_U(exported_length, |
| 1595 | PSA_EXPORT_KEY_OUTPUT_SIZE(type, |
| 1596 | psa_get_key_bits(&got_attributes))); |
Valerio Setti | 1eacae8 | 2023-07-28 16:07:03 +0200 | [diff] [blame] | 1597 | if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 1598 | TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
| 1599 | } else if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) { |
| 1600 | TEST_LE_U(exported_length, PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
| 1601 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1602 | |
| 1603 | destroy: |
| 1604 | /* Destroy the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1605 | PSA_ASSERT(psa_destroy_key(key)); |
| 1606 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1607 | |
| 1608 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1609 | /* |
| 1610 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1611 | * thus reset them as required. |
| 1612 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1613 | psa_reset_key_attributes(&got_attributes); |
| 1614 | psa_destroy_key(key); |
| 1615 | mbedtls_free(exported); |
| 1616 | mbedtls_free(reexported); |
| 1617 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1618 | } |
| 1619 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1620 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1621 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1622 | void import_export_public_key(data_t *data, |
| 1623 | int type_arg, // key pair or public key |
| 1624 | int alg_arg, |
| 1625 | int lifetime_arg, |
| 1626 | int export_size_delta, |
| 1627 | int expected_export_status_arg, |
| 1628 | data_t *expected_public_key) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1629 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1630 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1631 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1632 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1633 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1634 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1635 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1636 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1637 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1638 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1639 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1640 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1641 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1642 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1643 | psa_set_key_lifetime(&attributes, lifetime); |
| 1644 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1645 | psa_set_key_algorithm(&attributes, alg); |
| 1646 | psa_set_key_type(&attributes, type); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1647 | |
| 1648 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1649 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1650 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1651 | /* Export the public key */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1652 | TEST_CALLOC(exported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1653 | status = psa_export_public_key(key, |
| 1654 | exported, export_size, |
| 1655 | &exported_length); |
| 1656 | TEST_EQUAL(status, expected_export_status); |
| 1657 | if (status == PSA_SUCCESS) { |
| 1658 | 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] | 1659 | size_t bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1660 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1661 | bits = psa_get_key_bits(&attributes); |
| 1662 | TEST_LE_U(expected_public_key->len, |
| 1663 | PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1664 | TEST_LE_U(expected_public_key->len, |
| 1665 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1666 | TEST_LE_U(expected_public_key->len, |
| 1667 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1668 | TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1669 | exported, exported_length); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1670 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1671 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1672 | /* |
| 1673 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1674 | * thus reset them as required. |
| 1675 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1676 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1677 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1678 | mbedtls_free(exported); |
| 1679 | psa_destroy_key(key); |
| 1680 | PSA_DONE(); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1681 | } |
| 1682 | /* END_CASE */ |
| 1683 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1684 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1685 | void import_and_exercise_key(data_t *data, |
| 1686 | int type_arg, |
| 1687 | int bits_arg, |
| 1688 | int alg_arg) |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1689 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1690 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1691 | psa_key_type_t type = type_arg; |
| 1692 | size_t bits = bits_arg; |
| 1693 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1694 | 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] | 1695 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1696 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1697 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1698 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1699 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1700 | psa_set_key_usage_flags(&attributes, usage); |
| 1701 | psa_set_key_algorithm(&attributes, alg); |
| 1702 | psa_set_key_type(&attributes, type); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1703 | |
| 1704 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1705 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1706 | |
| 1707 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1708 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1709 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1710 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1711 | |
| 1712 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1713 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1714 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1715 | } |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1716 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1717 | PSA_ASSERT(psa_destroy_key(key)); |
| 1718 | test_operations_on_invalid_key(key); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1719 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1720 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1721 | /* |
| 1722 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1723 | * thus reset them as required. |
| 1724 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1725 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1726 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1727 | psa_reset_key_attributes(&attributes); |
| 1728 | psa_destroy_key(key); |
| 1729 | PSA_DONE(); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1730 | } |
| 1731 | /* END_CASE */ |
| 1732 | |
| 1733 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1734 | void effective_key_attributes(int type_arg, int expected_type_arg, |
| 1735 | int bits_arg, int expected_bits_arg, |
| 1736 | int usage_arg, int expected_usage_arg, |
| 1737 | int alg_arg, int expected_alg_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1738 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1739 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1740 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1741 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1742 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1743 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1744 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1745 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1746 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1747 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1748 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1749 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1750 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1751 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1752 | psa_set_key_usage_flags(&attributes, usage); |
| 1753 | psa_set_key_algorithm(&attributes, alg); |
| 1754 | psa_set_key_type(&attributes, key_type); |
| 1755 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1756 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1757 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
| 1758 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1759 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1760 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1761 | TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type); |
| 1762 | TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits); |
| 1763 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
| 1764 | TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1765 | |
| 1766 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1767 | /* |
| 1768 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1769 | * thus reset them as required. |
| 1770 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1771 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1772 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1773 | psa_destroy_key(key); |
| 1774 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1775 | } |
| 1776 | /* END_CASE */ |
| 1777 | |
| 1778 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1779 | void check_key_policy(int type_arg, int bits_arg, |
| 1780 | int usage_arg, int alg_arg) |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1781 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1782 | test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg, |
| 1783 | usage_arg, |
| 1784 | mbedtls_test_update_key_usage_flags(usage_arg), |
| 1785 | alg_arg, alg_arg); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1786 | goto exit; |
| 1787 | } |
| 1788 | /* END_CASE */ |
| 1789 | |
| 1790 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1791 | void key_attributes_init() |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1792 | { |
| 1793 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1794 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1795 | * 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] | 1796 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1797 | psa_key_attributes_t func = psa_key_attributes_init(); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1798 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1799 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1800 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1801 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1802 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1803 | TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE); |
| 1804 | TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE); |
| 1805 | TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1806 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1807 | TEST_EQUAL(psa_get_key_type(&func), 0); |
| 1808 | TEST_EQUAL(psa_get_key_type(&init), 0); |
| 1809 | TEST_EQUAL(psa_get_key_type(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1810 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1811 | TEST_EQUAL(psa_get_key_bits(&func), 0); |
| 1812 | TEST_EQUAL(psa_get_key_bits(&init), 0); |
| 1813 | TEST_EQUAL(psa_get_key_bits(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1814 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1815 | TEST_EQUAL(psa_get_key_usage_flags(&func), 0); |
| 1816 | TEST_EQUAL(psa_get_key_usage_flags(&init), 0); |
| 1817 | TEST_EQUAL(psa_get_key_usage_flags(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1818 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1819 | TEST_EQUAL(psa_get_key_algorithm(&func), 0); |
| 1820 | TEST_EQUAL(psa_get_key_algorithm(&init), 0); |
| 1821 | TEST_EQUAL(psa_get_key_algorithm(&zero), 0); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1822 | } |
| 1823 | /* END_CASE */ |
| 1824 | |
| 1825 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1826 | void mac_key_policy(int policy_usage_arg, |
| 1827 | int policy_alg_arg, |
| 1828 | int key_type_arg, |
| 1829 | data_t *key_data, |
| 1830 | int exercise_alg_arg, |
| 1831 | int expected_status_sign_arg, |
| 1832 | int expected_status_verify_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1833 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1834 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1835 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1836 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1837 | psa_key_type_t key_type = key_type_arg; |
| 1838 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 1839 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 1840 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1841 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1842 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 1843 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1844 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1845 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1846 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1847 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1848 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1849 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1850 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1851 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1852 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1853 | &key)); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1854 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1855 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 1856 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1857 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1858 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 1859 | TEST_EQUAL(status, expected_status_sign); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1860 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1861 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1862 | uint8_t input[128] = { 0 }; |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1863 | size_t mac_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1864 | TEST_EQUAL(psa_mac_compute(key, exercise_alg, |
| 1865 | input, 128, |
| 1866 | mac, PSA_MAC_MAX_SIZE, &mac_len), |
| 1867 | expected_status_sign); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1868 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1869 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1870 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 1871 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 1872 | if (status == PSA_SUCCESS) { |
| 1873 | status = psa_mac_update(&operation, input, 128); |
| 1874 | if (status == PSA_SUCCESS) { |
| 1875 | TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE, |
| 1876 | &mac_len), |
| 1877 | expected_status_sign); |
| 1878 | } else { |
| 1879 | TEST_EQUAL(status, expected_status_sign); |
| 1880 | } |
| 1881 | } else { |
| 1882 | TEST_EQUAL(status, expected_status_sign); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1883 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1884 | PSA_ASSERT(psa_mac_abort(&operation)); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1885 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1886 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1887 | status = psa_mac_verify(key, exercise_alg, input, 128, |
| 1888 | mac, mac_len); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1889 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1890 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 1891 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1892 | } else { |
| 1893 | TEST_EQUAL(status, expected_status_verify); |
| 1894 | } |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1895 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1896 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1897 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 1898 | if (status == PSA_SUCCESS) { |
| 1899 | status = psa_mac_update(&operation, input, 128); |
| 1900 | if (status == PSA_SUCCESS) { |
| 1901 | status = psa_mac_verify_finish(&operation, mac, mac_len); |
| 1902 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 1903 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1904 | } else { |
| 1905 | TEST_EQUAL(status, expected_status_verify); |
| 1906 | } |
| 1907 | } else { |
| 1908 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1909 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1910 | } else { |
| 1911 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1912 | } |
| 1913 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1914 | psa_mac_abort(&operation); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1915 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1916 | memset(mac, 0, sizeof(mac)); |
| 1917 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 1918 | TEST_EQUAL(status, expected_status_verify); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1919 | |
| 1920 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1921 | psa_mac_abort(&operation); |
| 1922 | psa_destroy_key(key); |
| 1923 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1924 | } |
| 1925 | /* END_CASE */ |
| 1926 | |
| 1927 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1928 | void cipher_key_policy(int policy_usage_arg, |
| 1929 | int policy_alg, |
| 1930 | int key_type, |
| 1931 | data_t *key_data, |
| 1932 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1933 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1934 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1935 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1936 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1937 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1938 | size_t output_buffer_size = 0; |
| 1939 | size_t input_buffer_size = 0; |
| 1940 | size_t output_length = 0; |
| 1941 | uint8_t *output = NULL; |
| 1942 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1943 | psa_status_t status; |
| 1944 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1945 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg); |
| 1946 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg, |
| 1947 | input_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1948 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1949 | TEST_CALLOC(input, input_buffer_size); |
| 1950 | TEST_CALLOC(output, output_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1951 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1952 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1953 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1954 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1955 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1956 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1957 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1958 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1959 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1960 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1961 | /* Check if no key usage flag implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1962 | TEST_EQUAL(policy_usage, |
| 1963 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1964 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1965 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1966 | status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size, |
| 1967 | output, output_buffer_size, |
| 1968 | &output_length); |
| 1969 | if (policy_alg == exercise_alg && |
| 1970 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1971 | PSA_ASSERT(status); |
| 1972 | } else { |
| 1973 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1974 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1975 | |
| 1976 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1977 | status = psa_cipher_encrypt_setup(&operation, key, exercise_alg); |
| 1978 | if (policy_alg == exercise_alg && |
| 1979 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1980 | PSA_ASSERT(status); |
| 1981 | } else { |
| 1982 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1983 | } |
| 1984 | psa_cipher_abort(&operation); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1985 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1986 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1987 | status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size, |
| 1988 | input, input_buffer_size, |
| 1989 | &output_length); |
| 1990 | if (policy_alg == exercise_alg && |
| 1991 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 1992 | PSA_ASSERT(status); |
| 1993 | } else { |
| 1994 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1995 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1996 | |
| 1997 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1998 | status = psa_cipher_decrypt_setup(&operation, key, exercise_alg); |
| 1999 | if (policy_alg == exercise_alg && |
| 2000 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2001 | PSA_ASSERT(status); |
| 2002 | } else { |
| 2003 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2004 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2005 | |
| 2006 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2007 | psa_cipher_abort(&operation); |
| 2008 | mbedtls_free(input); |
| 2009 | mbedtls_free(output); |
| 2010 | psa_destroy_key(key); |
| 2011 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2012 | } |
| 2013 | /* END_CASE */ |
| 2014 | |
| 2015 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2016 | void aead_key_policy(int policy_usage_arg, |
| 2017 | int policy_alg, |
| 2018 | int key_type, |
| 2019 | data_t *key_data, |
| 2020 | int nonce_length_arg, |
| 2021 | int tag_length_arg, |
| 2022 | int exercise_alg, |
| 2023 | int expected_status_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2024 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2025 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2026 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2027 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2028 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2029 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2030 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2031 | unsigned char nonce[16] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2032 | size_t nonce_length = nonce_length_arg; |
| 2033 | unsigned char tag[16]; |
| 2034 | size_t tag_length = tag_length_arg; |
| 2035 | size_t output_length; |
| 2036 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2037 | TEST_LE_U(nonce_length, sizeof(nonce)); |
| 2038 | TEST_LE_U(tag_length, sizeof(tag)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2039 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2040 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2041 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2042 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2043 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2044 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2045 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2046 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2047 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2048 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2049 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2050 | TEST_EQUAL(policy_usage, |
| 2051 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2052 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2053 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2054 | status = psa_aead_encrypt(key, exercise_alg, |
| 2055 | nonce, nonce_length, |
| 2056 | NULL, 0, |
| 2057 | NULL, 0, |
| 2058 | tag, tag_length, |
| 2059 | &output_length); |
| 2060 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2061 | TEST_EQUAL(status, expected_status); |
| 2062 | } else { |
| 2063 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2064 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2065 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2066 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2067 | status = psa_aead_encrypt_setup(&operation, key, exercise_alg); |
| 2068 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2069 | TEST_EQUAL(status, expected_status); |
| 2070 | } else { |
| 2071 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2072 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2073 | |
| 2074 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2075 | memset(tag, 0, sizeof(tag)); |
| 2076 | status = psa_aead_decrypt(key, exercise_alg, |
| 2077 | nonce, nonce_length, |
| 2078 | NULL, 0, |
| 2079 | tag, tag_length, |
| 2080 | NULL, 0, |
| 2081 | &output_length); |
| 2082 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2083 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2084 | } else if (expected_status == PSA_SUCCESS) { |
| 2085 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2086 | } else { |
| 2087 | TEST_EQUAL(status, expected_status); |
| 2088 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2089 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2090 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2091 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2092 | status = psa_aead_decrypt_setup(&operation, key, exercise_alg); |
| 2093 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2094 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2095 | } else { |
| 2096 | TEST_EQUAL(status, expected_status); |
| 2097 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2098 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2099 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2100 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2101 | psa_destroy_key(key); |
| 2102 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2103 | } |
| 2104 | /* END_CASE */ |
| 2105 | |
| 2106 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2107 | void asymmetric_encryption_key_policy(int policy_usage_arg, |
| 2108 | int policy_alg, |
| 2109 | int key_type, |
| 2110 | data_t *key_data, |
Valerio Setti | f202c29 | 2024-01-15 10:42:37 +0100 | [diff] [blame] | 2111 | int exercise_alg, |
| 2112 | int use_opaque_key) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2113 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2114 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2115 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2116 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2117 | psa_status_t status; |
| 2118 | size_t key_bits; |
| 2119 | size_t buffer_length; |
| 2120 | unsigned char *buffer = NULL; |
| 2121 | size_t output_length; |
| 2122 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2123 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2124 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2125 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2126 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2127 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2128 | |
Valerio Setti | f202c29 | 2024-01-15 10:42:37 +0100 | [diff] [blame] | 2129 | if (use_opaque_key) { |
| 2130 | psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION( |
| 2131 | PSA_KEY_PERSISTENCE_VOLATILE, TEST_DRIVER_LOCATION)); |
| 2132 | } |
| 2133 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2134 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2135 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2136 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2137 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2138 | TEST_EQUAL(policy_usage, |
| 2139 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2140 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2141 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 2142 | key_bits = psa_get_key_bits(&attributes); |
| 2143 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, |
| 2144 | exercise_alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2145 | TEST_CALLOC(buffer, buffer_length); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2146 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2147 | status = psa_asymmetric_encrypt(key, exercise_alg, |
| 2148 | NULL, 0, |
| 2149 | NULL, 0, |
| 2150 | buffer, buffer_length, |
| 2151 | &output_length); |
| 2152 | if (policy_alg == exercise_alg && |
| 2153 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2154 | PSA_ASSERT(status); |
| 2155 | } else { |
| 2156 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2157 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2159 | if (buffer_length != 0) { |
| 2160 | memset(buffer, 0, buffer_length); |
| 2161 | } |
| 2162 | status = psa_asymmetric_decrypt(key, exercise_alg, |
| 2163 | buffer, buffer_length, |
| 2164 | NULL, 0, |
| 2165 | buffer, buffer_length, |
| 2166 | &output_length); |
| 2167 | if (policy_alg == exercise_alg && |
| 2168 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2169 | TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING); |
| 2170 | } else { |
| 2171 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2172 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2173 | |
| 2174 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2175 | /* |
| 2176 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2177 | * thus reset them as required. |
| 2178 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2179 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2181 | psa_destroy_key(key); |
| 2182 | PSA_DONE(); |
| 2183 | mbedtls_free(buffer); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2184 | } |
| 2185 | /* END_CASE */ |
| 2186 | |
| 2187 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2188 | void asymmetric_signature_key_policy(int policy_usage_arg, |
| 2189 | int policy_alg, |
| 2190 | int key_type, |
| 2191 | data_t *key_data, |
| 2192 | int exercise_alg, |
| 2193 | int payload_length_arg, |
| 2194 | int expected_usage_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2195 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2196 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2197 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2198 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 2199 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2200 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2201 | unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 }; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2202 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2203 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2204 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2205 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2206 | int compatible_alg = payload_length_arg > 0; |
| 2207 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2208 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2209 | size_t signature_length; |
| 2210 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2211 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2212 | in the expected usage flags. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2213 | TEST_EQUAL(expected_usage, |
| 2214 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2215 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2216 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2217 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2218 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2219 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2220 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2221 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2222 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2223 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2224 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2225 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2226 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2227 | status = psa_sign_hash(key, exercise_alg, |
| 2228 | payload, payload_length, |
| 2229 | signature, sizeof(signature), |
| 2230 | &signature_length); |
| 2231 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) { |
| 2232 | PSA_ASSERT(status); |
| 2233 | } else { |
| 2234 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2235 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2236 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2237 | memset(signature, 0, sizeof(signature)); |
| 2238 | status = psa_verify_hash(key, exercise_alg, |
| 2239 | payload, payload_length, |
| 2240 | signature, sizeof(signature)); |
| 2241 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) { |
| 2242 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2243 | } else { |
| 2244 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2245 | } |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2246 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2247 | if (PSA_ALG_IS_SIGN_HASH(exercise_alg) && |
| 2248 | PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) { |
| 2249 | status = psa_sign_message(key, exercise_alg, |
| 2250 | payload, payload_length, |
| 2251 | signature, sizeof(signature), |
| 2252 | &signature_length); |
| 2253 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) { |
| 2254 | PSA_ASSERT(status); |
| 2255 | } else { |
| 2256 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2257 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2258 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2259 | memset(signature, 0, sizeof(signature)); |
| 2260 | status = psa_verify_message(key, exercise_alg, |
| 2261 | payload, payload_length, |
| 2262 | signature, sizeof(signature)); |
| 2263 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) { |
| 2264 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2265 | } else { |
| 2266 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2267 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2268 | } |
| 2269 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2270 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2271 | psa_destroy_key(key); |
| 2272 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2273 | } |
| 2274 | /* END_CASE */ |
| 2275 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2276 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2277 | void derive_key_policy(int policy_usage, |
| 2278 | int policy_alg, |
| 2279 | int key_type, |
| 2280 | data_t *key_data, |
| 2281 | int exercise_alg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2282 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2283 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2284 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2285 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2286 | psa_status_t status; |
| 2287 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2288 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2289 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2290 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2291 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2292 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2293 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2294 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2295 | &key)); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2296 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2297 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2298 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2299 | if (PSA_ALG_IS_TLS12_PRF(exercise_alg) || |
| 2300 | PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) { |
| 2301 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 2302 | &operation, |
| 2303 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2304 | (const uint8_t *) "", 0)); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2305 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2306 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2307 | status = psa_key_derivation_input_key(&operation, |
| 2308 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 2309 | key); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2310 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2311 | if (policy_alg == exercise_alg && |
| 2312 | (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) { |
| 2313 | PSA_ASSERT(status); |
| 2314 | } else { |
| 2315 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2316 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2317 | |
| 2318 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2319 | psa_key_derivation_abort(&operation); |
| 2320 | psa_destroy_key(key); |
| 2321 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2322 | } |
| 2323 | /* END_CASE */ |
| 2324 | |
| 2325 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2326 | void agreement_key_policy(int policy_usage, |
| 2327 | int policy_alg, |
| 2328 | int key_type_arg, |
| 2329 | data_t *key_data, |
| 2330 | int exercise_alg, |
| 2331 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2332 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2333 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2334 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2335 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2336 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2337 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2338 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2339 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2340 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2341 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2342 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2343 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2344 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2345 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2346 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2347 | &key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2349 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
| 2350 | status = mbedtls_test_psa_key_agreement_with_self(&operation, key); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2352 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2353 | |
| 2354 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2355 | psa_key_derivation_abort(&operation); |
| 2356 | psa_destroy_key(key); |
| 2357 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2358 | } |
| 2359 | /* END_CASE */ |
| 2360 | |
| 2361 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2362 | void key_policy_alg2(int key_type_arg, data_t *key_data, |
| 2363 | int usage_arg, int alg_arg, int alg2_arg) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2364 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2365 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2366 | psa_key_type_t key_type = key_type_arg; |
| 2367 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2368 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2369 | psa_key_usage_t usage = usage_arg; |
| 2370 | psa_algorithm_t alg = alg_arg; |
| 2371 | psa_algorithm_t alg2 = alg2_arg; |
| 2372 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2373 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2374 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2375 | psa_set_key_usage_flags(&attributes, usage); |
| 2376 | psa_set_key_algorithm(&attributes, alg); |
| 2377 | psa_set_key_enrollment_algorithm(&attributes, alg2); |
| 2378 | psa_set_key_type(&attributes, key_type); |
| 2379 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2380 | &key)); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2381 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2382 | /* Update the usage flags to obtain implicit usage flags */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2383 | usage = mbedtls_test_update_key_usage_flags(usage); |
| 2384 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 2385 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage); |
| 2386 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 2387 | TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2389 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2390 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2391 | } |
| 2392 | if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2393 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2394 | } |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2395 | |
| 2396 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2397 | /* |
| 2398 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2399 | * thus reset them as required. |
| 2400 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2401 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2402 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2403 | psa_destroy_key(key); |
| 2404 | PSA_DONE(); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2405 | } |
| 2406 | /* END_CASE */ |
| 2407 | |
| 2408 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2409 | void raw_agreement_key_policy(int policy_usage, |
| 2410 | int policy_alg, |
| 2411 | int key_type_arg, |
| 2412 | data_t *key_data, |
| 2413 | int exercise_alg, |
| 2414 | int expected_status_arg) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2415 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2416 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2417 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2418 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2419 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2420 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2421 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2422 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2423 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2424 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2425 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2426 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2427 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2428 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2429 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2430 | &key)); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2431 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2432 | status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2433 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2434 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2435 | |
| 2436 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2437 | psa_key_derivation_abort(&operation); |
| 2438 | psa_destroy_key(key); |
| 2439 | PSA_DONE(); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2440 | } |
| 2441 | /* END_CASE */ |
| 2442 | |
| 2443 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2444 | void copy_success(int source_usage_arg, |
| 2445 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4ea4ad0 | 2022-12-04 15:11:00 +0100 | [diff] [blame] | 2446 | int source_lifetime_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2447 | int type_arg, data_t *material, |
| 2448 | int copy_attributes, |
| 2449 | int target_usage_arg, |
| 2450 | int target_alg_arg, int target_alg2_arg, |
Gilles Peskine | 4ea4ad0 | 2022-12-04 15:11:00 +0100 | [diff] [blame] | 2451 | int target_lifetime_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2452 | int expected_usage_arg, |
| 2453 | int expected_alg_arg, int expected_alg2_arg) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2454 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2455 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2456 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2457 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2458 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2459 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2460 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 2461 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2462 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2463 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2464 | uint8_t *export_buffer = NULL; |
| 2465 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2466 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2467 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2468 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2469 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2470 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2471 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2472 | psa_set_key_type(&source_attributes, type_arg); |
| 2473 | psa_set_key_lifetime(&source_attributes, source_lifetime); |
| 2474 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2475 | material->x, material->len, |
| 2476 | &source_key)); |
| 2477 | PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2478 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2479 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2480 | if (copy_attributes) { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2481 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2482 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2483 | psa_set_key_lifetime(&target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2484 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2485 | if (target_usage_arg != -1) { |
| 2486 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2487 | } |
| 2488 | if (target_alg_arg != -1) { |
| 2489 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2490 | } |
| 2491 | if (target_alg2_arg != -1) { |
| 2492 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
| 2493 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2494 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2495 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2496 | /* Copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2497 | PSA_ASSERT(psa_copy_key(source_key, |
| 2498 | &target_attributes, &target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2499 | |
| 2500 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2501 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2502 | |
| 2503 | /* Test that the target slot has the expected content and policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2504 | PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes)); |
| 2505 | TEST_EQUAL(psa_get_key_type(&source_attributes), |
| 2506 | psa_get_key_type(&target_attributes)); |
| 2507 | TEST_EQUAL(psa_get_key_bits(&source_attributes), |
| 2508 | psa_get_key_bits(&target_attributes)); |
| 2509 | TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes)); |
| 2510 | TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes)); |
| 2511 | TEST_EQUAL(expected_alg2, |
| 2512 | psa_get_key_enrollment_algorithm(&target_attributes)); |
| 2513 | if (expected_usage & PSA_KEY_USAGE_EXPORT) { |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2514 | size_t length; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2515 | TEST_CALLOC(export_buffer, material->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2516 | PSA_ASSERT(psa_export_key(target_key, export_buffer, |
| 2517 | material->len, &length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2518 | TEST_MEMORY_COMPARE(material->x, material->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2519 | export_buffer, length); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2520 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2521 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2522 | if (!psa_key_lifetime_is_external(target_lifetime)) { |
| 2523 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2524 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2525 | } |
| 2526 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2527 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2528 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2529 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2530 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2531 | PSA_ASSERT(psa_destroy_key(target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2532 | |
| 2533 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2534 | /* |
| 2535 | * Source and target key attributes may have been returned by |
| 2536 | * psa_get_key_attributes() thus reset them as required. |
| 2537 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2538 | psa_reset_key_attributes(&source_attributes); |
| 2539 | psa_reset_key_attributes(&target_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2540 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2541 | PSA_DONE(); |
| 2542 | mbedtls_free(export_buffer); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2543 | } |
| 2544 | /* END_CASE */ |
| 2545 | |
| 2546 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2547 | void copy_fail(int source_usage_arg, |
| 2548 | int source_alg_arg, int source_alg2_arg, |
| 2549 | int source_lifetime_arg, |
| 2550 | int type_arg, data_t *material, |
| 2551 | int target_type_arg, int target_bits_arg, |
| 2552 | int target_usage_arg, |
| 2553 | int target_alg_arg, int target_alg2_arg, |
| 2554 | int target_id_arg, int target_lifetime_arg, |
| 2555 | int expected_status_arg) |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2556 | { |
| 2557 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2558 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2559 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2560 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2561 | 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] | 2562 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2563 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2564 | |
| 2565 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2566 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2567 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2568 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2569 | psa_set_key_type(&source_attributes, type_arg); |
| 2570 | psa_set_key_lifetime(&source_attributes, source_lifetime_arg); |
| 2571 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2572 | material->x, material->len, |
| 2573 | &source_key)); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2574 | |
| 2575 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2576 | psa_set_key_id(&target_attributes, key_id); |
| 2577 | psa_set_key_lifetime(&target_attributes, target_lifetime_arg); |
| 2578 | psa_set_key_type(&target_attributes, target_type_arg); |
| 2579 | psa_set_key_bits(&target_attributes, target_bits_arg); |
| 2580 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2581 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2582 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2583 | |
| 2584 | /* Try to copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2585 | TEST_EQUAL(psa_copy_key(source_key, |
| 2586 | &target_attributes, &target_key), |
| 2587 | expected_status_arg); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2588 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2589 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2590 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2591 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2592 | psa_reset_key_attributes(&source_attributes); |
| 2593 | psa_reset_key_attributes(&target_attributes); |
| 2594 | PSA_DONE(); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2595 | } |
| 2596 | /* END_CASE */ |
| 2597 | |
| 2598 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2599 | void hash_operation_init() |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2600 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2601 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2602 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2603 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2604 | * 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] | 2605 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2606 | psa_hash_operation_t func = psa_hash_operation_init(); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2607 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2608 | psa_hash_operation_t zero; |
| 2609 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2610 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2611 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2612 | /* A freshly-initialized hash operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2613 | TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)), |
| 2614 | PSA_ERROR_BAD_STATE); |
| 2615 | TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)), |
| 2616 | PSA_ERROR_BAD_STATE); |
| 2617 | TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)), |
| 2618 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2619 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2620 | /* A default hash operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2621 | PSA_ASSERT(psa_hash_abort(&func)); |
| 2622 | PSA_ASSERT(psa_hash_abort(&init)); |
| 2623 | PSA_ASSERT(psa_hash_abort(&zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2624 | } |
| 2625 | /* END_CASE */ |
| 2626 | |
| 2627 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2628 | void hash_setup(int alg_arg, |
| 2629 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2630 | { |
| 2631 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2632 | uint8_t *output = NULL; |
| 2633 | size_t output_size = 0; |
| 2634 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2635 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2636 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2637 | psa_status_t status; |
| 2638 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2639 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2640 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2641 | /* Hash Setup, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2642 | output_size = PSA_HASH_LENGTH(alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2643 | TEST_CALLOC(output, output_size); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2644 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2645 | status = psa_hash_compute(alg, NULL, 0, |
| 2646 | output, output_size, &output_length); |
| 2647 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2648 | |
| 2649 | /* Hash Setup, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2650 | status = psa_hash_setup(&operation, alg); |
| 2651 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2652 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2653 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2654 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2655 | |
| 2656 | /* If setup failed, reproduce the failure, so as to |
| 2657 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2658 | if (status != PSA_SUCCESS) { |
| 2659 | TEST_EQUAL(psa_hash_setup(&operation, alg), status); |
| 2660 | } |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2661 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2662 | /* Now the operation object should be reusable. */ |
| 2663 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2664 | PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG)); |
| 2665 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2666 | #endif |
| 2667 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2668 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2669 | mbedtls_free(output); |
| 2670 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2671 | } |
| 2672 | /* END_CASE */ |
| 2673 | |
| 2674 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2675 | void hash_compute_fail(int alg_arg, data_t *input, |
| 2676 | int output_size_arg, int expected_status_arg) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2677 | { |
| 2678 | psa_algorithm_t alg = alg_arg; |
| 2679 | uint8_t *output = NULL; |
| 2680 | size_t output_size = output_size_arg; |
| 2681 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2682 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2683 | psa_status_t expected_status = expected_status_arg; |
| 2684 | psa_status_t status; |
| 2685 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2686 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2687 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2688 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2689 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2690 | /* Hash Compute, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2691 | status = psa_hash_compute(alg, input->x, input->len, |
| 2692 | output, output_size, &output_length); |
| 2693 | TEST_EQUAL(status, expected_status); |
| 2694 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2695 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2696 | /* Hash Compute, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2697 | status = psa_hash_setup(&operation, alg); |
| 2698 | if (status == PSA_SUCCESS) { |
| 2699 | status = psa_hash_update(&operation, input->x, input->len); |
| 2700 | if (status == PSA_SUCCESS) { |
| 2701 | status = psa_hash_finish(&operation, output, output_size, |
| 2702 | &output_length); |
| 2703 | if (status == PSA_SUCCESS) { |
| 2704 | TEST_LE_U(output_length, output_size); |
| 2705 | } else { |
| 2706 | TEST_EQUAL(status, expected_status); |
| 2707 | } |
| 2708 | } else { |
| 2709 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2710 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2711 | } else { |
| 2712 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2713 | } |
| 2714 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2715 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2716 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2717 | mbedtls_free(output); |
| 2718 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2719 | } |
| 2720 | /* END_CASE */ |
| 2721 | |
| 2722 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2723 | void hash_compare_fail(int alg_arg, data_t *input, |
| 2724 | data_t *reference_hash, |
| 2725 | int expected_status_arg) |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2726 | { |
| 2727 | psa_algorithm_t alg = alg_arg; |
| 2728 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2729 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2730 | psa_status_t status; |
| 2731 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2732 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2733 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2734 | /* Hash Compare, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2735 | status = psa_hash_compare(alg, input->x, input->len, |
| 2736 | reference_hash->x, reference_hash->len); |
| 2737 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2738 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2739 | /* Hash Compare, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2740 | status = psa_hash_setup(&operation, alg); |
| 2741 | if (status == PSA_SUCCESS) { |
| 2742 | status = psa_hash_update(&operation, input->x, input->len); |
| 2743 | if (status == PSA_SUCCESS) { |
| 2744 | status = psa_hash_verify(&operation, reference_hash->x, |
| 2745 | reference_hash->len); |
| 2746 | TEST_EQUAL(status, expected_status); |
| 2747 | } else { |
| 2748 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2749 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2750 | } else { |
| 2751 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2752 | } |
| 2753 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2754 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2755 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2756 | PSA_DONE(); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2757 | } |
| 2758 | /* END_CASE */ |
| 2759 | |
| 2760 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2761 | void hash_compute_compare(int alg_arg, data_t *input, |
| 2762 | data_t *expected_output) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2763 | { |
| 2764 | psa_algorithm_t alg = alg_arg; |
| 2765 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2766 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2767 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2768 | size_t i; |
| 2769 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2770 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2771 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2772 | /* Compute with tight buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2773 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 2774 | output, PSA_HASH_LENGTH(alg), |
| 2775 | &output_length)); |
| 2776 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2777 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2778 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2779 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2780 | /* Compute with tight buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2781 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2782 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2783 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 2784 | PSA_HASH_LENGTH(alg), |
| 2785 | &output_length)); |
| 2786 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2787 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2788 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2789 | |
| 2790 | /* Compute with larger buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2791 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 2792 | output, sizeof(output), |
| 2793 | &output_length)); |
| 2794 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2795 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2796 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2797 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2798 | /* Compute with larger buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2799 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2800 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2801 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 2802 | sizeof(output), &output_length)); |
| 2803 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2804 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2805 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2806 | |
| 2807 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2808 | PSA_ASSERT(psa_hash_compare(alg, input->x, input->len, |
| 2809 | output, output_length)); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2810 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2811 | /* Compare with correct hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2812 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2813 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2814 | PSA_ASSERT(psa_hash_verify(&operation, output, |
| 2815 | output_length)); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2816 | |
| 2817 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2818 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2819 | output, output_length + 1), |
| 2820 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2821 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2822 | /* Compare with trailing garbage, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2823 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2824 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2825 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1), |
| 2826 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2827 | |
| 2828 | /* Compare with truncated hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2829 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2830 | output, output_length - 1), |
| 2831 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2832 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2833 | /* Compare with truncated hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2834 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2835 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2836 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1), |
| 2837 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2838 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2839 | /* Compare with corrupted value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2840 | for (i = 0; i < output_length; i++) { |
| 2841 | mbedtls_test_set_step(i); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2842 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2843 | |
| 2844 | /* One-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2845 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2846 | output, output_length), |
| 2847 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2848 | |
| 2849 | /* Multi-Part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2850 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2851 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2852 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length), |
| 2853 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2854 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2855 | output[i] ^= 1; |
| 2856 | } |
| 2857 | |
| 2858 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2859 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2860 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2861 | } |
| 2862 | /* END_CASE */ |
| 2863 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2864 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2865 | void hash_bad_order() |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2866 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2867 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2868 | unsigned char input[] = ""; |
| 2869 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2870 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2871 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2872 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2873 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 |
| 2874 | }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2875 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2876 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2877 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2878 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2879 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2880 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2881 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2882 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2883 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2884 | TEST_EQUAL(psa_hash_setup(&operation, alg), |
| 2885 | PSA_ERROR_BAD_STATE); |
| 2886 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2887 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2888 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2889 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2890 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2891 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2892 | PSA_ERROR_BAD_STATE); |
| 2893 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2894 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2895 | /* Check that update calls abort on error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2896 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 2897 | operation.id = UINT_MAX; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2898 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2899 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2900 | PSA_ERROR_BAD_STATE); |
| 2901 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2902 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2903 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2904 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2905 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2906 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2907 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2908 | hash, sizeof(hash), &hash_len)); |
| 2909 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2910 | PSA_ERROR_BAD_STATE); |
| 2911 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2912 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2913 | /* Call verify without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2914 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2915 | valid_hash, sizeof(valid_hash)), |
| 2916 | PSA_ERROR_BAD_STATE); |
| 2917 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2918 | |
| 2919 | /* Call verify after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2920 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2921 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2922 | hash, sizeof(hash), &hash_len)); |
| 2923 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2924 | valid_hash, sizeof(valid_hash)), |
| 2925 | PSA_ERROR_BAD_STATE); |
| 2926 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2927 | |
| 2928 | /* Call verify twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2929 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2930 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2931 | PSA_ASSERT(psa_hash_verify(&operation, |
| 2932 | valid_hash, sizeof(valid_hash))); |
| 2933 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2934 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2935 | valid_hash, sizeof(valid_hash)), |
| 2936 | PSA_ERROR_BAD_STATE); |
| 2937 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2938 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2939 | |
| 2940 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2941 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2942 | hash, sizeof(hash), &hash_len), |
| 2943 | PSA_ERROR_BAD_STATE); |
| 2944 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2945 | |
| 2946 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2947 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2948 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2949 | hash, sizeof(hash), &hash_len)); |
| 2950 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2951 | hash, sizeof(hash), &hash_len), |
| 2952 | PSA_ERROR_BAD_STATE); |
| 2953 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2954 | |
| 2955 | /* Call finish after calling verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2956 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2957 | PSA_ASSERT(psa_hash_verify(&operation, |
| 2958 | valid_hash, sizeof(valid_hash))); |
| 2959 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2960 | hash, sizeof(hash), &hash_len), |
| 2961 | PSA_ERROR_BAD_STATE); |
| 2962 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2963 | |
| 2964 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2965 | PSA_DONE(); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2966 | } |
| 2967 | /* END_CASE */ |
| 2968 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2969 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2970 | void hash_verify_bad_args() |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2971 | { |
| 2972 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2973 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2974 | * appended to it */ |
| 2975 | unsigned char hash[] = { |
| 2976 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2977 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2978 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb |
| 2979 | }; |
| 2980 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2981 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2982 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2983 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2984 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2985 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2986 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2987 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2988 | TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1), |
| 2989 | PSA_ERROR_INVALID_SIGNATURE); |
| 2990 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2991 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2992 | ASSERT_OPERATION_IS_INACTIVE(operation); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2993 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2994 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2995 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2996 | TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size), |
| 2997 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2998 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2999 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3000 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3001 | TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)), |
| 3002 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 3003 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3004 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3005 | PSA_DONE(); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3006 | } |
| 3007 | /* END_CASE */ |
| 3008 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3009 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3010 | void hash_finish_bad_args() |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3011 | { |
| 3012 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 3013 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3014 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 3015 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3016 | size_t hash_len; |
| 3017 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3018 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3019 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3020 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3021 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3022 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3023 | hash, expected_size - 1, &hash_len), |
| 3024 | PSA_ERROR_BUFFER_TOO_SMALL); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3025 | |
| 3026 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3027 | PSA_DONE(); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3028 | } |
| 3029 | /* END_CASE */ |
| 3030 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3031 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3032 | void hash_clone_source_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3033 | { |
| 3034 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3035 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3036 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 3037 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3038 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3039 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3040 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3041 | size_t hash_len; |
| 3042 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3043 | PSA_ASSERT(psa_crypto_init()); |
| 3044 | PSA_ASSERT(psa_hash_setup(&op_source, alg)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3045 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3046 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3047 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3048 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3049 | hash, sizeof(hash), &hash_len)); |
| 3050 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3051 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3052 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3053 | TEST_EQUAL(psa_hash_clone(&op_source, &op_setup), |
| 3054 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3055 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3056 | PSA_ASSERT(psa_hash_clone(&op_source, &op_init)); |
| 3057 | PSA_ASSERT(psa_hash_finish(&op_init, |
| 3058 | hash, sizeof(hash), &hash_len)); |
| 3059 | PSA_ASSERT(psa_hash_clone(&op_source, &op_finished)); |
| 3060 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3061 | hash, sizeof(hash), &hash_len)); |
| 3062 | PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted)); |
| 3063 | PSA_ASSERT(psa_hash_finish(&op_aborted, |
| 3064 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3065 | |
| 3066 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3067 | psa_hash_abort(&op_source); |
| 3068 | psa_hash_abort(&op_init); |
| 3069 | psa_hash_abort(&op_setup); |
| 3070 | psa_hash_abort(&op_finished); |
| 3071 | psa_hash_abort(&op_aborted); |
| 3072 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3073 | } |
| 3074 | /* END_CASE */ |
| 3075 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3076 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3077 | void hash_clone_target_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3078 | { |
| 3079 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3080 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3081 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3082 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3083 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3084 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3085 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 3086 | size_t hash_len; |
| 3087 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3088 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3089 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3090 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3091 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3092 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3093 | hash, sizeof(hash), &hash_len)); |
| 3094 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3095 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3096 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3097 | PSA_ASSERT(psa_hash_clone(&op_setup, &op_target)); |
| 3098 | PSA_ASSERT(psa_hash_finish(&op_target, |
| 3099 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3100 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3101 | TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE); |
| 3102 | TEST_EQUAL(psa_hash_clone(&op_finished, &op_target), |
| 3103 | PSA_ERROR_BAD_STATE); |
| 3104 | TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target), |
| 3105 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3106 | |
| 3107 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3108 | psa_hash_abort(&op_target); |
| 3109 | psa_hash_abort(&op_init); |
| 3110 | psa_hash_abort(&op_setup); |
| 3111 | psa_hash_abort(&op_finished); |
| 3112 | psa_hash_abort(&op_aborted); |
| 3113 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3114 | } |
| 3115 | /* END_CASE */ |
| 3116 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3117 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3118 | void mac_operation_init() |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3119 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3120 | const uint8_t input[1] = { 0 }; |
| 3121 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3122 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3123 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3124 | * 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] | 3125 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3126 | psa_mac_operation_t func = psa_mac_operation_init(); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3127 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 3128 | psa_mac_operation_t zero; |
| 3129 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3130 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3131 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3132 | /* A freshly-initialized MAC operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3133 | TEST_EQUAL(psa_mac_update(&func, |
| 3134 | input, sizeof(input)), |
| 3135 | PSA_ERROR_BAD_STATE); |
| 3136 | TEST_EQUAL(psa_mac_update(&init, |
| 3137 | input, sizeof(input)), |
| 3138 | PSA_ERROR_BAD_STATE); |
| 3139 | TEST_EQUAL(psa_mac_update(&zero, |
| 3140 | input, sizeof(input)), |
| 3141 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3142 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3143 | /* A default MAC operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3144 | PSA_ASSERT(psa_mac_abort(&func)); |
| 3145 | PSA_ASSERT(psa_mac_abort(&init)); |
| 3146 | PSA_ASSERT(psa_mac_abort(&zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3147 | } |
| 3148 | /* END_CASE */ |
| 3149 | |
| 3150 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3151 | void mac_setup(int key_type_arg, |
| 3152 | data_t *key, |
| 3153 | int alg_arg, |
| 3154 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3155 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3156 | psa_key_type_t key_type = key_type_arg; |
| 3157 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3158 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3159 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3160 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3161 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3162 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3163 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3164 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3165 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3166 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3167 | if (!exercise_mac_setup(key_type, key->x, key->len, alg, |
| 3168 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3169 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3170 | } |
| 3171 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3172 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3173 | /* The operation object should be reusable. */ |
| 3174 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3175 | if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 3176 | smoke_test_key_data, |
| 3177 | sizeof(smoke_test_key_data), |
| 3178 | KNOWN_SUPPORTED_MAC_ALG, |
| 3179 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3180 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3181 | } |
| 3182 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3183 | #endif |
| 3184 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3185 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3186 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3187 | } |
| 3188 | /* END_CASE */ |
| 3189 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3190 | /* 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] | 3191 | void mac_bad_order() |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3192 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3193 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3194 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 3195 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3196 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3197 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3198 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3199 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa |
| 3200 | }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3201 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3202 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3203 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 3204 | size_t sign_mac_length = 0; |
| 3205 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3206 | const uint8_t verify_mac[] = { |
| 3207 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 3208 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3209 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 |
| 3210 | }; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3211 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3212 | PSA_ASSERT(psa_crypto_init()); |
| 3213 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 3214 | psa_set_key_algorithm(&attributes, alg); |
| 3215 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3216 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3217 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3218 | &key)); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3219 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3220 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3221 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3222 | PSA_ERROR_BAD_STATE); |
| 3223 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3224 | |
| 3225 | /* Call sign finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3226 | TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac), |
| 3227 | &sign_mac_length), |
| 3228 | PSA_ERROR_BAD_STATE); |
| 3229 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3230 | |
| 3231 | /* Call verify finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3232 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3233 | verify_mac, sizeof(verify_mac)), |
| 3234 | PSA_ERROR_BAD_STATE); |
| 3235 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3236 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3237 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3238 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3239 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3240 | TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg), |
| 3241 | PSA_ERROR_BAD_STATE); |
| 3242 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3243 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3244 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3245 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3246 | /* Call update after sign finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3247 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3248 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3249 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3250 | sign_mac, sizeof(sign_mac), |
| 3251 | &sign_mac_length)); |
| 3252 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3253 | PSA_ERROR_BAD_STATE); |
| 3254 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3255 | |
| 3256 | /* Call update after verify finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3257 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3258 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3259 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3260 | verify_mac, sizeof(verify_mac))); |
| 3261 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3262 | PSA_ERROR_BAD_STATE); |
| 3263 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3264 | |
| 3265 | /* Call sign finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3266 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3267 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3268 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3269 | sign_mac, sizeof(sign_mac), |
| 3270 | &sign_mac_length)); |
| 3271 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3272 | sign_mac, sizeof(sign_mac), |
| 3273 | &sign_mac_length), |
| 3274 | PSA_ERROR_BAD_STATE); |
| 3275 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3276 | |
| 3277 | /* Call verify finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3278 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3279 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3280 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3281 | verify_mac, sizeof(verify_mac))); |
| 3282 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3283 | verify_mac, sizeof(verify_mac)), |
| 3284 | PSA_ERROR_BAD_STATE); |
| 3285 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3286 | |
| 3287 | /* Setup sign but try verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3288 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3289 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3290 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3291 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3292 | verify_mac, sizeof(verify_mac)), |
| 3293 | PSA_ERROR_BAD_STATE); |
| 3294 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3295 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3296 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3297 | |
| 3298 | /* Setup verify but try sign. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3299 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3300 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3301 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3302 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3303 | sign_mac, sizeof(sign_mac), |
| 3304 | &sign_mac_length), |
| 3305 | PSA_ERROR_BAD_STATE); |
| 3306 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3307 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3308 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3309 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3310 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3311 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3312 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3313 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3314 | } |
| 3315 | /* END_CASE */ |
| 3316 | |
| 3317 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3318 | void mac_sign_verify_multi(int key_type_arg, |
| 3319 | data_t *key_data, |
| 3320 | int alg_arg, |
| 3321 | data_t *input, |
| 3322 | int is_verify, |
| 3323 | data_t *expected_mac) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3324 | { |
| 3325 | size_t data_part_len = 0; |
| 3326 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3327 | 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] | 3328 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3329 | mbedtls_test_set_step(2000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3330 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3331 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3332 | alg_arg, |
| 3333 | input, data_part_len, |
| 3334 | expected_mac, |
| 3335 | is_verify, 0) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3336 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3337 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3338 | |
| 3339 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3340 | mbedtls_test_set_step(3000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3341 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3342 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3343 | alg_arg, |
| 3344 | input, data_part_len, |
| 3345 | expected_mac, |
| 3346 | is_verify, 1) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3347 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3348 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3349 | } |
| 3350 | |
| 3351 | /* Goto is required to silence warnings about unused labels, as we |
| 3352 | * don't actually do any test assertions in this function. */ |
| 3353 | goto exit; |
| 3354 | } |
| 3355 | /* END_CASE */ |
| 3356 | |
| 3357 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3358 | void mac_sign(int key_type_arg, |
| 3359 | data_t *key_data, |
| 3360 | int alg_arg, |
| 3361 | data_t *input, |
| 3362 | data_t *expected_mac) |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3363 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3364 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3365 | psa_key_type_t key_type = key_type_arg; |
| 3366 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3367 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3368 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3369 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3370 | size_t mac_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3371 | 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] | 3372 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3373 | const size_t output_sizes_to_test[] = { |
| 3374 | 0, |
| 3375 | 1, |
| 3376 | expected_mac->len - 1, |
| 3377 | expected_mac->len, |
| 3378 | expected_mac->len + 1, |
| 3379 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3380 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3381 | TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3382 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3383 | TEST_ASSERT(expected_mac->len == mac_buffer_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3385 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3386 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3387 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 3388 | psa_set_key_algorithm(&attributes, alg); |
| 3389 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3390 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3391 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3392 | &key)); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3393 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3394 | 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] | 3395 | const size_t output_size = output_sizes_to_test[i]; |
| 3396 | psa_status_t expected_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3397 | (output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3398 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3399 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3400 | mbedtls_test_set_step(output_size); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3401 | TEST_CALLOC(actual_mac, output_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3402 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3403 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3404 | TEST_EQUAL(psa_mac_compute(key, alg, |
| 3405 | input->x, input->len, |
| 3406 | actual_mac, output_size, &mac_length), |
| 3407 | expected_status); |
| 3408 | if (expected_status == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3409 | TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3410 | actual_mac, mac_length); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3411 | } |
| 3412 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3413 | if (output_size > 0) { |
| 3414 | memset(actual_mac, 0, output_size); |
| 3415 | } |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3416 | |
| 3417 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3418 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3419 | PSA_ASSERT(psa_mac_update(&operation, |
| 3420 | input->x, input->len)); |
| 3421 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3422 | actual_mac, output_size, |
| 3423 | &mac_length), |
| 3424 | expected_status); |
| 3425 | PSA_ASSERT(psa_mac_abort(&operation)); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3426 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3427 | if (expected_status == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3428 | TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3429 | actual_mac, mac_length); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3430 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3431 | mbedtls_free(actual_mac); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3432 | actual_mac = NULL; |
| 3433 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3434 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3435 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3436 | psa_mac_abort(&operation); |
| 3437 | psa_destroy_key(key); |
| 3438 | PSA_DONE(); |
| 3439 | mbedtls_free(actual_mac); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3440 | } |
| 3441 | /* END_CASE */ |
| 3442 | |
| 3443 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3444 | void mac_verify(int key_type_arg, |
| 3445 | data_t *key_data, |
| 3446 | int alg_arg, |
| 3447 | data_t *input, |
| 3448 | data_t *expected_mac) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3449 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3450 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3451 | psa_key_type_t key_type = key_type_arg; |
| 3452 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3453 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3454 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3455 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3456 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3457 | TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3458 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3459 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3460 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3461 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 3462 | psa_set_key_algorithm(&attributes, alg); |
| 3463 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3464 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3465 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3466 | &key)); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3467 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3468 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3469 | PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len, |
| 3470 | expected_mac->x, expected_mac->len)); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3471 | |
| 3472 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3473 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3474 | PSA_ASSERT(psa_mac_update(&operation, |
| 3475 | input->x, input->len)); |
| 3476 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3477 | expected_mac->x, |
| 3478 | expected_mac->len)); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3479 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3480 | /* Test a MAC that's too short, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3481 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3482 | input->x, input->len, |
| 3483 | expected_mac->x, |
| 3484 | expected_mac->len - 1), |
| 3485 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3486 | |
| 3487 | /* Test a MAC that's too short, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3488 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3489 | PSA_ASSERT(psa_mac_update(&operation, |
| 3490 | input->x, input->len)); |
| 3491 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3492 | expected_mac->x, |
| 3493 | expected_mac->len - 1), |
| 3494 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3495 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3496 | /* Test a MAC that's too long, one-shot case. */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3497 | TEST_CALLOC(perturbed_mac, expected_mac->len + 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3498 | memcpy(perturbed_mac, expected_mac->x, expected_mac->len); |
| 3499 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3500 | input->x, input->len, |
| 3501 | perturbed_mac, expected_mac->len + 1), |
| 3502 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3503 | |
| 3504 | /* Test a MAC that's too long, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3505 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3506 | PSA_ASSERT(psa_mac_update(&operation, |
| 3507 | input->x, input->len)); |
| 3508 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3509 | perturbed_mac, |
| 3510 | expected_mac->len + 1), |
| 3511 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3512 | |
| 3513 | /* Test changing one byte. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3514 | for (size_t i = 0; i < expected_mac->len; i++) { |
| 3515 | mbedtls_test_set_step(i); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3516 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3517 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3518 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3519 | input->x, input->len, |
| 3520 | perturbed_mac, expected_mac->len), |
| 3521 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3522 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3523 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3524 | PSA_ASSERT(psa_mac_update(&operation, |
| 3525 | input->x, input->len)); |
| 3526 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3527 | perturbed_mac, |
| 3528 | expected_mac->len), |
| 3529 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3530 | perturbed_mac[i] ^= 1; |
| 3531 | } |
| 3532 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3533 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3534 | psa_mac_abort(&operation); |
| 3535 | psa_destroy_key(key); |
| 3536 | PSA_DONE(); |
| 3537 | mbedtls_free(perturbed_mac); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3538 | } |
| 3539 | /* END_CASE */ |
| 3540 | |
| 3541 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3542 | void cipher_operation_init() |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3543 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3544 | const uint8_t input[1] = { 0 }; |
| 3545 | unsigned char output[1] = { 0 }; |
| 3546 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3547 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3548 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3549 | * 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] | 3550 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3551 | psa_cipher_operation_t func = psa_cipher_operation_init(); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3552 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3553 | psa_cipher_operation_t zero; |
| 3554 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3555 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3556 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3557 | /* A freshly-initialized cipher operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3558 | TEST_EQUAL(psa_cipher_update(&func, |
| 3559 | input, sizeof(input), |
| 3560 | output, sizeof(output), |
| 3561 | &output_length), |
| 3562 | PSA_ERROR_BAD_STATE); |
| 3563 | TEST_EQUAL(psa_cipher_update(&init, |
| 3564 | input, sizeof(input), |
| 3565 | output, sizeof(output), |
| 3566 | &output_length), |
| 3567 | PSA_ERROR_BAD_STATE); |
| 3568 | TEST_EQUAL(psa_cipher_update(&zero, |
| 3569 | input, sizeof(input), |
| 3570 | output, sizeof(output), |
| 3571 | &output_length), |
| 3572 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3573 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3574 | /* A default cipher operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3575 | PSA_ASSERT(psa_cipher_abort(&func)); |
| 3576 | PSA_ASSERT(psa_cipher_abort(&init)); |
| 3577 | PSA_ASSERT(psa_cipher_abort(&zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3578 | } |
| 3579 | /* END_CASE */ |
| 3580 | |
| 3581 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3582 | void cipher_setup(int key_type_arg, |
| 3583 | data_t *key, |
| 3584 | int alg_arg, |
| 3585 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3586 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3587 | psa_key_type_t key_type = key_type_arg; |
| 3588 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3589 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3590 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3591 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3592 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3593 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3594 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3595 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3596 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3597 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3598 | if (!exercise_cipher_setup(key_type, key->x, key->len, alg, |
| 3599 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3600 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3601 | } |
| 3602 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3603 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3604 | /* The operation object should be reusable. */ |
| 3605 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3606 | if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3607 | smoke_test_key_data, |
| 3608 | sizeof(smoke_test_key_data), |
| 3609 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3610 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3611 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3612 | } |
| 3613 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3614 | #endif |
| 3615 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3616 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3617 | psa_cipher_abort(&operation); |
| 3618 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3619 | } |
| 3620 | /* END_CASE */ |
| 3621 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3622 | /* 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] | 3623 | void cipher_bad_order() |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3624 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3625 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3626 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3627 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3628 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3629 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3630 | 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] | 3631 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3632 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3633 | 0xaa, 0xaa, 0xaa, 0xaa |
| 3634 | }; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3635 | const uint8_t text[] = { |
| 3636 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3637 | 0xbb, 0xbb, 0xbb, 0xbb |
| 3638 | }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3639 | 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] | 3640 | size_t length = 0; |
| 3641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3642 | PSA_ASSERT(psa_crypto_init()); |
| 3643 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3644 | psa_set_key_algorithm(&attributes, alg); |
| 3645 | psa_set_key_type(&attributes, key_type); |
| 3646 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3647 | &key)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3648 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3649 | /* Call encrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3650 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3651 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3652 | TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg), |
| 3653 | PSA_ERROR_BAD_STATE); |
| 3654 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3655 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3656 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3657 | |
| 3658 | /* Call decrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3659 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3660 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3661 | TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg), |
| 3662 | PSA_ERROR_BAD_STATE); |
| 3663 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3664 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3665 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3666 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3667 | /* Generate an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3668 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3669 | buffer, sizeof(buffer), |
| 3670 | &length), |
| 3671 | PSA_ERROR_BAD_STATE); |
| 3672 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3673 | |
| 3674 | /* Generate an IV twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3675 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3676 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3677 | buffer, sizeof(buffer), |
| 3678 | &length)); |
| 3679 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3680 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3681 | buffer, sizeof(buffer), |
| 3682 | &length), |
| 3683 | PSA_ERROR_BAD_STATE); |
| 3684 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3685 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3686 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3687 | |
| 3688 | /* Generate an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3689 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3690 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3691 | iv, sizeof(iv))); |
| 3692 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3693 | buffer, sizeof(buffer), |
| 3694 | &length), |
| 3695 | PSA_ERROR_BAD_STATE); |
| 3696 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3697 | |
| 3698 | /* Set an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3699 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3700 | iv, sizeof(iv)), |
| 3701 | PSA_ERROR_BAD_STATE); |
| 3702 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3703 | |
| 3704 | /* Set an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3705 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3706 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3707 | iv, sizeof(iv))); |
| 3708 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3709 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3710 | iv, sizeof(iv)), |
| 3711 | PSA_ERROR_BAD_STATE); |
| 3712 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3713 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3714 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3715 | |
| 3716 | /* Set an IV after it's already generated. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3717 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3718 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3719 | buffer, sizeof(buffer), |
| 3720 | &length)); |
| 3721 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3722 | iv, sizeof(iv)), |
| 3723 | PSA_ERROR_BAD_STATE); |
| 3724 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3725 | |
| 3726 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3727 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3728 | text, sizeof(text), |
| 3729 | buffer, sizeof(buffer), |
| 3730 | &length), |
| 3731 | PSA_ERROR_BAD_STATE); |
| 3732 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3733 | |
| 3734 | /* Call update without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3735 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3736 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3737 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3738 | text, sizeof(text), |
| 3739 | buffer, sizeof(buffer), |
| 3740 | &length), |
| 3741 | PSA_ERROR_BAD_STATE); |
| 3742 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3743 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3744 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3745 | |
| 3746 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3747 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3748 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3749 | iv, sizeof(iv))); |
| 3750 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3751 | buffer, sizeof(buffer), &length)); |
| 3752 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3753 | text, sizeof(text), |
| 3754 | buffer, sizeof(buffer), |
| 3755 | &length), |
| 3756 | PSA_ERROR_BAD_STATE); |
| 3757 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3758 | |
| 3759 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3760 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3761 | buffer, sizeof(buffer), &length), |
| 3762 | PSA_ERROR_BAD_STATE); |
| 3763 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3764 | |
| 3765 | /* Call finish without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3766 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3767 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3768 | * for cipher modes with padding. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3769 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3770 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3771 | buffer, sizeof(buffer), &length), |
| 3772 | PSA_ERROR_BAD_STATE); |
| 3773 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3774 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3775 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3776 | |
| 3777 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3778 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3779 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3780 | iv, sizeof(iv))); |
| 3781 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3782 | buffer, sizeof(buffer), &length)); |
| 3783 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3784 | buffer, sizeof(buffer), &length), |
| 3785 | PSA_ERROR_BAD_STATE); |
| 3786 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3787 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3788 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3789 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3790 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3791 | psa_cipher_abort(&operation); |
| 3792 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3793 | } |
| 3794 | /* END_CASE */ |
| 3795 | |
| 3796 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3797 | void cipher_encrypt_fail(int alg_arg, |
| 3798 | int key_type_arg, |
| 3799 | data_t *key_data, |
| 3800 | data_t *input, |
| 3801 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3802 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3803 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3804 | psa_status_t status; |
| 3805 | psa_key_type_t key_type = key_type_arg; |
| 3806 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3807 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3808 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 }; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3809 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; |
| 3810 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3811 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3812 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3813 | size_t output_length = 0; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3814 | size_t function_output_length; |
| 3815 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3816 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3817 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3818 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 3819 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3820 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3821 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3822 | psa_set_key_algorithm(&attributes, alg); |
| 3823 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3824 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3825 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 3826 | input->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3827 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3828 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3829 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3830 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3831 | } |
| 3832 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3833 | /* Encrypt, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3834 | status = psa_cipher_encrypt(key, alg, input->x, input->len, output, |
| 3835 | output_buffer_size, &output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3836 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3837 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3838 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3839 | /* Encrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3840 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
| 3841 | if (status == PSA_SUCCESS) { |
| 3842 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 3843 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3844 | iv, iv_size, |
| 3845 | &iv_length)); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3846 | } |
| 3847 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3848 | status = psa_cipher_update(&operation, input->x, input->len, |
| 3849 | output, output_buffer_size, |
| 3850 | &function_output_length); |
| 3851 | if (status == PSA_SUCCESS) { |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3852 | output_length += function_output_length; |
| 3853 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3854 | status = psa_cipher_finish(&operation, output + output_length, |
| 3855 | output_buffer_size - output_length, |
| 3856 | &function_output_length); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3857 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3858 | TEST_EQUAL(status, expected_status); |
| 3859 | } else { |
| 3860 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3861 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3862 | } else { |
| 3863 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3864 | } |
| 3865 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3866 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3867 | psa_cipher_abort(&operation); |
| 3868 | mbedtls_free(output); |
| 3869 | psa_destroy_key(key); |
| 3870 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3871 | } |
| 3872 | /* END_CASE */ |
| 3873 | |
| 3874 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3875 | void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data, |
| 3876 | data_t *input, int iv_length, |
| 3877 | int expected_result) |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3878 | { |
| 3879 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3880 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3881 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3882 | size_t output_buffer_size = 0; |
| 3883 | unsigned char *output = NULL; |
| 3884 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3885 | 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] | 3886 | TEST_CALLOC(output, output_buffer_size); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3887 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3888 | PSA_ASSERT(psa_crypto_init()); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3889 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3890 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3891 | psa_set_key_algorithm(&attributes, alg); |
| 3892 | psa_set_key_type(&attributes, key_type); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3893 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3894 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3895 | &key)); |
| 3896 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3897 | TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output, |
| 3898 | iv_length)); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3899 | |
| 3900 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3901 | psa_cipher_abort(&operation); |
| 3902 | mbedtls_free(output); |
| 3903 | psa_destroy_key(key); |
| 3904 | PSA_DONE(); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3905 | } |
| 3906 | /* END_CASE */ |
| 3907 | |
| 3908 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3909 | void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data, |
| 3910 | data_t *plaintext, data_t *ciphertext) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3911 | { |
| 3912 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3913 | psa_key_type_t key_type = key_type_arg; |
| 3914 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3915 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3916 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3917 | unsigned char *output = NULL; |
| 3918 | size_t output_buffer_size = 0; |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3919 | size_t output_length, length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3920 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3921 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3922 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3923 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3924 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3925 | TEST_LE_U(ciphertext->len, |
| 3926 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len)); |
| 3927 | TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len), |
| 3928 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len)); |
| 3929 | TEST_LE_U(plaintext->len, |
| 3930 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len)); |
| 3931 | TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len), |
| 3932 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len)); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3933 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3934 | |
| 3935 | /* Set up key and output buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3936 | psa_set_key_usage_flags(&attributes, |
| 3937 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3938 | psa_set_key_algorithm(&attributes, alg); |
| 3939 | psa_set_key_type(&attributes, key_type); |
| 3940 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3941 | &key)); |
| 3942 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 3943 | plaintext->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3944 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3945 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3946 | /* set_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3947 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3948 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 3949 | PSA_ERROR_BAD_STATE); |
| 3950 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3951 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 3952 | PSA_ERROR_BAD_STATE); |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3953 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3954 | /* generate_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3955 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3956 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 3957 | &length), |
| 3958 | PSA_ERROR_BAD_STATE); |
| 3959 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3960 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 3961 | &length), |
| 3962 | PSA_ERROR_BAD_STATE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3963 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3964 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3965 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3966 | output_length = 0; |
| 3967 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3968 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3969 | plaintext->x, plaintext->len, |
| 3970 | output, output_buffer_size, |
| 3971 | &length)); |
| 3972 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3973 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3974 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3975 | mbedtls_buffer_offset(output, output_length), |
| 3976 | output_buffer_size - output_length, |
| 3977 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3978 | output_length += length; |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3979 | TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3980 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3981 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3982 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3983 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3984 | output_length = 0; |
| 3985 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3986 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3987 | ciphertext->x, ciphertext->len, |
| 3988 | output, output_buffer_size, |
| 3989 | &length)); |
| 3990 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3991 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3992 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3993 | mbedtls_buffer_offset(output, output_length), |
| 3994 | output_buffer_size - output_length, |
| 3995 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3996 | output_length += length; |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3997 | TEST_MEMORY_COMPARE(plaintext->x, plaintext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3998 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3999 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4000 | /* One-shot encryption */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4001 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4002 | PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len, |
| 4003 | output, output_buffer_size, |
| 4004 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4005 | TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4006 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4007 | |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4008 | /* One-shot decryption */ |
| 4009 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4010 | PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len, |
| 4011 | output, output_buffer_size, |
| 4012 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4013 | TEST_MEMORY_COMPARE(plaintext->x, plaintext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4014 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4015 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4016 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4017 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4018 | mbedtls_free(output); |
| 4019 | psa_cipher_abort(&operation); |
| 4020 | psa_destroy_key(key); |
| 4021 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4022 | } |
| 4023 | /* END_CASE */ |
| 4024 | |
| 4025 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4026 | 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] | 4027 | { |
| 4028 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4029 | psa_algorithm_t alg = alg_arg; |
| 4030 | psa_key_type_t key_type = key_type_arg; |
| 4031 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4032 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4033 | psa_status_t status; |
| 4034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4035 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4036 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4037 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4038 | psa_set_key_algorithm(&attributes, alg); |
| 4039 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4040 | |
| 4041 | /* Usage of either of these two size macros would cause divide by zero |
| 4042 | * with incorrect key types previously. Input length should be irrelevant |
| 4043 | * here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4044 | TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16), |
| 4045 | 0); |
| 4046 | TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4047 | |
| 4048 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4049 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4050 | &key)); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4051 | |
| 4052 | /* Should fail due to invalid alg type (to support invalid key type). |
| 4053 | * Encrypt or decrypt will end up in the same place. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4054 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4055 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4056 | TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4057 | |
| 4058 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4059 | psa_cipher_abort(&operation); |
| 4060 | psa_destroy_key(key); |
| 4061 | PSA_DONE(); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4062 | } |
| 4063 | /* END_CASE */ |
| 4064 | |
| 4065 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4066 | void cipher_encrypt_validation(int alg_arg, |
| 4067 | int key_type_arg, |
| 4068 | data_t *key_data, |
| 4069 | data_t *input) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4070 | { |
| 4071 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4072 | psa_key_type_t key_type = key_type_arg; |
| 4073 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4074 | size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4075 | unsigned char *output1 = NULL; |
| 4076 | size_t output1_buffer_size = 0; |
| 4077 | size_t output1_length = 0; |
| 4078 | unsigned char *output2 = NULL; |
| 4079 | size_t output2_buffer_size = 0; |
| 4080 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4081 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4082 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4083 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4084 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4085 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4086 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4087 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4088 | psa_set_key_algorithm(&attributes, alg); |
| 4089 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4090 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4091 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4092 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4093 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4094 | TEST_CALLOC(output1, output1_buffer_size); |
| 4095 | TEST_CALLOC(output2, output2_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4096 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4097 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4098 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4099 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 4100 | /* The one-shot cipher encryption uses generated iv so validating |
| 4101 | the output is not possible. Validating with multipart encryption. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4102 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1, |
| 4103 | output1_buffer_size, &output1_length)); |
| 4104 | TEST_LE_U(output1_length, |
| 4105 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4106 | TEST_LE_U(output1_length, |
| 4107 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4108 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4109 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4110 | PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4111 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4112 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4113 | input->x, input->len, |
| 4114 | output2, output2_buffer_size, |
| 4115 | &function_output_length)); |
| 4116 | TEST_LE_U(function_output_length, |
| 4117 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4118 | TEST_LE_U(function_output_length, |
| 4119 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4120 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4121 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4122 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4123 | output2 + output2_length, |
| 4124 | output2_buffer_size - output2_length, |
| 4125 | &function_output_length)); |
| 4126 | TEST_LE_U(function_output_length, |
| 4127 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4128 | TEST_LE_U(function_output_length, |
| 4129 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4130 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4131 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4132 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4133 | TEST_MEMORY_COMPARE(output1 + iv_size, output1_length - iv_size, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4134 | output2, output2_length); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4135 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4136 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4137 | psa_cipher_abort(&operation); |
| 4138 | mbedtls_free(output1); |
| 4139 | mbedtls_free(output2); |
| 4140 | psa_destroy_key(key); |
| 4141 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4142 | } |
| 4143 | /* END_CASE */ |
| 4144 | |
| 4145 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4146 | void cipher_encrypt_multipart(int alg_arg, int key_type_arg, |
| 4147 | data_t *key_data, data_t *iv, |
| 4148 | data_t *input, |
| 4149 | int first_part_size_arg, |
| 4150 | int output1_length_arg, int output2_length_arg, |
| 4151 | data_t *expected_output, |
| 4152 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4153 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4154 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4155 | psa_key_type_t key_type = key_type_arg; |
| 4156 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4157 | psa_status_t status; |
| 4158 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4159 | size_t first_part_size = first_part_size_arg; |
| 4160 | size_t output1_length = output1_length_arg; |
| 4161 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4162 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4163 | size_t output_buffer_size = 0; |
| 4164 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4165 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4166 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4167 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4168 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4169 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4170 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4171 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4172 | psa_set_key_algorithm(&attributes, alg); |
| 4173 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4174 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4175 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4176 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4177 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4178 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4179 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4180 | if (iv->len > 0) { |
| 4181 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4182 | } |
| 4183 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4184 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4185 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4186 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4187 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4188 | TEST_LE_U(first_part_size, input->len); |
| 4189 | PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size, |
| 4190 | output, output_buffer_size, |
| 4191 | &function_output_length)); |
| 4192 | TEST_ASSERT(function_output_length == output1_length); |
| 4193 | TEST_LE_U(function_output_length, |
| 4194 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4195 | TEST_LE_U(function_output_length, |
| 4196 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4197 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4198 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4199 | if (first_part_size < input->len) { |
| 4200 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4201 | input->x + first_part_size, |
| 4202 | input->len - first_part_size, |
| 4203 | (output_buffer_size == 0 ? NULL : |
| 4204 | output + total_output_length), |
| 4205 | output_buffer_size - total_output_length, |
| 4206 | &function_output_length)); |
| 4207 | TEST_ASSERT(function_output_length == output2_length); |
| 4208 | TEST_LE_U(function_output_length, |
| 4209 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4210 | alg, |
| 4211 | input->len - first_part_size)); |
| 4212 | TEST_LE_U(function_output_length, |
| 4213 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4214 | total_output_length += function_output_length; |
| 4215 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4216 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4217 | status = psa_cipher_finish(&operation, |
| 4218 | (output_buffer_size == 0 ? NULL : |
| 4219 | output + total_output_length), |
| 4220 | output_buffer_size - total_output_length, |
| 4221 | &function_output_length); |
| 4222 | TEST_LE_U(function_output_length, |
| 4223 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4224 | TEST_LE_U(function_output_length, |
| 4225 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4226 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4227 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4228 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4229 | if (expected_status == PSA_SUCCESS) { |
| 4230 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4231 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4232 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4233 | output, total_output_length); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4234 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4235 | |
| 4236 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4237 | psa_cipher_abort(&operation); |
| 4238 | mbedtls_free(output); |
| 4239 | psa_destroy_key(key); |
| 4240 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4241 | } |
| 4242 | /* END_CASE */ |
| 4243 | |
| 4244 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4245 | void cipher_decrypt_multipart(int alg_arg, int key_type_arg, |
| 4246 | data_t *key_data, data_t *iv, |
| 4247 | data_t *input, |
| 4248 | int first_part_size_arg, |
| 4249 | int output1_length_arg, int output2_length_arg, |
| 4250 | data_t *expected_output, |
| 4251 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4252 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4253 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4254 | psa_key_type_t key_type = key_type_arg; |
| 4255 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4256 | psa_status_t status; |
| 4257 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4258 | size_t first_part_size = first_part_size_arg; |
| 4259 | size_t output1_length = output1_length_arg; |
| 4260 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4261 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4262 | size_t output_buffer_size = 0; |
| 4263 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4264 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4265 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4266 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4267 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4268 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4269 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4270 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4271 | psa_set_key_algorithm(&attributes, alg); |
| 4272 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4273 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4274 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4275 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4276 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4277 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4278 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4279 | if (iv->len > 0) { |
| 4280 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4281 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4282 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4283 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4284 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4285 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4286 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4287 | TEST_LE_U(first_part_size, input->len); |
| 4288 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4289 | input->x, first_part_size, |
| 4290 | output, output_buffer_size, |
| 4291 | &function_output_length)); |
| 4292 | TEST_ASSERT(function_output_length == output1_length); |
| 4293 | TEST_LE_U(function_output_length, |
| 4294 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4295 | TEST_LE_U(function_output_length, |
| 4296 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4297 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4298 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4299 | if (first_part_size < input->len) { |
| 4300 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4301 | input->x + first_part_size, |
| 4302 | input->len - first_part_size, |
| 4303 | (output_buffer_size == 0 ? NULL : |
| 4304 | output + total_output_length), |
| 4305 | output_buffer_size - total_output_length, |
| 4306 | &function_output_length)); |
| 4307 | TEST_ASSERT(function_output_length == output2_length); |
| 4308 | TEST_LE_U(function_output_length, |
| 4309 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4310 | alg, |
| 4311 | input->len - first_part_size)); |
| 4312 | TEST_LE_U(function_output_length, |
| 4313 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4314 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4315 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4316 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4317 | status = psa_cipher_finish(&operation, |
| 4318 | (output_buffer_size == 0 ? NULL : |
| 4319 | output + total_output_length), |
| 4320 | output_buffer_size - total_output_length, |
| 4321 | &function_output_length); |
| 4322 | TEST_LE_U(function_output_length, |
| 4323 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4324 | TEST_LE_U(function_output_length, |
| 4325 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4326 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4327 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4328 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4329 | if (expected_status == PSA_SUCCESS) { |
| 4330 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4331 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4332 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4333 | output, total_output_length); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4334 | } |
| 4335 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4336 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4337 | psa_cipher_abort(&operation); |
| 4338 | mbedtls_free(output); |
| 4339 | psa_destroy_key(key); |
| 4340 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4341 | } |
| 4342 | /* END_CASE */ |
| 4343 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4344 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4345 | void cipher_decrypt_fail(int alg_arg, |
| 4346 | int key_type_arg, |
| 4347 | data_t *key_data, |
| 4348 | data_t *iv, |
| 4349 | data_t *input_arg, |
| 4350 | int expected_status_arg) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4351 | { |
| 4352 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4353 | psa_status_t status; |
| 4354 | psa_key_type_t key_type = key_type_arg; |
| 4355 | psa_algorithm_t alg = alg_arg; |
| 4356 | psa_status_t expected_status = expected_status_arg; |
| 4357 | unsigned char *input = NULL; |
| 4358 | size_t input_buffer_size = 0; |
| 4359 | unsigned char *output = NULL; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4360 | unsigned char *output_multi = NULL; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4361 | size_t output_buffer_size = 0; |
| 4362 | size_t output_length = 0; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4363 | size_t function_output_length; |
| 4364 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4365 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4367 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 4368 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4370 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4371 | psa_set_key_algorithm(&attributes, alg); |
| 4372 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4374 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4375 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4376 | } |
| 4377 | |
| 4378 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4379 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4380 | if (input_buffer_size > 0) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4381 | TEST_CALLOC(input, input_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4382 | memcpy(input, iv->x, iv->len); |
| 4383 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4384 | } |
| 4385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4386 | 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] | 4387 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4388 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4389 | /* Decrypt, one-short */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4390 | status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4391 | output_buffer_size, &output_length); |
| 4392 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4393 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4394 | /* Decrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4395 | status = psa_cipher_decrypt_setup(&operation, key, alg); |
| 4396 | if (status == PSA_SUCCESS) { |
| 4397 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 4398 | input_arg->len) + |
| 4399 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4400 | TEST_CALLOC(output_multi, output_buffer_size); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4401 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4402 | if (iv->len > 0) { |
| 4403 | status = psa_cipher_set_iv(&operation, iv->x, iv->len); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4404 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4405 | if (status != PSA_SUCCESS) { |
| 4406 | TEST_EQUAL(status, expected_status); |
| 4407 | } |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4408 | } |
| 4409 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4410 | if (status == PSA_SUCCESS) { |
| 4411 | status = psa_cipher_update(&operation, |
| 4412 | input_arg->x, input_arg->len, |
| 4413 | output_multi, output_buffer_size, |
| 4414 | &function_output_length); |
| 4415 | if (status == PSA_SUCCESS) { |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4416 | output_length = function_output_length; |
| 4417 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4418 | status = psa_cipher_finish(&operation, |
| 4419 | output_multi + output_length, |
| 4420 | output_buffer_size - output_length, |
| 4421 | &function_output_length); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4422 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4423 | TEST_EQUAL(status, expected_status); |
| 4424 | } else { |
| 4425 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4426 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4427 | } else { |
| 4428 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4429 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4430 | } else { |
| 4431 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4432 | } |
| 4433 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4434 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4435 | psa_cipher_abort(&operation); |
| 4436 | mbedtls_free(input); |
| 4437 | mbedtls_free(output); |
| 4438 | mbedtls_free(output_multi); |
| 4439 | psa_destroy_key(key); |
| 4440 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4441 | } |
| 4442 | /* END_CASE */ |
| 4443 | |
| 4444 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4445 | void cipher_decrypt(int alg_arg, |
| 4446 | int key_type_arg, |
| 4447 | data_t *key_data, |
| 4448 | data_t *iv, |
| 4449 | data_t *input_arg, |
| 4450 | data_t *expected_output) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4451 | { |
| 4452 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4453 | psa_key_type_t key_type = key_type_arg; |
| 4454 | psa_algorithm_t alg = alg_arg; |
| 4455 | unsigned char *input = NULL; |
| 4456 | size_t input_buffer_size = 0; |
| 4457 | unsigned char *output = NULL; |
| 4458 | size_t output_buffer_size = 0; |
| 4459 | size_t output_length = 0; |
| 4460 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4461 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4462 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4463 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4464 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4465 | psa_set_key_algorithm(&attributes, alg); |
| 4466 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4467 | |
| 4468 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4469 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4470 | if (input_buffer_size > 0) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4471 | TEST_CALLOC(input, input_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4472 | memcpy(input, iv->x, iv->len); |
| 4473 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4474 | } |
| 4475 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4476 | 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] | 4477 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4478 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4479 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4480 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4481 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4482 | PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4483 | output_buffer_size, &output_length)); |
| 4484 | TEST_LE_U(output_length, |
| 4485 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size)); |
| 4486 | TEST_LE_U(output_length, |
| 4487 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4488 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4489 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4490 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4491 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4492 | mbedtls_free(input); |
| 4493 | mbedtls_free(output); |
| 4494 | psa_destroy_key(key); |
| 4495 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4496 | } |
| 4497 | /* END_CASE */ |
| 4498 | |
| 4499 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4500 | void cipher_verify_output(int alg_arg, |
| 4501 | int key_type_arg, |
| 4502 | data_t *key_data, |
| 4503 | data_t *input) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4504 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4505 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4506 | psa_key_type_t key_type = key_type_arg; |
| 4507 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4508 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4509 | size_t output1_size = 0; |
| 4510 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4511 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4512 | size_t output2_size = 0; |
| 4513 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4514 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4515 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4516 | PSA_ASSERT(psa_crypto_init()); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4517 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4518 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4519 | psa_set_key_algorithm(&attributes, alg); |
| 4520 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4521 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4522 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4523 | &key)); |
| 4524 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4525 | TEST_CALLOC(output1, output1_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4526 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4527 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, |
| 4528 | output1, output1_size, |
| 4529 | &output1_length)); |
| 4530 | TEST_LE_U(output1_length, |
| 4531 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4532 | TEST_LE_U(output1_length, |
| 4533 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4534 | |
| 4535 | output2_size = output1_length; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4536 | TEST_CALLOC(output2, output2_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4537 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4538 | PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length, |
| 4539 | output2, output2_size, |
| 4540 | &output2_length)); |
| 4541 | TEST_LE_U(output2_length, |
| 4542 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4543 | TEST_LE_U(output2_length, |
| 4544 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4545 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4546 | TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4547 | |
| 4548 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4549 | mbedtls_free(output1); |
| 4550 | mbedtls_free(output2); |
| 4551 | psa_destroy_key(key); |
| 4552 | PSA_DONE(); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4553 | } |
| 4554 | /* END_CASE */ |
| 4555 | |
| 4556 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4557 | void cipher_verify_output_multipart(int alg_arg, |
| 4558 | int key_type_arg, |
| 4559 | data_t *key_data, |
| 4560 | data_t *input, |
| 4561 | int first_part_size_arg) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4562 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4563 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4564 | psa_key_type_t key_type = key_type_arg; |
| 4565 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4566 | size_t first_part_size = first_part_size_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4567 | unsigned char iv[16] = { 0 }; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4568 | size_t iv_size = 16; |
| 4569 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4570 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4571 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4572 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4573 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4574 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4575 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4576 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4577 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 4578 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4579 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4580 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4581 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4582 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4583 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4584 | psa_set_key_algorithm(&attributes, alg); |
| 4585 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4586 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4587 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4588 | &key)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4589 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4590 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg)); |
| 4591 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4592 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4593 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 4594 | PSA_ASSERT(psa_cipher_generate_iv(&operation1, |
| 4595 | iv, iv_size, |
| 4596 | &iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4597 | } |
| 4598 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4599 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4600 | TEST_LE_U(output1_buffer_size, |
| 4601 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4602 | TEST_CALLOC(output1, output1_buffer_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4603 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4604 | TEST_LE_U(first_part_size, input->len); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4605 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4606 | PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size, |
| 4607 | output1, output1_buffer_size, |
| 4608 | &function_output_length)); |
| 4609 | TEST_LE_U(function_output_length, |
| 4610 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4611 | TEST_LE_U(function_output_length, |
| 4612 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4613 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4614 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4615 | PSA_ASSERT(psa_cipher_update(&operation1, |
| 4616 | input->x + first_part_size, |
| 4617 | input->len - first_part_size, |
David Horstmann | b8dc245 | 2024-02-06 17:03:13 +0000 | [diff] [blame^] | 4618 | output1 + output1_length, |
| 4619 | output1_buffer_size - output1_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 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, |
David Horstmann | b8dc245 | 2024-02-06 17:03:13 +0000 | [diff] [blame^] | 4665 | output2 + output2_length, |
| 4666 | output2_buffer_size - output2_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4667 | &function_output_length)); |
| 4668 | TEST_LE_U(function_output_length, |
| 4669 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4670 | alg, |
| 4671 | output1_length - first_part_size)); |
| 4672 | TEST_LE_U(function_output_length, |
| 4673 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4674 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4676 | PSA_ASSERT(psa_cipher_finish(&operation2, |
| 4677 | output2 + output2_length, |
| 4678 | output2_buffer_size - output2_length, |
| 4679 | &function_output_length)); |
| 4680 | TEST_LE_U(function_output_length, |
| 4681 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4682 | TEST_LE_U(function_output_length, |
| 4683 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4684 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4685 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4686 | PSA_ASSERT(psa_cipher_abort(&operation2)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4687 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4688 | TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4689 | |
| 4690 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4691 | psa_cipher_abort(&operation1); |
| 4692 | psa_cipher_abort(&operation2); |
| 4693 | mbedtls_free(output1); |
| 4694 | mbedtls_free(output2); |
| 4695 | psa_destroy_key(key); |
| 4696 | PSA_DONE(); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4697 | } |
| 4698 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 4699 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4700 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4701 | void aead_encrypt_decrypt(int key_type_arg, data_t *key_data, |
| 4702 | int alg_arg, |
| 4703 | data_t *nonce, |
| 4704 | data_t *additional_data, |
| 4705 | data_t *input_data, |
| 4706 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4707 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4708 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4709 | psa_key_type_t key_type = key_type_arg; |
| 4710 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4711 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4712 | unsigned char *output_data = NULL; |
| 4713 | size_t output_size = 0; |
| 4714 | size_t output_length = 0; |
| 4715 | unsigned char *output_data2 = NULL; |
| 4716 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4717 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4718 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4719 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4720 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4721 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4722 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4723 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4724 | psa_set_key_algorithm(&attributes, alg); |
| 4725 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4726 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4727 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4728 | &key)); |
| 4729 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4730 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4731 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4732 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4733 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4734 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4735 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4736 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4737 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
| 4738 | TEST_EQUAL(output_size, |
| 4739 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4740 | TEST_LE_U(output_size, |
| 4741 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4742 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4743 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4744 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4745 | status = psa_aead_encrypt(key, alg, |
| 4746 | nonce->x, nonce->len, |
| 4747 | additional_data->x, |
| 4748 | additional_data->len, |
| 4749 | input_data->x, input_data->len, |
| 4750 | output_data, output_size, |
| 4751 | &output_length); |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4752 | |
| 4753 | /* If the operation is not supported, just skip and not fail in case the |
| 4754 | * encryption involves a common limitation of cryptography hardwares and |
| 4755 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4756 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4757 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4758 | 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] | 4759 | } |
| 4760 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4761 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4762 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4763 | if (PSA_SUCCESS == expected_result) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4764 | TEST_CALLOC(output_data2, output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4765 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4766 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4767 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4768 | TEST_EQUAL(input_data->len, |
| 4769 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length)); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4771 | TEST_LE_U(input_data->len, |
| 4772 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length)); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4773 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4774 | TEST_EQUAL(psa_aead_decrypt(key, alg, |
| 4775 | nonce->x, nonce->len, |
| 4776 | additional_data->x, |
| 4777 | additional_data->len, |
| 4778 | output_data, output_length, |
| 4779 | output_data2, output_length, |
| 4780 | &output_length2), |
| 4781 | expected_result); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4782 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4783 | TEST_MEMORY_COMPARE(input_data->x, input_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4784 | output_data2, output_length2); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4785 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4786 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4787 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4788 | psa_destroy_key(key); |
| 4789 | mbedtls_free(output_data); |
| 4790 | mbedtls_free(output_data2); |
| 4791 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4792 | } |
| 4793 | /* END_CASE */ |
| 4794 | |
| 4795 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4796 | void aead_encrypt(int key_type_arg, data_t *key_data, |
| 4797 | int alg_arg, |
| 4798 | data_t *nonce, |
| 4799 | data_t *additional_data, |
| 4800 | data_t *input_data, |
| 4801 | data_t *expected_result) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4802 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4803 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4804 | psa_key_type_t key_type = key_type_arg; |
| 4805 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4806 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4807 | unsigned char *output_data = NULL; |
| 4808 | size_t output_size = 0; |
| 4809 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4810 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4811 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4812 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4813 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4814 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4815 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4816 | psa_set_key_algorithm(&attributes, alg); |
| 4817 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4818 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4819 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4820 | &key)); |
| 4821 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4822 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4823 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4824 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4825 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4826 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4827 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4828 | TEST_EQUAL(output_size, |
| 4829 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4830 | TEST_LE_U(output_size, |
| 4831 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4832 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4833 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4834 | status = psa_aead_encrypt(key, alg, |
| 4835 | nonce->x, nonce->len, |
| 4836 | additional_data->x, additional_data->len, |
| 4837 | input_data->x, input_data->len, |
| 4838 | output_data, output_size, |
| 4839 | &output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4840 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4841 | /* If the operation is not supported, just skip and not fail in case the |
| 4842 | * encryption involves a common limitation of cryptography hardwares and |
| 4843 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4844 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4845 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4846 | 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] | 4847 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4848 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4849 | PSA_ASSERT(status); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4850 | TEST_MEMORY_COMPARE(expected_result->x, expected_result->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4851 | output_data, output_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4852 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4853 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4854 | psa_destroy_key(key); |
| 4855 | mbedtls_free(output_data); |
| 4856 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4857 | } |
| 4858 | /* END_CASE */ |
| 4859 | |
| 4860 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4861 | void aead_decrypt(int key_type_arg, data_t *key_data, |
| 4862 | int alg_arg, |
| 4863 | data_t *nonce, |
| 4864 | data_t *additional_data, |
| 4865 | data_t *input_data, |
| 4866 | data_t *expected_data, |
| 4867 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4868 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4869 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4870 | psa_key_type_t key_type = key_type_arg; |
| 4871 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4872 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4873 | unsigned char *output_data = NULL; |
| 4874 | size_t output_size = 0; |
| 4875 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4876 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4877 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4878 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4879 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4880 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4881 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4882 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4883 | psa_set_key_algorithm(&attributes, alg); |
| 4884 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4885 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4886 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4887 | &key)); |
| 4888 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4889 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4890 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4891 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4892 | alg); |
| 4893 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4894 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4895 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4896 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4897 | TEST_EQUAL(output_size, |
| 4898 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4899 | TEST_LE_U(output_size, |
| 4900 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4901 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4902 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4903 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4904 | status = psa_aead_decrypt(key, alg, |
| 4905 | nonce->x, nonce->len, |
| 4906 | additional_data->x, |
| 4907 | additional_data->len, |
| 4908 | input_data->x, input_data->len, |
| 4909 | output_data, output_size, |
| 4910 | &output_length); |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4911 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4912 | /* If the operation is not supported, just skip and not fail in case the |
| 4913 | * decryption involves a common limitation of cryptography hardwares and |
| 4914 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4915 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4916 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4917 | 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] | 4918 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4919 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4920 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4921 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4922 | if (expected_result == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4923 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4924 | output_data, output_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4925 | } |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4926 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4927 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4928 | psa_destroy_key(key); |
| 4929 | mbedtls_free(output_data); |
| 4930 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4931 | } |
| 4932 | /* END_CASE */ |
| 4933 | |
| 4934 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4935 | void aead_multipart_encrypt(int key_type_arg, data_t *key_data, |
| 4936 | int alg_arg, |
| 4937 | data_t *nonce, |
| 4938 | data_t *additional_data, |
| 4939 | data_t *input_data, |
| 4940 | int do_set_lengths, |
| 4941 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4942 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4943 | size_t ad_part_len = 0; |
| 4944 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4945 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4946 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4947 | for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) { |
| 4948 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4949 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4950 | if (do_set_lengths) { |
| 4951 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4952 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4953 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4954 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4955 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4956 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4957 | |
| 4958 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4959 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4960 | alg_arg, nonce, |
| 4961 | additional_data, |
| 4962 | ad_part_len, |
| 4963 | input_data, -1, |
| 4964 | set_lengths_method, |
| 4965 | expected_output, |
| 4966 | 1, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4967 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4968 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4969 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4970 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 4971 | mbedtls_test_set_step(1000 + ad_part_len); |
| 4972 | |
| 4973 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4974 | alg_arg, nonce, |
| 4975 | additional_data, |
| 4976 | ad_part_len, |
| 4977 | input_data, -1, |
| 4978 | set_lengths_method, |
| 4979 | expected_output, |
| 4980 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4981 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4982 | } |
| 4983 | } |
| 4984 | |
| 4985 | for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) { |
| 4986 | /* Split data into length(data_part_len) parts. */ |
| 4987 | mbedtls_test_set_step(2000 + data_part_len); |
| 4988 | |
| 4989 | if (do_set_lengths) { |
| 4990 | if (data_part_len & 0x01) { |
| 4991 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4992 | } else { |
| 4993 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
| 4994 | } |
| 4995 | } |
| 4996 | |
| 4997 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4998 | alg_arg, nonce, |
| 4999 | additional_data, -1, |
| 5000 | input_data, data_part_len, |
| 5001 | set_lengths_method, |
| 5002 | expected_output, |
| 5003 | 1, 0)) { |
| 5004 | break; |
| 5005 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5006 | |
| 5007 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5008 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5009 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5010 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5011 | alg_arg, nonce, |
| 5012 | additional_data, -1, |
| 5013 | input_data, data_part_len, |
| 5014 | set_lengths_method, |
| 5015 | expected_output, |
| 5016 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5017 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5018 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5019 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5020 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5021 | /* Goto is required to silence warnings about unused labels, as we |
| 5022 | * don't actually do any test assertions in this function. */ |
| 5023 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5024 | } |
| 5025 | /* END_CASE */ |
| 5026 | |
| 5027 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5028 | void aead_multipart_decrypt(int key_type_arg, data_t *key_data, |
| 5029 | int alg_arg, |
| 5030 | data_t *nonce, |
| 5031 | data_t *additional_data, |
| 5032 | data_t *input_data, |
| 5033 | int do_set_lengths, |
| 5034 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5035 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5036 | size_t ad_part_len = 0; |
| 5037 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5038 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5039 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5040 | 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] | 5041 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5042 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5043 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5044 | if (do_set_lengths) { |
| 5045 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5046 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5047 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5048 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5049 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5050 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5051 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5052 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5053 | alg_arg, nonce, |
| 5054 | additional_data, |
| 5055 | ad_part_len, |
| 5056 | input_data, -1, |
| 5057 | set_lengths_method, |
| 5058 | expected_output, |
| 5059 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5060 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5061 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5062 | |
| 5063 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5064 | mbedtls_test_set_step(1000 + ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5065 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5066 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5067 | alg_arg, nonce, |
| 5068 | additional_data, |
| 5069 | ad_part_len, |
| 5070 | input_data, -1, |
| 5071 | set_lengths_method, |
| 5072 | expected_output, |
| 5073 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5074 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5075 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5076 | } |
| 5077 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5078 | 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] | 5079 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5080 | mbedtls_test_set_step(2000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5081 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5082 | if (do_set_lengths) { |
| 5083 | if (data_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5084 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5085 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5086 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5087 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5088 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5089 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5090 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5091 | alg_arg, nonce, |
| 5092 | additional_data, -1, |
| 5093 | input_data, data_part_len, |
| 5094 | set_lengths_method, |
| 5095 | expected_output, |
| 5096 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5097 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5098 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5099 | |
| 5100 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5101 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5102 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5103 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5104 | alg_arg, nonce, |
| 5105 | additional_data, -1, |
| 5106 | input_data, data_part_len, |
| 5107 | set_lengths_method, |
| 5108 | expected_output, |
| 5109 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5110 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5111 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5112 | } |
| 5113 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5114 | /* Goto is required to silence warnings about unused labels, as we |
| 5115 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5116 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5117 | } |
| 5118 | /* END_CASE */ |
| 5119 | |
| 5120 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5121 | void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data, |
| 5122 | int alg_arg, |
| 5123 | int nonce_length, |
| 5124 | int expected_nonce_length_arg, |
| 5125 | data_t *additional_data, |
| 5126 | data_t *input_data, |
| 5127 | int expected_status_arg) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5128 | { |
| 5129 | |
| 5130 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5131 | psa_key_type_t key_type = key_type_arg; |
| 5132 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5133 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5134 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5135 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5136 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5137 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5138 | size_t actual_nonce_length = 0; |
| 5139 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 5140 | unsigned char *output = NULL; |
| 5141 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5142 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5143 | size_t ciphertext_size = 0; |
| 5144 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5145 | size_t tag_length = 0; |
| 5146 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5147 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5148 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5150 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5151 | psa_set_key_algorithm(&attributes, alg); |
| 5152 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5153 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5154 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5155 | &key)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5156 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5157 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5159 | 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] | 5160 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5161 | TEST_CALLOC(output, output_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5162 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5163 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5164 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5165 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5166 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5167 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5168 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5169 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5170 | |
| 5171 | /* If the operation is not supported, just skip and not fail in case the |
| 5172 | * encryption involves a common limitation of cryptography hardwares and |
| 5173 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5174 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5175 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5176 | 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] | 5177 | } |
| 5178 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5179 | PSA_ASSERT(status); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5181 | status = psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5182 | nonce_length, |
| 5183 | &actual_nonce_length); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5184 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5185 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5186 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5187 | TEST_EQUAL(actual_nonce_length, expected_nonce_length); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 5188 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5189 | if (expected_status == PSA_SUCCESS) { |
| 5190 | TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type, |
| 5191 | alg)); |
| 5192 | } |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 5193 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5194 | TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 5195 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5196 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5197 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5198 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5199 | input_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5200 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5201 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5202 | additional_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5203 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5204 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5205 | output, output_size, |
| 5206 | &ciphertext_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5207 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5208 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5209 | &ciphertext_length, tag_buffer, |
| 5210 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5211 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5212 | |
| 5213 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5214 | psa_destroy_key(key); |
| 5215 | mbedtls_free(output); |
| 5216 | mbedtls_free(ciphertext); |
| 5217 | psa_aead_abort(&operation); |
| 5218 | PSA_DONE(); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5219 | } |
| 5220 | /* END_CASE */ |
| 5221 | |
| 5222 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5223 | void aead_multipart_set_nonce(int key_type_arg, data_t *key_data, |
| 5224 | int alg_arg, |
| 5225 | int nonce_length_arg, |
| 5226 | int set_lengths_method_arg, |
| 5227 | data_t *additional_data, |
| 5228 | data_t *input_data, |
| 5229 | int expected_status_arg) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5230 | { |
| 5231 | |
| 5232 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5233 | psa_key_type_t key_type = key_type_arg; |
| 5234 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5235 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5236 | uint8_t *nonce_buffer = NULL; |
| 5237 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5238 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5239 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5240 | unsigned char *output = NULL; |
| 5241 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5242 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5243 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5244 | size_t ciphertext_size = 0; |
| 5245 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5246 | size_t tag_length = 0; |
| 5247 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5248 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5249 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5250 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5251 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5252 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5253 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5254 | psa_set_key_algorithm(&attributes, alg); |
| 5255 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5256 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5257 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5258 | &key)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5259 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5260 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5261 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5262 | 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] | 5263 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5264 | TEST_CALLOC(output, output_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5266 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5267 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5268 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5269 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5270 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5271 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5272 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5273 | |
| 5274 | /* If the operation is not supported, just skip and not fail in case the |
| 5275 | * encryption involves a common limitation of cryptography hardwares and |
| 5276 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5277 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5278 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5279 | 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] | 5280 | } |
| 5281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5282 | PSA_ASSERT(status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5283 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5284 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5285 | if (nonce_length_arg == -1) { |
| 5286 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5287 | TEST_CALLOC(nonce_buffer, 4); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5288 | nonce_length = 0; |
| 5289 | } else { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5290 | /* If length is zero, then this will return NULL. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5291 | nonce_length = (size_t) nonce_length_arg; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5292 | TEST_CALLOC(nonce_buffer, nonce_length); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5293 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5294 | if (nonce_buffer) { |
| 5295 | for (index = 0; index < nonce_length - 1; ++index) { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5296 | nonce_buffer[index] = 'a' + index; |
| 5297 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5298 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5299 | } |
| 5300 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5301 | if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { |
| 5302 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5303 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5304 | } |
| 5305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5306 | status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5307 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5308 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5309 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5310 | if (expected_status == PSA_SUCCESS) { |
| 5311 | if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) { |
| 5312 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5313 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5314 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5315 | 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] | 5316 | expected_status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5317 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5318 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5319 | /* 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] | 5320 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5321 | additional_data->len), |
| 5322 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5323 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5324 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5325 | output, output_size, |
| 5326 | &ciphertext_length), |
| 5327 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5328 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5329 | TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5330 | &ciphertext_length, tag_buffer, |
| 5331 | PSA_AEAD_TAG_MAX_SIZE, &tag_length), |
| 5332 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5333 | } |
| 5334 | |
| 5335 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5336 | psa_destroy_key(key); |
| 5337 | mbedtls_free(output); |
| 5338 | mbedtls_free(ciphertext); |
| 5339 | mbedtls_free(nonce_buffer); |
| 5340 | psa_aead_abort(&operation); |
| 5341 | PSA_DONE(); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5342 | } |
| 5343 | /* END_CASE */ |
| 5344 | |
| 5345 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5346 | 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] | 5347 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5348 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5349 | data_t *nonce, |
| 5350 | data_t *additional_data, |
| 5351 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5352 | int expected_status_arg) |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5353 | { |
| 5354 | |
| 5355 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5356 | psa_key_type_t key_type = key_type_arg; |
| 5357 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5358 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5359 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5360 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5361 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5362 | unsigned char *output = NULL; |
| 5363 | unsigned char *ciphertext = NULL; |
| 5364 | size_t output_size = output_size_arg; |
| 5365 | size_t ciphertext_size = 0; |
| 5366 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5367 | size_t tag_length = 0; |
| 5368 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5370 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5371 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5372 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5373 | psa_set_key_algorithm(&attributes, alg); |
| 5374 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5375 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5376 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5377 | &key)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5378 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5379 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5380 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5381 | TEST_CALLOC(output, output_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5382 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5383 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5384 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5385 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5386 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5387 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5388 | |
| 5389 | /* If the operation is not supported, just skip and not fail in case the |
| 5390 | * encryption involves a common limitation of cryptography hardwares and |
| 5391 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5392 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5393 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5394 | 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] | 5395 | } |
| 5396 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5397 | PSA_ASSERT(status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5398 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5399 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5400 | input_data->len)); |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5401 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5402 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5403 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5404 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5405 | additional_data->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5406 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5407 | status = psa_aead_update(&operation, input_data->x, input_data->len, |
| 5408 | output, output_size, &ciphertext_length); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5409 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5410 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5411 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5412 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5413 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5414 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5415 | &ciphertext_length, tag_buffer, |
| 5416 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5417 | } |
| 5418 | |
| 5419 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5420 | psa_destroy_key(key); |
| 5421 | mbedtls_free(output); |
| 5422 | mbedtls_free(ciphertext); |
| 5423 | psa_aead_abort(&operation); |
| 5424 | PSA_DONE(); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5425 | } |
| 5426 | /* END_CASE */ |
| 5427 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5428 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5429 | void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data, |
| 5430 | int alg_arg, |
| 5431 | int finish_ciphertext_size_arg, |
| 5432 | int tag_size_arg, |
| 5433 | data_t *nonce, |
| 5434 | data_t *additional_data, |
| 5435 | data_t *input_data, |
| 5436 | int expected_status_arg) |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5437 | { |
| 5438 | |
| 5439 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5440 | psa_key_type_t key_type = key_type_arg; |
| 5441 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5442 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5443 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5444 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5445 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5446 | unsigned char *ciphertext = NULL; |
| 5447 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5448 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5449 | size_t ciphertext_size = 0; |
| 5450 | size_t ciphertext_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5451 | size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg; |
| 5452 | size_t tag_size = (size_t) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5453 | size_t tag_length = 0; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5454 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5455 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5456 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5457 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5458 | psa_set_key_algorithm(&attributes, alg); |
| 5459 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5460 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5461 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5462 | &key)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5463 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5464 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5465 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5466 | 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] | 5467 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5468 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5469 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5470 | TEST_CALLOC(finish_ciphertext, finish_ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5471 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5472 | TEST_CALLOC(tag_buffer, tag_size); |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5473 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5474 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5475 | |
| 5476 | /* If the operation is not supported, just skip and not fail in case the |
| 5477 | * encryption involves a common limitation of cryptography hardwares and |
| 5478 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5479 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5480 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5481 | 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] | 5482 | } |
| 5483 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5484 | PSA_ASSERT(status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5485 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5486 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5487 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5488 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5489 | input_data->len)); |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5490 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5491 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5492 | additional_data->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5493 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5494 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5495 | ciphertext, ciphertext_size, &ciphertext_length)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5496 | |
| 5497 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5498 | status = psa_aead_finish(&operation, finish_ciphertext, |
| 5499 | finish_ciphertext_size, |
| 5500 | &ciphertext_length, tag_buffer, |
| 5501 | tag_size, &tag_length); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5502 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5503 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5504 | |
| 5505 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5506 | psa_destroy_key(key); |
| 5507 | mbedtls_free(ciphertext); |
| 5508 | mbedtls_free(finish_ciphertext); |
| 5509 | mbedtls_free(tag_buffer); |
| 5510 | psa_aead_abort(&operation); |
| 5511 | PSA_DONE(); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5512 | } |
| 5513 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5514 | |
| 5515 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5516 | void aead_multipart_verify(int key_type_arg, data_t *key_data, |
| 5517 | int alg_arg, |
| 5518 | data_t *nonce, |
| 5519 | data_t *additional_data, |
| 5520 | data_t *input_data, |
| 5521 | data_t *tag, |
| 5522 | int tag_usage_arg, |
| 5523 | int expected_setup_status_arg, |
| 5524 | int expected_status_arg) |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5525 | { |
| 5526 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5527 | psa_key_type_t key_type = key_type_arg; |
| 5528 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5529 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5530 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5531 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5532 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5533 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5534 | unsigned char *plaintext = NULL; |
| 5535 | unsigned char *finish_plaintext = NULL; |
| 5536 | size_t plaintext_size = 0; |
| 5537 | size_t plaintext_length = 0; |
| 5538 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5539 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5540 | unsigned char *tag_buffer = NULL; |
| 5541 | size_t tag_size = 0; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5542 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5543 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5544 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5545 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 5546 | psa_set_key_algorithm(&attributes, alg); |
| 5547 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5548 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5549 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5550 | &key)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5551 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5552 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5553 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5554 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 5555 | input_data->len); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5556 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5557 | TEST_CALLOC(plaintext, plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5558 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5559 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5560 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5561 | TEST_CALLOC(finish_plaintext, verify_plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5562 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5563 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5564 | |
| 5565 | /* If the operation is not supported, just skip and not fail in case the |
| 5566 | * encryption involves a common limitation of cryptography hardwares and |
| 5567 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5568 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5569 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5570 | 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] | 5571 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5572 | TEST_EQUAL(status, expected_setup_status); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5573 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5574 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5575 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5576 | } |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5577 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5578 | PSA_ASSERT(status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5579 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5580 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5581 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5582 | status = psa_aead_set_lengths(&operation, additional_data->len, |
| 5583 | input_data->len); |
| 5584 | PSA_ASSERT(status); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5585 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5586 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5587 | additional_data->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5588 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5589 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 5590 | input_data->len, |
| 5591 | plaintext, plaintext_size, |
| 5592 | &plaintext_length)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5593 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5594 | if (tag_usage == USE_GIVEN_TAG) { |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5595 | tag_buffer = tag->x; |
| 5596 | tag_size = tag->len; |
| 5597 | } |
| 5598 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5599 | status = psa_aead_verify(&operation, finish_plaintext, |
| 5600 | verify_plaintext_size, |
| 5601 | &plaintext_length, |
| 5602 | tag_buffer, tag_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5603 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5604 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5605 | |
| 5606 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5607 | psa_destroy_key(key); |
| 5608 | mbedtls_free(plaintext); |
| 5609 | mbedtls_free(finish_plaintext); |
| 5610 | psa_aead_abort(&operation); |
| 5611 | PSA_DONE(); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5612 | } |
| 5613 | /* END_CASE */ |
| 5614 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5615 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5616 | void aead_multipart_setup(int key_type_arg, data_t *key_data, |
| 5617 | int alg_arg, int expected_status_arg) |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5618 | { |
| 5619 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5620 | psa_key_type_t key_type = key_type_arg; |
| 5621 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5622 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5623 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5624 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5625 | psa_status_t expected_status = expected_status_arg; |
| 5626 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5627 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5628 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5629 | psa_set_key_usage_flags(&attributes, |
| 5630 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5631 | psa_set_key_algorithm(&attributes, alg); |
| 5632 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5633 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5634 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5635 | &key)); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5636 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5637 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5638 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5639 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5640 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5641 | psa_aead_abort(&operation); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5642 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5643 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5644 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5645 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5646 | |
| 5647 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5648 | psa_destroy_key(key); |
| 5649 | psa_aead_abort(&operation); |
| 5650 | PSA_DONE(); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5651 | } |
| 5652 | /* END_CASE */ |
| 5653 | |
| 5654 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5655 | void aead_multipart_state_test(int key_type_arg, data_t *key_data, |
| 5656 | int alg_arg, |
| 5657 | data_t *nonce, |
| 5658 | data_t *additional_data, |
| 5659 | data_t *input_data) |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5660 | { |
| 5661 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5662 | psa_key_type_t key_type = key_type_arg; |
| 5663 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5664 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5665 | unsigned char *output_data = NULL; |
| 5666 | unsigned char *final_data = NULL; |
| 5667 | size_t output_size = 0; |
| 5668 | size_t finish_output_size = 0; |
| 5669 | size_t output_length = 0; |
| 5670 | size_t key_bits = 0; |
| 5671 | size_t tag_length = 0; |
| 5672 | size_t tag_size = 0; |
| 5673 | size_t nonce_length = 0; |
| 5674 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5675 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5676 | size_t output_part_length = 0; |
| 5677 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5678 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5679 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5680 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5681 | psa_set_key_usage_flags(&attributes, |
| 5682 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5683 | psa_set_key_algorithm(&attributes, alg); |
| 5684 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5685 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5686 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5687 | &key)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5688 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5689 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5690 | key_bits = psa_get_key_bits(&attributes); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5691 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5692 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5693 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5694 | TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5695 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5696 | 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] | 5697 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5698 | TEST_CALLOC(output_data, output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5699 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5700 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5701 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5702 | TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5703 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5704 | TEST_CALLOC(final_data, finish_output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5705 | |
| 5706 | /* Test all operations error without calling setup first. */ |
| 5707 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5708 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5709 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5710 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5711 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5712 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5713 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5714 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5715 | &nonce_length), |
| 5716 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5717 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5718 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5719 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5720 | /* ------------------------------------------------------- */ |
| 5721 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5722 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 5723 | input_data->len), |
| 5724 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5725 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5726 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5727 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5728 | /* ------------------------------------------------------- */ |
| 5729 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5730 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5731 | additional_data->len), |
| 5732 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5733 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5734 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5735 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5736 | /* ------------------------------------------------------- */ |
| 5737 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5738 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 5739 | input_data->len, output_data, |
| 5740 | output_size, &output_length), |
| 5741 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5742 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5743 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5744 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5745 | /* ------------------------------------------------------- */ |
| 5746 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5747 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 5748 | finish_output_size, |
| 5749 | &output_part_length, |
| 5750 | tag_buffer, tag_length, |
| 5751 | &tag_size), |
| 5752 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5753 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5754 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5755 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5756 | /* ------------------------------------------------------- */ |
| 5757 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5758 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 5759 | finish_output_size, |
| 5760 | &output_part_length, |
| 5761 | tag_buffer, |
| 5762 | tag_length), |
| 5763 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5764 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5765 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5766 | |
| 5767 | /* Test for double setups. */ |
| 5768 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5769 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5771 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 5772 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5773 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5774 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5775 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5776 | /* ------------------------------------------------------- */ |
| 5777 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5778 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5779 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5780 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 5781 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5782 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5783 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5784 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5785 | /* ------------------------------------------------------- */ |
| 5786 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5787 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5788 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5789 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 5790 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5791 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5792 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5793 | |
| 5794 | /* ------------------------------------------------------- */ |
| 5795 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5796 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5797 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5798 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 5799 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5800 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5801 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5802 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5803 | /* Test for not setting a nonce. */ |
| 5804 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5805 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5806 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5807 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5808 | additional_data->len), |
| 5809 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5810 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5811 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5812 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5813 | /* ------------------------------------------------------- */ |
| 5814 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5815 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5816 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5817 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 5818 | input_data->len, output_data, |
| 5819 | output_size, &output_length), |
| 5820 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5821 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5822 | psa_aead_abort(&operation); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5823 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5824 | /* ------------------------------------------------------- */ |
| 5825 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5826 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5827 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5828 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 5829 | finish_output_size, |
| 5830 | &output_part_length, |
| 5831 | tag_buffer, tag_length, |
| 5832 | &tag_size), |
| 5833 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5834 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5835 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5836 | |
| 5837 | /* ------------------------------------------------------- */ |
| 5838 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5839 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5840 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5841 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 5842 | finish_output_size, |
| 5843 | &output_part_length, |
| 5844 | tag_buffer, |
| 5845 | tag_length), |
| 5846 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5847 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5848 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5849 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5850 | /* Test for double setting nonce. */ |
| 5851 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5852 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5853 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5854 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5855 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5856 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5857 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5858 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5859 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5860 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5861 | /* Test for double generating nonce. */ |
| 5862 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5863 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5864 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5865 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5866 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5867 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5868 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5869 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5870 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5871 | &nonce_length), |
| 5872 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5873 | |
| 5874 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5875 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5876 | |
| 5877 | /* Test for generate nonce then set and vice versa */ |
| 5878 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5879 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5880 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5881 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5882 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5883 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5884 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5885 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5886 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5887 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5888 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5889 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5890 | /* Test for generating nonce after calling set lengths */ |
| 5891 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5892 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5893 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5894 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5895 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5896 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5897 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5898 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5899 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5900 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5901 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5902 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5903 | /* 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] | 5904 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5905 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5906 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5907 | if (operation.alg == PSA_ALG_CCM) { |
| 5908 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5909 | input_data->len), |
| 5910 | PSA_ERROR_INVALID_ARGUMENT); |
| 5911 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5912 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5913 | &nonce_length), |
| 5914 | PSA_ERROR_BAD_STATE); |
| 5915 | } else { |
| 5916 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5917 | input_data->len)); |
| 5918 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5919 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5920 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5921 | } |
| 5922 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5923 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5924 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5925 | /* 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] | 5926 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5927 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5928 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5929 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 5930 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5931 | input_data->len), |
| 5932 | PSA_ERROR_INVALID_ARGUMENT); |
| 5933 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5934 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5935 | &nonce_length), |
| 5936 | PSA_ERROR_BAD_STATE); |
| 5937 | } else { |
| 5938 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5939 | input_data->len)); |
| 5940 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5941 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5942 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5943 | } |
| 5944 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5945 | psa_aead_abort(&operation); |
Dave Rodgman | d26d744 | 2023-02-11 17:14:54 +0000 | [diff] [blame] | 5946 | #endif |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5947 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5948 | /* 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] | 5949 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5950 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5951 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5952 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5953 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5954 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5955 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5956 | if (operation.alg == PSA_ALG_CCM) { |
| 5957 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5958 | input_data->len), |
| 5959 | PSA_ERROR_INVALID_ARGUMENT); |
| 5960 | } else { |
| 5961 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5962 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5963 | } |
| 5964 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5965 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5966 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5967 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5968 | /* Test for setting nonce after calling set lengths */ |
| 5969 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5970 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5971 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5972 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5973 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5974 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5975 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5976 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5977 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5978 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5979 | /* 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] | 5980 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5981 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5982 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5983 | if (operation.alg == PSA_ALG_CCM) { |
| 5984 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5985 | input_data->len), |
| 5986 | PSA_ERROR_INVALID_ARGUMENT); |
| 5987 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5988 | PSA_ERROR_BAD_STATE); |
| 5989 | } else { |
| 5990 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5991 | input_data->len)); |
| 5992 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5993 | } |
| 5994 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5995 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5996 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5997 | /* 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] | 5998 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5999 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6000 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6001 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 6002 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 6003 | input_data->len), |
| 6004 | PSA_ERROR_INVALID_ARGUMENT); |
| 6005 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6006 | PSA_ERROR_BAD_STATE); |
| 6007 | } else { |
| 6008 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 6009 | input_data->len)); |
| 6010 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6011 | } |
| 6012 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6013 | psa_aead_abort(&operation); |
Dave Rodgman | a476363 | 2023-02-11 18:36:23 +0000 | [diff] [blame] | 6014 | #endif |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6015 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6016 | /* 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] | 6017 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6018 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6019 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6020 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6021 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6022 | if (operation.alg == PSA_ALG_CCM) { |
| 6023 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6024 | input_data->len), |
| 6025 | PSA_ERROR_INVALID_ARGUMENT); |
| 6026 | } else { |
| 6027 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6028 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6029 | } |
| 6030 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6031 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6032 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6033 | /* 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] | 6034 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6035 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6036 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6037 | if (operation.alg == PSA_ALG_GCM) { |
| 6038 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6039 | SIZE_MAX), |
| 6040 | PSA_ERROR_INVALID_ARGUMENT); |
| 6041 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6042 | PSA_ERROR_BAD_STATE); |
| 6043 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6044 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6045 | SIZE_MAX)); |
| 6046 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6047 | } |
| 6048 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6049 | psa_aead_abort(&operation); |
Dave Rodgman | 91e8321 | 2023-02-11 20:07:43 +0000 | [diff] [blame] | 6050 | #endif |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6051 | |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 6052 | /* 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] | 6053 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6054 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6055 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6056 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6057 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6058 | if (operation.alg == PSA_ALG_GCM) { |
| 6059 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6060 | SIZE_MAX), |
| 6061 | PSA_ERROR_INVALID_ARGUMENT); |
| 6062 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6063 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6064 | SIZE_MAX)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6065 | } |
| 6066 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6067 | psa_aead_abort(&operation); |
Dave Rodgman | 641288b | 2023-02-11 22:02:04 +0000 | [diff] [blame] | 6068 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6069 | |
| 6070 | /* ------------------------------------------------------- */ |
| 6071 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6072 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6073 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6074 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6075 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6076 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6077 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6078 | &nonce_length), |
| 6079 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6080 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6081 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6082 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6083 | /* Test for generating nonce in decrypt setup. */ |
| 6084 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6085 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6086 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6087 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6088 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6089 | &nonce_length), |
| 6090 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6091 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6092 | psa_aead_abort(&operation); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6093 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6094 | /* Test for setting lengths twice. */ |
| 6095 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6096 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6097 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6098 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6099 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6100 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6101 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6102 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6103 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6104 | input_data->len), |
| 6105 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6106 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6107 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6108 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6109 | /* Test for setting lengths after setting nonce + already starting data. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6110 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6111 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6112 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6113 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6114 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6115 | if (operation.alg == PSA_ALG_CCM) { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6116 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6117 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6118 | additional_data->len), |
| 6119 | PSA_ERROR_BAD_STATE); |
| 6120 | } else { |
| 6121 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6122 | additional_data->len)); |
| 6123 | |
| 6124 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6125 | input_data->len), |
| 6126 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6127 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6128 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6129 | |
| 6130 | /* ------------------------------------------------------- */ |
| 6131 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6132 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6133 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6134 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6135 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6136 | if (operation.alg == PSA_ALG_CCM) { |
| 6137 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6138 | input_data->len, output_data, |
| 6139 | output_size, &output_length), |
| 6140 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6141 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6142 | } else { |
| 6143 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6144 | input_data->len, output_data, |
| 6145 | output_size, &output_length)); |
| 6146 | |
| 6147 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6148 | input_data->len), |
| 6149 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6150 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6151 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6152 | |
| 6153 | /* ------------------------------------------------------- */ |
| 6154 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6155 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6156 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6157 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6159 | if (operation.alg == PSA_ALG_CCM) { |
| 6160 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6161 | finish_output_size, |
| 6162 | &output_part_length, |
| 6163 | tag_buffer, tag_length, |
| 6164 | &tag_size)); |
| 6165 | } else { |
| 6166 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6167 | finish_output_size, |
| 6168 | &output_part_length, |
| 6169 | tag_buffer, tag_length, |
| 6170 | &tag_size)); |
| 6171 | |
| 6172 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6173 | input_data->len), |
| 6174 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6175 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6176 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6177 | |
| 6178 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 6179 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6180 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6181 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6182 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6183 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6184 | &nonce_length)); |
| 6185 | if (operation.alg == PSA_ALG_CCM) { |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6186 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6187 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6188 | additional_data->len), |
| 6189 | PSA_ERROR_BAD_STATE); |
| 6190 | } else { |
| 6191 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6192 | additional_data->len)); |
| 6193 | |
| 6194 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6195 | input_data->len), |
| 6196 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6197 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6198 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6199 | |
| 6200 | /* ------------------------------------------------------- */ |
| 6201 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6202 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6203 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6204 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6205 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6206 | &nonce_length)); |
| 6207 | if (operation.alg == PSA_ALG_CCM) { |
| 6208 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6209 | input_data->len, output_data, |
| 6210 | output_size, &output_length), |
| 6211 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6212 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6213 | } else { |
| 6214 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6215 | input_data->len, output_data, |
| 6216 | output_size, &output_length)); |
| 6217 | |
| 6218 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6219 | input_data->len), |
| 6220 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6221 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6222 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6223 | |
| 6224 | /* ------------------------------------------------------- */ |
| 6225 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6226 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6227 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6228 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6229 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6230 | &nonce_length)); |
| 6231 | if (operation.alg == PSA_ALG_CCM) { |
| 6232 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6233 | finish_output_size, |
| 6234 | &output_part_length, |
| 6235 | tag_buffer, tag_length, |
| 6236 | &tag_size)); |
| 6237 | } else { |
| 6238 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6239 | finish_output_size, |
| 6240 | &output_part_length, |
| 6241 | tag_buffer, tag_length, |
| 6242 | &tag_size)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6243 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6244 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6245 | input_data->len), |
| 6246 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6247 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6248 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6249 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6250 | /* Test for not sending any additional data or data after setting non zero |
| 6251 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6252 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6253 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6254 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6255 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6256 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6257 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6258 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6259 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6260 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6261 | finish_output_size, |
| 6262 | &output_part_length, |
| 6263 | tag_buffer, tag_length, |
| 6264 | &tag_size), |
| 6265 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6266 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6267 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6268 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6269 | /* Test for not sending any additional data or data after setting non-zero |
| 6270 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6271 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6272 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6273 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6274 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6275 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6276 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6277 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6278 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6279 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6280 | finish_output_size, |
| 6281 | &output_part_length, |
| 6282 | tag_buffer, |
| 6283 | tag_length), |
| 6284 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6285 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6286 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6287 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6288 | /* Test for not sending any additional data after setting a non-zero length |
| 6289 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6290 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6291 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6292 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6293 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6294 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6295 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6296 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6297 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6298 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6299 | input_data->len, output_data, |
| 6300 | output_size, &output_length), |
| 6301 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6302 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6303 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6304 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6305 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 6306 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6307 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6308 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6309 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6310 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6311 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6312 | input_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6314 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6315 | additional_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6316 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6317 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6318 | finish_output_size, |
| 6319 | &output_part_length, |
| 6320 | tag_buffer, tag_length, |
| 6321 | &tag_size), |
| 6322 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6323 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6324 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6325 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6326 | /* Test for sending too much additional data after setting lengths. */ |
| 6327 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6328 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6329 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6330 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6331 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6332 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6333 | |
| 6334 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6335 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6336 | additional_data->len), |
| 6337 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6338 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6339 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6340 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6341 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6342 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6343 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6344 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6345 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6346 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6347 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6348 | input_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6349 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6350 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6351 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6353 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6354 | 1), |
| 6355 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6356 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6357 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6358 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6359 | /* Test for sending too much data after setting lengths. */ |
| 6360 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6361 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6362 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6363 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6364 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6365 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6367 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6368 | input_data->len, output_data, |
| 6369 | output_size, &output_length), |
| 6370 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6371 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6372 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6373 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6374 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6375 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6376 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6377 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6378 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6379 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6380 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6381 | input_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6382 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6383 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6384 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6386 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6387 | input_data->len, output_data, |
| 6388 | output_size, &output_length)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6389 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6390 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6391 | 1, output_data, |
| 6392 | output_size, &output_length), |
| 6393 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6394 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6395 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6396 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6397 | /* Test sending additional data after data. */ |
| 6398 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6399 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6400 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6401 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6402 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6403 | if (operation.alg != PSA_ALG_CCM) { |
| 6404 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6405 | input_data->len, output_data, |
| 6406 | output_size, &output_length)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6407 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6408 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6409 | additional_data->len), |
| 6410 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6411 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6412 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6413 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6414 | /* Test calling finish on decryption. */ |
| 6415 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6416 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6417 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6418 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6419 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6420 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6421 | finish_output_size, |
| 6422 | &output_part_length, |
| 6423 | tag_buffer, tag_length, |
| 6424 | &tag_size), |
| 6425 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6426 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6427 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6428 | |
| 6429 | /* Test calling verify on encryption. */ |
| 6430 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6431 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6433 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6434 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6435 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6436 | finish_output_size, |
| 6437 | &output_part_length, |
| 6438 | tag_buffer, |
| 6439 | tag_length), |
| 6440 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6441 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6442 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6443 | |
| 6444 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6445 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6446 | psa_destroy_key(key); |
| 6447 | psa_aead_abort(&operation); |
| 6448 | mbedtls_free(output_data); |
| 6449 | mbedtls_free(final_data); |
| 6450 | PSA_DONE(); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6451 | } |
| 6452 | /* END_CASE */ |
| 6453 | |
| 6454 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6455 | void signature_size(int type_arg, |
| 6456 | int bits, |
| 6457 | int alg_arg, |
| 6458 | int expected_size_arg) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6459 | { |
| 6460 | psa_key_type_t type = type_arg; |
| 6461 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6462 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6463 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6464 | TEST_EQUAL(actual_size, (size_t) expected_size_arg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6465 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6466 | exit: |
| 6467 | ; |
| 6468 | } |
| 6469 | /* END_CASE */ |
| 6470 | |
| 6471 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6472 | void sign_hash_deterministic(int key_type_arg, data_t *key_data, |
| 6473 | int alg_arg, data_t *input_data, |
| 6474 | data_t *output_data) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6475 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6476 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6477 | psa_key_type_t key_type = key_type_arg; |
| 6478 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6479 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6480 | unsigned char *signature = NULL; |
| 6481 | size_t signature_size; |
| 6482 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6483 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6484 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6485 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6486 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6487 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6488 | psa_set_key_algorithm(&attributes, alg); |
| 6489 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6490 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6491 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6492 | &key)); |
| 6493 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6494 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6495 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6496 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6497 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6498 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6499 | key_bits, alg); |
| 6500 | TEST_ASSERT(signature_size != 0); |
| 6501 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6502 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6503 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6504 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6505 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6506 | input_data->x, input_data->len, |
| 6507 | signature, signature_size, |
| 6508 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6509 | /* Verify that the signature is what is expected. */ |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 6510 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 6511 | signature, signature_length); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6512 | |
| 6513 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6514 | /* |
| 6515 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6516 | * thus reset them as required. |
| 6517 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6518 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6519 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6520 | psa_destroy_key(key); |
| 6521 | mbedtls_free(signature); |
| 6522 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6523 | } |
| 6524 | /* END_CASE */ |
| 6525 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6526 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6527 | /** |
| 6528 | * sign_hash_interruptible() test intentions: |
| 6529 | * |
| 6530 | * Note: This test can currently only handle ECDSA. |
| 6531 | * |
| 6532 | * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 6533 | * and private keys / keypairs only). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6534 | * |
| 6535 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 6536 | * expected for different max_ops values. |
| 6537 | * |
| 6538 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 6539 | * and that each successful stage completes some ops (this is not mandated by |
| 6540 | * the PSA specification, but is currently the case). |
| 6541 | * |
| 6542 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 6543 | * complete() calls does not alter the number of ops returned. |
| 6544 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6545 | void sign_hash_interruptible(int key_type_arg, data_t *key_data, |
| 6546 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6547 | data_t *output_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6548 | { |
| 6549 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6550 | psa_key_type_t key_type = key_type_arg; |
| 6551 | psa_algorithm_t alg = alg_arg; |
| 6552 | size_t key_bits; |
| 6553 | unsigned char *signature = NULL; |
| 6554 | size_t signature_size; |
| 6555 | size_t signature_length = 0xdeadbeef; |
| 6556 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6557 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6558 | uint32_t num_ops = 0; |
| 6559 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6560 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6561 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6562 | size_t min_completes = 0; |
| 6563 | size_t max_completes = 0; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6564 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6565 | psa_sign_hash_interruptible_operation_t operation = |
| 6566 | psa_sign_hash_interruptible_operation_init(); |
| 6567 | |
| 6568 | PSA_ASSERT(psa_crypto_init()); |
| 6569 | |
| 6570 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6571 | psa_set_key_algorithm(&attributes, alg); |
| 6572 | psa_set_key_type(&attributes, key_type); |
| 6573 | |
| 6574 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6575 | &key)); |
| 6576 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6577 | key_bits = psa_get_key_bits(&attributes); |
| 6578 | |
| 6579 | /* Allocate a buffer which has the size advertised by the |
| 6580 | * library. */ |
| 6581 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6582 | key_bits, alg); |
| 6583 | TEST_ASSERT(signature_size != 0); |
| 6584 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6585 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6586 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6587 | psa_interruptible_set_max_ops(max_ops); |
| 6588 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6589 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6590 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6591 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6592 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6593 | TEST_ASSERT(num_ops_prior == 0); |
| 6594 | |
| 6595 | /* Start performing the signature. */ |
| 6596 | PSA_ASSERT(psa_sign_hash_start(&operation, key, alg, |
| 6597 | input_data->x, input_data->len)); |
| 6598 | |
| 6599 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6600 | TEST_ASSERT(num_ops_prior == 0); |
| 6601 | |
| 6602 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6603 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6604 | status = psa_sign_hash_complete(&operation, signature, signature_size, |
| 6605 | &signature_length); |
| 6606 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6607 | num_completes++; |
| 6608 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6609 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 6610 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6611 | /* We are asserting here that every complete makes progress |
| 6612 | * (completes some ops), which is true of the internal |
| 6613 | * implementation and probably any implementation, however this is |
| 6614 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6615 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6616 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6617 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 6618 | |
| 6619 | /* Ensure calling get_num_ops() twice still returns the same |
| 6620 | * number of ops as previously reported. */ |
| 6621 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6622 | |
| 6623 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6624 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6625 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6626 | |
| 6627 | TEST_ASSERT(status == PSA_SUCCESS); |
| 6628 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6629 | TEST_LE_U(min_completes, num_completes); |
| 6630 | TEST_LE_U(num_completes, max_completes); |
| 6631 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6632 | /* Verify that the signature is what is expected. */ |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 6633 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 6634 | signature, signature_length); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6635 | |
| 6636 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6637 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6638 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6639 | TEST_ASSERT(num_ops == 0); |
| 6640 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6641 | exit: |
| 6642 | |
| 6643 | /* |
| 6644 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6645 | * thus reset them as required. |
| 6646 | */ |
| 6647 | psa_reset_key_attributes(&attributes); |
| 6648 | |
| 6649 | psa_destroy_key(key); |
| 6650 | mbedtls_free(signature); |
| 6651 | PSA_DONE(); |
| 6652 | } |
| 6653 | /* END_CASE */ |
| 6654 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6655 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6656 | void sign_hash_fail(int key_type_arg, data_t *key_data, |
| 6657 | int alg_arg, data_t *input_data, |
| 6658 | int signature_size_arg, int expected_status_arg) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6659 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6660 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6661 | psa_key_type_t key_type = key_type_arg; |
| 6662 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6663 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6664 | psa_status_t actual_status; |
| 6665 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 6666 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 6667 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6668 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6669 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6670 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6671 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6672 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6673 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6674 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6675 | psa_set_key_algorithm(&attributes, alg); |
| 6676 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6677 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6678 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6679 | &key)); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6680 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6681 | actual_status = psa_sign_hash(key, alg, |
| 6682 | input_data->x, input_data->len, |
| 6683 | signature, signature_size, |
| 6684 | &signature_length); |
| 6685 | TEST_EQUAL(actual_status, expected_status); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6686 | /* The value of *signature_length is unspecified on error, but |
| 6687 | * whatever it is, it should be less than signature_size, so that |
| 6688 | * if the caller tries to read *signature_length bytes without |
| 6689 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6690 | TEST_LE_U(signature_length, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6691 | |
| 6692 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6693 | psa_reset_key_attributes(&attributes); |
| 6694 | psa_destroy_key(key); |
| 6695 | mbedtls_free(signature); |
| 6696 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6697 | } |
| 6698 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6699 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6700 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6701 | /** |
| 6702 | * sign_hash_fail_interruptible() test intentions: |
| 6703 | * |
| 6704 | * Note: This test can currently only handle ECDSA. |
| 6705 | * |
| 6706 | * 1. Test that various failure cases for interruptible sign hash fail with the |
| 6707 | * correct error codes, and at the correct point (at start or during |
| 6708 | * complete). |
| 6709 | * |
| 6710 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 6711 | * expected for different max_ops values. |
| 6712 | * |
| 6713 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 6714 | * and that each successful stage completes some ops (this is not mandated by |
| 6715 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 6716 | * |
| 6717 | * 4. Check that calling complete() when start() fails and complete() |
| 6718 | * after completion results in a BAD_STATE error. |
| 6719 | * |
| 6720 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 6721 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6722 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6723 | void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 6724 | int alg_arg, data_t *input_data, |
| 6725 | int signature_size_arg, |
| 6726 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6727 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6728 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6729 | { |
| 6730 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6731 | psa_key_type_t key_type = key_type_arg; |
| 6732 | psa_algorithm_t alg = alg_arg; |
| 6733 | size_t signature_size = signature_size_arg; |
| 6734 | psa_status_t actual_status; |
| 6735 | psa_status_t expected_start_status = expected_start_status_arg; |
| 6736 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 6737 | unsigned char *signature = NULL; |
| 6738 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6739 | uint32_t num_ops = 0; |
| 6740 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6741 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6742 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6743 | size_t min_completes = 0; |
| 6744 | size_t max_completes = 0; |
| 6745 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6746 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6747 | psa_sign_hash_interruptible_operation_t operation = |
| 6748 | psa_sign_hash_interruptible_operation_init(); |
| 6749 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6750 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6751 | |
| 6752 | PSA_ASSERT(psa_crypto_init()); |
| 6753 | |
| 6754 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6755 | psa_set_key_algorithm(&attributes, alg); |
| 6756 | psa_set_key_type(&attributes, key_type); |
| 6757 | |
| 6758 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6759 | &key)); |
| 6760 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6761 | psa_interruptible_set_max_ops(max_ops); |
| 6762 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6763 | interruptible_signverify_get_minmax_completes(max_ops, |
| 6764 | expected_complete_status, |
| 6765 | &min_completes, |
| 6766 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6767 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6768 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6769 | TEST_ASSERT(num_ops_prior == 0); |
| 6770 | |
| 6771 | /* Start performing the signature. */ |
| 6772 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 6773 | input_data->x, input_data->len); |
| 6774 | |
| 6775 | TEST_EQUAL(actual_status, expected_start_status); |
| 6776 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6777 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 6778 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 6779 | * start failed. */ |
| 6780 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6781 | signature_size, |
| 6782 | &signature_length); |
| 6783 | |
| 6784 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 6785 | |
| 6786 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6787 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 6788 | input_data->x, input_data->len); |
| 6789 | |
| 6790 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 6791 | } |
| 6792 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6793 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6794 | TEST_ASSERT(num_ops_prior == 0); |
| 6795 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6796 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6797 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6798 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6799 | signature_size, |
| 6800 | &signature_length); |
| 6801 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6802 | num_completes++; |
| 6803 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6804 | if (actual_status == PSA_SUCCESS || |
| 6805 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6806 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6807 | /* We are asserting here that every complete makes progress |
| 6808 | * (completes some ops), which is true of the internal |
| 6809 | * implementation and probably any implementation, however this is |
| 6810 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6811 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6812 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6813 | num_ops_prior = num_ops; |
| 6814 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6815 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6816 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6817 | TEST_EQUAL(actual_status, expected_complete_status); |
| 6818 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 6819 | /* Check that another complete returns BAD_STATE. */ |
| 6820 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6821 | signature_size, |
| 6822 | &signature_length); |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6823 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 6824 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6825 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6826 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6827 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6828 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6829 | TEST_ASSERT(num_ops == 0); |
| 6830 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6831 | /* The value of *signature_length is unspecified on error, but |
| 6832 | * whatever it is, it should be less than signature_size, so that |
| 6833 | * if the caller tries to read *signature_length bytes without |
| 6834 | * checking the error code then they don't overflow a buffer. */ |
| 6835 | TEST_LE_U(signature_length, signature_size); |
| 6836 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6837 | TEST_LE_U(min_completes, num_completes); |
| 6838 | TEST_LE_U(num_completes, max_completes); |
| 6839 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6840 | exit: |
| 6841 | psa_reset_key_attributes(&attributes); |
| 6842 | psa_destroy_key(key); |
| 6843 | mbedtls_free(signature); |
| 6844 | PSA_DONE(); |
| 6845 | } |
| 6846 | /* END_CASE */ |
| 6847 | |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6848 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6849 | void sign_verify_hash(int key_type_arg, data_t *key_data, |
| 6850 | int alg_arg, data_t *input_data) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6851 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6852 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6853 | psa_key_type_t key_type = key_type_arg; |
| 6854 | psa_algorithm_t alg = alg_arg; |
| 6855 | size_t key_bits; |
| 6856 | unsigned char *signature = NULL; |
| 6857 | size_t signature_size; |
| 6858 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6859 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6860 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6861 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6862 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6863 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 6864 | psa_set_key_algorithm(&attributes, alg); |
| 6865 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6866 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6867 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6868 | &key)); |
| 6869 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6870 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6871 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6872 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6873 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6874 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6875 | key_bits, alg); |
| 6876 | TEST_ASSERT(signature_size != 0); |
| 6877 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6878 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6879 | |
| 6880 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6881 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6882 | input_data->x, input_data->len, |
| 6883 | signature, signature_size, |
| 6884 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6885 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6886 | TEST_LE_U(signature_length, signature_size); |
| 6887 | TEST_ASSERT(signature_length > 0); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6888 | |
| 6889 | /* Use the library to verify that the signature is correct. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6890 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 6891 | input_data->x, input_data->len, |
| 6892 | signature, signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6893 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6894 | if (input_data->len != 0) { |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6895 | /* Flip a bit in the input and verify that the signature is now |
| 6896 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6897 | * because ECDSA may ignore the last few bits of the input. */ |
| 6898 | input_data->x[0] ^= 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6899 | TEST_EQUAL(psa_verify_hash(key, alg, |
| 6900 | input_data->x, input_data->len, |
| 6901 | signature, signature_length), |
| 6902 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6903 | } |
| 6904 | |
| 6905 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6906 | /* |
| 6907 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6908 | * thus reset them as required. |
| 6909 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6910 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6911 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6912 | psa_destroy_key(key); |
| 6913 | mbedtls_free(signature); |
| 6914 | PSA_DONE(); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6915 | } |
| 6916 | /* END_CASE */ |
| 6917 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6918 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6919 | /** |
| 6920 | * sign_verify_hash_interruptible() test intentions: |
| 6921 | * |
| 6922 | * Note: This test can currently only handle ECDSA. |
| 6923 | * |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 6924 | * 1. Test that we can sign an input hash with the given keypair and then |
| 6925 | * afterwards verify that signature. This is currently the only way to test |
| 6926 | * non deterministic ECDSA, but this test can also handle deterministic. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6927 | * |
| 6928 | * 2. Test that after corrupting the hash, the verification detects an invalid |
| 6929 | * signature. |
| 6930 | * |
| 6931 | * 3. Test the number of calls to psa_sign_hash_complete() required are as |
| 6932 | * expected for different max_ops values. |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6933 | * |
| 6934 | * 4. Test that the number of ops done prior to starting signing and after abort |
| 6935 | * is zero and that each successful signing stage completes some ops (this is |
| 6936 | * not mandated by the PSA specification, but is currently the case). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6937 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6938 | 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] | 6939 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6940 | int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6941 | { |
| 6942 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6943 | psa_key_type_t key_type = key_type_arg; |
| 6944 | psa_algorithm_t alg = alg_arg; |
| 6945 | size_t key_bits; |
| 6946 | unsigned char *signature = NULL; |
| 6947 | size_t signature_size; |
| 6948 | size_t signature_length = 0xdeadbeef; |
| 6949 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6950 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6951 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6952 | uint32_t num_ops = 0; |
| 6953 | uint32_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6954 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6955 | size_t min_completes = 0; |
| 6956 | size_t max_completes = 0; |
| 6957 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6958 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 6959 | psa_sign_hash_interruptible_operation_init(); |
| 6960 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 6961 | psa_verify_hash_interruptible_operation_init(); |
| 6962 | |
| 6963 | PSA_ASSERT(psa_crypto_init()); |
| 6964 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6965 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 6966 | PSA_KEY_USAGE_VERIFY_HASH); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6967 | psa_set_key_algorithm(&attributes, alg); |
| 6968 | psa_set_key_type(&attributes, key_type); |
| 6969 | |
| 6970 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6971 | &key)); |
| 6972 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6973 | key_bits = psa_get_key_bits(&attributes); |
| 6974 | |
| 6975 | /* Allocate a buffer which has the size advertised by the |
| 6976 | * library. */ |
| 6977 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6978 | key_bits, alg); |
| 6979 | TEST_ASSERT(signature_size != 0); |
| 6980 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6981 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6982 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6983 | psa_interruptible_set_max_ops(max_ops); |
| 6984 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6985 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6986 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6987 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6988 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); |
| 6989 | TEST_ASSERT(num_ops_prior == 0); |
| 6990 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6991 | /* Start performing the signature. */ |
| 6992 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 6993 | input_data->x, input_data->len)); |
| 6994 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6995 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); |
| 6996 | TEST_ASSERT(num_ops_prior == 0); |
| 6997 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6998 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6999 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7000 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7001 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7002 | signature_size, |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7003 | &signature_length); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7004 | |
| 7005 | num_completes++; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7006 | |
| 7007 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 7008 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7009 | /* We are asserting here that every complete makes progress |
| 7010 | * (completes some ops), which is true of the internal |
| 7011 | * implementation and probably any implementation, however this is |
| 7012 | * not mandated by the PSA specification. */ |
| 7013 | TEST_ASSERT(num_ops > num_ops_prior); |
| 7014 | |
| 7015 | num_ops_prior = num_ops; |
| 7016 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7017 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7018 | |
| 7019 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7020 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7021 | TEST_LE_U(min_completes, num_completes); |
| 7022 | TEST_LE_U(num_completes, max_completes); |
| 7023 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7024 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7025 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7026 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7027 | TEST_ASSERT(num_ops == 0); |
| 7028 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7029 | /* Check that the signature length looks sensible. */ |
| 7030 | TEST_LE_U(signature_length, signature_size); |
| 7031 | TEST_ASSERT(signature_length > 0); |
| 7032 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7033 | num_completes = 0; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7034 | |
| 7035 | /* Start verification. */ |
| 7036 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7037 | input_data->x, input_data->len, |
| 7038 | signature, signature_length)); |
| 7039 | |
| 7040 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7041 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7042 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7043 | |
| 7044 | num_completes++; |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7045 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7046 | |
| 7047 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7048 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7049 | TEST_LE_U(min_completes, num_completes); |
| 7050 | TEST_LE_U(num_completes, max_completes); |
| 7051 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7052 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7053 | |
| 7054 | verify_operation = psa_verify_hash_interruptible_operation_init(); |
| 7055 | |
| 7056 | if (input_data->len != 0) { |
| 7057 | /* Flip a bit in the input and verify that the signature is now |
| 7058 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 7059 | * because ECDSA may ignore the last few bits of the input. */ |
| 7060 | input_data->x[0] ^= 1; |
| 7061 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7062 | /* Start verification. */ |
| 7063 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7064 | input_data->x, input_data->len, |
| 7065 | signature, signature_length)); |
| 7066 | |
| 7067 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7068 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7069 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7070 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7071 | |
| 7072 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7073 | } |
| 7074 | |
| 7075 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7076 | |
| 7077 | exit: |
| 7078 | /* |
| 7079 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7080 | * thus reset them as required. |
| 7081 | */ |
| 7082 | psa_reset_key_attributes(&attributes); |
| 7083 | |
| 7084 | psa_destroy_key(key); |
| 7085 | mbedtls_free(signature); |
| 7086 | PSA_DONE(); |
| 7087 | } |
| 7088 | /* END_CASE */ |
| 7089 | |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7090 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7091 | void verify_hash(int key_type_arg, data_t *key_data, |
| 7092 | int alg_arg, data_t *hash_data, |
| 7093 | data_t *signature_data) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7094 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7095 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7096 | psa_key_type_t key_type = key_type_arg; |
| 7097 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7098 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7099 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7100 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 7101 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7102 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7103 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7104 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7105 | psa_set_key_algorithm(&attributes, alg); |
| 7106 | psa_set_key_type(&attributes, key_type); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7107 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7108 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7109 | &key)); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7110 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7111 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 7112 | hash_data->x, hash_data->len, |
| 7113 | signature_data->x, signature_data->len)); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 7114 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7115 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7116 | psa_reset_key_attributes(&attributes); |
| 7117 | psa_destroy_key(key); |
| 7118 | PSA_DONE(); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7119 | } |
| 7120 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7121 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7122 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7123 | /** |
| 7124 | * verify_hash_interruptible() test intentions: |
| 7125 | * |
| 7126 | * Note: This test can currently only handle ECDSA. |
| 7127 | * |
| 7128 | * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 7129 | * only). Given this test only does verification it can accept public keys as |
| 7130 | * well as private keys / keypairs. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7131 | * |
| 7132 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7133 | * expected for different max_ops values. |
| 7134 | * |
| 7135 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7136 | * and that each successful stage completes some ops (this is not mandated by |
| 7137 | * the PSA specification, but is currently the case). |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7138 | * |
| 7139 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 7140 | * complete() calls does not alter the number of ops returned. |
| 7141 | * |
| 7142 | * 5. Test that after corrupting the hash, the verification detects an invalid |
| 7143 | * signature. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7144 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7145 | void verify_hash_interruptible(int key_type_arg, data_t *key_data, |
| 7146 | int alg_arg, data_t *hash_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7147 | data_t *signature_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7148 | { |
| 7149 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7150 | psa_key_type_t key_type = key_type_arg; |
| 7151 | psa_algorithm_t alg = alg_arg; |
| 7152 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7153 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7154 | uint32_t num_ops = 0; |
| 7155 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7156 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7157 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7158 | size_t min_completes = 0; |
| 7159 | size_t max_completes = 0; |
| 7160 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7161 | psa_verify_hash_interruptible_operation_t operation = |
| 7162 | psa_verify_hash_interruptible_operation_init(); |
| 7163 | |
| 7164 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 7165 | |
| 7166 | PSA_ASSERT(psa_crypto_init()); |
| 7167 | |
| 7168 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7169 | psa_set_key_algorithm(&attributes, alg); |
| 7170 | psa_set_key_type(&attributes, key_type); |
| 7171 | |
| 7172 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7173 | &key)); |
| 7174 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7175 | psa_interruptible_set_max_ops(max_ops); |
| 7176 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7177 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 7178 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7179 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7180 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7181 | |
| 7182 | TEST_ASSERT(num_ops_prior == 0); |
| 7183 | |
| 7184 | /* Start verification. */ |
| 7185 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7186 | hash_data->x, hash_data->len, |
| 7187 | signature_data->x, signature_data->len) |
| 7188 | ); |
| 7189 | |
| 7190 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7191 | |
| 7192 | TEST_ASSERT(num_ops_prior == 0); |
| 7193 | |
| 7194 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7195 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7196 | status = psa_verify_hash_complete(&operation); |
| 7197 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7198 | num_completes++; |
| 7199 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7200 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 7201 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7202 | /* We are asserting here that every complete makes progress |
| 7203 | * (completes some ops), which is true of the internal |
| 7204 | * implementation and probably any implementation, however this is |
| 7205 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7206 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7207 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7208 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 7209 | |
| 7210 | /* Ensure calling get_num_ops() twice still returns the same |
| 7211 | * number of ops as previously reported. */ |
| 7212 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7213 | |
| 7214 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7215 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7216 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7217 | |
| 7218 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7219 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7220 | TEST_LE_U(min_completes, num_completes); |
| 7221 | TEST_LE_U(num_completes, max_completes); |
| 7222 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7223 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7224 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7225 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7226 | TEST_ASSERT(num_ops == 0); |
| 7227 | |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7228 | if (hash_data->len != 0) { |
| 7229 | /* Flip a bit in the hash and verify that the signature is now detected |
| 7230 | * as invalid. Flip a bit at the beginning, not at the end, because |
| 7231 | * ECDSA may ignore the last few bits of the input. */ |
| 7232 | hash_data->x[0] ^= 1; |
| 7233 | |
| 7234 | /* Start verification. */ |
| 7235 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7236 | hash_data->x, hash_data->len, |
| 7237 | signature_data->x, signature_data->len)); |
| 7238 | |
| 7239 | /* Continue performing the signature until complete. */ |
| 7240 | do { |
| 7241 | status = psa_verify_hash_complete(&operation); |
| 7242 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7243 | |
| 7244 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7245 | } |
| 7246 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7247 | exit: |
| 7248 | psa_reset_key_attributes(&attributes); |
| 7249 | psa_destroy_key(key); |
| 7250 | PSA_DONE(); |
| 7251 | } |
| 7252 | /* END_CASE */ |
| 7253 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7254 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7255 | void verify_hash_fail(int key_type_arg, data_t *key_data, |
| 7256 | int alg_arg, data_t *hash_data, |
| 7257 | data_t *signature_data, |
| 7258 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7259 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7260 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7261 | psa_key_type_t key_type = key_type_arg; |
| 7262 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7263 | psa_status_t actual_status; |
| 7264 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7265 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7266 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7267 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7268 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7269 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7270 | psa_set_key_algorithm(&attributes, alg); |
| 7271 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7272 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7273 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7274 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7275 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7276 | actual_status = psa_verify_hash(key, alg, |
| 7277 | hash_data->x, hash_data->len, |
| 7278 | signature_data->x, signature_data->len); |
| 7279 | TEST_EQUAL(actual_status, expected_status); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7280 | |
| 7281 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7282 | psa_reset_key_attributes(&attributes); |
| 7283 | psa_destroy_key(key); |
| 7284 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7285 | } |
| 7286 | /* END_CASE */ |
| 7287 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7288 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7289 | /** |
| 7290 | * verify_hash_fail_interruptible() test intentions: |
| 7291 | * |
| 7292 | * Note: This test can currently only handle ECDSA. |
| 7293 | * |
| 7294 | * 1. Test that various failure cases for interruptible verify hash fail with |
| 7295 | * the correct error codes, and at the correct point (at start or during |
| 7296 | * complete). |
| 7297 | * |
| 7298 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7299 | * expected for different max_ops values. |
| 7300 | * |
| 7301 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7302 | * and that each successful stage completes some ops (this is not mandated by |
| 7303 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7304 | * |
| 7305 | * 4. Check that calling complete() when start() fails and complete() |
| 7306 | * after completion results in a BAD_STATE error. |
| 7307 | * |
| 7308 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 7309 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7310 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7311 | void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 7312 | int alg_arg, data_t *hash_data, |
| 7313 | data_t *signature_data, |
| 7314 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7315 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7316 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7317 | { |
| 7318 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7319 | psa_key_type_t key_type = key_type_arg; |
| 7320 | psa_algorithm_t alg = alg_arg; |
| 7321 | psa_status_t actual_status; |
| 7322 | psa_status_t expected_start_status = expected_start_status_arg; |
| 7323 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 7324 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7325 | uint32_t num_ops = 0; |
| 7326 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7327 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7328 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7329 | size_t min_completes = 0; |
| 7330 | size_t max_completes = 0; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7331 | psa_verify_hash_interruptible_operation_t operation = |
| 7332 | psa_verify_hash_interruptible_operation_init(); |
| 7333 | |
| 7334 | PSA_ASSERT(psa_crypto_init()); |
| 7335 | |
| 7336 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7337 | psa_set_key_algorithm(&attributes, alg); |
| 7338 | psa_set_key_type(&attributes, key_type); |
| 7339 | |
| 7340 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7341 | &key)); |
| 7342 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7343 | psa_interruptible_set_max_ops(max_ops); |
| 7344 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7345 | interruptible_signverify_get_minmax_completes(max_ops, |
| 7346 | expected_complete_status, |
| 7347 | &min_completes, |
| 7348 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7349 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7350 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7351 | TEST_ASSERT(num_ops_prior == 0); |
| 7352 | |
| 7353 | /* Start verification. */ |
| 7354 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7355 | hash_data->x, hash_data->len, |
| 7356 | signature_data->x, |
| 7357 | signature_data->len); |
| 7358 | |
| 7359 | TEST_EQUAL(actual_status, expected_start_status); |
| 7360 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7361 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 7362 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7363 | * start failed. */ |
| 7364 | actual_status = psa_verify_hash_complete(&operation); |
| 7365 | |
| 7366 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7367 | |
| 7368 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7369 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7370 | hash_data->x, hash_data->len, |
| 7371 | signature_data->x, |
| 7372 | signature_data->len); |
| 7373 | |
| 7374 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7375 | } |
| 7376 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7377 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7378 | TEST_ASSERT(num_ops_prior == 0); |
| 7379 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7380 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7381 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7382 | actual_status = psa_verify_hash_complete(&operation); |
| 7383 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7384 | num_completes++; |
| 7385 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7386 | if (actual_status == PSA_SUCCESS || |
| 7387 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7388 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7389 | /* We are asserting here that every complete makes progress |
| 7390 | * (completes some ops), which is true of the internal |
| 7391 | * implementation and probably any implementation, however this is |
| 7392 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7393 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7394 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7395 | num_ops_prior = num_ops; |
| 7396 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7397 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7398 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7399 | TEST_EQUAL(actual_status, expected_complete_status); |
| 7400 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 7401 | /* Check that another complete returns BAD_STATE. */ |
| 7402 | actual_status = psa_verify_hash_complete(&operation); |
| 7403 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7404 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7405 | TEST_LE_U(min_completes, num_completes); |
| 7406 | TEST_LE_U(num_completes, max_completes); |
| 7407 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7408 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7409 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7410 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7411 | TEST_ASSERT(num_ops == 0); |
| 7412 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7413 | exit: |
| 7414 | psa_reset_key_attributes(&attributes); |
| 7415 | psa_destroy_key(key); |
| 7416 | PSA_DONE(); |
| 7417 | } |
| 7418 | /* END_CASE */ |
| 7419 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7420 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7421 | /** |
| 7422 | * interruptible_signverify_hash_state_test() test intentions: |
| 7423 | * |
| 7424 | * Note: This test can currently only handle ECDSA. |
| 7425 | * |
| 7426 | * 1. Test that calling the various interruptible sign and verify hash functions |
| 7427 | * in incorrect orders returns BAD_STATE errors. |
| 7428 | */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7429 | void interruptible_signverify_hash_state_test(int key_type_arg, |
| 7430 | data_t *key_data, int alg_arg, data_t *input_data) |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7431 | { |
| 7432 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7433 | psa_key_type_t key_type = key_type_arg; |
| 7434 | psa_algorithm_t alg = alg_arg; |
| 7435 | size_t key_bits; |
| 7436 | unsigned char *signature = NULL; |
| 7437 | size_t signature_size; |
| 7438 | size_t signature_length = 0xdeadbeef; |
| 7439 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7440 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7441 | psa_sign_hash_interruptible_operation_init(); |
| 7442 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7443 | psa_verify_hash_interruptible_operation_init(); |
| 7444 | |
| 7445 | PSA_ASSERT(psa_crypto_init()); |
| 7446 | |
| 7447 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7448 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7449 | psa_set_key_algorithm(&attributes, alg); |
| 7450 | psa_set_key_type(&attributes, key_type); |
| 7451 | |
| 7452 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7453 | &key)); |
| 7454 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7455 | key_bits = psa_get_key_bits(&attributes); |
| 7456 | |
| 7457 | /* Allocate a buffer which has the size advertised by the |
| 7458 | * library. */ |
| 7459 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7460 | key_bits, alg); |
| 7461 | TEST_ASSERT(signature_size != 0); |
| 7462 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7463 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7464 | |
| 7465 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7466 | |
| 7467 | /* --- Attempt completes prior to starts --- */ |
| 7468 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7469 | signature_size, |
| 7470 | &signature_length), |
| 7471 | PSA_ERROR_BAD_STATE); |
| 7472 | |
| 7473 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7474 | |
| 7475 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7476 | PSA_ERROR_BAD_STATE); |
| 7477 | |
| 7478 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7479 | |
| 7480 | /* --- Aborts in all other places. --- */ |
| 7481 | psa_sign_hash_abort(&sign_operation); |
| 7482 | |
| 7483 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7484 | input_data->x, input_data->len)); |
| 7485 | |
| 7486 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7487 | |
| 7488 | psa_interruptible_set_max_ops(1); |
| 7489 | |
| 7490 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7491 | input_data->x, input_data->len)); |
| 7492 | |
| 7493 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7494 | signature_size, |
| 7495 | &signature_length), |
| 7496 | PSA_OPERATION_INCOMPLETE); |
| 7497 | |
| 7498 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7499 | |
| 7500 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7501 | |
| 7502 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7503 | input_data->x, input_data->len)); |
| 7504 | |
| 7505 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7506 | signature_size, |
| 7507 | &signature_length)); |
| 7508 | |
| 7509 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7510 | |
| 7511 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7512 | |
| 7513 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7514 | input_data->x, input_data->len, |
| 7515 | signature, signature_length)); |
| 7516 | |
| 7517 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7518 | |
| 7519 | psa_interruptible_set_max_ops(1); |
| 7520 | |
| 7521 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7522 | input_data->x, input_data->len, |
| 7523 | signature, signature_length)); |
| 7524 | |
| 7525 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7526 | PSA_OPERATION_INCOMPLETE); |
| 7527 | |
| 7528 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7529 | |
| 7530 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7531 | |
| 7532 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7533 | input_data->x, input_data->len, |
| 7534 | signature, signature_length)); |
| 7535 | |
| 7536 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7537 | |
| 7538 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7539 | |
| 7540 | /* --- Attempt double starts. --- */ |
| 7541 | |
| 7542 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7543 | input_data->x, input_data->len)); |
| 7544 | |
| 7545 | TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg, |
| 7546 | input_data->x, input_data->len), |
| 7547 | PSA_ERROR_BAD_STATE); |
| 7548 | |
| 7549 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7550 | |
| 7551 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7552 | input_data->x, input_data->len, |
| 7553 | signature, signature_length)); |
| 7554 | |
| 7555 | TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg, |
| 7556 | input_data->x, input_data->len, |
| 7557 | signature, signature_length), |
| 7558 | PSA_ERROR_BAD_STATE); |
| 7559 | |
| 7560 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7561 | |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7562 | exit: |
| 7563 | /* |
| 7564 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7565 | * thus reset them as required. |
| 7566 | */ |
| 7567 | psa_reset_key_attributes(&attributes); |
| 7568 | |
| 7569 | psa_destroy_key(key); |
| 7570 | mbedtls_free(signature); |
| 7571 | PSA_DONE(); |
| 7572 | } |
| 7573 | /* END_CASE */ |
| 7574 | |
| 7575 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7576 | /** |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7577 | * interruptible_signverify_hash_edgecase_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7578 | * |
| 7579 | * Note: This test can currently only handle ECDSA. |
| 7580 | * |
| 7581 | * 1. Test various edge cases in the interruptible sign and verify hash |
| 7582 | * interfaces. |
| 7583 | */ |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7584 | void interruptible_signverify_hash_edgecase_tests(int key_type_arg, |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7585 | data_t *key_data, int alg_arg, data_t *input_data) |
| 7586 | { |
| 7587 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7588 | psa_key_type_t key_type = key_type_arg; |
| 7589 | psa_algorithm_t alg = alg_arg; |
| 7590 | size_t key_bits; |
| 7591 | unsigned char *signature = NULL; |
| 7592 | size_t signature_size; |
| 7593 | size_t signature_length = 0xdeadbeef; |
| 7594 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7595 | uint8_t *input_buffer = NULL; |
| 7596 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7597 | psa_sign_hash_interruptible_operation_init(); |
| 7598 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7599 | psa_verify_hash_interruptible_operation_init(); |
| 7600 | |
| 7601 | PSA_ASSERT(psa_crypto_init()); |
| 7602 | |
| 7603 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7604 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7605 | psa_set_key_algorithm(&attributes, alg); |
| 7606 | psa_set_key_type(&attributes, key_type); |
| 7607 | |
| 7608 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7609 | &key)); |
| 7610 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7611 | key_bits = psa_get_key_bits(&attributes); |
| 7612 | |
| 7613 | /* Allocate a buffer which has the size advertised by the |
| 7614 | * library. */ |
| 7615 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7616 | key_bits, alg); |
| 7617 | TEST_ASSERT(signature_size != 0); |
| 7618 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7619 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7620 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7621 | /* --- Change function inputs mid run, to cause an error (sign only, |
| 7622 | * verify passes all inputs to start. --- */ |
| 7623 | |
| 7624 | psa_interruptible_set_max_ops(1); |
| 7625 | |
| 7626 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7627 | input_data->x, input_data->len)); |
| 7628 | |
| 7629 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7630 | signature_size, |
| 7631 | &signature_length), |
| 7632 | PSA_OPERATION_INCOMPLETE); |
| 7633 | |
| 7634 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7635 | 0, |
| 7636 | &signature_length), |
| 7637 | PSA_ERROR_BUFFER_TOO_SMALL); |
| 7638 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7639 | /* And test that this invalidates the operation. */ |
| 7640 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7641 | 0, |
| 7642 | &signature_length), |
| 7643 | PSA_ERROR_BAD_STATE); |
| 7644 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7645 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7646 | |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7647 | /* Trash the hash buffer in between start and complete, to ensure |
| 7648 | * no reliance on external buffers. */ |
| 7649 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7650 | |
Paul Elliott | 6c68df4 | 2023-10-23 15:33:37 +0100 | [diff] [blame] | 7651 | TEST_CALLOC(input_buffer, input_data->len); |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7652 | |
| 7653 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7654 | |
| 7655 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7656 | input_buffer, input_data->len)); |
| 7657 | |
| 7658 | memset(input_buffer, '!', input_data->len); |
| 7659 | mbedtls_free(input_buffer); |
| 7660 | input_buffer = NULL; |
| 7661 | |
| 7662 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7663 | signature_size, |
| 7664 | &signature_length)); |
| 7665 | |
| 7666 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7667 | |
Paul Elliott | 6c68df4 | 2023-10-23 15:33:37 +0100 | [diff] [blame] | 7668 | TEST_CALLOC(input_buffer, input_data->len); |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7669 | |
| 7670 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7671 | |
| 7672 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7673 | input_buffer, input_data->len, |
| 7674 | signature, signature_length)); |
| 7675 | |
| 7676 | memset(input_buffer, '!', input_data->len); |
| 7677 | mbedtls_free(input_buffer); |
| 7678 | input_buffer = NULL; |
| 7679 | |
| 7680 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7681 | |
| 7682 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7683 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7684 | exit: |
| 7685 | /* |
| 7686 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7687 | * thus reset them as required. |
| 7688 | */ |
| 7689 | psa_reset_key_attributes(&attributes); |
| 7690 | |
| 7691 | psa_destroy_key(key); |
| 7692 | mbedtls_free(signature); |
Paul Elliott | 6c68df4 | 2023-10-23 15:33:37 +0100 | [diff] [blame] | 7693 | mbedtls_free(input_buffer); |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7694 | PSA_DONE(); |
| 7695 | } |
| 7696 | /* END_CASE */ |
| 7697 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7698 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7699 | /** |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 7700 | * interruptible_signverify_hash_ops_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7701 | * |
| 7702 | * Note: This test can currently only handle ECDSA. |
| 7703 | * |
| 7704 | * 1. Test that setting max ops is reflected in both interruptible sign and |
| 7705 | * verify hash |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7706 | * 2. Test that changing the value of max_ops to unlimited during an operation |
| 7707 | * causes that operation to complete in the next call. |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7708 | * |
| 7709 | * 3. Test that calling get_num_ops() between complete calls gives the same |
| 7710 | * 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] | 7711 | */ |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 7712 | void interruptible_signverify_hash_ops_tests(int key_type_arg, |
| 7713 | data_t *key_data, int alg_arg, |
| 7714 | data_t *input_data) |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7715 | { |
| 7716 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7717 | psa_key_type_t key_type = key_type_arg; |
| 7718 | psa_algorithm_t alg = alg_arg; |
| 7719 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7720 | size_t key_bits; |
| 7721 | unsigned char *signature = NULL; |
| 7722 | size_t signature_size; |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7723 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7724 | uint32_t num_ops = 0; |
| 7725 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 7726 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7727 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7728 | psa_sign_hash_interruptible_operation_init(); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7729 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7730 | psa_verify_hash_interruptible_operation_init(); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7731 | |
| 7732 | PSA_ASSERT(psa_crypto_init()); |
| 7733 | |
| 7734 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7735 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7736 | psa_set_key_algorithm(&attributes, alg); |
| 7737 | psa_set_key_type(&attributes, key_type); |
| 7738 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7739 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key)); |
| 7740 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7741 | key_bits = psa_get_key_bits(&attributes); |
| 7742 | |
| 7743 | /* Allocate a buffer which has the size advertised by the |
| 7744 | * library. */ |
| 7745 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7746 | |
| 7747 | TEST_ASSERT(signature_size != 0); |
| 7748 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7749 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7750 | |
| 7751 | /* Check that default max ops gets set if we don't set it. */ |
| 7752 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7753 | input_data->x, input_data->len)); |
| 7754 | |
| 7755 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 7756 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7757 | |
| 7758 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7759 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7760 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7761 | input_data->x, input_data->len, |
| 7762 | signature, signature_size)); |
| 7763 | |
| 7764 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 7765 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7766 | |
| 7767 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7768 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7769 | /* Check that max ops gets set properly. */ |
| 7770 | |
| 7771 | psa_interruptible_set_max_ops(0xbeef); |
| 7772 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7773 | TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7774 | |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7775 | /* --- Ensure changing the max ops mid operation works (operation should |
| 7776 | * complete successfully after setting max ops to unlimited --- */ |
| 7777 | psa_interruptible_set_max_ops(1); |
| 7778 | |
| 7779 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7780 | input_data->x, input_data->len)); |
| 7781 | |
| 7782 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7783 | signature_size, |
| 7784 | &signature_length), |
| 7785 | PSA_OPERATION_INCOMPLETE); |
| 7786 | |
| 7787 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7788 | |
| 7789 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7790 | signature_size, |
| 7791 | &signature_length)); |
| 7792 | |
| 7793 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7794 | |
| 7795 | psa_interruptible_set_max_ops(1); |
| 7796 | |
| 7797 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7798 | input_data->x, input_data->len, |
| 7799 | signature, signature_length)); |
| 7800 | |
| 7801 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7802 | PSA_OPERATION_INCOMPLETE); |
| 7803 | |
| 7804 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7805 | |
| 7806 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7807 | |
| 7808 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7809 | |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7810 | /* --- Test that not calling get_num_ops inbetween complete calls does not |
| 7811 | * result in lost ops. ---*/ |
| 7812 | |
| 7813 | psa_interruptible_set_max_ops(1); |
| 7814 | |
| 7815 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7816 | input_data->x, input_data->len)); |
| 7817 | |
| 7818 | /* Continue performing the signature until complete. */ |
| 7819 | do { |
| 7820 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7821 | signature_size, |
| 7822 | &signature_length); |
| 7823 | |
| 7824 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7825 | |
| 7826 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7827 | |
| 7828 | PSA_ASSERT(status); |
| 7829 | |
| 7830 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7831 | |
| 7832 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7833 | input_data->x, input_data->len)); |
| 7834 | |
| 7835 | /* Continue performing the signature until complete. */ |
| 7836 | do { |
| 7837 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7838 | signature_size, |
| 7839 | &signature_length); |
| 7840 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7841 | |
| 7842 | PSA_ASSERT(status); |
| 7843 | |
| 7844 | TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation)); |
| 7845 | |
| 7846 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7847 | |
| 7848 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7849 | input_data->x, input_data->len, |
| 7850 | signature, signature_length)); |
| 7851 | |
| 7852 | /* Continue performing the verification until complete. */ |
| 7853 | do { |
| 7854 | status = psa_verify_hash_complete(&verify_operation); |
| 7855 | |
| 7856 | num_ops = psa_verify_hash_get_num_ops(&verify_operation); |
| 7857 | |
| 7858 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7859 | |
| 7860 | PSA_ASSERT(status); |
| 7861 | |
| 7862 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7863 | |
| 7864 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7865 | input_data->x, input_data->len, |
| 7866 | signature, signature_length)); |
| 7867 | |
| 7868 | /* Continue performing the verification until complete. */ |
| 7869 | do { |
| 7870 | status = psa_verify_hash_complete(&verify_operation); |
| 7871 | |
| 7872 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7873 | |
| 7874 | PSA_ASSERT(status); |
| 7875 | |
| 7876 | TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation)); |
| 7877 | |
| 7878 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7879 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7880 | exit: |
| 7881 | /* |
| 7882 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7883 | * thus reset them as required. |
| 7884 | */ |
| 7885 | psa_reset_key_attributes(&attributes); |
| 7886 | |
| 7887 | psa_destroy_key(key); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7888 | mbedtls_free(signature); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7889 | PSA_DONE(); |
| 7890 | } |
| 7891 | /* END_CASE */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7892 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7893 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7894 | void sign_message_deterministic(int key_type_arg, |
| 7895 | data_t *key_data, |
| 7896 | int alg_arg, |
| 7897 | data_t *input_data, |
| 7898 | data_t *output_data) |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7899 | { |
| 7900 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7901 | psa_key_type_t key_type = key_type_arg; |
| 7902 | psa_algorithm_t alg = alg_arg; |
| 7903 | size_t key_bits; |
| 7904 | unsigned char *signature = NULL; |
| 7905 | size_t signature_size; |
| 7906 | size_t signature_length = 0xdeadbeef; |
| 7907 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7908 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7909 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7910 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7911 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 7912 | psa_set_key_algorithm(&attributes, alg); |
| 7913 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7914 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7915 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7916 | &key)); |
| 7917 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7918 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7919 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7920 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7921 | TEST_ASSERT(signature_size != 0); |
| 7922 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7923 | TEST_CALLOC(signature, signature_size); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7924 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7925 | PSA_ASSERT(psa_sign_message(key, alg, |
| 7926 | input_data->x, input_data->len, |
| 7927 | signature, signature_size, |
| 7928 | &signature_length)); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7929 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 7930 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 7931 | signature, signature_length); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7932 | |
| 7933 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7934 | psa_reset_key_attributes(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7935 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7936 | psa_destroy_key(key); |
| 7937 | mbedtls_free(signature); |
| 7938 | PSA_DONE(); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7939 | |
| 7940 | } |
| 7941 | /* END_CASE */ |
| 7942 | |
| 7943 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7944 | void sign_message_fail(int key_type_arg, |
| 7945 | data_t *key_data, |
| 7946 | int alg_arg, |
| 7947 | data_t *input_data, |
| 7948 | int signature_size_arg, |
| 7949 | int expected_status_arg) |
| 7950 | { |
| 7951 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7952 | psa_key_type_t key_type = key_type_arg; |
| 7953 | psa_algorithm_t alg = alg_arg; |
| 7954 | size_t signature_size = signature_size_arg; |
| 7955 | psa_status_t actual_status; |
| 7956 | psa_status_t expected_status = expected_status_arg; |
| 7957 | unsigned char *signature = NULL; |
| 7958 | size_t signature_length = 0xdeadbeef; |
| 7959 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7960 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7961 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7962 | |
| 7963 | PSA_ASSERT(psa_crypto_init()); |
| 7964 | |
| 7965 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 7966 | psa_set_key_algorithm(&attributes, alg); |
| 7967 | psa_set_key_type(&attributes, key_type); |
| 7968 | |
| 7969 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7970 | &key)); |
| 7971 | |
| 7972 | actual_status = psa_sign_message(key, alg, |
| 7973 | input_data->x, input_data->len, |
| 7974 | signature, signature_size, |
| 7975 | &signature_length); |
| 7976 | TEST_EQUAL(actual_status, expected_status); |
| 7977 | /* The value of *signature_length is unspecified on error, but |
| 7978 | * whatever it is, it should be less than signature_size, so that |
| 7979 | * if the caller tries to read *signature_length bytes without |
| 7980 | * checking the error code then they don't overflow a buffer. */ |
| 7981 | TEST_LE_U(signature_length, signature_size); |
| 7982 | |
| 7983 | exit: |
| 7984 | psa_reset_key_attributes(&attributes); |
| 7985 | psa_destroy_key(key); |
| 7986 | mbedtls_free(signature); |
| 7987 | PSA_DONE(); |
| 7988 | } |
| 7989 | /* END_CASE */ |
| 7990 | |
| 7991 | /* BEGIN_CASE */ |
| 7992 | void sign_verify_message(int key_type_arg, |
| 7993 | data_t *key_data, |
| 7994 | int alg_arg, |
| 7995 | data_t *input_data) |
| 7996 | { |
| 7997 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7998 | psa_key_type_t key_type = key_type_arg; |
| 7999 | psa_algorithm_t alg = alg_arg; |
| 8000 | size_t key_bits; |
| 8001 | unsigned char *signature = NULL; |
| 8002 | size_t signature_size; |
| 8003 | size_t signature_length = 0xdeadbeef; |
| 8004 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8005 | |
| 8006 | PSA_ASSERT(psa_crypto_init()); |
| 8007 | |
| 8008 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 8009 | PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8010 | psa_set_key_algorithm(&attributes, alg); |
| 8011 | psa_set_key_type(&attributes, key_type); |
| 8012 | |
| 8013 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8014 | &key)); |
| 8015 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8016 | key_bits = psa_get_key_bits(&attributes); |
| 8017 | |
| 8018 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8019 | TEST_ASSERT(signature_size != 0); |
| 8020 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8021 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8022 | |
| 8023 | PSA_ASSERT(psa_sign_message(key, alg, |
| 8024 | input_data->x, input_data->len, |
| 8025 | signature, signature_size, |
| 8026 | &signature_length)); |
| 8027 | TEST_LE_U(signature_length, signature_size); |
| 8028 | TEST_ASSERT(signature_length > 0); |
| 8029 | |
| 8030 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8031 | input_data->x, input_data->len, |
| 8032 | signature, signature_length)); |
| 8033 | |
| 8034 | if (input_data->len != 0) { |
| 8035 | /* Flip a bit in the input and verify that the signature is now |
| 8036 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 8037 | * because ECDSA may ignore the last few bits of the input. */ |
| 8038 | input_data->x[0] ^= 1; |
| 8039 | TEST_EQUAL(psa_verify_message(key, alg, |
| 8040 | input_data->x, input_data->len, |
| 8041 | signature, signature_length), |
| 8042 | PSA_ERROR_INVALID_SIGNATURE); |
| 8043 | } |
| 8044 | |
| 8045 | exit: |
| 8046 | psa_reset_key_attributes(&attributes); |
| 8047 | |
| 8048 | psa_destroy_key(key); |
| 8049 | mbedtls_free(signature); |
| 8050 | PSA_DONE(); |
| 8051 | } |
| 8052 | /* END_CASE */ |
| 8053 | |
| 8054 | /* BEGIN_CASE */ |
| 8055 | void verify_message(int key_type_arg, |
| 8056 | data_t *key_data, |
| 8057 | int alg_arg, |
| 8058 | data_t *input_data, |
| 8059 | data_t *signature_data) |
| 8060 | { |
| 8061 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8062 | psa_key_type_t key_type = key_type_arg; |
| 8063 | psa_algorithm_t alg = alg_arg; |
| 8064 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8065 | |
| 8066 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 8067 | |
| 8068 | PSA_ASSERT(psa_crypto_init()); |
| 8069 | |
| 8070 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8071 | psa_set_key_algorithm(&attributes, alg); |
| 8072 | psa_set_key_type(&attributes, key_type); |
| 8073 | |
| 8074 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8075 | &key)); |
| 8076 | |
| 8077 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8078 | input_data->x, input_data->len, |
| 8079 | signature_data->x, signature_data->len)); |
| 8080 | |
| 8081 | exit: |
| 8082 | psa_reset_key_attributes(&attributes); |
| 8083 | psa_destroy_key(key); |
| 8084 | PSA_DONE(); |
| 8085 | } |
| 8086 | /* END_CASE */ |
| 8087 | |
| 8088 | /* BEGIN_CASE */ |
| 8089 | void verify_message_fail(int key_type_arg, |
| 8090 | data_t *key_data, |
| 8091 | int alg_arg, |
| 8092 | data_t *hash_data, |
| 8093 | data_t *signature_data, |
| 8094 | int expected_status_arg) |
| 8095 | { |
| 8096 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8097 | psa_key_type_t key_type = key_type_arg; |
| 8098 | psa_algorithm_t alg = alg_arg; |
| 8099 | psa_status_t actual_status; |
| 8100 | psa_status_t expected_status = expected_status_arg; |
| 8101 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8102 | |
| 8103 | PSA_ASSERT(psa_crypto_init()); |
| 8104 | |
| 8105 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8106 | psa_set_key_algorithm(&attributes, alg); |
| 8107 | psa_set_key_type(&attributes, key_type); |
| 8108 | |
| 8109 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8110 | &key)); |
| 8111 | |
| 8112 | actual_status = psa_verify_message(key, alg, |
| 8113 | hash_data->x, hash_data->len, |
| 8114 | signature_data->x, |
| 8115 | signature_data->len); |
| 8116 | TEST_EQUAL(actual_status, expected_status); |
| 8117 | |
| 8118 | exit: |
| 8119 | psa_reset_key_attributes(&attributes); |
| 8120 | psa_destroy_key(key); |
| 8121 | PSA_DONE(); |
| 8122 | } |
| 8123 | /* END_CASE */ |
| 8124 | |
| 8125 | /* BEGIN_CASE */ |
| 8126 | void asymmetric_encrypt(int key_type_arg, |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8127 | data_t *key_data, |
| 8128 | int alg_arg, |
| 8129 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8130 | data_t *label, |
| 8131 | int expected_output_length_arg, |
| 8132 | int expected_status_arg) |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8133 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8134 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8135 | psa_key_type_t key_type = key_type_arg; |
| 8136 | psa_algorithm_t alg = alg_arg; |
| 8137 | size_t expected_output_length = expected_output_length_arg; |
| 8138 | size_t key_bits; |
| 8139 | unsigned char *output = NULL; |
| 8140 | size_t output_size; |
| 8141 | size_t output_length = ~0; |
| 8142 | psa_status_t actual_status; |
| 8143 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8144 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8145 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8146 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 8147 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8148 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8149 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 8150 | psa_set_key_algorithm(&attributes, alg); |
| 8151 | psa_set_key_type(&attributes, key_type); |
| 8152 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8153 | &key)); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8154 | |
| 8155 | /* Determine the maximum output length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8156 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8157 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8159 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8160 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8161 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8162 | |
| 8163 | /* Encrypt the input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8164 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8165 | input_data->x, input_data->len, |
| 8166 | label->x, label->len, |
| 8167 | output, output_size, |
| 8168 | &output_length); |
| 8169 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8170 | if (actual_status == PSA_SUCCESS) { |
| 8171 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8172 | } else { |
| 8173 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8174 | } |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8175 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8176 | /* If the label is empty, the test framework puts a non-null pointer |
| 8177 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8178 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8179 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8180 | if (output_size != 0) { |
| 8181 | memset(output, 0, output_size); |
| 8182 | } |
| 8183 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8184 | input_data->x, input_data->len, |
| 8185 | NULL, label->len, |
| 8186 | output, output_size, |
| 8187 | &output_length); |
| 8188 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8189 | if (actual_status == PSA_SUCCESS) { |
| 8190 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8191 | } else { |
| 8192 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8193 | } |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8194 | } |
| 8195 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8196 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8197 | /* |
| 8198 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8199 | * thus reset them as required. |
| 8200 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8201 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8202 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8203 | psa_destroy_key(key); |
| 8204 | mbedtls_free(output); |
| 8205 | PSA_DONE(); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8206 | } |
| 8207 | /* END_CASE */ |
| 8208 | |
| 8209 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8210 | void asymmetric_encrypt_decrypt(int key_type_arg, |
| 8211 | data_t *key_data, |
| 8212 | int alg_arg, |
| 8213 | data_t *input_data, |
| 8214 | data_t *label) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8215 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8216 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8217 | psa_key_type_t key_type = key_type_arg; |
| 8218 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8219 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8220 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8221 | size_t output_size; |
| 8222 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8223 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8224 | size_t output2_size; |
| 8225 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8226 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8227 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8228 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8230 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 8231 | psa_set_key_algorithm(&attributes, alg); |
| 8232 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8233 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8234 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8235 | &key)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8236 | |
| 8237 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8238 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8239 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8240 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8241 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8242 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8243 | TEST_CALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8244 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8245 | output2_size = input_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8246 | TEST_LE_U(output2_size, |
| 8247 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)); |
| 8248 | TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8249 | TEST_CALLOC(output2, output2_size); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8250 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 8251 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 8252 | * the original plaintext because of the non-optional random |
| 8253 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8254 | PSA_ASSERT(psa_asymmetric_encrypt(key, alg, |
| 8255 | input_data->x, input_data->len, |
| 8256 | label->x, label->len, |
| 8257 | output, output_size, |
| 8258 | &output_length)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8259 | /* We don't know what ciphertext length to expect, but check that |
| 8260 | * it looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8261 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8263 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8264 | output, output_length, |
| 8265 | label->x, label->len, |
| 8266 | output2, output2_size, |
| 8267 | &output2_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8268 | TEST_MEMORY_COMPARE(input_data->x, input_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8269 | output2, output2_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8270 | |
| 8271 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8272 | /* |
| 8273 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8274 | * thus reset them as required. |
| 8275 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8276 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8277 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8278 | psa_destroy_key(key); |
| 8279 | mbedtls_free(output); |
| 8280 | mbedtls_free(output2); |
| 8281 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8282 | } |
| 8283 | /* END_CASE */ |
| 8284 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8285 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8286 | void asymmetric_decrypt(int key_type_arg, |
| 8287 | data_t *key_data, |
| 8288 | int alg_arg, |
| 8289 | data_t *input_data, |
| 8290 | data_t *label, |
| 8291 | data_t *expected_data) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8292 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8293 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8294 | psa_key_type_t key_type = key_type_arg; |
| 8295 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8296 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8297 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 8298 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8299 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8300 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8301 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8302 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8303 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8304 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8305 | psa_set_key_algorithm(&attributes, alg); |
| 8306 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8307 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8308 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8309 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8310 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8311 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8312 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8313 | |
| 8314 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8315 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8316 | TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8317 | TEST_CALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8318 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8319 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8320 | input_data->x, input_data->len, |
| 8321 | label->x, label->len, |
| 8322 | output, |
| 8323 | output_size, |
| 8324 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8325 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8326 | output, output_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8327 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8328 | /* If the label is empty, the test framework puts a non-null pointer |
| 8329 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8330 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8331 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8332 | if (output_size != 0) { |
| 8333 | memset(output, 0, output_size); |
| 8334 | } |
| 8335 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8336 | input_data->x, input_data->len, |
| 8337 | NULL, label->len, |
| 8338 | output, |
| 8339 | output_size, |
| 8340 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8341 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8342 | output, output_length); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8343 | } |
| 8344 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8345 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8346 | psa_reset_key_attributes(&attributes); |
| 8347 | psa_destroy_key(key); |
| 8348 | mbedtls_free(output); |
| 8349 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8350 | } |
| 8351 | /* END_CASE */ |
| 8352 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8353 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8354 | void asymmetric_decrypt_fail(int key_type_arg, |
| 8355 | data_t *key_data, |
| 8356 | int alg_arg, |
| 8357 | data_t *input_data, |
| 8358 | data_t *label, |
| 8359 | int output_size_arg, |
| 8360 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8361 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8362 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8363 | psa_key_type_t key_type = key_type_arg; |
| 8364 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8365 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 8366 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8367 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8368 | psa_status_t actual_status; |
| 8369 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8370 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8371 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8372 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8374 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8375 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8376 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8377 | psa_set_key_algorithm(&attributes, alg); |
| 8378 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8379 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8380 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8381 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8382 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8383 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8384 | input_data->x, input_data->len, |
| 8385 | label->x, label->len, |
| 8386 | output, output_size, |
| 8387 | &output_length); |
| 8388 | TEST_EQUAL(actual_status, expected_status); |
| 8389 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8390 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8391 | /* If the label is empty, the test framework puts a non-null pointer |
| 8392 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8393 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8394 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8395 | if (output_size != 0) { |
| 8396 | memset(output, 0, output_size); |
| 8397 | } |
| 8398 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8399 | input_data->x, input_data->len, |
| 8400 | NULL, label->len, |
| 8401 | output, output_size, |
| 8402 | &output_length); |
| 8403 | TEST_EQUAL(actual_status, expected_status); |
| 8404 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8405 | } |
| 8406 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8407 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8408 | psa_reset_key_attributes(&attributes); |
| 8409 | psa_destroy_key(key); |
| 8410 | mbedtls_free(output); |
| 8411 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8412 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8413 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8414 | |
| 8415 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8416 | void key_derivation_init() |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8417 | { |
| 8418 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 8419 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 8420 | * 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] | 8421 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8422 | size_t capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8423 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init(); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 8424 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8425 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8426 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8427 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8428 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8429 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8430 | TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity), |
| 8431 | PSA_ERROR_BAD_STATE); |
| 8432 | TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity), |
| 8433 | PSA_ERROR_BAD_STATE); |
| 8434 | TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity), |
| 8435 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8436 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8437 | /* A default operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8438 | PSA_ASSERT(psa_key_derivation_abort(&func)); |
| 8439 | PSA_ASSERT(psa_key_derivation_abort(&init)); |
| 8440 | PSA_ASSERT(psa_key_derivation_abort(&zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8441 | } |
| 8442 | /* END_CASE */ |
| 8443 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 8444 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8445 | void derive_setup(int alg_arg, int expected_status_arg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8446 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8447 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8448 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8449 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8450 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8451 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8452 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8453 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8454 | expected_status); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8455 | |
| 8456 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8457 | psa_key_derivation_abort(&operation); |
| 8458 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8459 | } |
| 8460 | /* END_CASE */ |
| 8461 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8462 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8463 | void derive_set_capacity(int alg_arg, int capacity_arg, |
| 8464 | int expected_status_arg) |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8465 | { |
| 8466 | psa_algorithm_t alg = alg_arg; |
| 8467 | size_t capacity = capacity_arg; |
| 8468 | psa_status_t expected_status = expected_status_arg; |
| 8469 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8470 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8471 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8472 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8473 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8474 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8475 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 8476 | expected_status); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8477 | |
| 8478 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8479 | psa_key_derivation_abort(&operation); |
| 8480 | PSA_DONE(); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8481 | } |
| 8482 | /* END_CASE */ |
| 8483 | |
| 8484 | /* BEGIN_CASE */ |
Kusumit Ghoderao | d60dfc0 | 2023-05-01 17:39:27 +0530 | [diff] [blame] | 8485 | void parse_binary_string_test(data_t *input, int output) |
| 8486 | { |
| 8487 | uint64_t value; |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 8488 | value = mbedtls_test_parse_binary_string(input); |
Kusumit Ghoderao | d60dfc0 | 2023-05-01 17:39:27 +0530 | [diff] [blame] | 8489 | TEST_EQUAL(value, output); |
| 8490 | } |
| 8491 | /* END_CASE */ |
| 8492 | |
| 8493 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8494 | void derive_input(int alg_arg, |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8495 | int step_arg1, int key_type_arg1, data_t *input1, |
| 8496 | int expected_status_arg1, |
| 8497 | int step_arg2, int key_type_arg2, data_t *input2, |
| 8498 | int expected_status_arg2, |
| 8499 | int step_arg3, int key_type_arg3, data_t *input3, |
| 8500 | int expected_status_arg3, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8501 | int output_key_type_arg, int expected_output_status_arg) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8502 | { |
| 8503 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8504 | psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 }; |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8505 | 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] | 8506 | psa_status_t expected_statuses[] = { expected_status_arg1, |
| 8507 | expected_status_arg2, |
| 8508 | expected_status_arg3 }; |
| 8509 | data_t *inputs[] = { input1, input2, input3 }; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8510 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8511 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8512 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8513 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8514 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8515 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8516 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8517 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8518 | psa_status_t expected_output_status = expected_output_status_arg; |
| 8519 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8520 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8521 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8522 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8523 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8524 | psa_set_key_algorithm(&attributes, alg); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8525 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8526 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8527 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8528 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8529 | mbedtls_test_set_step(i); |
| 8530 | if (steps[i] == 0) { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 8531 | /* Skip this step */ |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8532 | } else if (((psa_key_type_t) key_types[i]) != PSA_KEY_TYPE_NONE && |
| 8533 | key_types[i] != INPUT_INTEGER) { |
| 8534 | psa_set_key_type(&attributes, ((psa_key_type_t) key_types[i])); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8535 | PSA_ASSERT(psa_import_key(&attributes, |
| 8536 | inputs[i]->x, inputs[i]->len, |
| 8537 | &keys[i])); |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8538 | 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] | 8539 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8540 | // When taking a private key as secret input, use key agreement |
| 8541 | // to add the shared secret to the derivation |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8542 | TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self( |
| 8543 | &operation, keys[i]), |
| 8544 | expected_statuses[i]); |
| 8545 | } else { |
| 8546 | TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i], |
| 8547 | keys[i]), |
| 8548 | expected_statuses[i]); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8549 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8550 | } else { |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8551 | if (key_types[i] == INPUT_INTEGER) { |
| 8552 | TEST_EQUAL(psa_key_derivation_input_integer( |
| 8553 | &operation, steps[i], |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 8554 | mbedtls_test_parse_binary_string(inputs[i])), |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8555 | expected_statuses[i]); |
| 8556 | } else { |
Kusumit Ghoderao | 02326d5 | 2023-04-06 17:47:59 +0530 | [diff] [blame] | 8557 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8558 | &operation, steps[i], |
| 8559 | inputs[i]->x, inputs[i]->len), |
| 8560 | expected_statuses[i]); |
Kusumit Ghoderao | 02326d5 | 2023-04-06 17:47:59 +0530 | [diff] [blame] | 8561 | } |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8562 | } |
| 8563 | } |
| 8564 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8565 | if (output_key_type != PSA_KEY_TYPE_NONE) { |
| 8566 | psa_reset_key_attributes(&attributes); |
| 8567 | psa_set_key_type(&attributes, output_key_type); |
| 8568 | psa_set_key_bits(&attributes, 8); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8569 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8570 | psa_key_derivation_output_key(&attributes, &operation, |
| 8571 | &output_key); |
| 8572 | } else { |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8573 | uint8_t buffer[1]; |
| 8574 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8575 | psa_key_derivation_output_bytes(&operation, |
| 8576 | buffer, sizeof(buffer)); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8577 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8578 | TEST_EQUAL(actual_output_status, expected_output_status); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8579 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8580 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8581 | psa_key_derivation_abort(&operation); |
| 8582 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8583 | psa_destroy_key(keys[i]); |
| 8584 | } |
| 8585 | psa_destroy_key(output_key); |
| 8586 | PSA_DONE(); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8587 | } |
| 8588 | /* END_CASE */ |
| 8589 | |
Kusumit Ghoderao | 42b02b9 | 2023-06-06 16:48:46 +0530 | [diff] [blame] | 8590 | /* BEGIN_CASE*/ |
| 8591 | void derive_input_invalid_cost(int alg_arg, int64_t cost) |
| 8592 | { |
| 8593 | psa_algorithm_t alg = alg_arg; |
| 8594 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8595 | |
| 8596 | PSA_ASSERT(psa_crypto_init()); |
| 8597 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8598 | |
| 8599 | TEST_EQUAL(psa_key_derivation_input_integer(&operation, |
| 8600 | PSA_KEY_DERIVATION_INPUT_COST, |
| 8601 | cost), |
| 8602 | PSA_ERROR_NOT_SUPPORTED); |
| 8603 | |
| 8604 | exit: |
| 8605 | psa_key_derivation_abort(&operation); |
| 8606 | PSA_DONE(); |
| 8607 | } |
| 8608 | /* END_CASE*/ |
| 8609 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8610 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8611 | void derive_over_capacity(int alg_arg) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8612 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8613 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8614 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 8615 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8616 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8617 | unsigned char input1[] = "Input 1"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8618 | size_t input1_length = sizeof(input1); |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8619 | unsigned char input2[] = "Input 2"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8620 | size_t input2_length = sizeof(input2); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8621 | uint8_t buffer[42]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8622 | size_t capacity = sizeof(buffer); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 8623 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 8624 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8625 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8626 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8627 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8628 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8629 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8630 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8631 | psa_set_key_algorithm(&attributes, alg); |
| 8632 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8633 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8634 | PSA_ASSERT(psa_import_key(&attributes, |
| 8635 | key_data, sizeof(key_data), |
| 8636 | &key)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8637 | |
| 8638 | /* valid key derivation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8639 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8640 | input1, input1_length, |
| 8641 | input2, input2_length, |
| 8642 | capacity)) { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8643 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8644 | } |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8645 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8646 | /* state of operation shouldn't allow additional generation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8647 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8648 | PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8649 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8650 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8651 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8652 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity), |
| 8653 | PSA_ERROR_INSUFFICIENT_DATA); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8654 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8655 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8656 | psa_key_derivation_abort(&operation); |
| 8657 | psa_destroy_key(key); |
| 8658 | PSA_DONE(); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8659 | } |
| 8660 | /* END_CASE */ |
| 8661 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8662 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8663 | void derive_actions_without_setup() |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8664 | { |
| 8665 | uint8_t output_buffer[16]; |
| 8666 | size_t buffer_size = 16; |
| 8667 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8668 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8669 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8670 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8671 | output_buffer, buffer_size) |
| 8672 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8673 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8674 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8675 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8676 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8677 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8678 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8679 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8680 | output_buffer, buffer_size) |
| 8681 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8682 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8683 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8684 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8685 | |
| 8686 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8687 | psa_key_derivation_abort(&operation); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8688 | } |
| 8689 | /* END_CASE */ |
| 8690 | |
| 8691 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8692 | void derive_output(int alg_arg, |
| 8693 | int step1_arg, data_t *input1, int expected_status_arg1, |
| 8694 | int step2_arg, data_t *input2, int expected_status_arg2, |
| 8695 | int step3_arg, data_t *input3, int expected_status_arg3, |
| 8696 | int step4_arg, data_t *input4, int expected_status_arg4, |
| 8697 | data_t *key_agreement_peer_key, |
| 8698 | int requested_capacity_arg, |
| 8699 | data_t *expected_output1, |
| 8700 | data_t *expected_output2, |
| 8701 | int other_key_input_type, |
| 8702 | int key_input_type, |
| 8703 | int derive_type) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8704 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8705 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8706 | psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg }; |
| 8707 | data_t *inputs[] = { input1, input2, input3, input4 }; |
| 8708 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8709 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8710 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8711 | MBEDTLS_SVC_KEY_ID_INIT }; |
| 8712 | psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2, |
| 8713 | expected_status_arg3, expected_status_arg4 }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8714 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8715 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8716 | uint8_t *expected_outputs[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8717 | { expected_output1->x, expected_output2->x }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8718 | size_t output_sizes[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8719 | { expected_output1->len, expected_output2->len }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8720 | size_t output_buffer_size = 0; |
| 8721 | uint8_t *output_buffer = NULL; |
| 8722 | size_t expected_capacity; |
| 8723 | size_t current_capacity; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8724 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; |
| 8725 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; |
| 8726 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; |
| 8727 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8728 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8729 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8730 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8731 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8732 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
| 8733 | if (output_sizes[i] > output_buffer_size) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8734 | output_buffer_size = output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8735 | } |
| 8736 | if (output_sizes[i] == 0) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8737 | expected_outputs[i] = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8738 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8739 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8740 | TEST_CALLOC(output_buffer, output_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8741 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8742 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8743 | /* Extraction phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8744 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8745 | PSA_ASSERT(psa_key_derivation_set_capacity(&operation, |
| 8746 | requested_capacity)); |
| 8747 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8748 | switch (steps[i]) { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8749 | case 0: |
| 8750 | break; |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 8751 | case PSA_KEY_DERIVATION_INPUT_COST: |
| 8752 | TEST_EQUAL(psa_key_derivation_input_integer( |
Kusumit Ghoderao | f28e0f5 | 2023-06-06 15:03:22 +0530 | [diff] [blame] | 8753 | &operation, steps[i], |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 8754 | mbedtls_test_parse_binary_string(inputs[i])), |
Kusumit Ghoderao | f28e0f5 | 2023-06-06 15:03:22 +0530 | [diff] [blame] | 8755 | statuses[i]); |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 8756 | if (statuses[i] != PSA_SUCCESS) { |
| 8757 | goto exit; |
| 8758 | } |
| 8759 | break; |
| 8760 | case PSA_KEY_DERIVATION_INPUT_PASSWORD: |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8761 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8762 | switch (key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8763 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8764 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8765 | &operation, steps[i], |
| 8766 | inputs[i]->x, inputs[i]->len), |
| 8767 | statuses[i]); |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8768 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8769 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8770 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8771 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8772 | break; |
| 8773 | case 1: // input key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8774 | psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE); |
| 8775 | psa_set_key_algorithm(&attributes1, alg); |
| 8776 | psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8777 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8778 | PSA_ASSERT(psa_import_key(&attributes1, |
| 8779 | inputs[i]->x, inputs[i]->len, |
| 8780 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8781 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8782 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 8783 | PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1)); |
| 8784 | TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)), |
| 8785 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8786 | } |
| 8787 | |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 8788 | TEST_EQUAL(psa_key_derivation_input_key(&operation, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8789 | steps[i], |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 8790 | keys[i]), |
| 8791 | statuses[i]); |
| 8792 | |
| 8793 | if (statuses[i] != PSA_SUCCESS) { |
| 8794 | goto exit; |
| 8795 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8796 | break; |
| 8797 | default: |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 8798 | TEST_FAIL("default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8799 | break; |
| 8800 | } |
| 8801 | break; |
| 8802 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8803 | switch (other_key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8804 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8805 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 8806 | steps[i], |
| 8807 | inputs[i]->x, |
| 8808 | inputs[i]->len), |
| 8809 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8810 | break; |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 8811 | case 1: // input key, type DERIVE |
| 8812 | case 11: // input key, type RAW |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8813 | psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE); |
| 8814 | psa_set_key_algorithm(&attributes2, alg); |
| 8815 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8816 | |
| 8817 | // other secret of type RAW_DATA passed with input_key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8818 | if (other_key_input_type == 11) { |
| 8819 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA); |
| 8820 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8821 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8822 | PSA_ASSERT(psa_import_key(&attributes2, |
| 8823 | inputs[i]->x, inputs[i]->len, |
| 8824 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8825 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8826 | TEST_EQUAL(psa_key_derivation_input_key(&operation, |
| 8827 | steps[i], |
| 8828 | keys[i]), |
| 8829 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8830 | break; |
| 8831 | case 2: // key agreement |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8832 | psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE); |
| 8833 | psa_set_key_algorithm(&attributes3, alg); |
| 8834 | psa_set_key_type(&attributes3, |
| 8835 | PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8836 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8837 | PSA_ASSERT(psa_import_key(&attributes3, |
| 8838 | inputs[i]->x, inputs[i]->len, |
| 8839 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8840 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8841 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 8842 | &operation, |
| 8843 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 8844 | keys[i], key_agreement_peer_key->x, |
| 8845 | key_agreement_peer_key->len), statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8846 | break; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8847 | default: |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 8848 | TEST_FAIL("default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8849 | break; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8850 | } |
| 8851 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8852 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8853 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8854 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8855 | break; |
| 8856 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8857 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8858 | &operation, steps[i], |
| 8859 | inputs[i]->x, inputs[i]->len), statuses[i]); |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 8860 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8861 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 8862 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8863 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8864 | break; |
| 8865 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 8866 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8867 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8868 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8869 | ¤t_capacity)); |
| 8870 | TEST_EQUAL(current_capacity, requested_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8871 | expected_capacity = requested_capacity; |
| 8872 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8873 | if (derive_type == 1) { // output key |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8874 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; |
| 8875 | |
| 8876 | /* For output key derivation secret must be provided using |
| 8877 | input key, otherwise operation is not permitted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8878 | if (key_input_type == 1) { |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8879 | expected_status = PSA_SUCCESS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8880 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8881 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8882 | psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT); |
| 8883 | psa_set_key_algorithm(&attributes4, alg); |
| 8884 | psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE); |
| 8885 | psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8886 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8887 | TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation, |
| 8888 | &derived_key), expected_status); |
| 8889 | } else { // output bytes |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8890 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8891 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8892 | /* Read some bytes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8893 | status = psa_key_derivation_output_bytes(&operation, |
| 8894 | output_buffer, output_sizes[i]); |
| 8895 | if (expected_capacity == 0 && output_sizes[i] == 0) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8896 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8897 | TEST_ASSERT(status == PSA_SUCCESS || |
| 8898 | status == PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8899 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8900 | } else if (expected_capacity == 0 || |
| 8901 | output_sizes[i] > expected_capacity) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8902 | /* Capacity exceeded. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8903 | TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8904 | expected_capacity = 0; |
| 8905 | continue; |
| 8906 | } |
| 8907 | /* Success. Check the read data. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8908 | PSA_ASSERT(status); |
| 8909 | if (output_sizes[i] != 0) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8910 | TEST_MEMORY_COMPARE(output_buffer, output_sizes[i], |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8911 | expected_outputs[i], output_sizes[i]); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8912 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8913 | /* Check the operation status. */ |
| 8914 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8915 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8916 | ¤t_capacity)); |
| 8917 | TEST_EQUAL(expected_capacity, current_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8918 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8919 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8920 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8921 | |
| 8922 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8923 | mbedtls_free(output_buffer); |
| 8924 | psa_key_derivation_abort(&operation); |
| 8925 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8926 | psa_destroy_key(keys[i]); |
| 8927 | } |
| 8928 | psa_destroy_key(derived_key); |
| 8929 | PSA_DONE(); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8930 | } |
| 8931 | /* END_CASE */ |
| 8932 | |
| 8933 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8934 | void derive_full(int alg_arg, |
| 8935 | data_t *key_data, |
| 8936 | data_t *input1, |
| 8937 | data_t *input2, |
| 8938 | int requested_capacity_arg) |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8939 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8940 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8941 | psa_algorithm_t alg = alg_arg; |
| 8942 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8943 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8944 | unsigned char output_buffer[16]; |
| 8945 | size_t expected_capacity = requested_capacity; |
| 8946 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8947 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8948 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8949 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8950 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8951 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8952 | psa_set_key_algorithm(&attributes, alg); |
| 8953 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8954 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8955 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8956 | &key)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8957 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8958 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8959 | input1->x, input1->len, |
| 8960 | input2->x, input2->len, |
| 8961 | requested_capacity)) { |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 8962 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8963 | } |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 8964 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8965 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8966 | ¤t_capacity)); |
| 8967 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8968 | |
| 8969 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8970 | while (current_capacity > 0) { |
| 8971 | size_t read_size = sizeof(output_buffer); |
| 8972 | if (read_size > current_capacity) { |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8973 | read_size = current_capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8974 | } |
| 8975 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8976 | output_buffer, |
| 8977 | read_size)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8978 | expected_capacity -= read_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8979 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8980 | ¤t_capacity)); |
| 8981 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8982 | } |
| 8983 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8984 | /* Check that the operation refuses to go over capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8985 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1), |
| 8986 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8987 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8988 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8989 | |
| 8990 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8991 | psa_key_derivation_abort(&operation); |
| 8992 | psa_destroy_key(key); |
| 8993 | PSA_DONE(); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8994 | } |
| 8995 | /* END_CASE */ |
| 8996 | |
Stephan Koch | 78109f5 | 2023-04-12 14:19:36 +0200 | [diff] [blame] | 8997 | /* 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] | 8998 | void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg, |
| 8999 | int derivation_step, |
| 9000 | int capacity, int expected_capacity_status_arg, |
| 9001 | data_t *expected_output, |
| 9002 | int expected_output_status_arg) |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9003 | { |
| 9004 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 9005 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | d378504 | 2022-09-16 06:45:44 -0400 | [diff] [blame] | 9006 | 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] | 9007 | uint8_t *output_buffer = NULL; |
| 9008 | psa_status_t status; |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 9009 | psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg; |
| 9010 | psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg; |
| 9011 | 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] | 9012 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9013 | TEST_CALLOC(output_buffer, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9014 | PSA_ASSERT(psa_crypto_init()); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9015 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9016 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9017 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 9018 | expected_capacity_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9019 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9020 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 9021 | step, input->x, input->len), |
| 9022 | expected_input_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9023 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9024 | if (((psa_status_t) expected_input_status) != PSA_SUCCESS) { |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9025 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9026 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9027 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9028 | status = psa_key_derivation_output_bytes(&operation, output_buffer, |
| 9029 | expected_output->len); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9030 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9031 | TEST_EQUAL(status, expected_output_status); |
| 9032 | if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9033 | TEST_MEMORY_COMPARE(output_buffer, expected_output->len, expected_output->x, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9034 | expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9035 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9036 | |
| 9037 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9038 | mbedtls_free(output_buffer); |
| 9039 | psa_key_derivation_abort(&operation); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9040 | PSA_DONE(); |
| 9041 | } |
| 9042 | /* END_CASE */ |
| 9043 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9044 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9045 | void derive_key_exercise(int alg_arg, |
| 9046 | data_t *key_data, |
| 9047 | data_t *input1, |
| 9048 | data_t *input2, |
| 9049 | int derived_type_arg, |
| 9050 | int derived_bits_arg, |
| 9051 | int derived_usage_arg, |
| 9052 | int derived_alg_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9053 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9054 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9055 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9056 | psa_algorithm_t alg = alg_arg; |
| 9057 | psa_key_type_t derived_type = derived_type_arg; |
| 9058 | size_t derived_bits = derived_bits_arg; |
| 9059 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 9060 | psa_algorithm_t derived_alg = derived_alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9061 | size_t capacity = PSA_BITS_TO_BYTES(derived_bits); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9062 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9063 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 9064 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9065 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9066 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9067 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9068 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9069 | psa_set_key_algorithm(&attributes, alg); |
| 9070 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
| 9071 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 9072 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9073 | |
| 9074 | /* Derive a key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9075 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9076 | input1->x, input1->len, |
| 9077 | input2->x, input2->len, |
| 9078 | capacity)) { |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9079 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9080 | } |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9081 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9082 | psa_set_key_usage_flags(&attributes, derived_usage); |
| 9083 | psa_set_key_algorithm(&attributes, derived_alg); |
| 9084 | psa_set_key_type(&attributes, derived_type); |
| 9085 | psa_set_key_bits(&attributes, derived_bits); |
| 9086 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation, |
| 9087 | &derived_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9088 | |
| 9089 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9090 | PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes)); |
| 9091 | TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type); |
| 9092 | TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9093 | |
| 9094 | /* Exercise the derived key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9095 | if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) { |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9096 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9097 | } |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9098 | |
| 9099 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9100 | /* |
| 9101 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9102 | * thus reset them as required. |
| 9103 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9104 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9105 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9106 | psa_key_derivation_abort(&operation); |
| 9107 | psa_destroy_key(base_key); |
| 9108 | psa_destroy_key(derived_key); |
| 9109 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9110 | } |
| 9111 | /* END_CASE */ |
| 9112 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9113 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9114 | void derive_key_export(int alg_arg, |
| 9115 | data_t *key_data, |
| 9116 | data_t *input1, |
| 9117 | data_t *input2, |
| 9118 | int bytes1_arg, |
| 9119 | int bytes2_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9120 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9121 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9122 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9123 | psa_algorithm_t alg = alg_arg; |
| 9124 | size_t bytes1 = bytes1_arg; |
| 9125 | size_t bytes2 = bytes2_arg; |
| 9126 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9127 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9128 | uint8_t *output_buffer = NULL; |
| 9129 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9130 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9131 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9132 | size_t length; |
| 9133 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9134 | TEST_CALLOC(output_buffer, capacity); |
| 9135 | TEST_CALLOC(export_buffer, capacity); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9136 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9137 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9138 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9139 | psa_set_key_algorithm(&base_attributes, alg); |
| 9140 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9141 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9142 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9143 | |
| 9144 | /* Derive some material and output it. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9145 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9146 | input1->x, input1->len, |
| 9147 | input2->x, input2->len, |
| 9148 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9149 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9150 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9151 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9152 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9153 | output_buffer, |
| 9154 | capacity)); |
| 9155 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9156 | |
| 9157 | /* 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] | 9158 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9159 | input1->x, input1->len, |
| 9160 | input2->x, input2->len, |
| 9161 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9162 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9163 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9164 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9165 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9166 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9167 | psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA); |
| 9168 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1)); |
| 9169 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9170 | &derived_key)); |
| 9171 | PSA_ASSERT(psa_export_key(derived_key, |
| 9172 | export_buffer, bytes1, |
| 9173 | &length)); |
| 9174 | TEST_EQUAL(length, bytes1); |
| 9175 | PSA_ASSERT(psa_destroy_key(derived_key)); |
| 9176 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2)); |
| 9177 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9178 | &derived_key)); |
| 9179 | PSA_ASSERT(psa_export_key(derived_key, |
| 9180 | export_buffer + bytes1, bytes2, |
| 9181 | &length)); |
| 9182 | TEST_EQUAL(length, bytes2); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9183 | |
| 9184 | /* Compare the outputs from the two runs. */ |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9185 | TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9186 | export_buffer, capacity); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9187 | |
| 9188 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9189 | mbedtls_free(output_buffer); |
| 9190 | mbedtls_free(export_buffer); |
| 9191 | psa_key_derivation_abort(&operation); |
| 9192 | psa_destroy_key(base_key); |
| 9193 | psa_destroy_key(derived_key); |
| 9194 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9195 | } |
| 9196 | /* END_CASE */ |
| 9197 | |
| 9198 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9199 | void derive_key_type(int alg_arg, |
| 9200 | data_t *key_data, |
| 9201 | data_t *input1, |
| 9202 | data_t *input2, |
| 9203 | int key_type_arg, int bits_arg, |
| 9204 | data_t *expected_export) |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9205 | { |
| 9206 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9207 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9208 | const psa_algorithm_t alg = alg_arg; |
| 9209 | const psa_key_type_t key_type = key_type_arg; |
| 9210 | const size_t bits = bits_arg; |
| 9211 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9212 | const size_t export_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9213 | PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9214 | uint8_t *export_buffer = NULL; |
| 9215 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9216 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9217 | size_t export_length; |
| 9218 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9219 | TEST_CALLOC(export_buffer, export_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9220 | PSA_ASSERT(psa_crypto_init()); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9221 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9222 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9223 | psa_set_key_algorithm(&base_attributes, alg); |
| 9224 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9225 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9226 | &base_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9227 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9228 | if (mbedtls_test_psa_setup_key_derivation_wrap( |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9229 | &operation, base_key, alg, |
| 9230 | input1->x, input1->len, |
| 9231 | input2->x, input2->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9232 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) { |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9233 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9234 | } |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9235 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9236 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9237 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9238 | psa_set_key_type(&derived_attributes, key_type); |
| 9239 | psa_set_key_bits(&derived_attributes, bits); |
| 9240 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9241 | &derived_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9242 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9243 | PSA_ASSERT(psa_export_key(derived_key, |
| 9244 | export_buffer, export_buffer_size, |
| 9245 | &export_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9246 | TEST_MEMORY_COMPARE(export_buffer, export_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9247 | expected_export->x, expected_export->len); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9248 | |
| 9249 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9250 | mbedtls_free(export_buffer); |
| 9251 | psa_key_derivation_abort(&operation); |
| 9252 | psa_destroy_key(base_key); |
| 9253 | psa_destroy_key(derived_key); |
| 9254 | PSA_DONE(); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9255 | } |
| 9256 | /* END_CASE */ |
| 9257 | |
| 9258 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9259 | void derive_key(int alg_arg, |
| 9260 | data_t *key_data, data_t *input1, data_t *input2, |
| 9261 | int type_arg, int bits_arg, |
| 9262 | int expected_status_arg, |
| 9263 | int is_large_output) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9264 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9265 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9266 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9267 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 9268 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9269 | size_t bits = bits_arg; |
| 9270 | psa_status_t expected_status = expected_status_arg; |
| 9271 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9272 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9273 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9274 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9275 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9276 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9277 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9278 | psa_set_key_algorithm(&base_attributes, alg); |
| 9279 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9280 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9281 | &base_key)); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9282 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9283 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9284 | input1->x, input1->len, |
| 9285 | input2->x, input2->len, |
| 9286 | SIZE_MAX)) { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9287 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9288 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9289 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9290 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9291 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9292 | psa_set_key_type(&derived_attributes, type); |
| 9293 | psa_set_key_bits(&derived_attributes, bits); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9294 | |
| 9295 | psa_status_t status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9296 | psa_key_derivation_output_key(&derived_attributes, |
| 9297 | &operation, |
| 9298 | &derived_key); |
| 9299 | if (is_large_output > 0) { |
| 9300 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9301 | } |
| 9302 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9303 | |
| 9304 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9305 | psa_key_derivation_abort(&operation); |
| 9306 | psa_destroy_key(base_key); |
| 9307 | psa_destroy_key(derived_key); |
| 9308 | PSA_DONE(); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9309 | } |
| 9310 | /* END_CASE */ |
| 9311 | |
| 9312 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9313 | void key_agreement_setup(int alg_arg, |
| 9314 | int our_key_type_arg, int our_key_alg_arg, |
| 9315 | data_t *our_key_data, data_t *peer_key_data, |
| 9316 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9317 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9318 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9319 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 9320 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9321 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9322 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9323 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9324 | psa_status_t expected_status = expected_status_arg; |
| 9325 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9326 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9327 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9328 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9329 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9330 | psa_set_key_algorithm(&attributes, our_key_alg); |
| 9331 | psa_set_key_type(&attributes, our_key_type); |
| 9332 | PSA_ASSERT(psa_import_key(&attributes, |
| 9333 | our_key_data->x, our_key_data->len, |
| 9334 | &our_key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9335 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9336 | /* The tests currently include inputs that should fail at either step. |
| 9337 | * Test cases that fail at the setup step should be changed to call |
| 9338 | * key_derivation_setup instead, and this function should be renamed |
| 9339 | * to key_agreement_fail. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9340 | status = psa_key_derivation_setup(&operation, alg); |
| 9341 | if (status == PSA_SUCCESS) { |
| 9342 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 9343 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 9344 | our_key, |
| 9345 | peer_key_data->x, peer_key_data->len), |
| 9346 | expected_status); |
| 9347 | } else { |
| 9348 | TEST_ASSERT(status == expected_status); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9349 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9350 | |
| 9351 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9352 | psa_key_derivation_abort(&operation); |
| 9353 | psa_destroy_key(our_key); |
| 9354 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9355 | } |
| 9356 | /* END_CASE */ |
| 9357 | |
| 9358 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9359 | void raw_key_agreement(int alg_arg, |
| 9360 | int our_key_type_arg, data_t *our_key_data, |
| 9361 | data_t *peer_key_data, |
| 9362 | data_t *expected_output) |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9363 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9364 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9365 | psa_algorithm_t alg = alg_arg; |
| 9366 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9367 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9368 | unsigned char *output = NULL; |
| 9369 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9370 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9371 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9372 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9374 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9375 | psa_set_key_algorithm(&attributes, alg); |
| 9376 | psa_set_key_type(&attributes, our_key_type); |
| 9377 | PSA_ASSERT(psa_import_key(&attributes, |
| 9378 | our_key_data->x, our_key_data->len, |
| 9379 | &our_key)); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9380 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9381 | PSA_ASSERT(psa_get_key_attributes(our_key, &attributes)); |
| 9382 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9383 | |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9384 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9385 | TEST_LE_U(expected_output->len, |
| 9386 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits)); |
| 9387 | TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits), |
| 9388 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9389 | |
| 9390 | /* Good case with exact output size */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9391 | TEST_CALLOC(output, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9392 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9393 | peer_key_data->x, peer_key_data->len, |
| 9394 | output, expected_output->len, |
| 9395 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9396 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9397 | expected_output->x, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9398 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9399 | output = NULL; |
| 9400 | output_length = ~0; |
| 9401 | |
| 9402 | /* Larger buffer */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9403 | TEST_CALLOC(output, expected_output->len + 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9404 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9405 | peer_key_data->x, peer_key_data->len, |
| 9406 | output, expected_output->len + 1, |
| 9407 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9408 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9409 | expected_output->x, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9410 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9411 | output = NULL; |
| 9412 | output_length = ~0; |
| 9413 | |
| 9414 | /* Buffer too small */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9415 | TEST_CALLOC(output, expected_output->len - 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9416 | TEST_EQUAL(psa_raw_key_agreement(alg, our_key, |
| 9417 | peer_key_data->x, peer_key_data->len, |
| 9418 | output, expected_output->len - 1, |
| 9419 | &output_length), |
| 9420 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9421 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9422 | TEST_LE_U(output_length, expected_output->len - 1); |
| 9423 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9424 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9425 | |
| 9426 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9427 | mbedtls_free(output); |
| 9428 | psa_destroy_key(our_key); |
| 9429 | PSA_DONE(); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9430 | } |
| 9431 | /* END_CASE */ |
| 9432 | |
| 9433 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9434 | void key_agreement_capacity(int alg_arg, |
| 9435 | int our_key_type_arg, data_t *our_key_data, |
| 9436 | data_t *peer_key_data, |
| 9437 | int expected_capacity_arg) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9438 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9439 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9440 | psa_algorithm_t alg = alg_arg; |
| 9441 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9442 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9443 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9444 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9445 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9446 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9447 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9449 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9450 | psa_set_key_algorithm(&attributes, alg); |
| 9451 | psa_set_key_type(&attributes, our_key_type); |
| 9452 | PSA_ASSERT(psa_import_key(&attributes, |
| 9453 | our_key_data->x, our_key_data->len, |
| 9454 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9455 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9456 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9457 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9458 | &operation, |
| 9459 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9460 | peer_key_data->x, peer_key_data->len)); |
| 9461 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9462 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9463 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9464 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9465 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9466 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9467 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 9468 | /* Test the advertised capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9469 | PSA_ASSERT(psa_key_derivation_get_capacity( |
| 9470 | &operation, &actual_capacity)); |
| 9471 | TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9472 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9473 | /* Test the actual capacity by reading the output. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9474 | while (actual_capacity > sizeof(output)) { |
| 9475 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9476 | output, sizeof(output))); |
| 9477 | actual_capacity -= sizeof(output); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9478 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9479 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9480 | output, actual_capacity)); |
| 9481 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1), |
| 9482 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9483 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9484 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9485 | psa_key_derivation_abort(&operation); |
| 9486 | psa_destroy_key(our_key); |
| 9487 | PSA_DONE(); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9488 | } |
| 9489 | /* END_CASE */ |
| 9490 | |
Valerio Setti | ad81967 | 2023-12-29 12:14:41 +0100 | [diff] [blame] | 9491 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */ |
| 9492 | 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] | 9493 | { |
| 9494 | mbedtls_ecp_group_id grp_id = grp_id_arg; |
Valerio Setti | ad81967 | 2023-12-29 12:14:41 +0100 | [diff] [blame] | 9495 | psa_ecc_family_t ecc_family = psa_family_arg; |
| 9496 | size_t bits = bits_arg; |
| 9497 | size_t bits_tmp; |
Valerio Setti | bf999cb | 2023-12-28 17:48:13 +0100 | [diff] [blame] | 9498 | |
Valerio Setti | ad81967 | 2023-12-29 12:14:41 +0100 | [diff] [blame] | 9499 | TEST_EQUAL(ecc_family, mbedtls_ecc_group_to_psa(grp_id, &bits_tmp)); |
| 9500 | TEST_EQUAL(bits, bits_tmp); |
Valerio Setti | ac73952 | 2024-01-04 10:22:01 +0100 | [diff] [blame] | 9501 | TEST_EQUAL(grp_id, mbedtls_ecc_group_from_psa(ecc_family, bits)); |
Valerio Setti | bf999cb | 2023-12-28 17:48:13 +0100 | [diff] [blame] | 9502 | } |
| 9503 | /* END_CASE */ |
| 9504 | |
Valerio Setti | ac73952 | 2024-01-04 10:22:01 +0100 | [diff] [blame] | 9505 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */ |
| 9506 | void ecc_conversion_functions_fail() |
| 9507 | { |
| 9508 | size_t bits; |
| 9509 | |
Valerio Setti | db6e029 | 2024-01-05 10:15:45 +0100 | [diff] [blame] | 9510 | /* Invalid legacy curve identifiers. */ |
| 9511 | TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_MAX, &bits)); |
| 9512 | TEST_EQUAL(0, bits); |
Valerio Setti | ac73952 | 2024-01-04 10:22:01 +0100 | [diff] [blame] | 9513 | TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_NONE, &bits)); |
| 9514 | TEST_EQUAL(0, bits); |
| 9515 | |
| 9516 | /* Invalid PSA EC family. */ |
| 9517 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(0, 192)); |
| 9518 | /* Invalid bit-size for a valid EC family. */ |
| 9519 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_SECP_R1, 512)); |
| 9520 | |
| 9521 | /* Twisted-Edward curves are not supported yet. */ |
| 9522 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, |
| 9523 | mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 255)); |
| 9524 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, |
| 9525 | mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 448)); |
| 9526 | } |
| 9527 | /* END_CASE */ |
| 9528 | |
| 9529 | |
Valerio Setti | bf999cb | 2023-12-28 17:48:13 +0100 | [diff] [blame] | 9530 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9531 | void key_agreement_output(int alg_arg, |
| 9532 | int our_key_type_arg, data_t *our_key_data, |
| 9533 | data_t *peer_key_data, |
| 9534 | data_t *expected_output1, data_t *expected_output2) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9535 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9536 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9537 | psa_algorithm_t alg = alg_arg; |
| 9538 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9539 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9540 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9541 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9542 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9543 | TEST_CALLOC(actual_output, MAX(expected_output1->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9544 | expected_output2->len)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9545 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9546 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9547 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9548 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9549 | psa_set_key_algorithm(&attributes, alg); |
| 9550 | psa_set_key_type(&attributes, our_key_type); |
| 9551 | PSA_ASSERT(psa_import_key(&attributes, |
| 9552 | our_key_data->x, our_key_data->len, |
| 9553 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9554 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9555 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9556 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9557 | &operation, |
| 9558 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9559 | peer_key_data->x, peer_key_data->len)); |
| 9560 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9561 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9562 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9563 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9564 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9565 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9566 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9567 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9568 | actual_output, |
| 9569 | expected_output1->len)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9570 | TEST_MEMORY_COMPARE(actual_output, expected_output1->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9571 | expected_output1->x, expected_output1->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9572 | if (expected_output2->len != 0) { |
| 9573 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9574 | actual_output, |
| 9575 | expected_output2->len)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9576 | TEST_MEMORY_COMPARE(actual_output, expected_output2->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9577 | expected_output2->x, expected_output2->len); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9578 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9579 | |
| 9580 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9581 | psa_key_derivation_abort(&operation); |
| 9582 | psa_destroy_key(our_key); |
| 9583 | PSA_DONE(); |
| 9584 | mbedtls_free(actual_output); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9585 | } |
| 9586 | /* END_CASE */ |
| 9587 | |
| 9588 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9589 | void generate_random(int bytes_arg) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9590 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9591 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9592 | unsigned char *output = NULL; |
| 9593 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9594 | size_t i; |
| 9595 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9596 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9597 | TEST_ASSERT(bytes_arg >= 0); |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 9598 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9599 | TEST_CALLOC(output, bytes); |
| 9600 | TEST_CALLOC(changed, bytes); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9601 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9602 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9603 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9604 | /* Run several times, to ensure that every output byte will be |
| 9605 | * nonzero at least once with overwhelming probability |
| 9606 | * (2^(-8*number_of_runs)). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9607 | for (run = 0; run < 10; run++) { |
| 9608 | if (bytes != 0) { |
| 9609 | memset(output, 0, bytes); |
| 9610 | } |
| 9611 | PSA_ASSERT(psa_generate_random(output, bytes)); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9612 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9613 | for (i = 0; i < bytes; i++) { |
| 9614 | if (output[i] != 0) { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9615 | ++changed[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9616 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9617 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9618 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9619 | |
| 9620 | /* Check that every byte was changed to nonzero at least once. This |
| 9621 | * validates that psa_generate_random is overwriting every byte of |
| 9622 | * the output buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9623 | for (i = 0; i < bytes; i++) { |
| 9624 | TEST_ASSERT(changed[i] != 0); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9625 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9626 | |
| 9627 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9628 | PSA_DONE(); |
| 9629 | mbedtls_free(output); |
| 9630 | mbedtls_free(changed); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9631 | } |
| 9632 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9633 | |
| 9634 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9635 | void generate_key(int type_arg, |
| 9636 | int bits_arg, |
| 9637 | int usage_arg, |
| 9638 | int alg_arg, |
| 9639 | int expected_status_arg, |
| 9640 | int is_large_key) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9641 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9642 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9643 | psa_key_type_t type = type_arg; |
| 9644 | psa_key_usage_t usage = usage_arg; |
| 9645 | size_t bits = bits_arg; |
| 9646 | psa_algorithm_t alg = alg_arg; |
| 9647 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9648 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 9649 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9650 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9651 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9652 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9653 | psa_set_key_usage_flags(&attributes, usage); |
| 9654 | psa_set_key_algorithm(&attributes, alg); |
| 9655 | psa_set_key_type(&attributes, type); |
| 9656 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9657 | |
| 9658 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9659 | psa_status_t status = psa_generate_key(&attributes, &key); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9660 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9661 | if (is_large_key > 0) { |
| 9662 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9663 | } |
| 9664 | TEST_EQUAL(status, expected_status); |
| 9665 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9666 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9667 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9668 | |
| 9669 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9670 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 9671 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 9672 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9673 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 9674 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9675 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 9676 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9677 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9678 | |
| 9679 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9680 | /* |
| 9681 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9682 | * thus reset them as required. |
| 9683 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9684 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9685 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9686 | psa_destroy_key(key); |
| 9687 | PSA_DONE(); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9688 | } |
| 9689 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 9690 | |
Valerio Setti | 19fec54 | 2023-07-25 12:31:50 +0200 | [diff] [blame] | 9691 | /* 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] | 9692 | void generate_key_rsa(int bits_arg, |
| 9693 | data_t *e_arg, |
| 9694 | int expected_status_arg) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9695 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9696 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 9697 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9698 | size_t bits = bits_arg; |
| 9699 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 9700 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 9701 | psa_status_t expected_status = expected_status_arg; |
| 9702 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9703 | uint8_t *exported = NULL; |
| 9704 | size_t exported_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9705 | PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9706 | size_t exported_length = SIZE_MAX; |
| 9707 | uint8_t *e_read_buffer = NULL; |
| 9708 | int is_default_public_exponent = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9709 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9710 | size_t e_read_length = SIZE_MAX; |
| 9711 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9712 | if (e_arg->len == 0 || |
| 9713 | (e_arg->len == 3 && |
| 9714 | 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] | 9715 | is_default_public_exponent = 1; |
| 9716 | e_read_size = 0; |
| 9717 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9718 | TEST_CALLOC(e_read_buffer, e_read_size); |
| 9719 | TEST_CALLOC(exported, exported_size); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9720 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9721 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9722 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9723 | psa_set_key_usage_flags(&attributes, usage); |
| 9724 | psa_set_key_algorithm(&attributes, alg); |
| 9725 | PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type, |
| 9726 | e_arg->x, e_arg->len)); |
| 9727 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9728 | |
| 9729 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9730 | TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status); |
| 9731 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9732 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9733 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9734 | |
| 9735 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9736 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 9737 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 9738 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
Pengyu Lv | d90fbf7 | 2023-12-08 17:13:22 +0800 | [diff] [blame] | 9739 | psa_status_t status = psa_get_key_domain_parameters(&attributes, |
| 9740 | e_read_buffer, e_read_size, |
| 9741 | &e_read_length); |
| 9742 | |
| 9743 | |
| 9744 | #if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \ |
| 9745 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \ |
| 9746 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9747 | if (is_default_public_exponent) { |
| 9748 | TEST_EQUAL(e_read_length, 0); |
| 9749 | } else { |
Pengyu Lv | d90fbf7 | 2023-12-08 17:13:22 +0800 | [diff] [blame] | 9750 | TEST_EQUAL(status, PSA_SUCCESS); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9751 | 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] | 9752 | } |
Pengyu Lv | 9e976f3 | 2023-12-06 16:58:05 +0800 | [diff] [blame] | 9753 | #else |
Pengyu Lv | 9e976f3 | 2023-12-06 16:58:05 +0800 | [diff] [blame] | 9754 | (void) is_default_public_exponent; |
Pengyu Lv | d90fbf7 | 2023-12-08 17:13:22 +0800 | [diff] [blame] | 9755 | TEST_EQUAL(status, PSA_ERROR_NOT_SUPPORTED); |
Pengyu Lv | 9e976f3 | 2023-12-06 16:58:05 +0800 | [diff] [blame] | 9756 | #endif |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9757 | |
| 9758 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9759 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9760 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9761 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9762 | |
| 9763 | /* Export the key and check the public exponent. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9764 | PSA_ASSERT(psa_export_public_key(key, |
| 9765 | exported, exported_size, |
| 9766 | &exported_length)); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9767 | { |
| 9768 | uint8_t *p = exported; |
| 9769 | uint8_t *end = exported + exported_length; |
| 9770 | size_t len; |
| 9771 | /* RSAPublicKey ::= SEQUENCE { |
| 9772 | * modulus INTEGER, -- n |
| 9773 | * publicExponent INTEGER } -- e |
| 9774 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9775 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 9776 | MBEDTLS_ASN1_SEQUENCE | |
| 9777 | MBEDTLS_ASN1_CONSTRUCTED)); |
| 9778 | TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1)); |
| 9779 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 9780 | MBEDTLS_ASN1_INTEGER)); |
| 9781 | if (len >= 1 && p[0] == 0) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9782 | ++p; |
| 9783 | --len; |
| 9784 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9785 | if (e_arg->len == 0) { |
| 9786 | TEST_EQUAL(len, 3); |
| 9787 | TEST_EQUAL(p[0], 1); |
| 9788 | TEST_EQUAL(p[1], 0); |
| 9789 | TEST_EQUAL(p[2], 1); |
| 9790 | } else { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9791 | TEST_MEMORY_COMPARE(p, len, e_arg->x, e_arg->len); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9792 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9793 | } |
| 9794 | |
| 9795 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9796 | /* |
| 9797 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 9798 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 9799 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9800 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9801 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9802 | psa_destroy_key(key); |
| 9803 | PSA_DONE(); |
| 9804 | mbedtls_free(e_read_buffer); |
| 9805 | mbedtls_free(exported); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9806 | } |
| 9807 | /* END_CASE */ |
| 9808 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9809 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9810 | void persistent_key_load_key_from_storage(data_t *data, |
| 9811 | int type_arg, int bits_arg, |
| 9812 | int usage_flags_arg, int alg_arg, |
| 9813 | int generation_method) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9814 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9815 | 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] | 9816 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9817 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9818 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9819 | psa_key_type_t type = type_arg; |
| 9820 | size_t bits = bits_arg; |
| 9821 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 9822 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9823 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9824 | unsigned char *first_export = NULL; |
| 9825 | unsigned char *second_export = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9826 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits); |
Sergey | bef1f63 | 2023-03-06 15:25:06 -0700 | [diff] [blame] | 9827 | size_t first_exported_length = 0; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9828 | size_t second_exported_length; |
| 9829 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9830 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9831 | TEST_CALLOC(first_export, export_size); |
| 9832 | TEST_CALLOC(second_export, export_size); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9833 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9834 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9835 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9836 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9837 | psa_set_key_id(&attributes, key_id); |
| 9838 | psa_set_key_usage_flags(&attributes, usage_flags); |
| 9839 | psa_set_key_algorithm(&attributes, alg); |
| 9840 | psa_set_key_type(&attributes, type); |
| 9841 | psa_set_key_bits(&attributes, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9842 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9843 | switch (generation_method) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9844 | case IMPORT_KEY: |
| 9845 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9846 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, |
| 9847 | &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9848 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9849 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9850 | case GENERATE_KEY: |
| 9851 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9852 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9853 | break; |
| 9854 | |
| 9855 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 9856 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9857 | { |
| 9858 | /* Create base key */ |
| 9859 | psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256); |
| 9860 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9861 | psa_set_key_usage_flags(&base_attributes, |
| 9862 | PSA_KEY_USAGE_DERIVE); |
| 9863 | psa_set_key_algorithm(&base_attributes, derive_alg); |
| 9864 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9865 | PSA_ASSERT(psa_import_key(&base_attributes, |
| 9866 | data->x, data->len, |
| 9867 | &base_key)); |
| 9868 | /* Derive a key. */ |
| 9869 | PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg)); |
| 9870 | PSA_ASSERT(psa_key_derivation_input_key( |
| 9871 | &operation, |
| 9872 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key)); |
| 9873 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 9874 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
| 9875 | NULL, 0)); |
| 9876 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, |
| 9877 | &operation, |
| 9878 | &key)); |
| 9879 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
| 9880 | PSA_ASSERT(psa_destroy_key(base_key)); |
| 9881 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9882 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9883 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9884 | TEST_ASSUME(!"KDF not supported in this configuration"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9885 | #endif |
| 9886 | break; |
| 9887 | |
| 9888 | default: |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 9889 | TEST_FAIL("generation_method not implemented in test"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9890 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9891 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9892 | psa_reset_key_attributes(&attributes); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9893 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9894 | /* Export the key if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9895 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9896 | PSA_ASSERT(psa_export_key(key, |
| 9897 | first_export, export_size, |
| 9898 | &first_exported_length)); |
| 9899 | if (generation_method == IMPORT_KEY) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9900 | TEST_MEMORY_COMPARE(data->x, data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9901 | first_export, first_exported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9902 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9903 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9904 | |
| 9905 | /* Shutdown and restart */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9906 | PSA_ASSERT(psa_purge_key(key)); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 9907 | PSA_DONE(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9908 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9909 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9910 | /* Check key slot still contains key data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9911 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 9912 | TEST_ASSERT(mbedtls_svc_key_id_equal( |
| 9913 | psa_get_key_id(&attributes), key_id)); |
| 9914 | TEST_EQUAL(psa_get_key_lifetime(&attributes), |
| 9915 | PSA_KEY_LIFETIME_PERSISTENT); |
| 9916 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 9917 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 9918 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 9919 | mbedtls_test_update_key_usage_flags(usage_flags)); |
| 9920 | TEST_EQUAL(psa_get_key_algorithm(&attributes), alg); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9921 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9922 | /* Export the key again if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9923 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9924 | PSA_ASSERT(psa_export_key(key, |
| 9925 | second_export, export_size, |
| 9926 | &second_exported_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9927 | TEST_MEMORY_COMPARE(first_export, first_exported_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9928 | second_export, second_exported_length); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9929 | } |
| 9930 | |
| 9931 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9932 | if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9933 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9934 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9935 | |
| 9936 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9937 | /* |
| 9938 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9939 | * thus reset them as required. |
| 9940 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9941 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9942 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9943 | mbedtls_free(first_export); |
| 9944 | mbedtls_free(second_export); |
| 9945 | psa_key_derivation_abort(&operation); |
| 9946 | psa_destroy_key(base_key); |
| 9947 | psa_destroy_key(key); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 9948 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9949 | } |
| 9950 | /* END_CASE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9951 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 9952 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9953 | void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg, |
| 9954 | int primitive_arg, int hash_arg, int role_arg, |
| 9955 | int test_input, data_t *pw_data, |
| 9956 | int inj_err_type_arg, |
| 9957 | int expected_error_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9958 | { |
| 9959 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 9960 | psa_pake_operation_t operation = psa_pake_operation_init(); |
| 9961 | psa_algorithm_t alg = alg_arg; |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9962 | psa_pake_primitive_t primitive = primitive_arg; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 9963 | psa_key_type_t key_type_pw = key_type_pw_arg; |
| 9964 | psa_key_usage_t key_usage_pw = key_usage_pw_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9965 | psa_algorithm_t hash_alg = hash_arg; |
| 9966 | psa_pake_role_t role = role_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9967 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9968 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9969 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
| 9970 | psa_status_t expected_error = expected_error_arg; |
| 9971 | psa_status_t status; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9972 | unsigned char *output_buffer = NULL; |
| 9973 | size_t output_len = 0; |
| 9974 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9975 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9976 | |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9977 | size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9978 | PSA_PAKE_STEP_KEY_SHARE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9979 | TEST_CALLOC(output_buffer, buf_size); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9980 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9981 | if (pw_data->len > 0) { |
| 9982 | psa_set_key_usage_flags(&attributes, key_usage_pw); |
| 9983 | psa_set_key_algorithm(&attributes, alg); |
| 9984 | psa_set_key_type(&attributes, key_type_pw); |
| 9985 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 9986 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9987 | } |
| 9988 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9989 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 9990 | psa_pake_cs_set_primitive(&cipher_suite, primitive); |
| 9991 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9992 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9993 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 9994 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9995 | if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) { |
| 9996 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 9997 | expected_error); |
| 9998 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9999 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 10000 | expected_error); |
| 10001 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10002 | TEST_EQUAL(psa_pake_set_password_key(&operation, key), |
| 10003 | expected_error); |
| 10004 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10005 | TEST_EQUAL(psa_pake_set_role(&operation, role), |
| 10006 | expected_error); |
| 10007 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10008 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10009 | NULL, 0, NULL), |
| 10010 | expected_error); |
| 10011 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10012 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0), |
| 10013 | expected_error); |
| 10014 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10015 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10016 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10017 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10018 | status = psa_pake_setup(&operation, &cipher_suite); |
| 10019 | if (status != PSA_SUCCESS) { |
| 10020 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10021 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10022 | } |
| 10023 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10024 | if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) { |
| 10025 | TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite), |
| 10026 | expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10027 | goto exit; |
| 10028 | } |
| 10029 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10030 | status = psa_pake_set_role(&operation, role); |
| 10031 | if (status != PSA_SUCCESS) { |
| 10032 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10033 | goto exit; |
| 10034 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10035 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10036 | if (pw_data->len > 0) { |
| 10037 | status = psa_pake_set_password_key(&operation, key); |
| 10038 | if (status != PSA_SUCCESS) { |
| 10039 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10040 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10041 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10042 | } |
| 10043 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10044 | if (inj_err_type == INJECT_ERR_INVALID_USER) { |
| 10045 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 10046 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10047 | goto exit; |
| 10048 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 10049 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10050 | if (inj_err_type == INJECT_ERR_INVALID_PEER) { |
| 10051 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 10052 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10053 | goto exit; |
| 10054 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 10055 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10056 | if (inj_err_type == INJECT_ERR_SET_USER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10057 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10058 | TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4), |
| 10059 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10060 | goto exit; |
| 10061 | } |
| 10062 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10063 | if (inj_err_type == INJECT_ERR_SET_PEER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10064 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10065 | TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4), |
| 10066 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10067 | goto exit; |
| 10068 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 10069 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10070 | const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10071 | PSA_PAKE_STEP_KEY_SHARE); |
| 10072 | const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10073 | PSA_PAKE_STEP_ZK_PUBLIC); |
| 10074 | const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10075 | PSA_PAKE_STEP_ZK_PROOF); |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 10076 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10077 | if (test_input) { |
| 10078 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 10079 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0), |
| 10080 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10081 | goto exit; |
| 10082 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10083 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10084 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 10085 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 10086 | output_buffer, size_zk_proof), |
| 10087 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10088 | goto exit; |
| 10089 | } |
| 10090 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10091 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 10092 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10093 | output_buffer, size_zk_proof), |
| 10094 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10095 | goto exit; |
| 10096 | } |
| 10097 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10098 | status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10099 | output_buffer, size_key_share); |
| 10100 | if (status != PSA_SUCCESS) { |
| 10101 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10102 | goto exit; |
| 10103 | } |
| 10104 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10105 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 10106 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10107 | output_buffer, size_zk_public + 1), |
| 10108 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10109 | goto exit; |
| 10110 | } |
| 10111 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10112 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10113 | // 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] | 10114 | psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10115 | output_buffer, size_zk_public + 1); |
| 10116 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10117 | output_buffer, size_zk_public), |
| 10118 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10119 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10120 | } |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10121 | } else { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10122 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 10123 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10124 | NULL, 0, NULL), |
| 10125 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10126 | goto exit; |
| 10127 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10128 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10129 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 10130 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 10131 | output_buffer, buf_size, &output_len), |
| 10132 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10133 | goto exit; |
| 10134 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10135 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10136 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 10137 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10138 | output_buffer, buf_size, &output_len), |
| 10139 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10140 | goto exit; |
| 10141 | } |
| 10142 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10143 | status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10144 | output_buffer, buf_size, &output_len); |
| 10145 | if (status != PSA_SUCCESS) { |
| 10146 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10147 | goto exit; |
| 10148 | } |
| 10149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10150 | TEST_ASSERT(output_len > 0); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10151 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10152 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 10153 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10154 | output_buffer, size_zk_public - 1, &output_len), |
| 10155 | PSA_ERROR_BUFFER_TOO_SMALL); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10156 | goto exit; |
| 10157 | } |
| 10158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10159 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10160 | // 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] | 10161 | psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10162 | output_buffer, size_zk_public - 1, &output_len); |
| 10163 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10164 | output_buffer, buf_size, &output_len), |
| 10165 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10166 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10167 | } |
| 10168 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10169 | |
| 10170 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10171 | PSA_ASSERT(psa_destroy_key(key)); |
| 10172 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10173 | mbedtls_free(output_buffer); |
| 10174 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10175 | } |
| 10176 | /* END_CASE */ |
| 10177 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 10178 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10179 | void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg, |
| 10180 | int client_input_first, int inject_error, |
| 10181 | data_t *pw_data) |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10182 | { |
| 10183 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10184 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10185 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10186 | psa_algorithm_t alg = alg_arg; |
| 10187 | psa_algorithm_t hash_alg = hash_arg; |
| 10188 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10189 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10190 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10191 | PSA_INIT(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10192 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10193 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10194 | psa_set_key_algorithm(&attributes, alg); |
| 10195 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10196 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10197 | &key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10198 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10199 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10200 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10201 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10202 | |
| 10203 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10204 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10205 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10206 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10207 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10208 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10209 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10210 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10211 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10212 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10213 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10214 | client_input_first, 1, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10215 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10216 | if (inject_error == 1 || inject_error == 2) { |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10217 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10218 | } |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10219 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10220 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10221 | client_input_first, 2, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10222 | |
| 10223 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10224 | psa_destroy_key(key); |
| 10225 | psa_pake_abort(&server); |
| 10226 | psa_pake_abort(&client); |
| 10227 | PSA_DONE(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10228 | } |
| 10229 | /* END_CASE */ |
| 10230 | |
| 10231 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10232 | void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg, |
| 10233 | int derive_alg_arg, data_t *pw_data, |
| 10234 | int client_input_first, int inj_err_type_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10235 | { |
| 10236 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10237 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10238 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10239 | psa_algorithm_t alg = alg_arg; |
| 10240 | psa_algorithm_t hash_alg = hash_arg; |
| 10241 | psa_algorithm_t derive_alg = derive_alg_arg; |
| 10242 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10243 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10244 | psa_key_derivation_operation_t server_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10245 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10246 | psa_key_derivation_operation_t client_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10247 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10248 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10249 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10250 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10252 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10253 | psa_set_key_algorithm(&attributes, alg); |
| 10254 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10255 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10256 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10257 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10258 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10259 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10260 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10261 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10262 | /* Get shared key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10263 | PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg)); |
| 10264 | PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10266 | if (PSA_ALG_IS_TLS12_PRF(derive_alg) || |
| 10267 | PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) { |
| 10268 | PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive, |
| 10269 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10270 | (const uint8_t *) "", 0)); |
| 10271 | PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive, |
| 10272 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10273 | (const uint8_t *) "", 0)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10274 | } |
| 10275 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10276 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10277 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10278 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10279 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10280 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10282 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10283 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10284 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10285 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) { |
| 10286 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10287 | PSA_ERROR_BAD_STATE); |
| 10288 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10289 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10290 | goto exit; |
| 10291 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10292 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10293 | /* First round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10294 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10295 | client_input_first, 1, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10296 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10297 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) { |
| 10298 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10299 | PSA_ERROR_BAD_STATE); |
| 10300 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10301 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10302 | goto exit; |
| 10303 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10304 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10305 | /* Second round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10306 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10307 | client_input_first, 2, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10308 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10309 | PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive)); |
| 10310 | PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10311 | |
| 10312 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10313 | psa_key_derivation_abort(&server_derive); |
| 10314 | psa_key_derivation_abort(&client_derive); |
| 10315 | psa_destroy_key(key); |
| 10316 | psa_pake_abort(&server); |
| 10317 | psa_pake_abort(&client); |
| 10318 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10319 | } |
| 10320 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10321 | |
| 10322 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10323 | void ecjpake_size_macros() |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10324 | { |
| 10325 | const psa_algorithm_t alg = PSA_ALG_JPAKE; |
| 10326 | const size_t bits = 256; |
| 10327 | const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10328 | 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] | 10329 | 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] | 10330 | PSA_ECC_FAMILY_SECP_R1); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10331 | |
| 10332 | // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types |
| 10333 | /* 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] | 10334 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10335 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
| 10336 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10337 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10338 | /* The output for ZK_PROOF is the same bitsize as the curve */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10339 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10340 | PSA_BITS_TO_BYTES(bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10341 | |
| 10342 | /* Input sizes are the same as output sizes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10343 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10344 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE)); |
| 10345 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10346 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC)); |
| 10347 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10348 | 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] | 10349 | |
| 10350 | /* These inequalities will always hold even when other PAKEs are added */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10351 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10352 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10353 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10354 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10355 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10356 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10357 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10358 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10359 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10360 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10361 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10362 | PSA_PAKE_INPUT_MAX_SIZE); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10363 | } |
| 10364 | /* END_CASE */ |