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 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 9 | /* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random() |
| 10 | * uses mbedtls_ctr_drbg internally. */ |
| 11 | #include "mbedtls/ctr_drbg.h" |
| 12 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 13 | #include "psa/crypto.h" |
Ronald Cron | 4184107 | 2020-09-17 15:28:26 +0200 | [diff] [blame] | 14 | #include "psa_crypto_slot_management.h" |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 15 | |
Manuel Pégourié-Gonnard | 7abdf7e | 2023-03-09 11:17:43 +0100 | [diff] [blame] | 16 | /* For psa_can_do_hash() */ |
| 17 | #include "psa_crypto_core.h" |
| 18 | |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 19 | #include "test/asn1_helpers.h" |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 20 | #include "test/psa_crypto_helpers.h" |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 21 | #include "test/psa_exercise_key.h" |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 22 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 23 | #include "test/drivers/test_driver.h" |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 24 | #define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION |
| 25 | #else |
| 26 | #define TEST_DRIVER_LOCATION 0x7fffff |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 27 | #endif |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 28 | |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 29 | /* If this comes up, it's a bug in the test code or in the test data. */ |
| 30 | #define UNUSED 0xdeadbeef |
| 31 | |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 32 | /* Assert that an operation is (not) active. |
| 33 | * This serves as a proxy for checking if the operation is aborted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 34 | #define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0) |
| 35 | #define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 36 | |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 37 | #if defined(PSA_WANT_ALG_JPAKE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 38 | int ecjpake_operation_setup(psa_pake_operation_t *operation, |
| 39 | psa_pake_cipher_suite_t *cipher_suite, |
| 40 | psa_pake_role_t role, |
| 41 | mbedtls_svc_key_id_t key, |
| 42 | size_t key_available) |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 43 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 44 | PSA_ASSERT(psa_pake_abort(operation)); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 45 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 46 | PSA_ASSERT(psa_pake_setup(operation, cipher_suite)); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 47 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 48 | PSA_ASSERT(psa_pake_set_role(operation, role)); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 49 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 50 | if (key_available) { |
| 51 | PSA_ASSERT(psa_pake_set_password_key(operation, key)); |
| 52 | } |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 53 | return 0; |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 54 | exit: |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 55 | return 1; |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 56 | } |
| 57 | #endif |
| 58 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 59 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 60 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 61 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 62 | /** Test if a buffer contains a constant byte value. |
| 63 | * |
| 64 | * `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] | 65 | * |
| 66 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 67 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 68 | * \param size Size of the buffer in bytes. |
| 69 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 70 | * \return 1 if the buffer is all-bits-zero. |
| 71 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 72 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 73 | 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] | 74 | { |
| 75 | size_t i; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 76 | for (i = 0; i < size; i++) { |
| 77 | if (((unsigned char *) buffer)[i] != c) { |
| 78 | return 0; |
| 79 | } |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 80 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 81 | return 1; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 82 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 83 | #if defined(MBEDTLS_ASN1_WRITE_C) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 84 | /* 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] | 85 | static int asn1_write_10x(unsigned char **p, |
| 86 | unsigned char *start, |
| 87 | size_t bits, |
| 88 | unsigned char x) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 89 | { |
| 90 | int ret; |
| 91 | int len = bits / 8 + 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 92 | if (bits == 0) { |
| 93 | return MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 94 | } |
| 95 | if (bits <= 8 && x >= 1 << (bits - 1)) { |
| 96 | return MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 97 | } |
| 98 | if (*p < start || *p - start < (ptrdiff_t) len) { |
| 99 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 100 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 101 | *p -= len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 102 | (*p)[len-1] = x; |
| 103 | if (bits % 8 == 0) { |
| 104 | (*p)[1] |= 1; |
| 105 | } else { |
| 106 | (*p)[0] |= 1 << (bits % 8); |
| 107 | } |
| 108 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 109 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, |
| 110 | MBEDTLS_ASN1_INTEGER)); |
| 111 | return len; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 112 | } |
| 113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 114 | static int construct_fake_rsa_key(unsigned char *buffer, |
| 115 | size_t buffer_size, |
| 116 | unsigned char **p, |
| 117 | size_t bits, |
| 118 | int keypair) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 119 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 120 | size_t half_bits = (bits + 1) / 2; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 121 | int ret; |
| 122 | int len = 0; |
| 123 | /* Construct something that looks like a DER encoding of |
| 124 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 125 | * RSAPrivateKey ::= SEQUENCE { |
| 126 | * version Version, |
| 127 | * modulus INTEGER, -- n |
| 128 | * publicExponent INTEGER, -- e |
| 129 | * privateExponent INTEGER, -- d |
| 130 | * prime1 INTEGER, -- p |
| 131 | * prime2 INTEGER, -- q |
| 132 | * exponent1 INTEGER, -- d mod (p-1) |
| 133 | * exponent2 INTEGER, -- d mod (q-1) |
| 134 | * coefficient INTEGER, -- (inverse of q) mod p |
| 135 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 136 | * } |
| 137 | * Or, for a public key, the same structure with only |
| 138 | * version, modulus and publicExponent. |
| 139 | */ |
| 140 | *p = buffer + buffer_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 141 | if (keypair) { |
| 142 | MBEDTLS_ASN1_CHK_ADD(len, /* pq */ |
| 143 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 144 | MBEDTLS_ASN1_CHK_ADD(len, /* dq */ |
| 145 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 146 | MBEDTLS_ASN1_CHK_ADD(len, /* dp */ |
| 147 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 148 | MBEDTLS_ASN1_CHK_ADD(len, /* q */ |
| 149 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 150 | MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */ |
| 151 | asn1_write_10x(p, buffer, half_bits, 3)); |
| 152 | MBEDTLS_ASN1_CHK_ADD(len, /* d */ |
| 153 | asn1_write_10x(p, buffer, bits, 1)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 154 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 155 | MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */ |
| 156 | asn1_write_10x(p, buffer, 17, 1)); |
| 157 | MBEDTLS_ASN1_CHK_ADD(len, /* n */ |
| 158 | asn1_write_10x(p, buffer, bits, 1)); |
| 159 | if (keypair) { |
| 160 | MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */ |
| 161 | mbedtls_asn1_write_int(p, buffer, 0)); |
| 162 | } |
| 163 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 164 | { |
| 165 | const unsigned char tag = |
| 166 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 167 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 168 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 169 | return len; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 170 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 171 | #endif /* MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 172 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 173 | int exercise_mac_setup(psa_key_type_t key_type, |
| 174 | const unsigned char *key_bytes, |
| 175 | size_t key_length, |
| 176 | psa_algorithm_t alg, |
| 177 | psa_mac_operation_t *operation, |
| 178 | psa_status_t *status) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 179 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 180 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 181 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 183 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 184 | psa_set_key_algorithm(&attributes, alg); |
| 185 | psa_set_key_type(&attributes, key_type); |
| 186 | PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 187 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 188 | *status = psa_mac_sign_setup(operation, key, alg); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 189 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 190 | PSA_ASSERT(psa_mac_abort(operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 191 | /* If setup failed, reproduce the failure, so that the caller can |
| 192 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 193 | if (*status != PSA_SUCCESS) { |
| 194 | TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 195 | } |
| 196 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 197 | psa_destroy_key(key); |
| 198 | return 1; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 199 | |
| 200 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 201 | psa_destroy_key(key); |
| 202 | return 0; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 203 | } |
| 204 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 205 | int exercise_cipher_setup(psa_key_type_t key_type, |
| 206 | const unsigned char *key_bytes, |
| 207 | size_t key_length, |
| 208 | psa_algorithm_t alg, |
| 209 | psa_cipher_operation_t *operation, |
| 210 | psa_status_t *status) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 211 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 212 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 213 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 215 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 216 | psa_set_key_algorithm(&attributes, alg); |
| 217 | psa_set_key_type(&attributes, key_type); |
| 218 | PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 219 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 220 | *status = psa_cipher_encrypt_setup(operation, key, alg); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 221 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 222 | PSA_ASSERT(psa_cipher_abort(operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 223 | /* If setup failed, reproduce the failure, so that the caller can |
| 224 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 225 | if (*status != PSA_SUCCESS) { |
| 226 | TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg), |
| 227 | *status); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 228 | } |
| 229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 230 | psa_destroy_key(key); |
| 231 | return 1; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 232 | |
| 233 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 234 | psa_destroy_key(key); |
| 235 | return 0; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 236 | } |
| 237 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 238 | 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] | 239 | { |
| 240 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 241 | 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] | 242 | uint8_t buffer[1]; |
| 243 | size_t length; |
| 244 | int ok = 0; |
| 245 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 246 | psa_set_key_id(&attributes, key_id); |
| 247 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 248 | psa_set_key_algorithm(&attributes, PSA_ALG_CTR); |
| 249 | psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); |
| 250 | TEST_EQUAL(psa_get_key_attributes(key, &attributes), |
| 251 | PSA_ERROR_INVALID_HANDLE); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 252 | TEST_EQUAL( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 253 | 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] | 254 | TEST_EQUAL( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 255 | MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0); |
| 256 | TEST_EQUAL(psa_get_key_lifetime(&attributes), 0); |
| 257 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0); |
| 258 | TEST_EQUAL(psa_get_key_algorithm(&attributes), 0); |
| 259 | TEST_EQUAL(psa_get_key_type(&attributes), 0); |
| 260 | TEST_EQUAL(psa_get_key_bits(&attributes), 0); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 261 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 262 | TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length), |
| 263 | PSA_ERROR_INVALID_HANDLE); |
| 264 | TEST_EQUAL(psa_export_public_key(key, |
| 265 | buffer, sizeof(buffer), &length), |
| 266 | PSA_ERROR_INVALID_HANDLE); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 267 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 268 | ok = 1; |
| 269 | |
| 270 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 271 | /* |
| 272 | * Key attributes may have been returned by psa_get_key_attributes() |
| 273 | * thus reset them as required. |
| 274 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 275 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 276 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 277 | return ok; |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 278 | } |
| 279 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 280 | /* Assert that a key isn't reported as having a slot number. */ |
| 281 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 282 | #define ASSERT_NO_SLOT_NUMBER(attributes) \ |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 283 | do \ |
| 284 | { \ |
| 285 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 286 | TEST_EQUAL(psa_get_key_slot_number( \ |
| 287 | attributes, \ |
| 288 | &ASSERT_NO_SLOT_NUMBER_slot_number), \ |
| 289 | PSA_ERROR_INVALID_ARGUMENT); \ |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 290 | } \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 291 | while (0) |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 292 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 293 | #define ASSERT_NO_SLOT_NUMBER(attributes) \ |
| 294 | ((void) 0) |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 295 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 296 | |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 297 | #define INPUT_INTEGER 0x10000 /* Out of range of psa_key_type_t */ |
| 298 | |
| 299 | uint64_t parse_binary_string(data_t *bin_string) |
Kusumit Ghoderao | a14ae5a | 2023-04-19 14:16:26 +0530 | [diff] [blame] | 300 | { |
| 301 | uint64_t result = 0; |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 302 | TEST_LE_U(bin_string->len, 8); |
| 303 | for (size_t i = 0; i < bin_string->len; i++) { |
Kusumit Ghoderao | 0f2f996 | 2023-04-28 10:07:10 +0530 | [diff] [blame] | 304 | result = result << 8 | bin_string->x[i]; |
Kusumit Ghoderao | 3b27a7f | 2023-04-19 17:20:25 +0530 | [diff] [blame] | 305 | } |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 306 | exit: |
| 307 | return result; /* returns 0 if len > 8 */ |
Kusumit Ghoderao | a14ae5a | 2023-04-19 14:16:26 +0530 | [diff] [blame] | 308 | } |
| 309 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 310 | /* An overapproximation of the amount of storage needed for a key of the |
| 311 | * given type and with the given content. The API doesn't make it easy |
| 312 | * to find a good value for the size. The current implementation doesn't |
| 313 | * care about the value anyway. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 314 | #define KEY_BITS_FROM_DATA(type, data) \ |
| 315 | (data)->len |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 316 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 317 | typedef enum { |
| 318 | IMPORT_KEY = 0, |
| 319 | GENERATE_KEY = 1, |
| 320 | DERIVE_KEY = 2 |
| 321 | } generate_method; |
| 322 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 323 | typedef enum { |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 324 | DO_NOT_SET_LENGTHS = 0, |
| 325 | SET_LENGTHS_BEFORE_NONCE = 1, |
| 326 | SET_LENGTHS_AFTER_NONCE = 2 |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 327 | } set_lengths_method_t; |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 328 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 329 | typedef enum { |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 330 | USE_NULL_TAG = 0, |
| 331 | USE_GIVEN_TAG = 1, |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 332 | } tag_usage_method_t; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 333 | |
Kusumit Ghoderao | a14ae5a | 2023-04-19 14:16:26 +0530 | [diff] [blame] | 334 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 335 | /*! |
| 336 | * \brief Internal Function for AEAD multipart tests. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 337 | * \param key_type_arg Type of key passed in |
| 338 | * \param key_data The encryption / decryption key data |
| 339 | * \param alg_arg The type of algorithm used |
| 340 | * \param nonce Nonce data |
| 341 | * \param additional_data Additional data |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 342 | * \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] | 343 | * feed additional data in to be encrypted / |
| 344 | * decrypted. If -1, no chunking. |
| 345 | * \param input_data Data to encrypt / decrypt |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 346 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 347 | * the data in to be encrypted / decrypted. If |
| 348 | * -1, no chunking |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 349 | * \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] | 350 | * expected here, this controls whether or not |
| 351 | * to set lengths, and in what order with |
| 352 | * respect to set nonce. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 353 | * \param expected_output Expected output |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 354 | * \param is_encrypt If non-zero this is an encryption operation. |
Paul Elliott | 41ffae1 | 2021-07-22 21:52:01 +0100 | [diff] [blame] | 355 | * \param do_zero_parts If non-zero, interleave zero length chunks |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 356 | * with normal length chunks. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 357 | * \return int Zero on failure, non-zero on success. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 358 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 359 | static int aead_multipart_internal_func(int key_type_arg, data_t *key_data, |
| 360 | int alg_arg, |
| 361 | data_t *nonce, |
| 362 | data_t *additional_data, |
| 363 | int ad_part_len_arg, |
| 364 | data_t *input_data, |
| 365 | int data_part_len_arg, |
| 366 | set_lengths_method_t set_lengths_method, |
| 367 | data_t *expected_output, |
| 368 | int is_encrypt, |
| 369 | int do_zero_parts) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 370 | { |
| 371 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 372 | psa_key_type_t key_type = key_type_arg; |
| 373 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 374 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 375 | unsigned char *output_data = NULL; |
| 376 | unsigned char *part_data = NULL; |
| 377 | unsigned char *final_data = NULL; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 378 | size_t data_true_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 379 | size_t part_data_size = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 380 | size_t output_size = 0; |
| 381 | size_t final_output_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 382 | size_t output_length = 0; |
| 383 | size_t key_bits = 0; |
| 384 | size_t tag_length = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 385 | size_t part_offset = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 386 | size_t part_length = 0; |
| 387 | size_t output_part_length = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 388 | size_t tag_size = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 389 | size_t ad_part_len = 0; |
| 390 | size_t data_part_len = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 391 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 392 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 393 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 394 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 395 | int test_ok = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 396 | size_t part_count = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 397 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 398 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 399 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 400 | if (is_encrypt) { |
| 401 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 402 | } else { |
| 403 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 404 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 405 | |
| 406 | psa_set_key_algorithm(&attributes, alg); |
| 407 | psa_set_key_type(&attributes, key_type); |
| 408 | |
| 409 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 410 | &key)); |
| 411 | |
| 412 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 413 | key_bits = psa_get_key_bits(&attributes); |
| 414 | |
| 415 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); |
| 416 | |
| 417 | if (is_encrypt) { |
| 418 | /* Tag gets written at end of buffer. */ |
| 419 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 420 | (input_data->len + |
| 421 | tag_length)); |
| 422 | data_true_size = input_data->len; |
| 423 | } else { |
| 424 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 425 | (input_data->len - |
| 426 | tag_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 427 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 428 | /* Do not want to attempt to decrypt tag. */ |
| 429 | data_true_size = input_data->len - tag_length; |
| 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(output_data, 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 | final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
| 436 | TEST_LE_U(final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
| 437 | } else { |
| 438 | final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); |
| 439 | TEST_LE_U(final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 440 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 441 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 442 | TEST_CALLOC(final_data, final_output_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 444 | if (is_encrypt) { |
| 445 | status = psa_aead_encrypt_setup(&operation, key, alg); |
| 446 | } else { |
| 447 | status = psa_aead_decrypt_setup(&operation, key, alg); |
| 448 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 449 | |
| 450 | /* If the operation is not supported, just skip and not fail in case the |
| 451 | * encryption involves a common limitation of cryptography hardwares and |
| 452 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 453 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 454 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 455 | 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] | 456 | } |
| 457 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 458 | PSA_ASSERT(status); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 459 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 460 | if (set_lengths_method == DO_NOT_SET_LENGTHS) { |
| 461 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
| 462 | } else if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { |
| 463 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 464 | data_true_size)); |
| 465 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
| 466 | } else if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) { |
| 467 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 468 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 469 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 470 | data_true_size)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 471 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 472 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 473 | if (ad_part_len_arg != -1) { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 474 | /* Pass additional data in parts */ |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 475 | ad_part_len = (size_t) ad_part_len_arg; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 476 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 477 | for (part_offset = 0, part_count = 0; |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 478 | part_offset < additional_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 479 | part_offset += part_length, part_count++) { |
| 480 | if (do_zero_parts && (part_count & 0x01)) { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 481 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 482 | } else if (additional_data->len - part_offset < ad_part_len) { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 483 | part_length = additional_data->len - part_offset; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 484 | } else { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 485 | part_length = ad_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 486 | } |
| 487 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 488 | PSA_ASSERT(psa_aead_update_ad(&operation, |
| 489 | additional_data->x + part_offset, |
| 490 | part_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 491 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 492 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 493 | } else { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 494 | /* Pass additional data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 495 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 496 | additional_data->len)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 497 | } |
| 498 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 499 | if (data_part_len_arg != -1) { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 500 | /* Pass data in parts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 501 | data_part_len = (size_t) data_part_len_arg; |
| 502 | part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 503 | (size_t) data_part_len); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 504 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 505 | TEST_CALLOC(part_data, part_data_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 506 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 507 | for (part_offset = 0, part_count = 0; |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 508 | part_offset < data_true_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 509 | part_offset += part_length, part_count++) { |
| 510 | if (do_zero_parts && (part_count & 0x01)) { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 511 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 512 | } else if ((data_true_size - part_offset) < data_part_len) { |
| 513 | part_length = (data_true_size - part_offset); |
| 514 | } else { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 515 | part_length = data_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 516 | } |
| 517 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 518 | PSA_ASSERT(psa_aead_update(&operation, |
| 519 | (input_data->x + part_offset), |
| 520 | part_length, part_data, |
| 521 | part_data_size, |
| 522 | &output_part_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 523 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 524 | if (output_data && output_part_length) { |
| 525 | memcpy((output_data + output_length), part_data, |
| 526 | output_part_length); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 527 | } |
| 528 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 529 | output_length += output_part_length; |
| 530 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 531 | } else { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 532 | /* Pass all data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 533 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 534 | data_true_size, output_data, |
| 535 | output_size, &output_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 536 | } |
| 537 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 538 | if (is_encrypt) { |
| 539 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 540 | final_output_size, |
| 541 | &output_part_length, |
| 542 | tag_buffer, tag_length, |
| 543 | &tag_size)); |
| 544 | } else { |
| 545 | PSA_ASSERT(psa_aead_verify(&operation, final_data, |
| 546 | final_output_size, |
| 547 | &output_part_length, |
| 548 | (input_data->x + data_true_size), |
| 549 | tag_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 550 | } |
| 551 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 552 | if (output_data && output_part_length) { |
| 553 | memcpy((output_data + output_length), final_data, |
| 554 | output_part_length); |
| 555 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 556 | |
| 557 | output_length += output_part_length; |
| 558 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 559 | |
| 560 | /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE |
| 561 | * should be exact.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 562 | if (is_encrypt) { |
| 563 | TEST_EQUAL(tag_length, tag_size); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 564 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 565 | if (output_data && tag_length) { |
| 566 | memcpy((output_data + output_length), tag_buffer, |
| 567 | tag_length); |
| 568 | } |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 569 | |
| 570 | output_length += tag_length; |
| 571 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 572 | TEST_EQUAL(output_length, |
| 573 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 574 | input_data->len)); |
| 575 | TEST_LE_U(output_length, |
| 576 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
| 577 | } else { |
| 578 | TEST_EQUAL(output_length, |
| 579 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, |
| 580 | input_data->len)); |
| 581 | TEST_LE_U(output_length, |
| 582 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 583 | } |
| 584 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 585 | |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 586 | TEST_BUFFERS_EQUAL(expected_output->x, expected_output->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 587 | output_data, output_length); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 588 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 589 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 590 | test_ok = 1; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 591 | |
| 592 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 593 | psa_destroy_key(key); |
| 594 | psa_aead_abort(&operation); |
| 595 | mbedtls_free(output_data); |
| 596 | mbedtls_free(part_data); |
| 597 | mbedtls_free(final_data); |
| 598 | PSA_DONE(); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 599 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 600 | return test_ok; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 601 | } |
| 602 | |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 603 | /*! |
| 604 | * \brief Internal Function for MAC multipart tests. |
| 605 | * \param key_type_arg Type of key passed in |
| 606 | * \param key_data The encryption / decryption key data |
| 607 | * \param alg_arg The type of algorithm used |
| 608 | * \param input_data Data to encrypt / decrypt |
| 609 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 610 | * the data in to be encrypted / decrypted. If |
| 611 | * -1, no chunking |
| 612 | * \param expected_output Expected output |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 613 | * \param is_verify If non-zero this is a verify operation. |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 614 | * \param do_zero_parts If non-zero, interleave zero length chunks |
| 615 | * with normal length chunks. |
| 616 | * \return int Zero on failure, non-zero on success. |
| 617 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 618 | static int mac_multipart_internal_func(int key_type_arg, data_t *key_data, |
| 619 | int alg_arg, |
| 620 | data_t *input_data, |
| 621 | int data_part_len_arg, |
| 622 | data_t *expected_output, |
| 623 | int is_verify, |
| 624 | int do_zero_parts) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 625 | { |
| 626 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 627 | psa_key_type_t key_type = key_type_arg; |
| 628 | psa_algorithm_t alg = alg_arg; |
| 629 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 630 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
| 631 | size_t part_offset = 0; |
| 632 | size_t part_length = 0; |
| 633 | size_t data_part_len = 0; |
| 634 | size_t mac_len = 0; |
| 635 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 636 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 637 | |
| 638 | int test_ok = 0; |
| 639 | size_t part_count = 0; |
| 640 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 641 | PSA_INIT(); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 642 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 643 | if (is_verify) { |
| 644 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 645 | } else { |
| 646 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 647 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 648 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 649 | psa_set_key_algorithm(&attributes, alg); |
| 650 | psa_set_key_type(&attributes, key_type); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 651 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 652 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 653 | &key)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 654 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 655 | if (is_verify) { |
| 656 | status = psa_mac_verify_setup(&operation, key, alg); |
| 657 | } else { |
| 658 | status = psa_mac_sign_setup(&operation, key, alg); |
| 659 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 660 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 661 | PSA_ASSERT(status); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 662 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 663 | if (data_part_len_arg != -1) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 664 | /* Pass data in parts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 665 | data_part_len = (size_t) data_part_len_arg; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 666 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 667 | for (part_offset = 0, part_count = 0; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 668 | part_offset < input_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 669 | part_offset += part_length, part_count++) { |
| 670 | if (do_zero_parts && (part_count & 0x01)) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 671 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 672 | } else if ((input_data->len - part_offset) < data_part_len) { |
| 673 | part_length = (input_data->len - part_offset); |
| 674 | } else { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 675 | part_length = data_part_len; |
| 676 | } |
| 677 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 678 | PSA_ASSERT(psa_mac_update(&operation, |
| 679 | (input_data->x + part_offset), |
| 680 | part_length)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 681 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 682 | } else { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 683 | /* Pass all data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 684 | PSA_ASSERT(psa_mac_update(&operation, input_data->x, |
| 685 | input_data->len)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 686 | } |
| 687 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 688 | if (is_verify) { |
| 689 | PSA_ASSERT(psa_mac_verify_finish(&operation, expected_output->x, |
| 690 | expected_output->len)); |
| 691 | } else { |
| 692 | PSA_ASSERT(psa_mac_sign_finish(&operation, mac, |
| 693 | PSA_MAC_MAX_SIZE, &mac_len)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 694 | |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 695 | TEST_BUFFERS_EQUAL(expected_output->x, expected_output->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 696 | mac, mac_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | test_ok = 1; |
| 700 | |
| 701 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 702 | psa_destroy_key(key); |
| 703 | psa_mac_abort(&operation); |
| 704 | PSA_DONE(); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 705 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 706 | return test_ok; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 707 | } |
| 708 | |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 709 | #if defined(PSA_WANT_ALG_JPAKE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 710 | static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive, |
| 711 | psa_pake_operation_t *server, |
| 712 | psa_pake_operation_t *client, |
| 713 | int client_input_first, |
| 714 | int round, int inject_error) |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 715 | { |
| 716 | unsigned char *buffer0 = NULL, *buffer1 = NULL; |
| 717 | size_t buffer_length = ( |
| 718 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) + |
| 719 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) + |
| 720 | 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] | 721 | /* The output should be exactly this size according to the spec */ |
| 722 | const size_t expected_size_key_share = |
| 723 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE); |
| 724 | /* The output should be exactly this size according to the spec */ |
| 725 | const size_t expected_size_zk_public = |
| 726 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC); |
| 727 | /* The output can be smaller: the spec allows stripping leading zeroes */ |
| 728 | const size_t max_expected_size_zk_proof = |
| 729 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 730 | size_t buffer0_off = 0; |
| 731 | size_t buffer1_off = 0; |
| 732 | size_t s_g1_len, s_g2_len, s_a_len; |
| 733 | size_t s_g1_off, s_g2_off, s_a_off; |
| 734 | size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len; |
| 735 | size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off; |
| 736 | size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len; |
| 737 | size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off; |
| 738 | size_t c_g1_len, c_g2_len, c_a_len; |
| 739 | size_t c_g1_off, c_g2_off, c_a_off; |
| 740 | size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len; |
| 741 | size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off; |
| 742 | size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len; |
| 743 | size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off; |
| 744 | psa_status_t expected_status = PSA_SUCCESS; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 745 | psa_status_t status; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 746 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 747 | TEST_CALLOC(buffer0, buffer_length); |
| 748 | TEST_CALLOC(buffer1, buffer_length); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 749 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 750 | switch (round) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 751 | case 1: |
| 752 | /* Server first round Output */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 753 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 754 | buffer0 + buffer0_off, |
| 755 | 512 - buffer0_off, &s_g1_len)); |
| 756 | TEST_EQUAL(s_g1_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 757 | s_g1_off = buffer0_off; |
| 758 | buffer0_off += s_g1_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 759 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 760 | buffer0 + buffer0_off, |
| 761 | 512 - buffer0_off, &s_x1_pk_len)); |
| 762 | TEST_EQUAL(s_x1_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 763 | s_x1_pk_off = buffer0_off; |
| 764 | buffer0_off += s_x1_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 765 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 766 | buffer0 + buffer0_off, |
| 767 | 512 - buffer0_off, &s_x1_pr_len)); |
| 768 | TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 769 | s_x1_pr_off = buffer0_off; |
| 770 | buffer0_off += s_x1_pr_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 771 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 772 | buffer0 + buffer0_off, |
| 773 | 512 - buffer0_off, &s_g2_len)); |
| 774 | TEST_EQUAL(s_g2_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 775 | s_g2_off = buffer0_off; |
| 776 | buffer0_off += s_g2_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 777 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 778 | buffer0 + buffer0_off, |
| 779 | 512 - buffer0_off, &s_x2_pk_len)); |
| 780 | TEST_EQUAL(s_x2_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 781 | s_x2_pk_off = buffer0_off; |
| 782 | buffer0_off += s_x2_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 783 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 784 | buffer0 + buffer0_off, |
| 785 | 512 - buffer0_off, &s_x2_pr_len)); |
| 786 | TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 787 | s_x2_pr_off = buffer0_off; |
| 788 | buffer0_off += s_x2_pr_len; |
| 789 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 790 | if (inject_error == 1) { |
Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 791 | buffer0[s_x1_pr_off + 8] ^= 1; |
| 792 | buffer0[s_x2_pr_off + 7] ^= 1; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 793 | expected_status = PSA_ERROR_DATA_INVALID; |
| 794 | } |
| 795 | |
Neil Armstrong | 51009d7 | 2022-09-05 17:59:54 +0200 | [diff] [blame] | 796 | /* |
| 797 | * When injecting errors in inputs, the implementation is |
| 798 | * free to detect it right away of with a delay. |
| 799 | * This permits delaying the error until the end of the input |
| 800 | * sequence, if no error appears then, this will be treated |
| 801 | * as an error. |
| 802 | */ |
| 803 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 804 | if (client_input_first == 1) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 805 | /* Client first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 806 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 807 | buffer0 + s_g1_off, s_g1_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_PUBLIC, |
| 816 | buffer0 + s_x1_pk_off, |
| 817 | s_x1_pk_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_ZK_PROOF, |
| 826 | buffer0 + s_x1_pr_off, |
| 827 | s_x1_pr_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_KEY_SHARE, |
| 836 | buffer0 + s_g2_off, |
| 837 | s_g2_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_PUBLIC, |
| 846 | buffer0 + s_x2_pk_off, |
| 847 | s_x2_pk_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 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 855 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 856 | buffer0 + s_x2_pr_off, |
| 857 | s_x2_pr_len); |
| 858 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 859 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 860 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 861 | } else { |
| 862 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 863 | } |
| 864 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 865 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 866 | if (inject_error == 1) { |
| 867 | TEST_ASSERT( |
| 868 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 869 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | /* Client first round Output */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 873 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 874 | buffer1 + buffer1_off, |
| 875 | 512 - buffer1_off, &c_g1_len)); |
| 876 | TEST_EQUAL(c_g1_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 877 | c_g1_off = buffer1_off; |
| 878 | buffer1_off += c_g1_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 879 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 880 | buffer1 + buffer1_off, |
| 881 | 512 - buffer1_off, &c_x1_pk_len)); |
| 882 | TEST_EQUAL(c_x1_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 883 | c_x1_pk_off = buffer1_off; |
| 884 | buffer1_off += c_x1_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 885 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 886 | buffer1 + buffer1_off, |
| 887 | 512 - buffer1_off, &c_x1_pr_len)); |
| 888 | TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 889 | c_x1_pr_off = buffer1_off; |
| 890 | buffer1_off += c_x1_pr_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 891 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 892 | buffer1 + buffer1_off, |
| 893 | 512 - buffer1_off, &c_g2_len)); |
| 894 | TEST_EQUAL(c_g2_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 895 | c_g2_off = buffer1_off; |
| 896 | buffer1_off += c_g2_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 897 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 898 | buffer1 + buffer1_off, |
| 899 | 512 - buffer1_off, &c_x2_pk_len)); |
| 900 | TEST_EQUAL(c_x2_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 901 | c_x2_pk_off = buffer1_off; |
| 902 | buffer1_off += c_x2_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 903 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 904 | buffer1 + buffer1_off, |
| 905 | 512 - buffer1_off, &c_x2_pr_len)); |
| 906 | TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 907 | c_x2_pr_off = buffer1_off; |
| 908 | buffer1_off += c_x2_pr_len; |
| 909 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 910 | if (client_input_first == 0) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 911 | /* Client first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 912 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 913 | buffer0 + s_g1_off, s_g1_len); |
| 914 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 915 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +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_PUBLIC, |
| 922 | buffer0 + s_x1_pk_off, |
| 923 | s_x1_pk_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_ZK_PROOF, |
| 932 | buffer0 + s_x1_pr_off, |
| 933 | s_x1_pr_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_KEY_SHARE, |
| 942 | buffer0 + s_g2_off, |
| 943 | s_g2_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_PUBLIC, |
| 952 | buffer0 + s_x2_pk_off, |
| 953 | s_x2_pk_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 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 961 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 962 | buffer0 + s_x2_pr_off, |
| 963 | s_x2_pr_len); |
| 964 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 965 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 966 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 967 | } else { |
| 968 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 969 | } |
| 970 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 971 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 972 | if (inject_error == 1) { |
| 973 | TEST_ASSERT( |
| 974 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 975 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 976 | } |
| 977 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 978 | if (inject_error == 2) { |
Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 979 | buffer1[c_x1_pr_off + 12] ^= 1; |
| 980 | buffer1[c_x2_pr_off + 7] ^= 1; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 981 | expected_status = PSA_ERROR_DATA_INVALID; |
| 982 | } |
| 983 | |
| 984 | /* Server first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 985 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 986 | buffer1 + c_g1_off, c_g1_len); |
| 987 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 988 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 989 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 990 | } else { |
| 991 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 992 | } |
| 993 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 994 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 995 | buffer1 + c_x1_pk_off, c_x1_pk_len); |
| 996 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 997 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 998 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 999 | } else { |
| 1000 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1001 | } |
| 1002 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1003 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1004 | buffer1 + c_x1_pr_off, c_x1_pr_len); |
| 1005 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1006 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1007 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1008 | } else { |
| 1009 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1010 | } |
| 1011 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1012 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 1013 | buffer1 + c_g2_off, c_g2_len); |
| 1014 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1015 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1016 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1017 | } else { |
| 1018 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1019 | } |
| 1020 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1021 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1022 | buffer1 + c_x2_pk_off, c_x2_pk_len); |
| 1023 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1024 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1025 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1026 | } else { |
| 1027 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1028 | } |
| 1029 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1030 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1031 | buffer1 + c_x2_pr_off, c_x2_pr_len); |
| 1032 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1033 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1034 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1035 | } else { |
| 1036 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1037 | } |
| 1038 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1039 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1040 | if (inject_error == 2) { |
| 1041 | TEST_ASSERT( |
| 1042 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1043 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1044 | |
| 1045 | break; |
| 1046 | |
| 1047 | case 2: |
| 1048 | /* Server second round Output */ |
| 1049 | buffer0_off = 0; |
| 1050 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1051 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 1052 | buffer0 + buffer0_off, |
| 1053 | 512 - buffer0_off, &s_a_len)); |
| 1054 | TEST_EQUAL(s_a_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1055 | s_a_off = buffer0_off; |
| 1056 | buffer0_off += s_a_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1057 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1058 | buffer0 + buffer0_off, |
| 1059 | 512 - buffer0_off, &s_x2s_pk_len)); |
| 1060 | TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1061 | s_x2s_pk_off = buffer0_off; |
| 1062 | buffer0_off += s_x2s_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1063 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1064 | buffer0 + buffer0_off, |
| 1065 | 512 - buffer0_off, &s_x2s_pr_len)); |
| 1066 | TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1067 | s_x2s_pr_off = buffer0_off; |
| 1068 | buffer0_off += s_x2s_pr_len; |
| 1069 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1070 | if (inject_error == 3) { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1071 | buffer0[s_x2s_pk_off + 12] += 0x33; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1072 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1073 | } |
| 1074 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1075 | if (client_input_first == 1) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1076 | /* Client second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1077 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1078 | buffer0 + s_a_off, s_a_len); |
| 1079 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1080 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +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_PUBLIC, |
| 1087 | buffer0 + s_x2s_pk_off, |
| 1088 | s_x2s_pk_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 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1096 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1097 | buffer0 + s_x2s_pr_off, |
| 1098 | s_x2s_pr_len); |
| 1099 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1100 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1101 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1102 | } else { |
| 1103 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1104 | } |
| 1105 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1106 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1107 | if (inject_error == 3) { |
| 1108 | TEST_ASSERT( |
| 1109 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1110 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | /* Client second round Output */ |
| 1114 | buffer1_off = 0; |
| 1115 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1116 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1117 | buffer1 + buffer1_off, |
| 1118 | 512 - buffer1_off, &c_a_len)); |
| 1119 | TEST_EQUAL(c_a_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1120 | c_a_off = buffer1_off; |
| 1121 | buffer1_off += c_a_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1122 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1123 | buffer1 + buffer1_off, |
| 1124 | 512 - buffer1_off, &c_x2s_pk_len)); |
| 1125 | TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1126 | c_x2s_pk_off = buffer1_off; |
| 1127 | buffer1_off += c_x2s_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1128 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1129 | buffer1 + buffer1_off, |
| 1130 | 512 - buffer1_off, &c_x2s_pr_len)); |
| 1131 | TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1132 | c_x2s_pr_off = buffer1_off; |
| 1133 | buffer1_off += c_x2s_pr_len; |
| 1134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1135 | if (client_input_first == 0) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1136 | /* Client second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1137 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1138 | buffer0 + s_a_off, s_a_len); |
| 1139 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1140 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +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_PUBLIC, |
| 1147 | buffer0 + s_x2s_pk_off, |
| 1148 | s_x2s_pk_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 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1156 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1157 | buffer0 + s_x2s_pr_off, |
| 1158 | s_x2s_pr_len); |
| 1159 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1160 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1161 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1162 | } else { |
| 1163 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1164 | } |
| 1165 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1166 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1167 | if (inject_error == 3) { |
| 1168 | TEST_ASSERT( |
| 1169 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1170 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1171 | } |
| 1172 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1173 | if (inject_error == 4) { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1174 | buffer1[c_x2s_pk_off + 7] += 0x28; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1175 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1176 | } |
| 1177 | |
| 1178 | /* Server second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1179 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 1180 | buffer1 + c_a_off, c_a_len); |
| 1181 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1182 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1183 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1184 | } else { |
| 1185 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1186 | } |
| 1187 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1188 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1189 | buffer1 + c_x2s_pk_off, c_x2s_pk_len); |
| 1190 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1191 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1192 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1193 | } else { |
| 1194 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1195 | } |
| 1196 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1197 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1198 | buffer1 + c_x2s_pr_off, c_x2s_pr_len); |
| 1199 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1200 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1201 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1202 | } else { |
| 1203 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1204 | } |
| 1205 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1206 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1207 | if (inject_error == 4) { |
| 1208 | TEST_ASSERT( |
| 1209 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1210 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1211 | |
| 1212 | break; |
| 1213 | |
| 1214 | } |
| 1215 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1216 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1217 | mbedtls_free(buffer0); |
| 1218 | mbedtls_free(buffer1); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1219 | } |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 1220 | #endif /* PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1221 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1222 | typedef enum { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 1223 | INJECT_ERR_NONE = 0, |
| 1224 | INJECT_ERR_UNINITIALIZED_ACCESS, |
| 1225 | INJECT_ERR_DUPLICATE_SETUP, |
| 1226 | INJECT_ERR_INVALID_USER, |
| 1227 | INJECT_ERR_INVALID_PEER, |
| 1228 | INJECT_ERR_SET_USER, |
| 1229 | INJECT_ERR_SET_PEER, |
| 1230 | INJECT_EMPTY_IO_BUFFER, |
| 1231 | INJECT_UNKNOWN_STEP, |
| 1232 | INJECT_INVALID_FIRST_STEP, |
| 1233 | INJECT_WRONG_BUFFER_SIZE, |
| 1234 | INJECT_VALID_OPERATION_AFTER_FAILURE, |
| 1235 | INJECT_ANTICIPATE_KEY_DERIVATION_1, |
| 1236 | INJECT_ANTICIPATE_KEY_DERIVATION_2, |
| 1237 | } ecjpake_injected_failure_t; |
| 1238 | |
Paul Elliott | 01885fa | 2023-02-09 12:07:30 +0000 | [diff] [blame] | 1239 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Paul Elliott | 1243f93 | 2023-02-07 11:21:10 +0000 | [diff] [blame] | 1240 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1241 | static void interruptible_signverify_get_minmax_completes(uint32_t max_ops, |
| 1242 | psa_status_t expected_status, |
| 1243 | size_t *min_completes, |
| 1244 | size_t *max_completes) |
| 1245 | { |
| 1246 | |
| 1247 | /* This is slightly contrived, but we only really know that with a minimum |
| 1248 | value of max_ops that a successful operation should take more than one op |
| 1249 | to complete, and likewise that with a max_ops of |
| 1250 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */ |
| 1251 | if (max_ops == 0 || max_ops == 1) { |
Paul Elliott | c86d45e | 2023-02-15 17:38:05 +0000 | [diff] [blame] | 1252 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1253 | if (expected_status == PSA_SUCCESS) { |
| 1254 | *min_completes = 2; |
| 1255 | } else { |
| 1256 | *min_completes = 1; |
| 1257 | } |
| 1258 | |
| 1259 | *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED; |
| 1260 | } else { |
| 1261 | *min_completes = 1; |
| 1262 | *max_completes = 1; |
| 1263 | } |
| 1264 | } |
Paul Elliott | 01885fa | 2023-02-09 12:07:30 +0000 | [diff] [blame] | 1265 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1266 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1267 | /* END_HEADER */ |
| 1268 | |
| 1269 | /* BEGIN_DEPENDENCIES |
| 1270 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1271 | * END_DEPENDENCIES |
| 1272 | */ |
| 1273 | |
| 1274 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 7abdf7e | 2023-03-09 11:17:43 +0100 | [diff] [blame] | 1275 | void psa_can_do_hash() |
| 1276 | { |
| 1277 | /* We can't test that this is specific to drivers until partial init has |
| 1278 | * been implemented, but we can at least test before/after full init. */ |
| 1279 | TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE)); |
| 1280 | PSA_INIT(); |
| 1281 | TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE)); |
| 1282 | PSA_DONE(); |
| 1283 | } |
| 1284 | /* END_CASE */ |
| 1285 | |
| 1286 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1287 | void static_checks() |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1288 | { |
| 1289 | size_t max_truncated_mac_size = |
| 1290 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1291 | |
| 1292 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1293 | * encoding. The shifted mask is the maximum truncated value. The |
| 1294 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1295 | TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size); |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1296 | } |
| 1297 | /* END_CASE */ |
| 1298 | |
| 1299 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1300 | void import_with_policy(int type_arg, |
| 1301 | int usage_arg, int alg_arg, |
| 1302 | int expected_status_arg) |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1303 | { |
| 1304 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1305 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1306 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1307 | psa_key_type_t type = type_arg; |
| 1308 | psa_key_usage_t usage = usage_arg; |
| 1309 | psa_algorithm_t alg = alg_arg; |
| 1310 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1311 | const uint8_t key_material[16] = { 0 }; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1312 | psa_status_t status; |
| 1313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1314 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1315 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1316 | psa_set_key_type(&attributes, type); |
| 1317 | psa_set_key_usage_flags(&attributes, usage); |
| 1318 | psa_set_key_algorithm(&attributes, alg); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1319 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1320 | status = psa_import_key(&attributes, |
| 1321 | key_material, sizeof(key_material), |
| 1322 | &key); |
| 1323 | TEST_EQUAL(status, expected_status); |
| 1324 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1325 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1326 | } |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1327 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1328 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1329 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1330 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), |
| 1331 | mbedtls_test_update_key_usage_flags(usage)); |
| 1332 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 1333 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1334 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1335 | PSA_ASSERT(psa_destroy_key(key)); |
| 1336 | test_operations_on_invalid_key(key); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1337 | |
| 1338 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1339 | /* |
| 1340 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1341 | * thus reset them as required. |
| 1342 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1343 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1344 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1345 | psa_destroy_key(key); |
| 1346 | PSA_DONE(); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1347 | } |
| 1348 | /* END_CASE */ |
| 1349 | |
| 1350 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1351 | void import_with_data(data_t *data, int type_arg, |
| 1352 | int attr_bits_arg, |
| 1353 | int expected_status_arg) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1354 | { |
| 1355 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1356 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1357 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1358 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1359 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1360 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1361 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1362 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1363 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1364 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1365 | psa_set_key_type(&attributes, type); |
| 1366 | psa_set_key_bits(&attributes, attr_bits); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1367 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1368 | status = psa_import_key(&attributes, data->x, data->len, &key); |
| 1369 | TEST_EQUAL(status, expected_status); |
| 1370 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1371 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1372 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1374 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1375 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1376 | if (attr_bits != 0) { |
| 1377 | TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes)); |
| 1378 | } |
| 1379 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1380 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1381 | PSA_ASSERT(psa_destroy_key(key)); |
| 1382 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1383 | |
| 1384 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1385 | /* |
| 1386 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1387 | * thus reset them as required. |
| 1388 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1389 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1390 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1391 | psa_destroy_key(key); |
| 1392 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1393 | } |
| 1394 | /* END_CASE */ |
| 1395 | |
| 1396 | /* BEGIN_CASE */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1397 | /* Construct and attempt to import a large unstructured key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1398 | void import_large_key(int type_arg, int byte_size_arg, |
| 1399 | int expected_status_arg) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1400 | { |
| 1401 | psa_key_type_t type = type_arg; |
| 1402 | size_t byte_size = byte_size_arg; |
| 1403 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1404 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1405 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1406 | psa_status_t status; |
| 1407 | uint8_t *buffer = NULL; |
| 1408 | size_t buffer_size = byte_size + 1; |
| 1409 | size_t n; |
| 1410 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1411 | /* Skip the test case if the target running the test cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1412 | * accommodate large keys due to heap size constraints */ |
Tom Cosgrove | 412a813 | 2023-07-20 16:55:14 +0100 | [diff] [blame] | 1413 | TEST_CALLOC_OR_SKIP(buffer, buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1414 | memset(buffer, 'K', byte_size); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1415 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1416 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1417 | |
| 1418 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1419 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1420 | psa_set_key_type(&attributes, type); |
| 1421 | status = psa_import_key(&attributes, buffer, byte_size, &key); |
| 1422 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 1423 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1424 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1425 | if (status == PSA_SUCCESS) { |
| 1426 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1427 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 1428 | TEST_EQUAL(psa_get_key_bits(&attributes), |
| 1429 | PSA_BYTES_TO_BITS(byte_size)); |
| 1430 | ASSERT_NO_SLOT_NUMBER(&attributes); |
| 1431 | memset(buffer, 0, byte_size + 1); |
| 1432 | PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n)); |
| 1433 | for (n = 0; n < byte_size; n++) { |
| 1434 | TEST_EQUAL(buffer[n], 'K'); |
| 1435 | } |
| 1436 | for (n = byte_size; n < buffer_size; n++) { |
| 1437 | TEST_EQUAL(buffer[n], 0); |
| 1438 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1442 | /* |
| 1443 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1444 | * thus reset them as required. |
| 1445 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1446 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1447 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1448 | psa_destroy_key(key); |
| 1449 | PSA_DONE(); |
| 1450 | mbedtls_free(buffer); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1451 | } |
| 1452 | /* END_CASE */ |
| 1453 | |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 1454 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1455 | /* Import an RSA key with a valid structure (but not valid numbers |
| 1456 | * inside, beyond having sensible size and parity). This is expected to |
| 1457 | * fail for large keys. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1458 | 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] | 1459 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1460 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1461 | size_t bits = bits_arg; |
| 1462 | psa_status_t expected_status = expected_status_arg; |
| 1463 | psa_status_t status; |
| 1464 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1465 | 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] | 1466 | size_t buffer_size = /* Slight overapproximations */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1467 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1468 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1469 | unsigned char *p; |
| 1470 | int ret; |
| 1471 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1472 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1473 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1474 | PSA_ASSERT(psa_crypto_init()); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 1475 | TEST_CALLOC(buffer, buffer_size); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1476 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1477 | TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p, |
| 1478 | bits, keypair)) >= 0); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1479 | length = ret; |
| 1480 | |
| 1481 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1482 | psa_set_key_type(&attributes, type); |
| 1483 | status = psa_import_key(&attributes, p, length, &key); |
| 1484 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1485 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1486 | if (status == PSA_SUCCESS) { |
| 1487 | PSA_ASSERT(psa_destroy_key(key)); |
| 1488 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1489 | |
| 1490 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1491 | mbedtls_free(buffer); |
| 1492 | PSA_DONE(); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1493 | } |
| 1494 | /* END_CASE */ |
| 1495 | |
| 1496 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1497 | void import_export(data_t *data, |
| 1498 | int type_arg, |
| 1499 | int usage_arg, int alg_arg, |
| 1500 | int lifetime_arg, |
| 1501 | int expected_bits, |
| 1502 | int export_size_delta, |
| 1503 | int expected_export_status_arg, |
| 1504 | /*whether reexport must give the original input exactly*/ |
| 1505 | int canonical_input) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1506 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1507 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1508 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1509 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1510 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1511 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1512 | psa_key_lifetime_t lifetime = lifetime_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1513 | unsigned char *exported = NULL; |
| 1514 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1515 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1516 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1517 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1518 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1519 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1520 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1521 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 1522 | TEST_CALLOC(exported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1523 | if (!canonical_input) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 1524 | TEST_CALLOC(reexported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1525 | } |
| 1526 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1527 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1528 | psa_set_key_lifetime(&attributes, lifetime); |
| 1529 | psa_set_key_usage_flags(&attributes, usage_arg); |
| 1530 | psa_set_key_algorithm(&attributes, alg); |
| 1531 | psa_set_key_type(&attributes, type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1532 | |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1533 | if (PSA_KEY_TYPE_IS_DH(type) && |
| 1534 | expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) { |
Przemek Stekiel | 654bef0 | 2022-12-15 13:28:02 +0100 | [diff] [blame] | 1535 | /* Simulate that buffer is too small, by decreasing its size by 1 byte. */ |
| 1536 | export_size -= 1; |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1537 | } |
| 1538 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1539 | /* Import the key */ |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1540 | TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key), |
Przemek Stekiel | 2e7c33d | 2023-04-27 12:29:45 +0200 | [diff] [blame] | 1541 | PSA_SUCCESS); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1542 | |
| 1543 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1544 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1545 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1546 | TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits); |
| 1547 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1548 | |
| 1549 | /* Export the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1550 | status = psa_export_key(key, exported, export_size, &exported_length); |
| 1551 | TEST_EQUAL(status, expected_export_status); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1552 | |
| 1553 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1554 | * and export_size. On errors, the exported length must be 0. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1555 | TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH); |
| 1556 | TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0); |
| 1557 | TEST_LE_U(exported_length, export_size); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1558 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1559 | TEST_ASSERT(mem_is_char(exported + exported_length, 0, |
| 1560 | export_size - exported_length)); |
| 1561 | if (status != PSA_SUCCESS) { |
| 1562 | TEST_EQUAL(exported_length, 0); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1563 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1564 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1565 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1566 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1567 | * this validates the canonical representations. For canonical inputs, |
| 1568 | * this doesn't directly validate the implementation, but it still helps |
| 1569 | * by cross-validating the test data with the sanity check code. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1570 | if (!psa_key_lifetime_is_external(lifetime)) { |
| 1571 | if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) { |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1572 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1573 | } |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1574 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1575 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1576 | if (canonical_input) { |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 1577 | TEST_BUFFERS_EQUAL(data->x, data->len, exported, exported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1578 | } else { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1579 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1580 | PSA_ASSERT(psa_import_key(&attributes, exported, exported_length, |
| 1581 | &key2)); |
| 1582 | PSA_ASSERT(psa_export_key(key2, |
| 1583 | reexported, |
| 1584 | export_size, |
| 1585 | &reexported_length)); |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 1586 | TEST_BUFFERS_EQUAL(exported, exported_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1587 | reexported, reexported_length); |
| 1588 | PSA_ASSERT(psa_destroy_key(key2)); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1589 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1590 | TEST_LE_U(exported_length, |
| 1591 | PSA_EXPORT_KEY_OUTPUT_SIZE(type, |
| 1592 | psa_get_key_bits(&got_attributes))); |
| 1593 | TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1594 | |
| 1595 | destroy: |
| 1596 | /* Destroy the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1597 | PSA_ASSERT(psa_destroy_key(key)); |
| 1598 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1599 | |
| 1600 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1601 | /* |
| 1602 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1603 | * thus reset them as required. |
| 1604 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1605 | psa_reset_key_attributes(&got_attributes); |
| 1606 | psa_destroy_key(key); |
| 1607 | mbedtls_free(exported); |
| 1608 | mbedtls_free(reexported); |
| 1609 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1610 | } |
| 1611 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1612 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1613 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1614 | void import_export_public_key(data_t *data, |
| 1615 | int type_arg, // key pair or public key |
| 1616 | int alg_arg, |
| 1617 | int lifetime_arg, |
| 1618 | int export_size_delta, |
| 1619 | int expected_export_status_arg, |
| 1620 | data_t *expected_public_key) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1621 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1622 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1623 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1624 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1625 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1626 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1627 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1628 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1629 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1630 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1631 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1632 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1633 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1634 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1635 | psa_set_key_lifetime(&attributes, lifetime); |
| 1636 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1637 | psa_set_key_algorithm(&attributes, alg); |
| 1638 | psa_set_key_type(&attributes, type); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1639 | |
| 1640 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1641 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1642 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1643 | /* Export the public key */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 1644 | TEST_CALLOC(exported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1645 | status = psa_export_public_key(key, |
| 1646 | exported, export_size, |
| 1647 | &exported_length); |
| 1648 | TEST_EQUAL(status, expected_export_status); |
| 1649 | if (status == PSA_SUCCESS) { |
| 1650 | 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] | 1651 | size_t bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1652 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1653 | bits = psa_get_key_bits(&attributes); |
| 1654 | TEST_LE_U(expected_public_key->len, |
| 1655 | PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1656 | TEST_LE_U(expected_public_key->len, |
| 1657 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1658 | TEST_LE_U(expected_public_key->len, |
| 1659 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 1660 | TEST_BUFFERS_EQUAL(expected_public_key->x, expected_public_key->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1661 | exported, exported_length); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1662 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1663 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1664 | /* |
| 1665 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1666 | * thus reset them as required. |
| 1667 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1668 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1669 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1670 | mbedtls_free(exported); |
| 1671 | psa_destroy_key(key); |
| 1672 | PSA_DONE(); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1673 | } |
| 1674 | /* END_CASE */ |
| 1675 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1676 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1677 | void import_and_exercise_key(data_t *data, |
| 1678 | int type_arg, |
| 1679 | int bits_arg, |
| 1680 | int alg_arg) |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1681 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1682 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1683 | psa_key_type_t type = type_arg; |
| 1684 | size_t bits = bits_arg; |
| 1685 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1686 | 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] | 1687 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1688 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1689 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1690 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1691 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1692 | psa_set_key_usage_flags(&attributes, usage); |
| 1693 | psa_set_key_algorithm(&attributes, alg); |
| 1694 | psa_set_key_type(&attributes, type); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1695 | |
| 1696 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1697 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1698 | |
| 1699 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1700 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1701 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1702 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1703 | |
| 1704 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1705 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1706 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1707 | } |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1708 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1709 | PSA_ASSERT(psa_destroy_key(key)); |
| 1710 | test_operations_on_invalid_key(key); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1711 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1712 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1713 | /* |
| 1714 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1715 | * thus reset them as required. |
| 1716 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1717 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1718 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1719 | psa_reset_key_attributes(&attributes); |
| 1720 | psa_destroy_key(key); |
| 1721 | PSA_DONE(); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1722 | } |
| 1723 | /* END_CASE */ |
| 1724 | |
| 1725 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1726 | void effective_key_attributes(int type_arg, int expected_type_arg, |
| 1727 | int bits_arg, int expected_bits_arg, |
| 1728 | int usage_arg, int expected_usage_arg, |
| 1729 | int alg_arg, int expected_alg_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1730 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1731 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1732 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1733 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1734 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1735 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1736 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1737 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1738 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1739 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1740 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1741 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1742 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1743 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1744 | psa_set_key_usage_flags(&attributes, usage); |
| 1745 | psa_set_key_algorithm(&attributes, alg); |
| 1746 | psa_set_key_type(&attributes, key_type); |
| 1747 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1748 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1749 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
| 1750 | psa_reset_key_attributes(&attributes); |
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_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1753 | TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type); |
| 1754 | TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits); |
| 1755 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
| 1756 | TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1757 | |
| 1758 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1759 | /* |
| 1760 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1761 | * thus reset them as required. |
| 1762 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1763 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1764 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1765 | psa_destroy_key(key); |
| 1766 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1767 | } |
| 1768 | /* END_CASE */ |
| 1769 | |
| 1770 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1771 | void check_key_policy(int type_arg, int bits_arg, |
| 1772 | int usage_arg, int alg_arg) |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1773 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1774 | test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg, |
| 1775 | usage_arg, |
| 1776 | mbedtls_test_update_key_usage_flags(usage_arg), |
| 1777 | alg_arg, alg_arg); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1778 | goto exit; |
| 1779 | } |
| 1780 | /* END_CASE */ |
| 1781 | |
| 1782 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1783 | void key_attributes_init() |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1784 | { |
| 1785 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1786 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1787 | * 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] | 1788 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1789 | psa_key_attributes_t func = psa_key_attributes_init(); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1790 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1791 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1792 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1793 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1794 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1795 | TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE); |
| 1796 | TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE); |
| 1797 | TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1798 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1799 | TEST_EQUAL(psa_get_key_type(&func), 0); |
| 1800 | TEST_EQUAL(psa_get_key_type(&init), 0); |
| 1801 | TEST_EQUAL(psa_get_key_type(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1802 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1803 | TEST_EQUAL(psa_get_key_bits(&func), 0); |
| 1804 | TEST_EQUAL(psa_get_key_bits(&init), 0); |
| 1805 | TEST_EQUAL(psa_get_key_bits(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1806 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1807 | TEST_EQUAL(psa_get_key_usage_flags(&func), 0); |
| 1808 | TEST_EQUAL(psa_get_key_usage_flags(&init), 0); |
| 1809 | TEST_EQUAL(psa_get_key_usage_flags(&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_algorithm(&func), 0); |
| 1812 | TEST_EQUAL(psa_get_key_algorithm(&init), 0); |
| 1813 | TEST_EQUAL(psa_get_key_algorithm(&zero), 0); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1814 | } |
| 1815 | /* END_CASE */ |
| 1816 | |
| 1817 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1818 | void mac_key_policy(int policy_usage_arg, |
| 1819 | int policy_alg_arg, |
| 1820 | int key_type_arg, |
| 1821 | data_t *key_data, |
| 1822 | int exercise_alg_arg, |
| 1823 | int expected_status_sign_arg, |
| 1824 | int expected_status_verify_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1825 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1826 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1827 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1828 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1829 | psa_key_type_t key_type = key_type_arg; |
| 1830 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 1831 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 1832 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1833 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1834 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 1835 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1836 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1837 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1838 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1839 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1840 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1841 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1842 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1843 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1844 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1845 | &key)); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1846 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1847 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 1848 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1849 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1850 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 1851 | TEST_EQUAL(status, expected_status_sign); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1852 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1853 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1854 | uint8_t input[128] = { 0 }; |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1855 | size_t mac_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1856 | TEST_EQUAL(psa_mac_compute(key, exercise_alg, |
| 1857 | input, 128, |
| 1858 | mac, PSA_MAC_MAX_SIZE, &mac_len), |
| 1859 | expected_status_sign); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1860 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1861 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1862 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 1863 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 1864 | if (status == PSA_SUCCESS) { |
| 1865 | status = psa_mac_update(&operation, input, 128); |
| 1866 | if (status == PSA_SUCCESS) { |
| 1867 | TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE, |
| 1868 | &mac_len), |
| 1869 | expected_status_sign); |
| 1870 | } else { |
| 1871 | TEST_EQUAL(status, expected_status_sign); |
| 1872 | } |
| 1873 | } else { |
| 1874 | TEST_EQUAL(status, expected_status_sign); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1875 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1876 | PSA_ASSERT(psa_mac_abort(&operation)); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1877 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1878 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1879 | status = psa_mac_verify(key, exercise_alg, input, 128, |
| 1880 | mac, mac_len); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1881 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1882 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 1883 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1884 | } else { |
| 1885 | TEST_EQUAL(status, expected_status_verify); |
| 1886 | } |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1887 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1888 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1889 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 1890 | if (status == PSA_SUCCESS) { |
| 1891 | status = psa_mac_update(&operation, input, 128); |
| 1892 | if (status == PSA_SUCCESS) { |
| 1893 | status = psa_mac_verify_finish(&operation, mac, mac_len); |
| 1894 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 1895 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1896 | } else { |
| 1897 | TEST_EQUAL(status, expected_status_verify); |
| 1898 | } |
| 1899 | } else { |
| 1900 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1901 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1902 | } else { |
| 1903 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1904 | } |
| 1905 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1906 | psa_mac_abort(&operation); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1907 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1908 | memset(mac, 0, sizeof(mac)); |
| 1909 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 1910 | TEST_EQUAL(status, expected_status_verify); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1911 | |
| 1912 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1913 | psa_mac_abort(&operation); |
| 1914 | psa_destroy_key(key); |
| 1915 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1916 | } |
| 1917 | /* END_CASE */ |
| 1918 | |
| 1919 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1920 | void cipher_key_policy(int policy_usage_arg, |
| 1921 | int policy_alg, |
| 1922 | int key_type, |
| 1923 | data_t *key_data, |
| 1924 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1925 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1926 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1927 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1928 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1929 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1930 | size_t output_buffer_size = 0; |
| 1931 | size_t input_buffer_size = 0; |
| 1932 | size_t output_length = 0; |
| 1933 | uint8_t *output = NULL; |
| 1934 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1935 | psa_status_t status; |
| 1936 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1937 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg); |
| 1938 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg, |
| 1939 | input_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1940 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 1941 | TEST_CALLOC(input, input_buffer_size); |
| 1942 | TEST_CALLOC(output, output_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1943 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1944 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1945 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1946 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1947 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1948 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1949 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1950 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1951 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1952 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1953 | /* Check if no key usage flag implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1954 | TEST_EQUAL(policy_usage, |
| 1955 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1956 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1957 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1958 | status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size, |
| 1959 | output, output_buffer_size, |
| 1960 | &output_length); |
| 1961 | if (policy_alg == exercise_alg && |
| 1962 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1963 | PSA_ASSERT(status); |
| 1964 | } else { |
| 1965 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1966 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1967 | |
| 1968 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1969 | status = psa_cipher_encrypt_setup(&operation, key, exercise_alg); |
| 1970 | if (policy_alg == exercise_alg && |
| 1971 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1972 | PSA_ASSERT(status); |
| 1973 | } else { |
| 1974 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1975 | } |
| 1976 | psa_cipher_abort(&operation); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1977 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1978 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1979 | status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size, |
| 1980 | input, input_buffer_size, |
| 1981 | &output_length); |
| 1982 | if (policy_alg == exercise_alg && |
| 1983 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 1984 | PSA_ASSERT(status); |
| 1985 | } else { |
| 1986 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1987 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1988 | |
| 1989 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1990 | status = psa_cipher_decrypt_setup(&operation, key, exercise_alg); |
| 1991 | if (policy_alg == exercise_alg && |
| 1992 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 1993 | PSA_ASSERT(status); |
| 1994 | } else { |
| 1995 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1996 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1997 | |
| 1998 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1999 | psa_cipher_abort(&operation); |
| 2000 | mbedtls_free(input); |
| 2001 | mbedtls_free(output); |
| 2002 | psa_destroy_key(key); |
| 2003 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2004 | } |
| 2005 | /* END_CASE */ |
| 2006 | |
| 2007 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2008 | void aead_key_policy(int policy_usage_arg, |
| 2009 | int policy_alg, |
| 2010 | int key_type, |
| 2011 | data_t *key_data, |
| 2012 | int nonce_length_arg, |
| 2013 | int tag_length_arg, |
| 2014 | int exercise_alg, |
| 2015 | int expected_status_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2016 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2017 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2018 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2019 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2020 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2021 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2022 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2023 | unsigned char nonce[16] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2024 | size_t nonce_length = nonce_length_arg; |
| 2025 | unsigned char tag[16]; |
| 2026 | size_t tag_length = tag_length_arg; |
| 2027 | size_t output_length; |
| 2028 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2029 | TEST_LE_U(nonce_length, sizeof(nonce)); |
| 2030 | TEST_LE_U(tag_length, sizeof(tag)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2031 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2032 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2033 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2034 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2035 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2036 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2037 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2038 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2039 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2040 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2041 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2042 | TEST_EQUAL(policy_usage, |
| 2043 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2044 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2045 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2046 | status = psa_aead_encrypt(key, exercise_alg, |
| 2047 | nonce, nonce_length, |
| 2048 | NULL, 0, |
| 2049 | NULL, 0, |
| 2050 | tag, tag_length, |
| 2051 | &output_length); |
| 2052 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2053 | TEST_EQUAL(status, expected_status); |
| 2054 | } else { |
| 2055 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2056 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2057 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2058 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2059 | status = psa_aead_encrypt_setup(&operation, key, exercise_alg); |
| 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 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2065 | |
| 2066 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2067 | memset(tag, 0, sizeof(tag)); |
| 2068 | status = psa_aead_decrypt(key, exercise_alg, |
| 2069 | nonce, nonce_length, |
| 2070 | NULL, 0, |
| 2071 | tag, tag_length, |
| 2072 | NULL, 0, |
| 2073 | &output_length); |
| 2074 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2075 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2076 | } else if (expected_status == PSA_SUCCESS) { |
| 2077 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2078 | } else { |
| 2079 | TEST_EQUAL(status, expected_status); |
| 2080 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2081 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2082 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2083 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2084 | status = psa_aead_decrypt_setup(&operation, key, exercise_alg); |
| 2085 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2086 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2087 | } else { |
| 2088 | TEST_EQUAL(status, expected_status); |
| 2089 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2090 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2091 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2092 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2093 | psa_destroy_key(key); |
| 2094 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2095 | } |
| 2096 | /* END_CASE */ |
| 2097 | |
| 2098 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2099 | void asymmetric_encryption_key_policy(int policy_usage_arg, |
| 2100 | int policy_alg, |
| 2101 | int key_type, |
| 2102 | data_t *key_data, |
| 2103 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2104 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2105 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2106 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2107 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2108 | psa_status_t status; |
| 2109 | size_t key_bits; |
| 2110 | size_t buffer_length; |
| 2111 | unsigned char *buffer = NULL; |
| 2112 | size_t output_length; |
| 2113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2114 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2115 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2116 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2117 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2118 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2119 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2120 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2121 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2122 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2123 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2124 | TEST_EQUAL(policy_usage, |
| 2125 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2126 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2127 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 2128 | key_bits = psa_get_key_bits(&attributes); |
| 2129 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, |
| 2130 | exercise_alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 2131 | TEST_CALLOC(buffer, buffer_length); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2132 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2133 | status = psa_asymmetric_encrypt(key, exercise_alg, |
| 2134 | NULL, 0, |
| 2135 | NULL, 0, |
| 2136 | buffer, buffer_length, |
| 2137 | &output_length); |
| 2138 | if (policy_alg == exercise_alg && |
| 2139 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2140 | PSA_ASSERT(status); |
| 2141 | } else { |
| 2142 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2143 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2144 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2145 | if (buffer_length != 0) { |
| 2146 | memset(buffer, 0, buffer_length); |
| 2147 | } |
| 2148 | status = psa_asymmetric_decrypt(key, exercise_alg, |
| 2149 | buffer, buffer_length, |
| 2150 | NULL, 0, |
| 2151 | buffer, buffer_length, |
| 2152 | &output_length); |
| 2153 | if (policy_alg == exercise_alg && |
| 2154 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2155 | TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING); |
| 2156 | } else { |
| 2157 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2158 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2159 | |
| 2160 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2161 | /* |
| 2162 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2163 | * thus reset them as required. |
| 2164 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2165 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2166 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2167 | psa_destroy_key(key); |
| 2168 | PSA_DONE(); |
| 2169 | mbedtls_free(buffer); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2170 | } |
| 2171 | /* END_CASE */ |
| 2172 | |
| 2173 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2174 | void asymmetric_signature_key_policy(int policy_usage_arg, |
| 2175 | int policy_alg, |
| 2176 | int key_type, |
| 2177 | data_t *key_data, |
| 2178 | int exercise_alg, |
| 2179 | int payload_length_arg, |
| 2180 | int expected_usage_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2181 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2182 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2183 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2184 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 2185 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2186 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2187 | unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 }; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2188 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2189 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2190 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2191 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2192 | int compatible_alg = payload_length_arg > 0; |
| 2193 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2194 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2195 | size_t signature_length; |
| 2196 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2197 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2198 | in the expected usage flags. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2199 | TEST_EQUAL(expected_usage, |
| 2200 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2201 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2202 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2203 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2204 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2205 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2206 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2207 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2208 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2209 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2210 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2211 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2212 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2213 | status = psa_sign_hash(key, exercise_alg, |
| 2214 | payload, payload_length, |
| 2215 | signature, sizeof(signature), |
| 2216 | &signature_length); |
| 2217 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) { |
| 2218 | PSA_ASSERT(status); |
| 2219 | } else { |
| 2220 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2221 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2222 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2223 | memset(signature, 0, sizeof(signature)); |
| 2224 | status = psa_verify_hash(key, exercise_alg, |
| 2225 | payload, payload_length, |
| 2226 | signature, sizeof(signature)); |
| 2227 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) { |
| 2228 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2229 | } else { |
| 2230 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2231 | } |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2232 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2233 | if (PSA_ALG_IS_SIGN_HASH(exercise_alg) && |
| 2234 | PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) { |
| 2235 | status = psa_sign_message(key, exercise_alg, |
| 2236 | payload, payload_length, |
| 2237 | signature, sizeof(signature), |
| 2238 | &signature_length); |
| 2239 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) { |
| 2240 | PSA_ASSERT(status); |
| 2241 | } else { |
| 2242 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2243 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2244 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2245 | memset(signature, 0, sizeof(signature)); |
| 2246 | status = psa_verify_message(key, exercise_alg, |
| 2247 | payload, payload_length, |
| 2248 | signature, sizeof(signature)); |
| 2249 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) { |
| 2250 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2251 | } else { |
| 2252 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2253 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2254 | } |
| 2255 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2256 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2257 | psa_destroy_key(key); |
| 2258 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2259 | } |
| 2260 | /* END_CASE */ |
| 2261 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2262 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2263 | void derive_key_policy(int policy_usage, |
| 2264 | int policy_alg, |
| 2265 | int key_type, |
| 2266 | data_t *key_data, |
| 2267 | int exercise_alg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2268 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2269 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2270 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2271 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2272 | psa_status_t status; |
| 2273 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2274 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2275 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2276 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2277 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2278 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2279 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2280 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2281 | &key)); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2282 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2283 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2284 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2285 | if (PSA_ALG_IS_TLS12_PRF(exercise_alg) || |
| 2286 | PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) { |
| 2287 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 2288 | &operation, |
| 2289 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2290 | (const uint8_t *) "", 0)); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2291 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2292 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2293 | status = psa_key_derivation_input_key(&operation, |
| 2294 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 2295 | key); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2296 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2297 | if (policy_alg == exercise_alg && |
| 2298 | (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) { |
| 2299 | PSA_ASSERT(status); |
| 2300 | } else { |
| 2301 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2302 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2303 | |
| 2304 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2305 | psa_key_derivation_abort(&operation); |
| 2306 | psa_destroy_key(key); |
| 2307 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2308 | } |
| 2309 | /* END_CASE */ |
| 2310 | |
| 2311 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2312 | void agreement_key_policy(int policy_usage, |
| 2313 | int policy_alg, |
| 2314 | int key_type_arg, |
| 2315 | data_t *key_data, |
| 2316 | int exercise_alg, |
| 2317 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2318 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2319 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2320 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2321 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2322 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2323 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2324 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2325 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2326 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2327 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2328 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2329 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2330 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2331 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2332 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2333 | &key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2334 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2335 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
| 2336 | status = mbedtls_test_psa_key_agreement_with_self(&operation, key); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2337 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2338 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2339 | |
| 2340 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2341 | psa_key_derivation_abort(&operation); |
| 2342 | psa_destroy_key(key); |
| 2343 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2344 | } |
| 2345 | /* END_CASE */ |
| 2346 | |
| 2347 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2348 | void key_policy_alg2(int key_type_arg, data_t *key_data, |
| 2349 | int usage_arg, int alg_arg, int alg2_arg) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2350 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2351 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2352 | psa_key_type_t key_type = key_type_arg; |
| 2353 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2354 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2355 | psa_key_usage_t usage = usage_arg; |
| 2356 | psa_algorithm_t alg = alg_arg; |
| 2357 | psa_algorithm_t alg2 = alg2_arg; |
| 2358 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2359 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2360 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2361 | psa_set_key_usage_flags(&attributes, usage); |
| 2362 | psa_set_key_algorithm(&attributes, alg); |
| 2363 | psa_set_key_enrollment_algorithm(&attributes, alg2); |
| 2364 | psa_set_key_type(&attributes, key_type); |
| 2365 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2366 | &key)); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2367 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2368 | /* Update the usage flags to obtain implicit usage flags */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2369 | usage = mbedtls_test_update_key_usage_flags(usage); |
| 2370 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 2371 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage); |
| 2372 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 2373 | TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2); |
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 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2376 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2377 | } |
| 2378 | if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2379 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2380 | } |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2381 | |
| 2382 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2383 | /* |
| 2384 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2385 | * thus reset them as required. |
| 2386 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2387 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2389 | psa_destroy_key(key); |
| 2390 | PSA_DONE(); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2391 | } |
| 2392 | /* END_CASE */ |
| 2393 | |
| 2394 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2395 | void raw_agreement_key_policy(int policy_usage, |
| 2396 | int policy_alg, |
| 2397 | int key_type_arg, |
| 2398 | data_t *key_data, |
| 2399 | int exercise_alg, |
| 2400 | int expected_status_arg) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2401 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2402 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2403 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2404 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2405 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2406 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2407 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2408 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2409 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2410 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2411 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2412 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2413 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2414 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2415 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2416 | &key)); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2417 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2418 | status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2419 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2420 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2421 | |
| 2422 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2423 | psa_key_derivation_abort(&operation); |
| 2424 | psa_destroy_key(key); |
| 2425 | PSA_DONE(); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2426 | } |
| 2427 | /* END_CASE */ |
| 2428 | |
| 2429 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2430 | void copy_success(int source_usage_arg, |
| 2431 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4ea4ad0 | 2022-12-04 15:11:00 +0100 | [diff] [blame] | 2432 | int source_lifetime_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2433 | int type_arg, data_t *material, |
| 2434 | int copy_attributes, |
| 2435 | int target_usage_arg, |
| 2436 | int target_alg_arg, int target_alg2_arg, |
Gilles Peskine | 4ea4ad0 | 2022-12-04 15:11:00 +0100 | [diff] [blame] | 2437 | int target_lifetime_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2438 | int expected_usage_arg, |
| 2439 | int expected_alg_arg, int expected_alg2_arg) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2440 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2441 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2442 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2443 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2444 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2445 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2446 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 2447 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2448 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2449 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2450 | uint8_t *export_buffer = NULL; |
| 2451 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2452 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2453 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2454 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2455 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2456 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2457 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2458 | psa_set_key_type(&source_attributes, type_arg); |
| 2459 | psa_set_key_lifetime(&source_attributes, source_lifetime); |
| 2460 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2461 | material->x, material->len, |
| 2462 | &source_key)); |
| 2463 | PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2464 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2465 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2466 | if (copy_attributes) { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2467 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2468 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2469 | psa_set_key_lifetime(&target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2470 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2471 | if (target_usage_arg != -1) { |
| 2472 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2473 | } |
| 2474 | if (target_alg_arg != -1) { |
| 2475 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2476 | } |
| 2477 | if (target_alg2_arg != -1) { |
| 2478 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
| 2479 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2480 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2481 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2482 | /* Copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2483 | PSA_ASSERT(psa_copy_key(source_key, |
| 2484 | &target_attributes, &target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2485 | |
| 2486 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2487 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2488 | |
| 2489 | /* Test that the target slot has the expected content and policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2490 | PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes)); |
| 2491 | TEST_EQUAL(psa_get_key_type(&source_attributes), |
| 2492 | psa_get_key_type(&target_attributes)); |
| 2493 | TEST_EQUAL(psa_get_key_bits(&source_attributes), |
| 2494 | psa_get_key_bits(&target_attributes)); |
| 2495 | TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes)); |
| 2496 | TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes)); |
| 2497 | TEST_EQUAL(expected_alg2, |
| 2498 | psa_get_key_enrollment_algorithm(&target_attributes)); |
| 2499 | if (expected_usage & PSA_KEY_USAGE_EXPORT) { |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2500 | size_t length; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 2501 | TEST_CALLOC(export_buffer, material->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2502 | PSA_ASSERT(psa_export_key(target_key, export_buffer, |
| 2503 | material->len, &length)); |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 2504 | TEST_BUFFERS_EQUAL(material->x, material->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2505 | export_buffer, length); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2506 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2507 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2508 | if (!psa_key_lifetime_is_external(target_lifetime)) { |
| 2509 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2510 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2511 | } |
| 2512 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2513 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2514 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2515 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2516 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2517 | PSA_ASSERT(psa_destroy_key(target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2518 | |
| 2519 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2520 | /* |
| 2521 | * Source and target key attributes may have been returned by |
| 2522 | * psa_get_key_attributes() thus reset them as required. |
| 2523 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2524 | psa_reset_key_attributes(&source_attributes); |
| 2525 | psa_reset_key_attributes(&target_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2526 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2527 | PSA_DONE(); |
| 2528 | mbedtls_free(export_buffer); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2529 | } |
| 2530 | /* END_CASE */ |
| 2531 | |
| 2532 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2533 | void copy_fail(int source_usage_arg, |
| 2534 | int source_alg_arg, int source_alg2_arg, |
| 2535 | int source_lifetime_arg, |
| 2536 | int type_arg, data_t *material, |
| 2537 | int target_type_arg, int target_bits_arg, |
| 2538 | int target_usage_arg, |
| 2539 | int target_alg_arg, int target_alg2_arg, |
| 2540 | int target_id_arg, int target_lifetime_arg, |
| 2541 | int expected_status_arg) |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2542 | { |
| 2543 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2544 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2545 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2546 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2547 | 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] | 2548 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2549 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2550 | |
| 2551 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2552 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2553 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2554 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2555 | psa_set_key_type(&source_attributes, type_arg); |
| 2556 | psa_set_key_lifetime(&source_attributes, source_lifetime_arg); |
| 2557 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2558 | material->x, material->len, |
| 2559 | &source_key)); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2560 | |
| 2561 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2562 | psa_set_key_id(&target_attributes, key_id); |
| 2563 | psa_set_key_lifetime(&target_attributes, target_lifetime_arg); |
| 2564 | psa_set_key_type(&target_attributes, target_type_arg); |
| 2565 | psa_set_key_bits(&target_attributes, target_bits_arg); |
| 2566 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2567 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2568 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2569 | |
| 2570 | /* Try to copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2571 | TEST_EQUAL(psa_copy_key(source_key, |
| 2572 | &target_attributes, &target_key), |
| 2573 | expected_status_arg); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2574 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2575 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2576 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2577 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2578 | psa_reset_key_attributes(&source_attributes); |
| 2579 | psa_reset_key_attributes(&target_attributes); |
| 2580 | PSA_DONE(); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2581 | } |
| 2582 | /* END_CASE */ |
| 2583 | |
| 2584 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2585 | void hash_operation_init() |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2586 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2587 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2588 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2589 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2590 | * 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] | 2591 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2592 | psa_hash_operation_t func = psa_hash_operation_init(); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2593 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2594 | psa_hash_operation_t zero; |
| 2595 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2596 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2597 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2598 | /* A freshly-initialized hash operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2599 | TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)), |
| 2600 | PSA_ERROR_BAD_STATE); |
| 2601 | TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)), |
| 2602 | PSA_ERROR_BAD_STATE); |
| 2603 | TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)), |
| 2604 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2605 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2606 | /* A default hash operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2607 | PSA_ASSERT(psa_hash_abort(&func)); |
| 2608 | PSA_ASSERT(psa_hash_abort(&init)); |
| 2609 | PSA_ASSERT(psa_hash_abort(&zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2610 | } |
| 2611 | /* END_CASE */ |
| 2612 | |
| 2613 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2614 | void hash_setup(int alg_arg, |
| 2615 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2616 | { |
| 2617 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2618 | uint8_t *output = NULL; |
| 2619 | size_t output_size = 0; |
| 2620 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2621 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2622 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2623 | psa_status_t status; |
| 2624 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2625 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2626 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2627 | /* Hash Setup, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2628 | output_size = PSA_HASH_LENGTH(alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 2629 | TEST_CALLOC(output, output_size); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2630 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2631 | status = psa_hash_compute(alg, NULL, 0, |
| 2632 | output, output_size, &output_length); |
| 2633 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2634 | |
| 2635 | /* Hash Setup, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2636 | status = psa_hash_setup(&operation, alg); |
| 2637 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2638 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2639 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2640 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2641 | |
| 2642 | /* If setup failed, reproduce the failure, so as to |
| 2643 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2644 | if (status != PSA_SUCCESS) { |
| 2645 | TEST_EQUAL(psa_hash_setup(&operation, alg), status); |
| 2646 | } |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2647 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2648 | /* Now the operation object should be reusable. */ |
| 2649 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2650 | PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG)); |
| 2651 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2652 | #endif |
| 2653 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2654 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2655 | mbedtls_free(output); |
| 2656 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2657 | } |
| 2658 | /* END_CASE */ |
| 2659 | |
| 2660 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2661 | void hash_compute_fail(int alg_arg, data_t *input, |
| 2662 | int output_size_arg, int expected_status_arg) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2663 | { |
| 2664 | psa_algorithm_t alg = alg_arg; |
| 2665 | uint8_t *output = NULL; |
| 2666 | size_t output_size = output_size_arg; |
| 2667 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2668 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2669 | psa_status_t expected_status = expected_status_arg; |
| 2670 | psa_status_t status; |
| 2671 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 2672 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2673 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2674 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2675 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2676 | /* Hash Compute, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2677 | status = psa_hash_compute(alg, input->x, input->len, |
| 2678 | output, output_size, &output_length); |
| 2679 | TEST_EQUAL(status, expected_status); |
| 2680 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2681 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2682 | /* Hash Compute, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2683 | status = psa_hash_setup(&operation, alg); |
| 2684 | if (status == PSA_SUCCESS) { |
| 2685 | status = psa_hash_update(&operation, input->x, input->len); |
| 2686 | if (status == PSA_SUCCESS) { |
| 2687 | status = psa_hash_finish(&operation, output, output_size, |
| 2688 | &output_length); |
| 2689 | if (status == PSA_SUCCESS) { |
| 2690 | TEST_LE_U(output_length, output_size); |
| 2691 | } else { |
| 2692 | TEST_EQUAL(status, expected_status); |
| 2693 | } |
| 2694 | } else { |
| 2695 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2696 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2697 | } else { |
| 2698 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2699 | } |
| 2700 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2701 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2702 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2703 | mbedtls_free(output); |
| 2704 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2705 | } |
| 2706 | /* END_CASE */ |
| 2707 | |
| 2708 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2709 | void hash_compare_fail(int alg_arg, data_t *input, |
| 2710 | data_t *reference_hash, |
| 2711 | int expected_status_arg) |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2712 | { |
| 2713 | psa_algorithm_t alg = alg_arg; |
| 2714 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2715 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2716 | psa_status_t status; |
| 2717 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2718 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2719 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2720 | /* Hash Compare, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2721 | status = psa_hash_compare(alg, input->x, input->len, |
| 2722 | reference_hash->x, reference_hash->len); |
| 2723 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2724 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2725 | /* Hash Compare, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2726 | status = psa_hash_setup(&operation, alg); |
| 2727 | if (status == PSA_SUCCESS) { |
| 2728 | status = psa_hash_update(&operation, input->x, input->len); |
| 2729 | if (status == PSA_SUCCESS) { |
| 2730 | status = psa_hash_verify(&operation, reference_hash->x, |
| 2731 | reference_hash->len); |
| 2732 | TEST_EQUAL(status, expected_status); |
| 2733 | } else { |
| 2734 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2735 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2736 | } else { |
| 2737 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2738 | } |
| 2739 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2740 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2741 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2742 | PSA_DONE(); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2743 | } |
| 2744 | /* END_CASE */ |
| 2745 | |
| 2746 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2747 | void hash_compute_compare(int alg_arg, data_t *input, |
| 2748 | data_t *expected_output) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2749 | { |
| 2750 | psa_algorithm_t alg = alg_arg; |
| 2751 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2752 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2753 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2754 | size_t i; |
| 2755 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2756 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2757 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2758 | /* Compute with tight buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2759 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 2760 | output, PSA_HASH_LENGTH(alg), |
| 2761 | &output_length)); |
| 2762 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 2763 | TEST_BUFFERS_EQUAL(output, output_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2764 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2765 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2766 | /* Compute with tight buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2767 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2768 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2769 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 2770 | PSA_HASH_LENGTH(alg), |
| 2771 | &output_length)); |
| 2772 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 2773 | TEST_BUFFERS_EQUAL(output, output_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2774 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2775 | |
| 2776 | /* Compute with larger buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2777 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 2778 | output, sizeof(output), |
| 2779 | &output_length)); |
| 2780 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 2781 | TEST_BUFFERS_EQUAL(output, output_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2782 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2783 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2784 | /* Compute with larger buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2785 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2786 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2787 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 2788 | sizeof(output), &output_length)); |
| 2789 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 2790 | TEST_BUFFERS_EQUAL(output, output_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2791 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2792 | |
| 2793 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2794 | PSA_ASSERT(psa_hash_compare(alg, input->x, input->len, |
| 2795 | output, output_length)); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2796 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2797 | /* Compare with correct hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2798 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2799 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2800 | PSA_ASSERT(psa_hash_verify(&operation, output, |
| 2801 | output_length)); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2802 | |
| 2803 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2804 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2805 | output, output_length + 1), |
| 2806 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2807 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2808 | /* Compare with trailing garbage, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2809 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2810 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2811 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1), |
| 2812 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2813 | |
| 2814 | /* Compare with truncated hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2815 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2816 | output, output_length - 1), |
| 2817 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2818 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2819 | /* Compare with truncated hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2820 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2821 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2822 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1), |
| 2823 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2824 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2825 | /* Compare with corrupted value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2826 | for (i = 0; i < output_length; i++) { |
| 2827 | mbedtls_test_set_step(i); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2828 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2829 | |
| 2830 | /* One-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2831 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2832 | output, output_length), |
| 2833 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2834 | |
| 2835 | /* Multi-Part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2836 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2837 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2838 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length), |
| 2839 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2840 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2841 | output[i] ^= 1; |
| 2842 | } |
| 2843 | |
| 2844 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2845 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2846 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2847 | } |
| 2848 | /* END_CASE */ |
| 2849 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2850 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2851 | void hash_bad_order() |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2852 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2853 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2854 | unsigned char input[] = ""; |
| 2855 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2856 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2857 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2858 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2859 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 |
| 2860 | }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2861 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2862 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2863 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2864 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2865 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2866 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2867 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2868 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2869 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2870 | TEST_EQUAL(psa_hash_setup(&operation, alg), |
| 2871 | PSA_ERROR_BAD_STATE); |
| 2872 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2873 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2874 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2875 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2876 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2877 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2878 | PSA_ERROR_BAD_STATE); |
| 2879 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2880 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2881 | /* Check that update calls abort on error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2882 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 2883 | operation.id = UINT_MAX; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2884 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2885 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2886 | PSA_ERROR_BAD_STATE); |
| 2887 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2888 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2889 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2890 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2891 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2892 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2893 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2894 | hash, sizeof(hash), &hash_len)); |
| 2895 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2896 | PSA_ERROR_BAD_STATE); |
| 2897 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2898 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2899 | /* Call verify without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2900 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2901 | valid_hash, sizeof(valid_hash)), |
| 2902 | PSA_ERROR_BAD_STATE); |
| 2903 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2904 | |
| 2905 | /* Call verify 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_verify(&operation, |
| 2910 | valid_hash, sizeof(valid_hash)), |
| 2911 | PSA_ERROR_BAD_STATE); |
| 2912 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2913 | |
| 2914 | /* Call verify twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2915 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2916 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2917 | PSA_ASSERT(psa_hash_verify(&operation, |
| 2918 | valid_hash, sizeof(valid_hash))); |
| 2919 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2920 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2921 | valid_hash, sizeof(valid_hash)), |
| 2922 | PSA_ERROR_BAD_STATE); |
| 2923 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2924 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2925 | |
| 2926 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2927 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2928 | hash, sizeof(hash), &hash_len), |
| 2929 | PSA_ERROR_BAD_STATE); |
| 2930 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2931 | |
| 2932 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2933 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2934 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2935 | hash, sizeof(hash), &hash_len)); |
| 2936 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2937 | hash, sizeof(hash), &hash_len), |
| 2938 | PSA_ERROR_BAD_STATE); |
| 2939 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2940 | |
| 2941 | /* Call finish after calling verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2942 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2943 | PSA_ASSERT(psa_hash_verify(&operation, |
| 2944 | valid_hash, sizeof(valid_hash))); |
| 2945 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2946 | hash, sizeof(hash), &hash_len), |
| 2947 | PSA_ERROR_BAD_STATE); |
| 2948 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2949 | |
| 2950 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2951 | PSA_DONE(); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2952 | } |
| 2953 | /* END_CASE */ |
| 2954 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2955 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2956 | void hash_verify_bad_args() |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2957 | { |
| 2958 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2959 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2960 | * appended to it */ |
| 2961 | unsigned char hash[] = { |
| 2962 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2963 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2964 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb |
| 2965 | }; |
| 2966 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2967 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2968 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2969 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2970 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2971 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2972 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2973 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2974 | TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1), |
| 2975 | PSA_ERROR_INVALID_SIGNATURE); |
| 2976 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2977 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2978 | ASSERT_OPERATION_IS_INACTIVE(operation); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2979 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2980 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2981 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2982 | TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size), |
| 2983 | PSA_ERROR_INVALID_SIGNATURE); |
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 hash longer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2986 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2987 | TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)), |
| 2988 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2989 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2990 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2991 | PSA_DONE(); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2992 | } |
| 2993 | /* END_CASE */ |
| 2994 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2995 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2996 | void hash_finish_bad_args() |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2997 | { |
| 2998 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2999 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3000 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 3001 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3002 | size_t hash_len; |
| 3003 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3004 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3005 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3006 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3007 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3008 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3009 | hash, expected_size - 1, &hash_len), |
| 3010 | PSA_ERROR_BUFFER_TOO_SMALL); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3011 | |
| 3012 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3013 | PSA_DONE(); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3014 | } |
| 3015 | /* END_CASE */ |
| 3016 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3017 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3018 | void hash_clone_source_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3019 | { |
| 3020 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3021 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3022 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 3023 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3024 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3025 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3026 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3027 | size_t hash_len; |
| 3028 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3029 | PSA_ASSERT(psa_crypto_init()); |
| 3030 | PSA_ASSERT(psa_hash_setup(&op_source, alg)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3031 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3032 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3033 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3034 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3035 | hash, sizeof(hash), &hash_len)); |
| 3036 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3037 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3038 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3039 | TEST_EQUAL(psa_hash_clone(&op_source, &op_setup), |
| 3040 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3041 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3042 | PSA_ASSERT(psa_hash_clone(&op_source, &op_init)); |
| 3043 | PSA_ASSERT(psa_hash_finish(&op_init, |
| 3044 | hash, sizeof(hash), &hash_len)); |
| 3045 | PSA_ASSERT(psa_hash_clone(&op_source, &op_finished)); |
| 3046 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3047 | hash, sizeof(hash), &hash_len)); |
| 3048 | PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted)); |
| 3049 | PSA_ASSERT(psa_hash_finish(&op_aborted, |
| 3050 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3051 | |
| 3052 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3053 | psa_hash_abort(&op_source); |
| 3054 | psa_hash_abort(&op_init); |
| 3055 | psa_hash_abort(&op_setup); |
| 3056 | psa_hash_abort(&op_finished); |
| 3057 | psa_hash_abort(&op_aborted); |
| 3058 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3059 | } |
| 3060 | /* END_CASE */ |
| 3061 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3062 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3063 | void hash_clone_target_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3064 | { |
| 3065 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3066 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3067 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3068 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3069 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3070 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3071 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 3072 | size_t hash_len; |
| 3073 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3074 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3075 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3076 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3077 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3078 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3079 | hash, sizeof(hash), &hash_len)); |
| 3080 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3081 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3082 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3083 | PSA_ASSERT(psa_hash_clone(&op_setup, &op_target)); |
| 3084 | PSA_ASSERT(psa_hash_finish(&op_target, |
| 3085 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3086 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3087 | TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE); |
| 3088 | TEST_EQUAL(psa_hash_clone(&op_finished, &op_target), |
| 3089 | PSA_ERROR_BAD_STATE); |
| 3090 | TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target), |
| 3091 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3092 | |
| 3093 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3094 | psa_hash_abort(&op_target); |
| 3095 | psa_hash_abort(&op_init); |
| 3096 | psa_hash_abort(&op_setup); |
| 3097 | psa_hash_abort(&op_finished); |
| 3098 | psa_hash_abort(&op_aborted); |
| 3099 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3100 | } |
| 3101 | /* END_CASE */ |
| 3102 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3103 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3104 | void mac_operation_init() |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3105 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3106 | const uint8_t input[1] = { 0 }; |
| 3107 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3108 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3109 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3110 | * 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] | 3111 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3112 | psa_mac_operation_t func = psa_mac_operation_init(); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3113 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 3114 | psa_mac_operation_t zero; |
| 3115 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3116 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3117 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3118 | /* A freshly-initialized MAC operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3119 | TEST_EQUAL(psa_mac_update(&func, |
| 3120 | input, sizeof(input)), |
| 3121 | PSA_ERROR_BAD_STATE); |
| 3122 | TEST_EQUAL(psa_mac_update(&init, |
| 3123 | input, sizeof(input)), |
| 3124 | PSA_ERROR_BAD_STATE); |
| 3125 | TEST_EQUAL(psa_mac_update(&zero, |
| 3126 | input, sizeof(input)), |
| 3127 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3128 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3129 | /* A default MAC operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3130 | PSA_ASSERT(psa_mac_abort(&func)); |
| 3131 | PSA_ASSERT(psa_mac_abort(&init)); |
| 3132 | PSA_ASSERT(psa_mac_abort(&zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3133 | } |
| 3134 | /* END_CASE */ |
| 3135 | |
| 3136 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3137 | void mac_setup(int key_type_arg, |
| 3138 | data_t *key, |
| 3139 | int alg_arg, |
| 3140 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3141 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3142 | psa_key_type_t key_type = key_type_arg; |
| 3143 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3144 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3145 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3146 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3147 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3148 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3149 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3151 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3153 | if (!exercise_mac_setup(key_type, key->x, key->len, alg, |
| 3154 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3155 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3156 | } |
| 3157 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3158 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3159 | /* The operation object should be reusable. */ |
| 3160 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3161 | if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 3162 | smoke_test_key_data, |
| 3163 | sizeof(smoke_test_key_data), |
| 3164 | KNOWN_SUPPORTED_MAC_ALG, |
| 3165 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3166 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3167 | } |
| 3168 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3169 | #endif |
| 3170 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3171 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3172 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3173 | } |
| 3174 | /* END_CASE */ |
| 3175 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3176 | /* 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] | 3177 | void mac_bad_order() |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3178 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3179 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3180 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 3181 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3182 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3183 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3184 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3185 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa |
| 3186 | }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3187 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3188 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3189 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 3190 | size_t sign_mac_length = 0; |
| 3191 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3192 | const uint8_t verify_mac[] = { |
| 3193 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 3194 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3195 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 |
| 3196 | }; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3197 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3198 | PSA_ASSERT(psa_crypto_init()); |
| 3199 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 3200 | psa_set_key_algorithm(&attributes, alg); |
| 3201 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3202 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3203 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3204 | &key)); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3205 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3206 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3207 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3208 | PSA_ERROR_BAD_STATE); |
| 3209 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3210 | |
| 3211 | /* Call sign finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3212 | TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac), |
| 3213 | &sign_mac_length), |
| 3214 | PSA_ERROR_BAD_STATE); |
| 3215 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3216 | |
| 3217 | /* Call verify finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3218 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3219 | verify_mac, sizeof(verify_mac)), |
| 3220 | PSA_ERROR_BAD_STATE); |
| 3221 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3222 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3223 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3224 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3225 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3226 | TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg), |
| 3227 | PSA_ERROR_BAD_STATE); |
| 3228 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3229 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3230 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3231 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3232 | /* Call update after sign finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3233 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3234 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3235 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3236 | sign_mac, sizeof(sign_mac), |
| 3237 | &sign_mac_length)); |
| 3238 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3239 | PSA_ERROR_BAD_STATE); |
| 3240 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3241 | |
| 3242 | /* Call update after verify finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3243 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3244 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3245 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3246 | verify_mac, sizeof(verify_mac))); |
| 3247 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3248 | PSA_ERROR_BAD_STATE); |
| 3249 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3250 | |
| 3251 | /* Call sign finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3252 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3253 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3254 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3255 | sign_mac, sizeof(sign_mac), |
| 3256 | &sign_mac_length)); |
| 3257 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3258 | sign_mac, sizeof(sign_mac), |
| 3259 | &sign_mac_length), |
| 3260 | PSA_ERROR_BAD_STATE); |
| 3261 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3262 | |
| 3263 | /* Call verify finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3264 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3265 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3266 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3267 | verify_mac, sizeof(verify_mac))); |
| 3268 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3269 | verify_mac, sizeof(verify_mac)), |
| 3270 | PSA_ERROR_BAD_STATE); |
| 3271 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3272 | |
| 3273 | /* Setup sign but try verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3274 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3275 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3276 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3277 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3278 | verify_mac, sizeof(verify_mac)), |
| 3279 | PSA_ERROR_BAD_STATE); |
| 3280 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3281 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3282 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3283 | |
| 3284 | /* Setup verify but try sign. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3285 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3286 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3287 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3288 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3289 | sign_mac, sizeof(sign_mac), |
| 3290 | &sign_mac_length), |
| 3291 | PSA_ERROR_BAD_STATE); |
| 3292 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3293 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3294 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3295 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3296 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3297 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3298 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3299 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3300 | } |
| 3301 | /* END_CASE */ |
| 3302 | |
| 3303 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3304 | void mac_sign_verify_multi(int key_type_arg, |
| 3305 | data_t *key_data, |
| 3306 | int alg_arg, |
| 3307 | data_t *input, |
| 3308 | int is_verify, |
| 3309 | data_t *expected_mac) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3310 | { |
| 3311 | size_t data_part_len = 0; |
| 3312 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3313 | 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] | 3314 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3315 | mbedtls_test_set_step(2000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3316 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3317 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3318 | alg_arg, |
| 3319 | input, data_part_len, |
| 3320 | expected_mac, |
| 3321 | is_verify, 0) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3322 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3323 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3324 | |
| 3325 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3326 | mbedtls_test_set_step(3000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3327 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3328 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3329 | alg_arg, |
| 3330 | input, data_part_len, |
| 3331 | expected_mac, |
| 3332 | is_verify, 1) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3333 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3334 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3335 | } |
| 3336 | |
| 3337 | /* Goto is required to silence warnings about unused labels, as we |
| 3338 | * don't actually do any test assertions in this function. */ |
| 3339 | goto exit; |
| 3340 | } |
| 3341 | /* END_CASE */ |
| 3342 | |
| 3343 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3344 | void mac_sign(int key_type_arg, |
| 3345 | data_t *key_data, |
| 3346 | int alg_arg, |
| 3347 | data_t *input, |
| 3348 | data_t *expected_mac) |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3349 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3350 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3351 | psa_key_type_t key_type = key_type_arg; |
| 3352 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3353 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3354 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3355 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3356 | size_t mac_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3357 | 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] | 3358 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3359 | const size_t output_sizes_to_test[] = { |
| 3360 | 0, |
| 3361 | 1, |
| 3362 | expected_mac->len - 1, |
| 3363 | expected_mac->len, |
| 3364 | expected_mac->len + 1, |
| 3365 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3367 | TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3368 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3369 | TEST_ASSERT(expected_mac->len == mac_buffer_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3371 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3372 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3373 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 3374 | psa_set_key_algorithm(&attributes, alg); |
| 3375 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3377 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3378 | &key)); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3379 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3380 | 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] | 3381 | const size_t output_size = output_sizes_to_test[i]; |
| 3382 | psa_status_t expected_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3383 | (output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3384 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3386 | mbedtls_test_set_step(output_size); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 3387 | TEST_CALLOC(actual_mac, output_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3388 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3389 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3390 | TEST_EQUAL(psa_mac_compute(key, alg, |
| 3391 | input->x, input->len, |
| 3392 | actual_mac, output_size, &mac_length), |
| 3393 | expected_status); |
| 3394 | if (expected_status == PSA_SUCCESS) { |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 3395 | TEST_BUFFERS_EQUAL(expected_mac->x, expected_mac->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3396 | actual_mac, mac_length); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3397 | } |
| 3398 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3399 | if (output_size > 0) { |
| 3400 | memset(actual_mac, 0, output_size); |
| 3401 | } |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3402 | |
| 3403 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3404 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3405 | PSA_ASSERT(psa_mac_update(&operation, |
| 3406 | input->x, input->len)); |
| 3407 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3408 | actual_mac, output_size, |
| 3409 | &mac_length), |
| 3410 | expected_status); |
| 3411 | PSA_ASSERT(psa_mac_abort(&operation)); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3412 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3413 | if (expected_status == PSA_SUCCESS) { |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 3414 | TEST_BUFFERS_EQUAL(expected_mac->x, expected_mac->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3415 | actual_mac, mac_length); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3416 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3417 | mbedtls_free(actual_mac); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3418 | actual_mac = NULL; |
| 3419 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3420 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3421 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3422 | psa_mac_abort(&operation); |
| 3423 | psa_destroy_key(key); |
| 3424 | PSA_DONE(); |
| 3425 | mbedtls_free(actual_mac); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3426 | } |
| 3427 | /* END_CASE */ |
| 3428 | |
| 3429 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3430 | void mac_verify(int key_type_arg, |
| 3431 | data_t *key_data, |
| 3432 | int alg_arg, |
| 3433 | data_t *input, |
| 3434 | data_t *expected_mac) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3435 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3436 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3437 | psa_key_type_t key_type = key_type_arg; |
| 3438 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3439 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3440 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3441 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3442 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3443 | TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3444 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3445 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3446 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3447 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 3448 | psa_set_key_algorithm(&attributes, alg); |
| 3449 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3450 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3451 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3452 | &key)); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3453 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3454 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3455 | PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len, |
| 3456 | expected_mac->x, expected_mac->len)); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3457 | |
| 3458 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3459 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3460 | PSA_ASSERT(psa_mac_update(&operation, |
| 3461 | input->x, input->len)); |
| 3462 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3463 | expected_mac->x, |
| 3464 | expected_mac->len)); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3465 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3466 | /* Test a MAC that's too short, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3467 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3468 | input->x, input->len, |
| 3469 | expected_mac->x, |
| 3470 | expected_mac->len - 1), |
| 3471 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3472 | |
| 3473 | /* Test a MAC that's too short, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3474 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3475 | PSA_ASSERT(psa_mac_update(&operation, |
| 3476 | input->x, input->len)); |
| 3477 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3478 | expected_mac->x, |
| 3479 | expected_mac->len - 1), |
| 3480 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3481 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3482 | /* Test a MAC that's too long, one-shot case. */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 3483 | TEST_CALLOC(perturbed_mac, expected_mac->len + 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3484 | memcpy(perturbed_mac, expected_mac->x, expected_mac->len); |
| 3485 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3486 | input->x, input->len, |
| 3487 | perturbed_mac, expected_mac->len + 1), |
| 3488 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3489 | |
| 3490 | /* Test a MAC that's too long, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3491 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3492 | PSA_ASSERT(psa_mac_update(&operation, |
| 3493 | input->x, input->len)); |
| 3494 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3495 | perturbed_mac, |
| 3496 | expected_mac->len + 1), |
| 3497 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3498 | |
| 3499 | /* Test changing one byte. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3500 | for (size_t i = 0; i < expected_mac->len; i++) { |
| 3501 | mbedtls_test_set_step(i); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3502 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3503 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3504 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3505 | input->x, input->len, |
| 3506 | perturbed_mac, expected_mac->len), |
| 3507 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3508 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3509 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3510 | PSA_ASSERT(psa_mac_update(&operation, |
| 3511 | input->x, input->len)); |
| 3512 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3513 | perturbed_mac, |
| 3514 | expected_mac->len), |
| 3515 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3516 | perturbed_mac[i] ^= 1; |
| 3517 | } |
| 3518 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3519 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3520 | psa_mac_abort(&operation); |
| 3521 | psa_destroy_key(key); |
| 3522 | PSA_DONE(); |
| 3523 | mbedtls_free(perturbed_mac); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3524 | } |
| 3525 | /* END_CASE */ |
| 3526 | |
| 3527 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3528 | void cipher_operation_init() |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3529 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3530 | const uint8_t input[1] = { 0 }; |
| 3531 | unsigned char output[1] = { 0 }; |
| 3532 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3533 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3534 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3535 | * 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] | 3536 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3537 | psa_cipher_operation_t func = psa_cipher_operation_init(); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3538 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3539 | psa_cipher_operation_t zero; |
| 3540 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3541 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3542 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3543 | /* A freshly-initialized cipher operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3544 | TEST_EQUAL(psa_cipher_update(&func, |
| 3545 | input, sizeof(input), |
| 3546 | output, sizeof(output), |
| 3547 | &output_length), |
| 3548 | PSA_ERROR_BAD_STATE); |
| 3549 | TEST_EQUAL(psa_cipher_update(&init, |
| 3550 | input, sizeof(input), |
| 3551 | output, sizeof(output), |
| 3552 | &output_length), |
| 3553 | PSA_ERROR_BAD_STATE); |
| 3554 | TEST_EQUAL(psa_cipher_update(&zero, |
| 3555 | input, sizeof(input), |
| 3556 | output, sizeof(output), |
| 3557 | &output_length), |
| 3558 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3559 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3560 | /* A default cipher operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3561 | PSA_ASSERT(psa_cipher_abort(&func)); |
| 3562 | PSA_ASSERT(psa_cipher_abort(&init)); |
| 3563 | PSA_ASSERT(psa_cipher_abort(&zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3564 | } |
| 3565 | /* END_CASE */ |
| 3566 | |
| 3567 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3568 | void cipher_setup(int key_type_arg, |
| 3569 | data_t *key, |
| 3570 | int alg_arg, |
| 3571 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3572 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3573 | psa_key_type_t key_type = key_type_arg; |
| 3574 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3575 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3576 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3577 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3578 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3579 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3580 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3581 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3582 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3583 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3584 | if (!exercise_cipher_setup(key_type, key->x, key->len, alg, |
| 3585 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3586 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3587 | } |
| 3588 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3589 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3590 | /* The operation object should be reusable. */ |
| 3591 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3592 | if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3593 | smoke_test_key_data, |
| 3594 | sizeof(smoke_test_key_data), |
| 3595 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3596 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3597 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3598 | } |
| 3599 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3600 | #endif |
| 3601 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3602 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3603 | psa_cipher_abort(&operation); |
| 3604 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3605 | } |
| 3606 | /* END_CASE */ |
| 3607 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3608 | /* 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] | 3609 | void cipher_bad_order() |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3610 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3611 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3612 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3613 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3614 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3615 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3616 | 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] | 3617 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3618 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3619 | 0xaa, 0xaa, 0xaa, 0xaa |
| 3620 | }; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3621 | const uint8_t text[] = { |
| 3622 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3623 | 0xbb, 0xbb, 0xbb, 0xbb |
| 3624 | }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3625 | 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] | 3626 | size_t length = 0; |
| 3627 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3628 | PSA_ASSERT(psa_crypto_init()); |
| 3629 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3630 | psa_set_key_algorithm(&attributes, alg); |
| 3631 | psa_set_key_type(&attributes, key_type); |
| 3632 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3633 | &key)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3634 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3635 | /* Call encrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3636 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3637 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3638 | TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg), |
| 3639 | PSA_ERROR_BAD_STATE); |
| 3640 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3641 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3642 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3643 | |
| 3644 | /* Call decrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3645 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3646 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3647 | TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg), |
| 3648 | PSA_ERROR_BAD_STATE); |
| 3649 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3650 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3651 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3652 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3653 | /* Generate an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3654 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3655 | buffer, sizeof(buffer), |
| 3656 | &length), |
| 3657 | PSA_ERROR_BAD_STATE); |
| 3658 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3659 | |
| 3660 | /* Generate an IV twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3661 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3662 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3663 | buffer, sizeof(buffer), |
| 3664 | &length)); |
| 3665 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3666 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3667 | buffer, sizeof(buffer), |
| 3668 | &length), |
| 3669 | PSA_ERROR_BAD_STATE); |
| 3670 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3671 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3672 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3673 | |
| 3674 | /* Generate an IV after it's already set. */ |
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_set_iv(&operation, |
| 3677 | iv, sizeof(iv))); |
| 3678 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3679 | buffer, sizeof(buffer), |
| 3680 | &length), |
| 3681 | PSA_ERROR_BAD_STATE); |
| 3682 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3683 | |
| 3684 | /* Set an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3685 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3686 | iv, sizeof(iv)), |
| 3687 | PSA_ERROR_BAD_STATE); |
| 3688 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3689 | |
| 3690 | /* Set an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3691 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3692 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3693 | iv, sizeof(iv))); |
| 3694 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3695 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3696 | iv, sizeof(iv)), |
| 3697 | PSA_ERROR_BAD_STATE); |
| 3698 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3699 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3700 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3701 | |
| 3702 | /* Set an IV after it's already generated. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3703 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3704 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3705 | buffer, sizeof(buffer), |
| 3706 | &length)); |
| 3707 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3708 | iv, sizeof(iv)), |
| 3709 | PSA_ERROR_BAD_STATE); |
| 3710 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3711 | |
| 3712 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3713 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3714 | text, sizeof(text), |
| 3715 | buffer, sizeof(buffer), |
| 3716 | &length), |
| 3717 | PSA_ERROR_BAD_STATE); |
| 3718 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3719 | |
| 3720 | /* Call update without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3721 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3722 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3723 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3724 | text, sizeof(text), |
| 3725 | buffer, sizeof(buffer), |
| 3726 | &length), |
| 3727 | PSA_ERROR_BAD_STATE); |
| 3728 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3729 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3730 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3731 | |
| 3732 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3733 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3734 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3735 | iv, sizeof(iv))); |
| 3736 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3737 | buffer, sizeof(buffer), &length)); |
| 3738 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3739 | text, sizeof(text), |
| 3740 | buffer, sizeof(buffer), |
| 3741 | &length), |
| 3742 | PSA_ERROR_BAD_STATE); |
| 3743 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3744 | |
| 3745 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3746 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3747 | buffer, sizeof(buffer), &length), |
| 3748 | PSA_ERROR_BAD_STATE); |
| 3749 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3750 | |
| 3751 | /* Call finish without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3752 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3753 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3754 | * for cipher modes with padding. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3755 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3756 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3757 | buffer, sizeof(buffer), &length), |
| 3758 | PSA_ERROR_BAD_STATE); |
| 3759 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3760 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3761 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3762 | |
| 3763 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3764 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3765 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3766 | iv, sizeof(iv))); |
| 3767 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3768 | buffer, sizeof(buffer), &length)); |
| 3769 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3770 | buffer, sizeof(buffer), &length), |
| 3771 | PSA_ERROR_BAD_STATE); |
| 3772 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3773 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3774 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3775 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3776 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3777 | psa_cipher_abort(&operation); |
| 3778 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3779 | } |
| 3780 | /* END_CASE */ |
| 3781 | |
| 3782 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3783 | void cipher_encrypt_fail(int alg_arg, |
| 3784 | int key_type_arg, |
| 3785 | data_t *key_data, |
| 3786 | data_t *input, |
| 3787 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3788 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3789 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3790 | psa_status_t status; |
| 3791 | psa_key_type_t key_type = key_type_arg; |
| 3792 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3793 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3794 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 }; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3795 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; |
| 3796 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3797 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3798 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3799 | size_t output_length = 0; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3800 | size_t function_output_length; |
| 3801 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3802 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3803 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3804 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 3805 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3806 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3807 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3808 | psa_set_key_algorithm(&attributes, alg); |
| 3809 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3810 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3811 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 3812 | input->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 3813 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3814 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3815 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3816 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3817 | } |
| 3818 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3819 | /* Encrypt, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3820 | status = psa_cipher_encrypt(key, alg, input->x, input->len, output, |
| 3821 | output_buffer_size, &output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3822 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3823 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3824 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3825 | /* Encrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3826 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
| 3827 | if (status == PSA_SUCCESS) { |
| 3828 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 3829 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3830 | iv, iv_size, |
| 3831 | &iv_length)); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3832 | } |
| 3833 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3834 | status = psa_cipher_update(&operation, input->x, input->len, |
| 3835 | output, output_buffer_size, |
| 3836 | &function_output_length); |
| 3837 | if (status == PSA_SUCCESS) { |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3838 | output_length += function_output_length; |
| 3839 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3840 | status = psa_cipher_finish(&operation, output + output_length, |
| 3841 | output_buffer_size - output_length, |
| 3842 | &function_output_length); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3843 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3844 | TEST_EQUAL(status, expected_status); |
| 3845 | } else { |
| 3846 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3847 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3848 | } else { |
| 3849 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3850 | } |
| 3851 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3852 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3853 | psa_cipher_abort(&operation); |
| 3854 | mbedtls_free(output); |
| 3855 | psa_destroy_key(key); |
| 3856 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3857 | } |
| 3858 | /* END_CASE */ |
| 3859 | |
| 3860 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3861 | void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data, |
| 3862 | data_t *input, int iv_length, |
| 3863 | int expected_result) |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3864 | { |
| 3865 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3866 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3867 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3868 | size_t output_buffer_size = 0; |
| 3869 | unsigned char *output = NULL; |
| 3870 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3871 | 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^] | 3872 | TEST_CALLOC(output, output_buffer_size); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3873 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3874 | PSA_ASSERT(psa_crypto_init()); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3875 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3876 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3877 | psa_set_key_algorithm(&attributes, alg); |
| 3878 | psa_set_key_type(&attributes, key_type); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3879 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3880 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3881 | &key)); |
| 3882 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3883 | TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output, |
| 3884 | iv_length)); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3885 | |
| 3886 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3887 | psa_cipher_abort(&operation); |
| 3888 | mbedtls_free(output); |
| 3889 | psa_destroy_key(key); |
| 3890 | PSA_DONE(); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3891 | } |
| 3892 | /* END_CASE */ |
| 3893 | |
| 3894 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3895 | void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data, |
| 3896 | data_t *plaintext, data_t *ciphertext) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3897 | { |
| 3898 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3899 | psa_key_type_t key_type = key_type_arg; |
| 3900 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3901 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3902 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3903 | unsigned char *output = NULL; |
| 3904 | size_t output_buffer_size = 0; |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3905 | size_t output_length, length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3906 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3907 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3908 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3909 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3910 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3911 | TEST_LE_U(ciphertext->len, |
| 3912 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len)); |
| 3913 | TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len), |
| 3914 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len)); |
| 3915 | TEST_LE_U(plaintext->len, |
| 3916 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len)); |
| 3917 | TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len), |
| 3918 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len)); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3919 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3920 | |
| 3921 | /* Set up key and output buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3922 | psa_set_key_usage_flags(&attributes, |
| 3923 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3924 | psa_set_key_algorithm(&attributes, alg); |
| 3925 | psa_set_key_type(&attributes, key_type); |
| 3926 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3927 | &key)); |
| 3928 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 3929 | plaintext->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 3930 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3931 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3932 | /* set_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3933 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3934 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 3935 | PSA_ERROR_BAD_STATE); |
| 3936 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3937 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 3938 | PSA_ERROR_BAD_STATE); |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3939 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3940 | /* generate_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3941 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3942 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 3943 | &length), |
| 3944 | PSA_ERROR_BAD_STATE); |
| 3945 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3946 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 3947 | &length), |
| 3948 | PSA_ERROR_BAD_STATE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3949 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3950 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3951 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3952 | output_length = 0; |
| 3953 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3954 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3955 | plaintext->x, plaintext->len, |
| 3956 | output, output_buffer_size, |
| 3957 | &length)); |
| 3958 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3959 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3960 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3961 | mbedtls_buffer_offset(output, output_length), |
| 3962 | output_buffer_size - output_length, |
| 3963 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3964 | output_length += length; |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 3965 | TEST_BUFFERS_EQUAL(ciphertext->x, ciphertext->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3966 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3967 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3968 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3969 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3970 | output_length = 0; |
| 3971 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3972 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3973 | ciphertext->x, ciphertext->len, |
| 3974 | output, output_buffer_size, |
| 3975 | &length)); |
| 3976 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3977 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3978 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3979 | mbedtls_buffer_offset(output, output_length), |
| 3980 | output_buffer_size - output_length, |
| 3981 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3982 | output_length += length; |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 3983 | TEST_BUFFERS_EQUAL(plaintext->x, plaintext->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3984 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3985 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3986 | /* One-shot encryption */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3987 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3988 | PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len, |
| 3989 | output, output_buffer_size, |
| 3990 | &output_length)); |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 3991 | TEST_BUFFERS_EQUAL(ciphertext->x, ciphertext->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3992 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3993 | |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3994 | /* One-shot decryption */ |
| 3995 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3996 | PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len, |
| 3997 | output, output_buffer_size, |
| 3998 | &output_length)); |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 3999 | TEST_BUFFERS_EQUAL(plaintext->x, plaintext->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4000 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4001 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4002 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4003 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4004 | mbedtls_free(output); |
| 4005 | psa_cipher_abort(&operation); |
| 4006 | psa_destroy_key(key); |
| 4007 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4008 | } |
| 4009 | /* END_CASE */ |
| 4010 | |
| 4011 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4012 | 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] | 4013 | { |
| 4014 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4015 | psa_algorithm_t alg = alg_arg; |
| 4016 | psa_key_type_t key_type = key_type_arg; |
| 4017 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4018 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4019 | psa_status_t status; |
| 4020 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4021 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4022 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4023 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4024 | psa_set_key_algorithm(&attributes, alg); |
| 4025 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4026 | |
| 4027 | /* Usage of either of these two size macros would cause divide by zero |
| 4028 | * with incorrect key types previously. Input length should be irrelevant |
| 4029 | * here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4030 | TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16), |
| 4031 | 0); |
| 4032 | TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4033 | |
| 4034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4035 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4036 | &key)); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4037 | |
| 4038 | /* Should fail due to invalid alg type (to support invalid key type). |
| 4039 | * Encrypt or decrypt will end up in the same place. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4040 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4041 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4042 | TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4043 | |
| 4044 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4045 | psa_cipher_abort(&operation); |
| 4046 | psa_destroy_key(key); |
| 4047 | PSA_DONE(); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4048 | } |
| 4049 | /* END_CASE */ |
| 4050 | |
| 4051 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4052 | void cipher_encrypt_validation(int alg_arg, |
| 4053 | int key_type_arg, |
| 4054 | data_t *key_data, |
| 4055 | data_t *input) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4056 | { |
| 4057 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4058 | psa_key_type_t key_type = key_type_arg; |
| 4059 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4060 | size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4061 | unsigned char *output1 = NULL; |
| 4062 | size_t output1_buffer_size = 0; |
| 4063 | size_t output1_length = 0; |
| 4064 | unsigned char *output2 = NULL; |
| 4065 | size_t output2_buffer_size = 0; |
| 4066 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4067 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4068 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4069 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4070 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4071 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4072 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4073 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4074 | psa_set_key_algorithm(&attributes, alg); |
| 4075 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4076 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4077 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4078 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4079 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 4080 | TEST_CALLOC(output1, output1_buffer_size); |
| 4081 | TEST_CALLOC(output2, output2_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4082 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4083 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4084 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4085 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 4086 | /* The one-shot cipher encryption uses generated iv so validating |
| 4087 | the output is not possible. Validating with multipart encryption. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4088 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1, |
| 4089 | output1_buffer_size, &output1_length)); |
| 4090 | TEST_LE_U(output1_length, |
| 4091 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4092 | TEST_LE_U(output1_length, |
| 4093 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4094 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4095 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4096 | PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4097 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4098 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4099 | input->x, input->len, |
| 4100 | output2, output2_buffer_size, |
| 4101 | &function_output_length)); |
| 4102 | TEST_LE_U(function_output_length, |
| 4103 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4104 | TEST_LE_U(function_output_length, |
| 4105 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4106 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4107 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4108 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4109 | output2 + output2_length, |
| 4110 | output2_buffer_size - output2_length, |
| 4111 | &function_output_length)); |
| 4112 | TEST_LE_U(function_output_length, |
| 4113 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4114 | TEST_LE_U(function_output_length, |
| 4115 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4116 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4117 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4118 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 4119 | TEST_BUFFERS_EQUAL(output1 + iv_size, output1_length - iv_size, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4120 | output2, output2_length); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4121 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4122 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4123 | psa_cipher_abort(&operation); |
| 4124 | mbedtls_free(output1); |
| 4125 | mbedtls_free(output2); |
| 4126 | psa_destroy_key(key); |
| 4127 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4128 | } |
| 4129 | /* END_CASE */ |
| 4130 | |
| 4131 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4132 | void cipher_encrypt_multipart(int alg_arg, int key_type_arg, |
| 4133 | data_t *key_data, data_t *iv, |
| 4134 | data_t *input, |
| 4135 | int first_part_size_arg, |
| 4136 | int output1_length_arg, int output2_length_arg, |
| 4137 | data_t *expected_output, |
| 4138 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4139 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4140 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4141 | psa_key_type_t key_type = key_type_arg; |
| 4142 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4143 | psa_status_t status; |
| 4144 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4145 | size_t first_part_size = first_part_size_arg; |
| 4146 | size_t output1_length = output1_length_arg; |
| 4147 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4148 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4149 | size_t output_buffer_size = 0; |
| 4150 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4151 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4152 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4153 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4154 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4155 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4156 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4157 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4158 | psa_set_key_algorithm(&attributes, alg); |
| 4159 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4160 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4161 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4162 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4164 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4165 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4166 | if (iv->len > 0) { |
| 4167 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4168 | } |
| 4169 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4170 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4171 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 4172 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4173 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4174 | TEST_LE_U(first_part_size, input->len); |
| 4175 | PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size, |
| 4176 | output, output_buffer_size, |
| 4177 | &function_output_length)); |
| 4178 | TEST_ASSERT(function_output_length == output1_length); |
| 4179 | TEST_LE_U(function_output_length, |
| 4180 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4181 | TEST_LE_U(function_output_length, |
| 4182 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4183 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4184 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4185 | if (first_part_size < input->len) { |
| 4186 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4187 | input->x + first_part_size, |
| 4188 | input->len - first_part_size, |
| 4189 | (output_buffer_size == 0 ? NULL : |
| 4190 | output + total_output_length), |
| 4191 | output_buffer_size - total_output_length, |
| 4192 | &function_output_length)); |
| 4193 | TEST_ASSERT(function_output_length == output2_length); |
| 4194 | TEST_LE_U(function_output_length, |
| 4195 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4196 | alg, |
| 4197 | input->len - first_part_size)); |
| 4198 | TEST_LE_U(function_output_length, |
| 4199 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4200 | total_output_length += function_output_length; |
| 4201 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4202 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4203 | status = psa_cipher_finish(&operation, |
| 4204 | (output_buffer_size == 0 ? NULL : |
| 4205 | output + total_output_length), |
| 4206 | output_buffer_size - total_output_length, |
| 4207 | &function_output_length); |
| 4208 | TEST_LE_U(function_output_length, |
| 4209 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4210 | TEST_LE_U(function_output_length, |
| 4211 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4212 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4213 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4215 | if (expected_status == PSA_SUCCESS) { |
| 4216 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4217 | |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 4218 | TEST_BUFFERS_EQUAL(expected_output->x, expected_output->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4219 | output, total_output_length); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4220 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4221 | |
| 4222 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4223 | psa_cipher_abort(&operation); |
| 4224 | mbedtls_free(output); |
| 4225 | psa_destroy_key(key); |
| 4226 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4227 | } |
| 4228 | /* END_CASE */ |
| 4229 | |
| 4230 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4231 | void cipher_decrypt_multipart(int alg_arg, int key_type_arg, |
| 4232 | data_t *key_data, data_t *iv, |
| 4233 | data_t *input, |
| 4234 | int first_part_size_arg, |
| 4235 | int output1_length_arg, int output2_length_arg, |
| 4236 | data_t *expected_output, |
| 4237 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4238 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4239 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4240 | psa_key_type_t key_type = key_type_arg; |
| 4241 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4242 | psa_status_t status; |
| 4243 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4244 | size_t first_part_size = first_part_size_arg; |
| 4245 | size_t output1_length = output1_length_arg; |
| 4246 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4247 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4248 | size_t output_buffer_size = 0; |
| 4249 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4250 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4251 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4252 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4253 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4254 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4256 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4257 | psa_set_key_algorithm(&attributes, alg); |
| 4258 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4259 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4260 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4261 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4263 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4264 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4265 | if (iv->len > 0) { |
| 4266 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4267 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4268 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4269 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4270 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 4271 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4272 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4273 | TEST_LE_U(first_part_size, input->len); |
| 4274 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4275 | input->x, first_part_size, |
| 4276 | output, output_buffer_size, |
| 4277 | &function_output_length)); |
| 4278 | TEST_ASSERT(function_output_length == output1_length); |
| 4279 | TEST_LE_U(function_output_length, |
| 4280 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4281 | TEST_LE_U(function_output_length, |
| 4282 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4283 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4284 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4285 | if (first_part_size < input->len) { |
| 4286 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4287 | input->x + first_part_size, |
| 4288 | input->len - first_part_size, |
| 4289 | (output_buffer_size == 0 ? NULL : |
| 4290 | output + total_output_length), |
| 4291 | output_buffer_size - total_output_length, |
| 4292 | &function_output_length)); |
| 4293 | TEST_ASSERT(function_output_length == output2_length); |
| 4294 | TEST_LE_U(function_output_length, |
| 4295 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4296 | alg, |
| 4297 | input->len - first_part_size)); |
| 4298 | TEST_LE_U(function_output_length, |
| 4299 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4300 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4301 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4302 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4303 | status = psa_cipher_finish(&operation, |
| 4304 | (output_buffer_size == 0 ? NULL : |
| 4305 | output + total_output_length), |
| 4306 | output_buffer_size - total_output_length, |
| 4307 | &function_output_length); |
| 4308 | TEST_LE_U(function_output_length, |
| 4309 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4310 | TEST_LE_U(function_output_length, |
| 4311 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4312 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4313 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4314 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4315 | if (expected_status == PSA_SUCCESS) { |
| 4316 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4317 | |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 4318 | TEST_BUFFERS_EQUAL(expected_output->x, expected_output->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4319 | output, total_output_length); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4320 | } |
| 4321 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4322 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4323 | psa_cipher_abort(&operation); |
| 4324 | mbedtls_free(output); |
| 4325 | psa_destroy_key(key); |
| 4326 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4327 | } |
| 4328 | /* END_CASE */ |
| 4329 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4330 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4331 | void cipher_decrypt_fail(int alg_arg, |
| 4332 | int key_type_arg, |
| 4333 | data_t *key_data, |
| 4334 | data_t *iv, |
| 4335 | data_t *input_arg, |
| 4336 | int expected_status_arg) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4337 | { |
| 4338 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4339 | psa_status_t status; |
| 4340 | psa_key_type_t key_type = key_type_arg; |
| 4341 | psa_algorithm_t alg = alg_arg; |
| 4342 | psa_status_t expected_status = expected_status_arg; |
| 4343 | unsigned char *input = NULL; |
| 4344 | size_t input_buffer_size = 0; |
| 4345 | unsigned char *output = NULL; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4346 | unsigned char *output_multi = NULL; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4347 | size_t output_buffer_size = 0; |
| 4348 | size_t output_length = 0; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4349 | size_t function_output_length; |
| 4350 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4351 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4353 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 4354 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4356 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4357 | psa_set_key_algorithm(&attributes, alg); |
| 4358 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4359 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4360 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4361 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4362 | } |
| 4363 | |
| 4364 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4365 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4366 | if (input_buffer_size > 0) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 4367 | TEST_CALLOC(input, input_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4368 | memcpy(input, iv->x, iv->len); |
| 4369 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4370 | } |
| 4371 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4372 | 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^] | 4373 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4374 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4375 | /* Decrypt, one-short */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4376 | status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4377 | output_buffer_size, &output_length); |
| 4378 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4379 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4380 | /* Decrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4381 | status = psa_cipher_decrypt_setup(&operation, key, alg); |
| 4382 | if (status == PSA_SUCCESS) { |
| 4383 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 4384 | input_arg->len) + |
| 4385 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 4386 | TEST_CALLOC(output_multi, output_buffer_size); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4387 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4388 | if (iv->len > 0) { |
| 4389 | status = psa_cipher_set_iv(&operation, iv->x, iv->len); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4390 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4391 | if (status != PSA_SUCCESS) { |
| 4392 | TEST_EQUAL(status, expected_status); |
| 4393 | } |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4394 | } |
| 4395 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4396 | if (status == PSA_SUCCESS) { |
| 4397 | status = psa_cipher_update(&operation, |
| 4398 | input_arg->x, input_arg->len, |
| 4399 | output_multi, output_buffer_size, |
| 4400 | &function_output_length); |
| 4401 | if (status == PSA_SUCCESS) { |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4402 | output_length = function_output_length; |
| 4403 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4404 | status = psa_cipher_finish(&operation, |
| 4405 | output_multi + output_length, |
| 4406 | output_buffer_size - output_length, |
| 4407 | &function_output_length); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4408 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4409 | TEST_EQUAL(status, expected_status); |
| 4410 | } else { |
| 4411 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4412 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4413 | } else { |
| 4414 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4415 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4416 | } else { |
| 4417 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4418 | } |
| 4419 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4420 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4421 | psa_cipher_abort(&operation); |
| 4422 | mbedtls_free(input); |
| 4423 | mbedtls_free(output); |
| 4424 | mbedtls_free(output_multi); |
| 4425 | psa_destroy_key(key); |
| 4426 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4427 | } |
| 4428 | /* END_CASE */ |
| 4429 | |
| 4430 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4431 | void cipher_decrypt(int alg_arg, |
| 4432 | int key_type_arg, |
| 4433 | data_t *key_data, |
| 4434 | data_t *iv, |
| 4435 | data_t *input_arg, |
| 4436 | data_t *expected_output) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4437 | { |
| 4438 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4439 | psa_key_type_t key_type = key_type_arg; |
| 4440 | psa_algorithm_t alg = alg_arg; |
| 4441 | unsigned char *input = NULL; |
| 4442 | size_t input_buffer_size = 0; |
| 4443 | unsigned char *output = NULL; |
| 4444 | size_t output_buffer_size = 0; |
| 4445 | size_t output_length = 0; |
| 4446 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4447 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4448 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4449 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4450 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4451 | psa_set_key_algorithm(&attributes, alg); |
| 4452 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4453 | |
| 4454 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4455 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4456 | if (input_buffer_size > 0) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 4457 | TEST_CALLOC(input, input_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4458 | memcpy(input, iv->x, iv->len); |
| 4459 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4460 | } |
| 4461 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4462 | 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^] | 4463 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4464 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4465 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4466 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4467 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4468 | PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4469 | output_buffer_size, &output_length)); |
| 4470 | TEST_LE_U(output_length, |
| 4471 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size)); |
| 4472 | TEST_LE_U(output_length, |
| 4473 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4474 | |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 4475 | TEST_BUFFERS_EQUAL(expected_output->x, expected_output->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4476 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4477 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4478 | mbedtls_free(input); |
| 4479 | mbedtls_free(output); |
| 4480 | psa_destroy_key(key); |
| 4481 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4482 | } |
| 4483 | /* END_CASE */ |
| 4484 | |
| 4485 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4486 | void cipher_verify_output(int alg_arg, |
| 4487 | int key_type_arg, |
| 4488 | data_t *key_data, |
| 4489 | data_t *input) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4490 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4491 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4492 | psa_key_type_t key_type = key_type_arg; |
| 4493 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4494 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4495 | size_t output1_size = 0; |
| 4496 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4497 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4498 | size_t output2_size = 0; |
| 4499 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4500 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4501 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4502 | PSA_ASSERT(psa_crypto_init()); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4503 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4504 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4505 | psa_set_key_algorithm(&attributes, alg); |
| 4506 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4507 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4508 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4509 | &key)); |
| 4510 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 4511 | TEST_CALLOC(output1, output1_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4512 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4513 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, |
| 4514 | output1, output1_size, |
| 4515 | &output1_length)); |
| 4516 | TEST_LE_U(output1_length, |
| 4517 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4518 | TEST_LE_U(output1_length, |
| 4519 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4520 | |
| 4521 | output2_size = output1_length; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 4522 | TEST_CALLOC(output2, output2_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4523 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4524 | PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length, |
| 4525 | output2, output2_size, |
| 4526 | &output2_length)); |
| 4527 | TEST_LE_U(output2_length, |
| 4528 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4529 | TEST_LE_U(output2_length, |
| 4530 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4531 | |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 4532 | TEST_BUFFERS_EQUAL(input->x, input->len, output2, output2_length); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4533 | |
| 4534 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4535 | mbedtls_free(output1); |
| 4536 | mbedtls_free(output2); |
| 4537 | psa_destroy_key(key); |
| 4538 | PSA_DONE(); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4539 | } |
| 4540 | /* END_CASE */ |
| 4541 | |
| 4542 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4543 | void cipher_verify_output_multipart(int alg_arg, |
| 4544 | int key_type_arg, |
| 4545 | data_t *key_data, |
| 4546 | data_t *input, |
| 4547 | int first_part_size_arg) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4548 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4549 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4550 | psa_key_type_t key_type = key_type_arg; |
| 4551 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4552 | size_t first_part_size = first_part_size_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4553 | unsigned char iv[16] = { 0 }; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4554 | size_t iv_size = 16; |
| 4555 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4556 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4557 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4558 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4559 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4560 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4561 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4562 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4563 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 4564 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4565 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4566 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4567 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4568 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4569 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4570 | psa_set_key_algorithm(&attributes, alg); |
| 4571 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4572 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4573 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4574 | &key)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4575 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4576 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg)); |
| 4577 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4578 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4579 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 4580 | PSA_ASSERT(psa_cipher_generate_iv(&operation1, |
| 4581 | iv, iv_size, |
| 4582 | &iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4583 | } |
| 4584 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4585 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4586 | TEST_LE_U(output1_buffer_size, |
| 4587 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 4588 | TEST_CALLOC(output1, output1_buffer_size); |
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 | TEST_LE_U(first_part_size, input->len); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4591 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4592 | PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size, |
| 4593 | output1, output1_buffer_size, |
| 4594 | &function_output_length)); |
| 4595 | TEST_LE_U(function_output_length, |
| 4596 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4597 | TEST_LE_U(function_output_length, |
| 4598 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4599 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4600 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4601 | PSA_ASSERT(psa_cipher_update(&operation1, |
| 4602 | input->x + first_part_size, |
| 4603 | input->len - first_part_size, |
| 4604 | output1, output1_buffer_size, |
| 4605 | &function_output_length)); |
| 4606 | TEST_LE_U(function_output_length, |
| 4607 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4608 | alg, |
| 4609 | input->len - first_part_size)); |
| 4610 | TEST_LE_U(function_output_length, |
| 4611 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4612 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4613 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4614 | PSA_ASSERT(psa_cipher_finish(&operation1, |
| 4615 | output1 + output1_length, |
| 4616 | output1_buffer_size - output1_length, |
| 4617 | &function_output_length)); |
| 4618 | TEST_LE_U(function_output_length, |
| 4619 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4620 | TEST_LE_U(function_output_length, |
| 4621 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4622 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4623 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4624 | PSA_ASSERT(psa_cipher_abort(&operation1)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4625 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4626 | output2_buffer_size = output1_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4627 | TEST_LE_U(output2_buffer_size, |
| 4628 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4629 | TEST_LE_U(output2_buffer_size, |
| 4630 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 4631 | TEST_CALLOC(output2, output2_buffer_size); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4632 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4633 | if (iv_length > 0) { |
| 4634 | PSA_ASSERT(psa_cipher_set_iv(&operation2, |
| 4635 | iv, iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4636 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4637 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4638 | PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size, |
| 4639 | output2, output2_buffer_size, |
| 4640 | &function_output_length)); |
| 4641 | TEST_LE_U(function_output_length, |
| 4642 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4643 | TEST_LE_U(function_output_length, |
| 4644 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4645 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4646 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4647 | PSA_ASSERT(psa_cipher_update(&operation2, |
| 4648 | output1 + first_part_size, |
| 4649 | output1_length - first_part_size, |
| 4650 | output2, output2_buffer_size, |
| 4651 | &function_output_length)); |
| 4652 | TEST_LE_U(function_output_length, |
| 4653 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4654 | alg, |
| 4655 | output1_length - first_part_size)); |
| 4656 | TEST_LE_U(function_output_length, |
| 4657 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4658 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4659 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4660 | PSA_ASSERT(psa_cipher_finish(&operation2, |
| 4661 | output2 + output2_length, |
| 4662 | output2_buffer_size - output2_length, |
| 4663 | &function_output_length)); |
| 4664 | TEST_LE_U(function_output_length, |
| 4665 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4666 | TEST_LE_U(function_output_length, |
| 4667 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4668 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4669 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4670 | PSA_ASSERT(psa_cipher_abort(&operation2)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4671 | |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 4672 | TEST_BUFFERS_EQUAL(input->x, input->len, output2, output2_length); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4673 | |
| 4674 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4675 | psa_cipher_abort(&operation1); |
| 4676 | psa_cipher_abort(&operation2); |
| 4677 | mbedtls_free(output1); |
| 4678 | mbedtls_free(output2); |
| 4679 | psa_destroy_key(key); |
| 4680 | PSA_DONE(); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4681 | } |
| 4682 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 4683 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4684 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4685 | void aead_encrypt_decrypt(int key_type_arg, data_t *key_data, |
| 4686 | int alg_arg, |
| 4687 | data_t *nonce, |
| 4688 | data_t *additional_data, |
| 4689 | data_t *input_data, |
| 4690 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4691 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4692 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4693 | psa_key_type_t key_type = key_type_arg; |
| 4694 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4695 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4696 | unsigned char *output_data = NULL; |
| 4697 | size_t output_size = 0; |
| 4698 | size_t output_length = 0; |
| 4699 | unsigned char *output_data2 = NULL; |
| 4700 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4701 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4702 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4703 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4704 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4705 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4706 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4707 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4708 | psa_set_key_algorithm(&attributes, alg); |
| 4709 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4710 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4711 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4712 | &key)); |
| 4713 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4714 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4715 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4716 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4717 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4718 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4719 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4720 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4721 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
| 4722 | TEST_EQUAL(output_size, |
| 4723 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4724 | TEST_LE_U(output_size, |
| 4725 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4726 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 4727 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4728 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4729 | status = psa_aead_encrypt(key, alg, |
| 4730 | nonce->x, nonce->len, |
| 4731 | additional_data->x, |
| 4732 | additional_data->len, |
| 4733 | input_data->x, input_data->len, |
| 4734 | output_data, output_size, |
| 4735 | &output_length); |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4736 | |
| 4737 | /* If the operation is not supported, just skip and not fail in case the |
| 4738 | * encryption involves a common limitation of cryptography hardwares and |
| 4739 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4740 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4741 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4742 | 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] | 4743 | } |
| 4744 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4745 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4746 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4747 | if (PSA_SUCCESS == expected_result) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 4748 | TEST_CALLOC(output_data2, output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4749 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4750 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4751 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4752 | TEST_EQUAL(input_data->len, |
| 4753 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length)); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4754 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4755 | TEST_LE_U(input_data->len, |
| 4756 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length)); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4757 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4758 | TEST_EQUAL(psa_aead_decrypt(key, alg, |
| 4759 | nonce->x, nonce->len, |
| 4760 | additional_data->x, |
| 4761 | additional_data->len, |
| 4762 | output_data, output_length, |
| 4763 | output_data2, output_length, |
| 4764 | &output_length2), |
| 4765 | expected_result); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4766 | |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 4767 | TEST_BUFFERS_EQUAL(input_data->x, input_data->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4768 | output_data2, output_length2); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4769 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4770 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4771 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4772 | psa_destroy_key(key); |
| 4773 | mbedtls_free(output_data); |
| 4774 | mbedtls_free(output_data2); |
| 4775 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4776 | } |
| 4777 | /* END_CASE */ |
| 4778 | |
| 4779 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4780 | void aead_encrypt(int key_type_arg, data_t *key_data, |
| 4781 | int alg_arg, |
| 4782 | data_t *nonce, |
| 4783 | data_t *additional_data, |
| 4784 | data_t *input_data, |
| 4785 | data_t *expected_result) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4786 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4787 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4788 | psa_key_type_t key_type = key_type_arg; |
| 4789 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4790 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4791 | unsigned char *output_data = NULL; |
| 4792 | size_t output_size = 0; |
| 4793 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4794 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4795 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4796 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4797 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4798 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4799 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4800 | psa_set_key_algorithm(&attributes, alg); |
| 4801 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4802 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4803 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4804 | &key)); |
| 4805 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4806 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4807 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4808 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4809 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4810 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4811 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4812 | TEST_EQUAL(output_size, |
| 4813 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4814 | TEST_LE_U(output_size, |
| 4815 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 4816 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4817 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4818 | status = psa_aead_encrypt(key, alg, |
| 4819 | nonce->x, nonce->len, |
| 4820 | additional_data->x, additional_data->len, |
| 4821 | input_data->x, input_data->len, |
| 4822 | output_data, output_size, |
| 4823 | &output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4824 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4825 | /* If the operation is not supported, just skip and not fail in case the |
| 4826 | * encryption involves a common limitation of cryptography hardwares and |
| 4827 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4828 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4829 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4830 | 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] | 4831 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4832 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4833 | PSA_ASSERT(status); |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 4834 | TEST_BUFFERS_EQUAL(expected_result->x, expected_result->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4835 | output_data, output_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4836 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4837 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4838 | psa_destroy_key(key); |
| 4839 | mbedtls_free(output_data); |
| 4840 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4841 | } |
| 4842 | /* END_CASE */ |
| 4843 | |
| 4844 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4845 | void aead_decrypt(int key_type_arg, data_t *key_data, |
| 4846 | int alg_arg, |
| 4847 | data_t *nonce, |
| 4848 | data_t *additional_data, |
| 4849 | data_t *input_data, |
| 4850 | data_t *expected_data, |
| 4851 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4852 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4853 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4854 | psa_key_type_t key_type = key_type_arg; |
| 4855 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4856 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4857 | unsigned char *output_data = NULL; |
| 4858 | size_t output_size = 0; |
| 4859 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4860 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4861 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4862 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4863 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4864 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4865 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4866 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4867 | psa_set_key_algorithm(&attributes, alg); |
| 4868 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4869 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4870 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4871 | &key)); |
| 4872 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4873 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4874 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4875 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4876 | alg); |
| 4877 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4878 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4879 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4880 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4881 | TEST_EQUAL(output_size, |
| 4882 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4883 | TEST_LE_U(output_size, |
| 4884 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4885 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 4886 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4887 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4888 | status = psa_aead_decrypt(key, alg, |
| 4889 | nonce->x, nonce->len, |
| 4890 | additional_data->x, |
| 4891 | additional_data->len, |
| 4892 | input_data->x, input_data->len, |
| 4893 | output_data, output_size, |
| 4894 | &output_length); |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4895 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4896 | /* If the operation is not supported, just skip and not fail in case the |
| 4897 | * decryption involves a common limitation of cryptography hardwares and |
| 4898 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4899 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4900 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4901 | 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] | 4902 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4903 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4904 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4905 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4906 | if (expected_result == PSA_SUCCESS) { |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 4907 | TEST_BUFFERS_EQUAL(expected_data->x, expected_data->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4908 | output_data, output_length); |
| 4909 | } |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4910 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4911 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4912 | psa_destroy_key(key); |
| 4913 | mbedtls_free(output_data); |
| 4914 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4915 | } |
| 4916 | /* END_CASE */ |
| 4917 | |
| 4918 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4919 | void aead_multipart_encrypt(int key_type_arg, data_t *key_data, |
| 4920 | int alg_arg, |
| 4921 | data_t *nonce, |
| 4922 | data_t *additional_data, |
| 4923 | data_t *input_data, |
| 4924 | int do_set_lengths, |
| 4925 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4926 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4927 | size_t ad_part_len = 0; |
| 4928 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4929 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4930 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4931 | for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) { |
| 4932 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4933 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4934 | if (do_set_lengths) { |
| 4935 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4936 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4937 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4938 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4939 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4940 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4941 | |
| 4942 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4943 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4944 | alg_arg, nonce, |
| 4945 | additional_data, |
| 4946 | ad_part_len, |
| 4947 | input_data, -1, |
| 4948 | set_lengths_method, |
| 4949 | expected_output, |
| 4950 | 1, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4951 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4952 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4953 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4954 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 4955 | mbedtls_test_set_step(1000 + ad_part_len); |
| 4956 | |
| 4957 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4958 | alg_arg, nonce, |
| 4959 | additional_data, |
| 4960 | ad_part_len, |
| 4961 | input_data, -1, |
| 4962 | set_lengths_method, |
| 4963 | expected_output, |
| 4964 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4965 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4966 | } |
| 4967 | } |
| 4968 | |
| 4969 | for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) { |
| 4970 | /* Split data into length(data_part_len) parts. */ |
| 4971 | mbedtls_test_set_step(2000 + data_part_len); |
| 4972 | |
| 4973 | if (do_set_lengths) { |
| 4974 | if (data_part_len & 0x01) { |
| 4975 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4976 | } else { |
| 4977 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
| 4978 | } |
| 4979 | } |
| 4980 | |
| 4981 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4982 | alg_arg, nonce, |
| 4983 | additional_data, -1, |
| 4984 | input_data, data_part_len, |
| 4985 | set_lengths_method, |
| 4986 | expected_output, |
| 4987 | 1, 0)) { |
| 4988 | break; |
| 4989 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4990 | |
| 4991 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4992 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4993 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4994 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4995 | alg_arg, nonce, |
| 4996 | additional_data, -1, |
| 4997 | input_data, data_part_len, |
| 4998 | set_lengths_method, |
| 4999 | expected_output, |
| 5000 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5001 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5002 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5003 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5004 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5005 | /* Goto is required to silence warnings about unused labels, as we |
| 5006 | * don't actually do any test assertions in this function. */ |
| 5007 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5008 | } |
| 5009 | /* END_CASE */ |
| 5010 | |
| 5011 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5012 | void aead_multipart_decrypt(int key_type_arg, data_t *key_data, |
| 5013 | int alg_arg, |
| 5014 | data_t *nonce, |
| 5015 | data_t *additional_data, |
| 5016 | data_t *input_data, |
| 5017 | int do_set_lengths, |
| 5018 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5019 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5020 | size_t ad_part_len = 0; |
| 5021 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5022 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5023 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5024 | for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5025 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5026 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5027 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5028 | if (do_set_lengths) { |
| 5029 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5030 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5031 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5032 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5033 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5034 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5035 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5036 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5037 | alg_arg, nonce, |
| 5038 | additional_data, |
| 5039 | ad_part_len, |
| 5040 | input_data, -1, |
| 5041 | set_lengths_method, |
| 5042 | expected_output, |
| 5043 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5044 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5045 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5046 | |
| 5047 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5048 | mbedtls_test_set_step(1000 + ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5049 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5050 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5051 | alg_arg, nonce, |
| 5052 | additional_data, |
| 5053 | ad_part_len, |
| 5054 | input_data, -1, |
| 5055 | set_lengths_method, |
| 5056 | expected_output, |
| 5057 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5058 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5059 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5060 | } |
| 5061 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5062 | 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] | 5063 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5064 | mbedtls_test_set_step(2000 + data_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 (do_set_lengths) { |
| 5067 | if (data_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5068 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5069 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5070 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5071 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5072 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5073 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5074 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5075 | alg_arg, nonce, |
| 5076 | additional_data, -1, |
| 5077 | input_data, data_part_len, |
| 5078 | set_lengths_method, |
| 5079 | expected_output, |
| 5080 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5081 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5082 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5083 | |
| 5084 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5085 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5086 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5087 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5088 | alg_arg, nonce, |
| 5089 | additional_data, -1, |
| 5090 | input_data, data_part_len, |
| 5091 | set_lengths_method, |
| 5092 | expected_output, |
| 5093 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5094 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5095 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5096 | } |
| 5097 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5098 | /* Goto is required to silence warnings about unused labels, as we |
| 5099 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5100 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5101 | } |
| 5102 | /* END_CASE */ |
| 5103 | |
| 5104 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5105 | void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data, |
| 5106 | int alg_arg, |
| 5107 | int nonce_length, |
| 5108 | int expected_nonce_length_arg, |
| 5109 | data_t *additional_data, |
| 5110 | data_t *input_data, |
| 5111 | int expected_status_arg) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5112 | { |
| 5113 | |
| 5114 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5115 | psa_key_type_t key_type = key_type_arg; |
| 5116 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5117 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5118 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5119 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5120 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5121 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5122 | size_t actual_nonce_length = 0; |
| 5123 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 5124 | unsigned char *output = NULL; |
| 5125 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5126 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5127 | size_t ciphertext_size = 0; |
| 5128 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5129 | size_t tag_length = 0; |
| 5130 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5131 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5132 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5133 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5134 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5135 | psa_set_key_algorithm(&attributes, alg); |
| 5136 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5137 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5138 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5139 | &key)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5140 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5141 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5142 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5143 | 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] | 5144 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 5145 | TEST_CALLOC(output, output_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5146 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5147 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5149 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5150 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 5151 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5153 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5154 | |
| 5155 | /* If the operation is not supported, just skip and not fail in case the |
| 5156 | * encryption involves a common limitation of cryptography hardwares and |
| 5157 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5158 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5159 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5160 | 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] | 5161 | } |
| 5162 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5163 | PSA_ASSERT(status); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5164 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5165 | status = psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5166 | nonce_length, |
| 5167 | &actual_nonce_length); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5168 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5169 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5170 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5171 | TEST_EQUAL(actual_nonce_length, expected_nonce_length); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 5172 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5173 | if (expected_status == PSA_SUCCESS) { |
| 5174 | TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type, |
| 5175 | alg)); |
| 5176 | } |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 5177 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5178 | TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 5179 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5180 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5181 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5182 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5183 | input_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5184 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5185 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5186 | additional_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5187 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5188 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5189 | output, output_size, |
| 5190 | &ciphertext_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5191 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5192 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5193 | &ciphertext_length, tag_buffer, |
| 5194 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5195 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5196 | |
| 5197 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5198 | psa_destroy_key(key); |
| 5199 | mbedtls_free(output); |
| 5200 | mbedtls_free(ciphertext); |
| 5201 | psa_aead_abort(&operation); |
| 5202 | PSA_DONE(); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5203 | } |
| 5204 | /* END_CASE */ |
| 5205 | |
| 5206 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5207 | void aead_multipart_set_nonce(int key_type_arg, data_t *key_data, |
| 5208 | int alg_arg, |
| 5209 | int nonce_length_arg, |
| 5210 | int set_lengths_method_arg, |
| 5211 | data_t *additional_data, |
| 5212 | data_t *input_data, |
| 5213 | int expected_status_arg) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5214 | { |
| 5215 | |
| 5216 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5217 | psa_key_type_t key_type = key_type_arg; |
| 5218 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5219 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5220 | uint8_t *nonce_buffer = NULL; |
| 5221 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5222 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5223 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5224 | unsigned char *output = NULL; |
| 5225 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5226 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5227 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5228 | size_t ciphertext_size = 0; |
| 5229 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5230 | size_t tag_length = 0; |
| 5231 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5232 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5233 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5234 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5235 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5236 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5237 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5238 | psa_set_key_algorithm(&attributes, alg); |
| 5239 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5240 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5241 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5242 | &key)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5243 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5244 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5245 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5246 | 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] | 5247 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 5248 | TEST_CALLOC(output, output_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5249 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5250 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5252 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5253 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 5254 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5256 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5257 | |
| 5258 | /* If the operation is not supported, just skip and not fail in case the |
| 5259 | * encryption involves a common limitation of cryptography hardwares and |
| 5260 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5261 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5262 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5263 | 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] | 5264 | } |
| 5265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5266 | PSA_ASSERT(status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5267 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5268 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5269 | if (nonce_length_arg == -1) { |
| 5270 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 5271 | TEST_CALLOC(nonce_buffer, 4); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5272 | nonce_length = 0; |
| 5273 | } else { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5274 | /* If length is zero, then this will return NULL. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5275 | nonce_length = (size_t) nonce_length_arg; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 5276 | TEST_CALLOC(nonce_buffer, nonce_length); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5277 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5278 | if (nonce_buffer) { |
| 5279 | for (index = 0; index < nonce_length - 1; ++index) { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5280 | nonce_buffer[index] = 'a' + index; |
| 5281 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5282 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5283 | } |
| 5284 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5285 | if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { |
| 5286 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5287 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5288 | } |
| 5289 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5290 | status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5291 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5292 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5293 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5294 | if (expected_status == PSA_SUCCESS) { |
| 5295 | if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) { |
| 5296 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5297 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5298 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5299 | 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] | 5300 | expected_status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5301 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5302 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5303 | /* 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] | 5304 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5305 | additional_data->len), |
| 5306 | expected_status); |
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(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5309 | output, output_size, |
| 5310 | &ciphertext_length), |
| 5311 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5312 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5313 | TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5314 | &ciphertext_length, tag_buffer, |
| 5315 | PSA_AEAD_TAG_MAX_SIZE, &tag_length), |
| 5316 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5317 | } |
| 5318 | |
| 5319 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5320 | psa_destroy_key(key); |
| 5321 | mbedtls_free(output); |
| 5322 | mbedtls_free(ciphertext); |
| 5323 | mbedtls_free(nonce_buffer); |
| 5324 | psa_aead_abort(&operation); |
| 5325 | PSA_DONE(); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5326 | } |
| 5327 | /* END_CASE */ |
| 5328 | |
| 5329 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5330 | 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] | 5331 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5332 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5333 | data_t *nonce, |
| 5334 | data_t *additional_data, |
| 5335 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5336 | int expected_status_arg) |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5337 | { |
| 5338 | |
| 5339 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5340 | psa_key_type_t key_type = key_type_arg; |
| 5341 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5342 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5343 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5344 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5345 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5346 | unsigned char *output = NULL; |
| 5347 | unsigned char *ciphertext = NULL; |
| 5348 | size_t output_size = output_size_arg; |
| 5349 | size_t ciphertext_size = 0; |
| 5350 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5351 | size_t tag_length = 0; |
| 5352 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5354 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5356 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5357 | psa_set_key_algorithm(&attributes, alg); |
| 5358 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5359 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5360 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5361 | &key)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5362 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5363 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5364 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 5365 | TEST_CALLOC(output, output_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5367 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5368 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 5369 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5371 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5372 | |
| 5373 | /* If the operation is not supported, just skip and not fail in case the |
| 5374 | * encryption involves a common limitation of cryptography hardwares and |
| 5375 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5376 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5377 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5378 | 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] | 5379 | } |
| 5380 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5381 | PSA_ASSERT(status); |
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 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5384 | input_data->len)); |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5386 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5387 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5388 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5389 | additional_data->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5390 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5391 | status = psa_aead_update(&operation, input_data->x, input_data->len, |
| 5392 | output, output_size, &ciphertext_length); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5393 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5394 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5395 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5396 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5397 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5398 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5399 | &ciphertext_length, tag_buffer, |
| 5400 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5401 | } |
| 5402 | |
| 5403 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5404 | psa_destroy_key(key); |
| 5405 | mbedtls_free(output); |
| 5406 | mbedtls_free(ciphertext); |
| 5407 | psa_aead_abort(&operation); |
| 5408 | PSA_DONE(); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5409 | } |
| 5410 | /* END_CASE */ |
| 5411 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5412 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5413 | void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data, |
| 5414 | int alg_arg, |
| 5415 | int finish_ciphertext_size_arg, |
| 5416 | int tag_size_arg, |
| 5417 | data_t *nonce, |
| 5418 | data_t *additional_data, |
| 5419 | data_t *input_data, |
| 5420 | int expected_status_arg) |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5421 | { |
| 5422 | |
| 5423 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5424 | psa_key_type_t key_type = key_type_arg; |
| 5425 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5426 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5427 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5428 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5429 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5430 | unsigned char *ciphertext = NULL; |
| 5431 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5432 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5433 | size_t ciphertext_size = 0; |
| 5434 | size_t ciphertext_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5435 | size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg; |
| 5436 | size_t tag_size = (size_t) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5437 | size_t tag_length = 0; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5438 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5439 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5440 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5441 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5442 | psa_set_key_algorithm(&attributes, alg); |
| 5443 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5444 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5445 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5446 | &key)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5447 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5448 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5449 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5450 | 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] | 5451 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 5452 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5453 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 5454 | TEST_CALLOC(finish_ciphertext, finish_ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5455 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 5456 | TEST_CALLOC(tag_buffer, tag_size); |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5457 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5458 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5459 | |
| 5460 | /* If the operation is not supported, just skip and not fail in case the |
| 5461 | * encryption involves a common limitation of cryptography hardwares and |
| 5462 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5463 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5464 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5465 | 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] | 5466 | } |
| 5467 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5468 | PSA_ASSERT(status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5469 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5470 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5471 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5472 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5473 | input_data->len)); |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5474 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5475 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5476 | additional_data->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5477 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5478 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5479 | ciphertext, ciphertext_size, &ciphertext_length)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5480 | |
| 5481 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5482 | status = psa_aead_finish(&operation, finish_ciphertext, |
| 5483 | finish_ciphertext_size, |
| 5484 | &ciphertext_length, tag_buffer, |
| 5485 | tag_size, &tag_length); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5486 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5487 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5488 | |
| 5489 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5490 | psa_destroy_key(key); |
| 5491 | mbedtls_free(ciphertext); |
| 5492 | mbedtls_free(finish_ciphertext); |
| 5493 | mbedtls_free(tag_buffer); |
| 5494 | psa_aead_abort(&operation); |
| 5495 | PSA_DONE(); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5496 | } |
| 5497 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5498 | |
| 5499 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5500 | void aead_multipart_verify(int key_type_arg, data_t *key_data, |
| 5501 | int alg_arg, |
| 5502 | data_t *nonce, |
| 5503 | data_t *additional_data, |
| 5504 | data_t *input_data, |
| 5505 | data_t *tag, |
| 5506 | int tag_usage_arg, |
| 5507 | int expected_setup_status_arg, |
| 5508 | int expected_status_arg) |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5509 | { |
| 5510 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5511 | psa_key_type_t key_type = key_type_arg; |
| 5512 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5513 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5514 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5515 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5516 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5517 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5518 | unsigned char *plaintext = NULL; |
| 5519 | unsigned char *finish_plaintext = NULL; |
| 5520 | size_t plaintext_size = 0; |
| 5521 | size_t plaintext_length = 0; |
| 5522 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5523 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5524 | unsigned char *tag_buffer = NULL; |
| 5525 | size_t tag_size = 0; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5526 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5527 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5528 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5529 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 5530 | psa_set_key_algorithm(&attributes, alg); |
| 5531 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5532 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5533 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5534 | &key)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5535 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5536 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5537 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5538 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 5539 | input_data->len); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5540 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 5541 | TEST_CALLOC(plaintext, plaintext_size); |
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 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5544 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 5545 | TEST_CALLOC(finish_plaintext, verify_plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5546 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5547 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5548 | |
| 5549 | /* If the operation is not supported, just skip and not fail in case the |
| 5550 | * encryption involves a common limitation of cryptography hardwares and |
| 5551 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5552 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5553 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5554 | 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] | 5555 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5556 | TEST_EQUAL(status, expected_setup_status); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5557 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5558 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5559 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5560 | } |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5562 | PSA_ASSERT(status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5563 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5564 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5565 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5566 | status = psa_aead_set_lengths(&operation, additional_data->len, |
| 5567 | input_data->len); |
| 5568 | PSA_ASSERT(status); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5569 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5570 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5571 | additional_data->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5572 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5573 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 5574 | input_data->len, |
| 5575 | plaintext, plaintext_size, |
| 5576 | &plaintext_length)); |
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 | if (tag_usage == USE_GIVEN_TAG) { |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5579 | tag_buffer = tag->x; |
| 5580 | tag_size = tag->len; |
| 5581 | } |
| 5582 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5583 | status = psa_aead_verify(&operation, finish_plaintext, |
| 5584 | verify_plaintext_size, |
| 5585 | &plaintext_length, |
| 5586 | tag_buffer, tag_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5587 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5588 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5589 | |
| 5590 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5591 | psa_destroy_key(key); |
| 5592 | mbedtls_free(plaintext); |
| 5593 | mbedtls_free(finish_plaintext); |
| 5594 | psa_aead_abort(&operation); |
| 5595 | PSA_DONE(); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5596 | } |
| 5597 | /* END_CASE */ |
| 5598 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5599 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5600 | void aead_multipart_setup(int key_type_arg, data_t *key_data, |
| 5601 | int alg_arg, int expected_status_arg) |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5602 | { |
| 5603 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5604 | psa_key_type_t key_type = key_type_arg; |
| 5605 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5606 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5607 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5608 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5609 | psa_status_t expected_status = expected_status_arg; |
| 5610 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5611 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5612 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5613 | psa_set_key_usage_flags(&attributes, |
| 5614 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5615 | psa_set_key_algorithm(&attributes, alg); |
| 5616 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5617 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5618 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5619 | &key)); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5620 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5621 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5622 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5623 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5624 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5625 | psa_aead_abort(&operation); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5626 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5627 | status = psa_aead_decrypt_setup(&operation, key, alg); |
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 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5630 | |
| 5631 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5632 | psa_destroy_key(key); |
| 5633 | psa_aead_abort(&operation); |
| 5634 | PSA_DONE(); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5635 | } |
| 5636 | /* END_CASE */ |
| 5637 | |
| 5638 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5639 | void aead_multipart_state_test(int key_type_arg, data_t *key_data, |
| 5640 | int alg_arg, |
| 5641 | data_t *nonce, |
| 5642 | data_t *additional_data, |
| 5643 | data_t *input_data) |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5644 | { |
| 5645 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5646 | psa_key_type_t key_type = key_type_arg; |
| 5647 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5648 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5649 | unsigned char *output_data = NULL; |
| 5650 | unsigned char *final_data = NULL; |
| 5651 | size_t output_size = 0; |
| 5652 | size_t finish_output_size = 0; |
| 5653 | size_t output_length = 0; |
| 5654 | size_t key_bits = 0; |
| 5655 | size_t tag_length = 0; |
| 5656 | size_t tag_size = 0; |
| 5657 | size_t nonce_length = 0; |
| 5658 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5659 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5660 | size_t output_part_length = 0; |
| 5661 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5662 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5663 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5664 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5665 | psa_set_key_usage_flags(&attributes, |
| 5666 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5667 | psa_set_key_algorithm(&attributes, alg); |
| 5668 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5669 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5670 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5671 | &key)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5672 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5673 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5674 | key_bits = psa_get_key_bits(&attributes); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5676 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5677 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5678 | TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5679 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5680 | 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] | 5681 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 5682 | TEST_CALLOC(output_data, output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5683 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5684 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
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 | TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5687 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 5688 | TEST_CALLOC(final_data, finish_output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5689 | |
| 5690 | /* Test all operations error without calling setup first. */ |
| 5691 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5692 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5693 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5694 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5695 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5696 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5697 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5698 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5699 | &nonce_length), |
| 5700 | PSA_ERROR_BAD_STATE); |
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 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5703 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5704 | /* ------------------------------------------------------- */ |
| 5705 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5706 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 5707 | input_data->len), |
| 5708 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5709 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5710 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5711 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5712 | /* ------------------------------------------------------- */ |
| 5713 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5714 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5715 | additional_data->len), |
| 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_update(&operation, input_data->x, |
| 5723 | input_data->len, output_data, |
| 5724 | output_size, &output_length), |
| 5725 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5726 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5727 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5728 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5729 | /* ------------------------------------------------------- */ |
| 5730 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5731 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 5732 | finish_output_size, |
| 5733 | &output_part_length, |
| 5734 | tag_buffer, tag_length, |
| 5735 | &tag_size), |
| 5736 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5737 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5738 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5739 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5740 | /* ------------------------------------------------------- */ |
| 5741 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5742 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 5743 | finish_output_size, |
| 5744 | &output_part_length, |
| 5745 | tag_buffer, |
| 5746 | tag_length), |
| 5747 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5748 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5749 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5750 | |
| 5751 | /* Test for double setups. */ |
| 5752 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5753 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5754 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5755 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 5756 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5757 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5758 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5759 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5760 | /* ------------------------------------------------------- */ |
| 5761 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5762 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5763 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5764 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 5765 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5766 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5767 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5768 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5769 | /* ------------------------------------------------------- */ |
| 5770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5771 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5772 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5773 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 5774 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5775 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5776 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5777 | |
| 5778 | /* ------------------------------------------------------- */ |
| 5779 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5780 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5781 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5782 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 5783 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5784 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5785 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5786 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5787 | /* Test for not setting a nonce. */ |
| 5788 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5789 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5790 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5791 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5792 | additional_data->len), |
| 5793 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5794 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5795 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5796 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5797 | /* ------------------------------------------------------- */ |
| 5798 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5799 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5800 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5801 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 5802 | input_data->len, output_data, |
| 5803 | output_size, &output_length), |
| 5804 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5805 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5806 | psa_aead_abort(&operation); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5807 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5808 | /* ------------------------------------------------------- */ |
| 5809 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5810 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5811 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5812 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 5813 | finish_output_size, |
| 5814 | &output_part_length, |
| 5815 | tag_buffer, tag_length, |
| 5816 | &tag_size), |
| 5817 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5818 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5819 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5820 | |
| 5821 | /* ------------------------------------------------------- */ |
| 5822 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5823 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5824 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5825 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 5826 | finish_output_size, |
| 5827 | &output_part_length, |
| 5828 | tag_buffer, |
| 5829 | tag_length), |
| 5830 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5831 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5832 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5833 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5834 | /* Test for double setting nonce. */ |
| 5835 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5836 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5837 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5838 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5839 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5840 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5841 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5842 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5843 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5844 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5845 | /* Test for double generating nonce. */ |
| 5846 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5847 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5848 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5849 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5850 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5851 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5852 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5853 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5854 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5855 | &nonce_length), |
| 5856 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5857 | |
| 5858 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5859 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5860 | |
| 5861 | /* Test for generate nonce then set and vice versa */ |
| 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_set_nonce(&operation, nonce->x, nonce->len), |
| 5870 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5871 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5872 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5873 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5874 | /* Test for generating nonce after calling set lengths */ |
| 5875 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5876 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5877 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5878 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5879 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +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)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5884 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5885 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5886 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5887 | /* 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] | 5888 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5889 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5890 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5891 | if (operation.alg == PSA_ALG_CCM) { |
| 5892 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5893 | input_data->len), |
| 5894 | PSA_ERROR_INVALID_ARGUMENT); |
| 5895 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5896 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5897 | &nonce_length), |
| 5898 | PSA_ERROR_BAD_STATE); |
| 5899 | } else { |
| 5900 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5901 | input_data->len)); |
| 5902 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5903 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5904 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5905 | } |
| 5906 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5907 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5908 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5909 | /* 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] | 5910 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5911 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5912 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5913 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 5914 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5915 | input_data->len), |
| 5916 | PSA_ERROR_INVALID_ARGUMENT); |
| 5917 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5918 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5919 | &nonce_length), |
| 5920 | PSA_ERROR_BAD_STATE); |
| 5921 | } else { |
| 5922 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5923 | input_data->len)); |
| 5924 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5925 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5926 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5927 | } |
| 5928 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5929 | psa_aead_abort(&operation); |
Dave Rodgman | d26d744 | 2023-02-11 17:14:54 +0000 | [diff] [blame] | 5930 | #endif |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5931 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5932 | /* 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] | 5933 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5934 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5935 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5936 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5937 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5938 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5939 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5940 | if (operation.alg == PSA_ALG_CCM) { |
| 5941 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5942 | input_data->len), |
| 5943 | PSA_ERROR_INVALID_ARGUMENT); |
| 5944 | } else { |
| 5945 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5946 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5947 | } |
| 5948 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5949 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5950 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5951 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5952 | /* Test for setting nonce after calling set lengths */ |
| 5953 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5954 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5955 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5956 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5957 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5958 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5959 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5960 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5961 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5962 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5963 | /* 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] | 5964 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5965 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5966 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5967 | if (operation.alg == PSA_ALG_CCM) { |
| 5968 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5969 | input_data->len), |
| 5970 | PSA_ERROR_INVALID_ARGUMENT); |
| 5971 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5972 | PSA_ERROR_BAD_STATE); |
| 5973 | } else { |
| 5974 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5975 | input_data->len)); |
| 5976 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5977 | } |
| 5978 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5979 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5980 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5981 | /* 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] | 5982 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5983 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5984 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5985 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 5986 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5987 | input_data->len), |
| 5988 | PSA_ERROR_INVALID_ARGUMENT); |
| 5989 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5990 | PSA_ERROR_BAD_STATE); |
| 5991 | } else { |
| 5992 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5993 | input_data->len)); |
| 5994 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5995 | } |
| 5996 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5997 | psa_aead_abort(&operation); |
Dave Rodgman | a476363 | 2023-02-11 18:36:23 +0000 | [diff] [blame] | 5998 | #endif |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5999 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6000 | /* 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] | 6001 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6002 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6003 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6004 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6005 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6006 | if (operation.alg == PSA_ALG_CCM) { |
| 6007 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6008 | input_data->len), |
| 6009 | PSA_ERROR_INVALID_ARGUMENT); |
| 6010 | } else { |
| 6011 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6012 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6013 | } |
| 6014 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6015 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6016 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6017 | /* 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] | 6018 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6019 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6020 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6021 | if (operation.alg == PSA_ALG_GCM) { |
| 6022 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6023 | SIZE_MAX), |
| 6024 | PSA_ERROR_INVALID_ARGUMENT); |
| 6025 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6026 | PSA_ERROR_BAD_STATE); |
| 6027 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6028 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6029 | SIZE_MAX)); |
| 6030 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6031 | } |
| 6032 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6033 | psa_aead_abort(&operation); |
Dave Rodgman | 91e8321 | 2023-02-11 20:07:43 +0000 | [diff] [blame] | 6034 | #endif |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6035 | |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 6036 | /* 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] | 6037 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6038 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6039 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6040 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6041 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6042 | if (operation.alg == PSA_ALG_GCM) { |
| 6043 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6044 | SIZE_MAX), |
| 6045 | PSA_ERROR_INVALID_ARGUMENT); |
| 6046 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6047 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6048 | SIZE_MAX)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6049 | } |
| 6050 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6051 | psa_aead_abort(&operation); |
Dave Rodgman | 641288b | 2023-02-11 22:02:04 +0000 | [diff] [blame] | 6052 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6053 | |
| 6054 | /* ------------------------------------------------------- */ |
| 6055 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6056 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6057 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6058 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6059 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6060 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6061 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6062 | &nonce_length), |
| 6063 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6064 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6065 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6066 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6067 | /* Test for generating nonce in decrypt setup. */ |
| 6068 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6069 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6070 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6071 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6072 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6073 | &nonce_length), |
| 6074 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6075 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6076 | psa_aead_abort(&operation); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6077 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6078 | /* Test for setting lengths twice. */ |
| 6079 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6080 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6081 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6082 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6083 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6084 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6085 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6086 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6087 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6088 | input_data->len), |
| 6089 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6090 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6091 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6092 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6093 | /* Test for setting lengths after setting nonce + already starting data. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6094 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6095 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6096 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6097 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6098 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6099 | if (operation.alg == PSA_ALG_CCM) { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6100 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6101 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6102 | additional_data->len), |
| 6103 | PSA_ERROR_BAD_STATE); |
| 6104 | } else { |
| 6105 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6106 | additional_data->len)); |
| 6107 | |
| 6108 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6109 | input_data->len), |
| 6110 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6111 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6112 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6113 | |
| 6114 | /* ------------------------------------------------------- */ |
| 6115 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6116 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6117 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6118 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6119 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6120 | if (operation.alg == PSA_ALG_CCM) { |
| 6121 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6122 | input_data->len, output_data, |
| 6123 | output_size, &output_length), |
| 6124 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6125 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6126 | } else { |
| 6127 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6128 | input_data->len, output_data, |
| 6129 | output_size, &output_length)); |
| 6130 | |
| 6131 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6132 | input_data->len), |
| 6133 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6134 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6135 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6136 | |
| 6137 | /* ------------------------------------------------------- */ |
| 6138 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6139 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6140 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6141 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6142 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6143 | if (operation.alg == PSA_ALG_CCM) { |
| 6144 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6145 | finish_output_size, |
| 6146 | &output_part_length, |
| 6147 | tag_buffer, tag_length, |
| 6148 | &tag_size)); |
| 6149 | } else { |
| 6150 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6151 | finish_output_size, |
| 6152 | &output_part_length, |
| 6153 | tag_buffer, tag_length, |
| 6154 | &tag_size)); |
| 6155 | |
| 6156 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6157 | input_data->len), |
| 6158 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6159 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6160 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6161 | |
| 6162 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 6163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6164 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6165 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6166 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6167 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6168 | &nonce_length)); |
| 6169 | if (operation.alg == PSA_ALG_CCM) { |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6170 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6171 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6172 | additional_data->len), |
| 6173 | PSA_ERROR_BAD_STATE); |
| 6174 | } else { |
| 6175 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6176 | additional_data->len)); |
| 6177 | |
| 6178 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6179 | input_data->len), |
| 6180 | PSA_ERROR_BAD_STATE); |
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_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6183 | |
| 6184 | /* ------------------------------------------------------- */ |
| 6185 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6186 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6187 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6188 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6189 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6190 | &nonce_length)); |
| 6191 | if (operation.alg == PSA_ALG_CCM) { |
| 6192 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6193 | input_data->len, output_data, |
| 6194 | output_size, &output_length), |
| 6195 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6196 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6197 | } else { |
| 6198 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6199 | input_data->len, output_data, |
| 6200 | output_size, &output_length)); |
| 6201 | |
| 6202 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6203 | input_data->len), |
| 6204 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6205 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6206 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6207 | |
| 6208 | /* ------------------------------------------------------- */ |
| 6209 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6210 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6211 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6212 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6213 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6214 | &nonce_length)); |
| 6215 | if (operation.alg == PSA_ALG_CCM) { |
| 6216 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6217 | finish_output_size, |
| 6218 | &output_part_length, |
| 6219 | tag_buffer, tag_length, |
| 6220 | &tag_size)); |
| 6221 | } else { |
| 6222 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6223 | finish_output_size, |
| 6224 | &output_part_length, |
| 6225 | tag_buffer, tag_length, |
| 6226 | &tag_size)); |
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 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6229 | input_data->len), |
| 6230 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6231 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6232 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6233 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6234 | /* Test for not sending any additional data or data after setting non zero |
| 6235 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6236 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6237 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6238 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6239 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6240 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6241 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6242 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6243 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6244 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6245 | finish_output_size, |
| 6246 | &output_part_length, |
| 6247 | tag_buffer, tag_length, |
| 6248 | &tag_size), |
| 6249 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6250 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6251 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6252 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6253 | /* Test for not sending any additional data or data after setting non-zero |
| 6254 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6256 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6257 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6258 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->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 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6261 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6263 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6264 | finish_output_size, |
| 6265 | &output_part_length, |
| 6266 | tag_buffer, |
| 6267 | tag_length), |
| 6268 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6269 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6270 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6271 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6272 | /* Test for not sending any additional data after setting a non-zero length |
| 6273 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6274 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6275 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6276 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6277 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->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 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6280 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6282 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6283 | input_data->len, output_data, |
| 6284 | output_size, &output_length), |
| 6285 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6286 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6287 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6288 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6289 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 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 | f94bd99 | 2021-09-19 18:15:59 +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 | f94bd99 | 2021-09-19 18:15:59 +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 | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6297 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6298 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6299 | additional_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6300 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6301 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6302 | finish_output_size, |
| 6303 | &output_part_length, |
| 6304 | tag_buffer, tag_length, |
| 6305 | &tag_size), |
| 6306 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6307 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6308 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6309 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6310 | /* Test for sending too much additional data after setting lengths. */ |
| 6311 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6312 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6314 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6315 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6316 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6317 | |
| 6318 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6319 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6320 | additional_data->len), |
| 6321 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6322 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6323 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6324 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6325 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6326 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6327 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6328 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6329 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6330 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6331 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6332 | input_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6333 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6334 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6335 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6336 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6337 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6338 | 1), |
| 6339 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6340 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6341 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6342 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6343 | /* Test for sending too much data after setting lengths. */ |
| 6344 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6345 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6346 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6347 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6349 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6350 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6351 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6352 | input_data->len, output_data, |
| 6353 | output_size, &output_length), |
| 6354 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6356 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6357 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6358 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6359 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6360 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6361 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6362 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6363 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6364 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6365 | input_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6367 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6368 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6370 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6371 | input_data->len, output_data, |
| 6372 | output_size, &output_length)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6374 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6375 | 1, output_data, |
| 6376 | output_size, &output_length), |
| 6377 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6378 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6379 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6380 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6381 | /* Test sending additional data after data. */ |
| 6382 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6383 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6385 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6386 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6387 | if (operation.alg != PSA_ALG_CCM) { |
| 6388 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6389 | input_data->len, output_data, |
| 6390 | output_size, &output_length)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6392 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6393 | additional_data->len), |
| 6394 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6395 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6396 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6397 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6398 | /* Test calling finish on decryption. */ |
| 6399 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6400 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6401 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6402 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6403 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6404 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6405 | finish_output_size, |
| 6406 | &output_part_length, |
| 6407 | tag_buffer, tag_length, |
| 6408 | &tag_size), |
| 6409 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6410 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6411 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6412 | |
| 6413 | /* Test calling verify on encryption. */ |
| 6414 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6415 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6416 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6417 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6418 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6419 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6420 | finish_output_size, |
| 6421 | &output_part_length, |
| 6422 | tag_buffer, |
| 6423 | tag_length), |
| 6424 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6425 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6426 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6427 | |
| 6428 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6429 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6430 | psa_destroy_key(key); |
| 6431 | psa_aead_abort(&operation); |
| 6432 | mbedtls_free(output_data); |
| 6433 | mbedtls_free(final_data); |
| 6434 | PSA_DONE(); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6435 | } |
| 6436 | /* END_CASE */ |
| 6437 | |
| 6438 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6439 | void signature_size(int type_arg, |
| 6440 | int bits, |
| 6441 | int alg_arg, |
| 6442 | int expected_size_arg) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6443 | { |
| 6444 | psa_key_type_t type = type_arg; |
| 6445 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6446 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6447 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6448 | TEST_EQUAL(actual_size, (size_t) expected_size_arg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6449 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6450 | exit: |
| 6451 | ; |
| 6452 | } |
| 6453 | /* END_CASE */ |
| 6454 | |
| 6455 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6456 | void sign_hash_deterministic(int key_type_arg, data_t *key_data, |
| 6457 | int alg_arg, data_t *input_data, |
| 6458 | data_t *output_data) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6459 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6460 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6461 | psa_key_type_t key_type = key_type_arg; |
| 6462 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6463 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6464 | unsigned char *signature = NULL; |
| 6465 | size_t signature_size; |
| 6466 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6467 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6468 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6469 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6470 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6471 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6472 | psa_set_key_algorithm(&attributes, alg); |
| 6473 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6474 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6475 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6476 | &key)); |
| 6477 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6478 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6479 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6480 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6481 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6482 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6483 | key_bits, alg); |
| 6484 | TEST_ASSERT(signature_size != 0); |
| 6485 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 6486 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6487 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6488 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6489 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6490 | input_data->x, input_data->len, |
| 6491 | signature, signature_size, |
| 6492 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6493 | /* Verify that the signature is what is expected. */ |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 6494 | TEST_BUFFERS_EQUAL(output_data->x, output_data->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6495 | signature, signature_length); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6496 | |
| 6497 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6498 | /* |
| 6499 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6500 | * thus reset them as required. |
| 6501 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6502 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6503 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6504 | psa_destroy_key(key); |
| 6505 | mbedtls_free(signature); |
| 6506 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6507 | } |
| 6508 | /* END_CASE */ |
| 6509 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6510 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6511 | /** |
| 6512 | * sign_hash_interruptible() test intentions: |
| 6513 | * |
| 6514 | * Note: This test can currently only handle ECDSA. |
| 6515 | * |
| 6516 | * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 6517 | * and private keys / keypairs only). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6518 | * |
| 6519 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 6520 | * expected for different max_ops values. |
| 6521 | * |
| 6522 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 6523 | * and that each successful stage completes some ops (this is not mandated by |
| 6524 | * the PSA specification, but is currently the case). |
| 6525 | * |
| 6526 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 6527 | * complete() calls does not alter the number of ops returned. |
| 6528 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6529 | void sign_hash_interruptible(int key_type_arg, data_t *key_data, |
| 6530 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6531 | data_t *output_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6532 | { |
| 6533 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6534 | psa_key_type_t key_type = key_type_arg; |
| 6535 | psa_algorithm_t alg = alg_arg; |
| 6536 | size_t key_bits; |
| 6537 | unsigned char *signature = NULL; |
| 6538 | size_t signature_size; |
| 6539 | size_t signature_length = 0xdeadbeef; |
| 6540 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6541 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6542 | uint32_t num_ops = 0; |
| 6543 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6544 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6545 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6546 | size_t min_completes = 0; |
| 6547 | size_t max_completes = 0; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6548 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6549 | psa_sign_hash_interruptible_operation_t operation = |
| 6550 | psa_sign_hash_interruptible_operation_init(); |
| 6551 | |
| 6552 | PSA_ASSERT(psa_crypto_init()); |
| 6553 | |
| 6554 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6555 | psa_set_key_algorithm(&attributes, alg); |
| 6556 | psa_set_key_type(&attributes, key_type); |
| 6557 | |
| 6558 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6559 | &key)); |
| 6560 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6561 | key_bits = psa_get_key_bits(&attributes); |
| 6562 | |
| 6563 | /* Allocate a buffer which has the size advertised by the |
| 6564 | * library. */ |
| 6565 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6566 | key_bits, alg); |
| 6567 | TEST_ASSERT(signature_size != 0); |
| 6568 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 6569 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6570 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6571 | psa_interruptible_set_max_ops(max_ops); |
| 6572 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6573 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6574 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6575 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6576 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6577 | TEST_ASSERT(num_ops_prior == 0); |
| 6578 | |
| 6579 | /* Start performing the signature. */ |
| 6580 | PSA_ASSERT(psa_sign_hash_start(&operation, key, alg, |
| 6581 | input_data->x, input_data->len)); |
| 6582 | |
| 6583 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6584 | TEST_ASSERT(num_ops_prior == 0); |
| 6585 | |
| 6586 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6587 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6588 | status = psa_sign_hash_complete(&operation, signature, signature_size, |
| 6589 | &signature_length); |
| 6590 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6591 | num_completes++; |
| 6592 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6593 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 6594 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6595 | /* We are asserting here that every complete makes progress |
| 6596 | * (completes some ops), which is true of the internal |
| 6597 | * implementation and probably any implementation, however this is |
| 6598 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6599 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6600 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6601 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 6602 | |
| 6603 | /* Ensure calling get_num_ops() twice still returns the same |
| 6604 | * number of ops as previously reported. */ |
| 6605 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6606 | |
| 6607 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6608 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6609 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6610 | |
| 6611 | TEST_ASSERT(status == PSA_SUCCESS); |
| 6612 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6613 | TEST_LE_U(min_completes, num_completes); |
| 6614 | TEST_LE_U(num_completes, max_completes); |
| 6615 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6616 | /* Verify that the signature is what is expected. */ |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 6617 | TEST_BUFFERS_EQUAL(output_data->x, output_data->len, |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6618 | signature, signature_length); |
| 6619 | |
| 6620 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6621 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6622 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6623 | TEST_ASSERT(num_ops == 0); |
| 6624 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6625 | exit: |
| 6626 | |
| 6627 | /* |
| 6628 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6629 | * thus reset them as required. |
| 6630 | */ |
| 6631 | psa_reset_key_attributes(&attributes); |
| 6632 | |
| 6633 | psa_destroy_key(key); |
| 6634 | mbedtls_free(signature); |
| 6635 | PSA_DONE(); |
| 6636 | } |
| 6637 | /* END_CASE */ |
| 6638 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6639 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6640 | void sign_hash_fail(int key_type_arg, data_t *key_data, |
| 6641 | int alg_arg, data_t *input_data, |
| 6642 | int signature_size_arg, int expected_status_arg) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6643 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6644 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6645 | psa_key_type_t key_type = key_type_arg; |
| 6646 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6647 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6648 | psa_status_t actual_status; |
| 6649 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 6650 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 6651 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6652 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6653 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 6654 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6655 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6656 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6657 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6658 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6659 | psa_set_key_algorithm(&attributes, alg); |
| 6660 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6661 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6662 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6663 | &key)); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6664 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6665 | actual_status = psa_sign_hash(key, alg, |
| 6666 | input_data->x, input_data->len, |
| 6667 | signature, signature_size, |
| 6668 | &signature_length); |
| 6669 | TEST_EQUAL(actual_status, expected_status); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6670 | /* The value of *signature_length is unspecified on error, but |
| 6671 | * whatever it is, it should be less than signature_size, so that |
| 6672 | * if the caller tries to read *signature_length bytes without |
| 6673 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6674 | TEST_LE_U(signature_length, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6675 | |
| 6676 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6677 | psa_reset_key_attributes(&attributes); |
| 6678 | psa_destroy_key(key); |
| 6679 | mbedtls_free(signature); |
| 6680 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6681 | } |
| 6682 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6683 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6684 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6685 | /** |
| 6686 | * sign_hash_fail_interruptible() test intentions: |
| 6687 | * |
| 6688 | * Note: This test can currently only handle ECDSA. |
| 6689 | * |
| 6690 | * 1. Test that various failure cases for interruptible sign hash fail with the |
| 6691 | * correct error codes, and at the correct point (at start or during |
| 6692 | * complete). |
| 6693 | * |
| 6694 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 6695 | * expected for different max_ops values. |
| 6696 | * |
| 6697 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 6698 | * and that each successful stage completes some ops (this is not mandated by |
| 6699 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 6700 | * |
| 6701 | * 4. Check that calling complete() when start() fails and complete() |
| 6702 | * after completion results in a BAD_STATE error. |
| 6703 | * |
| 6704 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 6705 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6706 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6707 | void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 6708 | int alg_arg, data_t *input_data, |
| 6709 | int signature_size_arg, |
| 6710 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6711 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6712 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6713 | { |
| 6714 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6715 | psa_key_type_t key_type = key_type_arg; |
| 6716 | psa_algorithm_t alg = alg_arg; |
| 6717 | size_t signature_size = signature_size_arg; |
| 6718 | psa_status_t actual_status; |
| 6719 | psa_status_t expected_start_status = expected_start_status_arg; |
| 6720 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 6721 | unsigned char *signature = NULL; |
| 6722 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6723 | uint32_t num_ops = 0; |
| 6724 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6725 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6726 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6727 | size_t min_completes = 0; |
| 6728 | size_t max_completes = 0; |
| 6729 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6730 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6731 | psa_sign_hash_interruptible_operation_t operation = |
| 6732 | psa_sign_hash_interruptible_operation_init(); |
| 6733 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 6734 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6735 | |
| 6736 | PSA_ASSERT(psa_crypto_init()); |
| 6737 | |
| 6738 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6739 | psa_set_key_algorithm(&attributes, alg); |
| 6740 | psa_set_key_type(&attributes, key_type); |
| 6741 | |
| 6742 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6743 | &key)); |
| 6744 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6745 | psa_interruptible_set_max_ops(max_ops); |
| 6746 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6747 | interruptible_signverify_get_minmax_completes(max_ops, |
| 6748 | expected_complete_status, |
| 6749 | &min_completes, |
| 6750 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6751 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6752 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6753 | TEST_ASSERT(num_ops_prior == 0); |
| 6754 | |
| 6755 | /* Start performing the signature. */ |
| 6756 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 6757 | input_data->x, input_data->len); |
| 6758 | |
| 6759 | TEST_EQUAL(actual_status, expected_start_status); |
| 6760 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6761 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 6762 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 6763 | * start failed. */ |
| 6764 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6765 | signature_size, |
| 6766 | &signature_length); |
| 6767 | |
| 6768 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 6769 | |
| 6770 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6771 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 6772 | input_data->x, input_data->len); |
| 6773 | |
| 6774 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 6775 | } |
| 6776 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6777 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6778 | TEST_ASSERT(num_ops_prior == 0); |
| 6779 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6780 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6781 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6782 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6783 | signature_size, |
| 6784 | &signature_length); |
| 6785 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6786 | num_completes++; |
| 6787 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6788 | if (actual_status == PSA_SUCCESS || |
| 6789 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6790 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6791 | /* We are asserting here that every complete makes progress |
| 6792 | * (completes some ops), which is true of the internal |
| 6793 | * implementation and probably any implementation, however this is |
| 6794 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6795 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6796 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6797 | num_ops_prior = num_ops; |
| 6798 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6799 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6800 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6801 | TEST_EQUAL(actual_status, expected_complete_status); |
| 6802 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 6803 | /* Check that another complete returns BAD_STATE. */ |
| 6804 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6805 | signature_size, |
| 6806 | &signature_length); |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6807 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 6808 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6809 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6810 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6811 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6812 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6813 | TEST_ASSERT(num_ops == 0); |
| 6814 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6815 | /* The value of *signature_length is unspecified on error, but |
| 6816 | * whatever it is, it should be less than signature_size, so that |
| 6817 | * if the caller tries to read *signature_length bytes without |
| 6818 | * checking the error code then they don't overflow a buffer. */ |
| 6819 | TEST_LE_U(signature_length, signature_size); |
| 6820 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6821 | TEST_LE_U(min_completes, num_completes); |
| 6822 | TEST_LE_U(num_completes, max_completes); |
| 6823 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6824 | exit: |
| 6825 | psa_reset_key_attributes(&attributes); |
| 6826 | psa_destroy_key(key); |
| 6827 | mbedtls_free(signature); |
| 6828 | PSA_DONE(); |
| 6829 | } |
| 6830 | /* END_CASE */ |
| 6831 | |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6832 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6833 | void sign_verify_hash(int key_type_arg, data_t *key_data, |
| 6834 | int alg_arg, data_t *input_data) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6835 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6836 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6837 | psa_key_type_t key_type = key_type_arg; |
| 6838 | psa_algorithm_t alg = alg_arg; |
| 6839 | size_t key_bits; |
| 6840 | unsigned char *signature = NULL; |
| 6841 | size_t signature_size; |
| 6842 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6843 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6844 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6845 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6846 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6847 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 6848 | psa_set_key_algorithm(&attributes, alg); |
| 6849 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6850 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6851 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6852 | &key)); |
| 6853 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6854 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6855 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6856 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6857 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6858 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6859 | key_bits, alg); |
| 6860 | TEST_ASSERT(signature_size != 0); |
| 6861 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 6862 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6863 | |
| 6864 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6865 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6866 | input_data->x, input_data->len, |
| 6867 | signature, signature_size, |
| 6868 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6869 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6870 | TEST_LE_U(signature_length, signature_size); |
| 6871 | TEST_ASSERT(signature_length > 0); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6872 | |
| 6873 | /* Use the library to verify that the signature is correct. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6874 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 6875 | input_data->x, input_data->len, |
| 6876 | signature, signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6877 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6878 | if (input_data->len != 0) { |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6879 | /* Flip a bit in the input and verify that the signature is now |
| 6880 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6881 | * because ECDSA may ignore the last few bits of the input. */ |
| 6882 | input_data->x[0] ^= 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6883 | TEST_EQUAL(psa_verify_hash(key, alg, |
| 6884 | input_data->x, input_data->len, |
| 6885 | signature, signature_length), |
| 6886 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6887 | } |
| 6888 | |
| 6889 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6890 | /* |
| 6891 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6892 | * thus reset them as required. |
| 6893 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6894 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6895 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6896 | psa_destroy_key(key); |
| 6897 | mbedtls_free(signature); |
| 6898 | PSA_DONE(); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6899 | } |
| 6900 | /* END_CASE */ |
| 6901 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6902 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6903 | /** |
| 6904 | * sign_verify_hash_interruptible() test intentions: |
| 6905 | * |
| 6906 | * Note: This test can currently only handle ECDSA. |
| 6907 | * |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 6908 | * 1. Test that we can sign an input hash with the given keypair and then |
| 6909 | * afterwards verify that signature. This is currently the only way to test |
| 6910 | * non deterministic ECDSA, but this test can also handle deterministic. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6911 | * |
| 6912 | * 2. Test that after corrupting the hash, the verification detects an invalid |
| 6913 | * signature. |
| 6914 | * |
| 6915 | * 3. Test the number of calls to psa_sign_hash_complete() required are as |
| 6916 | * expected for different max_ops values. |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6917 | * |
| 6918 | * 4. Test that the number of ops done prior to starting signing and after abort |
| 6919 | * is zero and that each successful signing stage completes some ops (this is |
| 6920 | * not mandated by the PSA specification, but is currently the case). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6921 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6922 | 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] | 6923 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6924 | int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6925 | { |
| 6926 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6927 | psa_key_type_t key_type = key_type_arg; |
| 6928 | psa_algorithm_t alg = alg_arg; |
| 6929 | size_t key_bits; |
| 6930 | unsigned char *signature = NULL; |
| 6931 | size_t signature_size; |
| 6932 | size_t signature_length = 0xdeadbeef; |
| 6933 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6934 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6935 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6936 | uint32_t num_ops = 0; |
| 6937 | uint32_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6938 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6939 | size_t min_completes = 0; |
| 6940 | size_t max_completes = 0; |
| 6941 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6942 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 6943 | psa_sign_hash_interruptible_operation_init(); |
| 6944 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 6945 | psa_verify_hash_interruptible_operation_init(); |
| 6946 | |
| 6947 | PSA_ASSERT(psa_crypto_init()); |
| 6948 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6949 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 6950 | PSA_KEY_USAGE_VERIFY_HASH); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6951 | psa_set_key_algorithm(&attributes, alg); |
| 6952 | psa_set_key_type(&attributes, key_type); |
| 6953 | |
| 6954 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6955 | &key)); |
| 6956 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6957 | key_bits = psa_get_key_bits(&attributes); |
| 6958 | |
| 6959 | /* Allocate a buffer which has the size advertised by the |
| 6960 | * library. */ |
| 6961 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6962 | key_bits, alg); |
| 6963 | TEST_ASSERT(signature_size != 0); |
| 6964 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 6965 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6966 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6967 | psa_interruptible_set_max_ops(max_ops); |
| 6968 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6969 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6970 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6971 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6972 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); |
| 6973 | TEST_ASSERT(num_ops_prior == 0); |
| 6974 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6975 | /* Start performing the signature. */ |
| 6976 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 6977 | input_data->x, input_data->len)); |
| 6978 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6979 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); |
| 6980 | TEST_ASSERT(num_ops_prior == 0); |
| 6981 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6982 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6983 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6984 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6985 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 6986 | signature_size, |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6987 | &signature_length); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6988 | |
| 6989 | num_completes++; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6990 | |
| 6991 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 6992 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 6993 | /* We are asserting here that every complete makes progress |
| 6994 | * (completes some ops), which is true of the internal |
| 6995 | * implementation and probably any implementation, however this is |
| 6996 | * not mandated by the PSA specification. */ |
| 6997 | TEST_ASSERT(num_ops > num_ops_prior); |
| 6998 | |
| 6999 | num_ops_prior = num_ops; |
| 7000 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7001 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7002 | |
| 7003 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7004 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7005 | TEST_LE_U(min_completes, num_completes); |
| 7006 | TEST_LE_U(num_completes, max_completes); |
| 7007 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7008 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7009 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7010 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7011 | TEST_ASSERT(num_ops == 0); |
| 7012 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7013 | /* Check that the signature length looks sensible. */ |
| 7014 | TEST_LE_U(signature_length, signature_size); |
| 7015 | TEST_ASSERT(signature_length > 0); |
| 7016 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7017 | num_completes = 0; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7018 | |
| 7019 | /* Start verification. */ |
| 7020 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7021 | input_data->x, input_data->len, |
| 7022 | signature, signature_length)); |
| 7023 | |
| 7024 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7025 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7026 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7027 | |
| 7028 | num_completes++; |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7029 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7030 | |
| 7031 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7032 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7033 | TEST_LE_U(min_completes, num_completes); |
| 7034 | TEST_LE_U(num_completes, max_completes); |
| 7035 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7036 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7037 | |
| 7038 | verify_operation = psa_verify_hash_interruptible_operation_init(); |
| 7039 | |
| 7040 | if (input_data->len != 0) { |
| 7041 | /* Flip a bit in the input and verify that the signature is now |
| 7042 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 7043 | * because ECDSA may ignore the last few bits of the input. */ |
| 7044 | input_data->x[0] ^= 1; |
| 7045 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7046 | /* Start verification. */ |
| 7047 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7048 | input_data->x, input_data->len, |
| 7049 | signature, signature_length)); |
| 7050 | |
| 7051 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7052 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7053 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7054 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7055 | |
| 7056 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7057 | } |
| 7058 | |
| 7059 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7060 | |
| 7061 | exit: |
| 7062 | /* |
| 7063 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7064 | * thus reset them as required. |
| 7065 | */ |
| 7066 | psa_reset_key_attributes(&attributes); |
| 7067 | |
| 7068 | psa_destroy_key(key); |
| 7069 | mbedtls_free(signature); |
| 7070 | PSA_DONE(); |
| 7071 | } |
| 7072 | /* END_CASE */ |
| 7073 | |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7074 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7075 | void verify_hash(int key_type_arg, data_t *key_data, |
| 7076 | int alg_arg, data_t *hash_data, |
| 7077 | data_t *signature_data) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7078 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7079 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7080 | psa_key_type_t key_type = key_type_arg; |
| 7081 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7082 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7083 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7084 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 7085 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7086 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7087 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7088 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7089 | psa_set_key_algorithm(&attributes, alg); |
| 7090 | psa_set_key_type(&attributes, key_type); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7091 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7092 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7093 | &key)); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7094 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7095 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 7096 | hash_data->x, hash_data->len, |
| 7097 | signature_data->x, signature_data->len)); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 7098 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7099 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7100 | psa_reset_key_attributes(&attributes); |
| 7101 | psa_destroy_key(key); |
| 7102 | PSA_DONE(); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7103 | } |
| 7104 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7105 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7106 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7107 | /** |
| 7108 | * verify_hash_interruptible() test intentions: |
| 7109 | * |
| 7110 | * Note: This test can currently only handle ECDSA. |
| 7111 | * |
| 7112 | * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 7113 | * only). Given this test only does verification it can accept public keys as |
| 7114 | * well as private keys / keypairs. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7115 | * |
| 7116 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7117 | * expected for different max_ops values. |
| 7118 | * |
| 7119 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7120 | * and that each successful stage completes some ops (this is not mandated by |
| 7121 | * the PSA specification, but is currently the case). |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7122 | * |
| 7123 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 7124 | * complete() calls does not alter the number of ops returned. |
| 7125 | * |
| 7126 | * 5. Test that after corrupting the hash, the verification detects an invalid |
| 7127 | * signature. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7128 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7129 | void verify_hash_interruptible(int key_type_arg, data_t *key_data, |
| 7130 | int alg_arg, data_t *hash_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7131 | data_t *signature_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7132 | { |
| 7133 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7134 | psa_key_type_t key_type = key_type_arg; |
| 7135 | psa_algorithm_t alg = alg_arg; |
| 7136 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7137 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7138 | uint32_t num_ops = 0; |
| 7139 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7140 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7141 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7142 | size_t min_completes = 0; |
| 7143 | size_t max_completes = 0; |
| 7144 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7145 | psa_verify_hash_interruptible_operation_t operation = |
| 7146 | psa_verify_hash_interruptible_operation_init(); |
| 7147 | |
| 7148 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 7149 | |
| 7150 | PSA_ASSERT(psa_crypto_init()); |
| 7151 | |
| 7152 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7153 | psa_set_key_algorithm(&attributes, alg); |
| 7154 | psa_set_key_type(&attributes, key_type); |
| 7155 | |
| 7156 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7157 | &key)); |
| 7158 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7159 | psa_interruptible_set_max_ops(max_ops); |
| 7160 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7161 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 7162 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7163 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7164 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7165 | |
| 7166 | TEST_ASSERT(num_ops_prior == 0); |
| 7167 | |
| 7168 | /* Start verification. */ |
| 7169 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7170 | hash_data->x, hash_data->len, |
| 7171 | signature_data->x, signature_data->len) |
| 7172 | ); |
| 7173 | |
| 7174 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7175 | |
| 7176 | TEST_ASSERT(num_ops_prior == 0); |
| 7177 | |
| 7178 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7179 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7180 | status = psa_verify_hash_complete(&operation); |
| 7181 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7182 | num_completes++; |
| 7183 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7184 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 7185 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7186 | /* We are asserting here that every complete makes progress |
| 7187 | * (completes some ops), which is true of the internal |
| 7188 | * implementation and probably any implementation, however this is |
| 7189 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7190 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7191 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7192 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 7193 | |
| 7194 | /* Ensure calling get_num_ops() twice still returns the same |
| 7195 | * number of ops as previously reported. */ |
| 7196 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7197 | |
| 7198 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7199 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7200 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7201 | |
| 7202 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7203 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7204 | TEST_LE_U(min_completes, num_completes); |
| 7205 | TEST_LE_U(num_completes, max_completes); |
| 7206 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7207 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7208 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7209 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7210 | TEST_ASSERT(num_ops == 0); |
| 7211 | |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7212 | if (hash_data->len != 0) { |
| 7213 | /* Flip a bit in the hash and verify that the signature is now detected |
| 7214 | * as invalid. Flip a bit at the beginning, not at the end, because |
| 7215 | * ECDSA may ignore the last few bits of the input. */ |
| 7216 | hash_data->x[0] ^= 1; |
| 7217 | |
| 7218 | /* Start verification. */ |
| 7219 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7220 | hash_data->x, hash_data->len, |
| 7221 | signature_data->x, signature_data->len)); |
| 7222 | |
| 7223 | /* Continue performing the signature until complete. */ |
| 7224 | do { |
| 7225 | status = psa_verify_hash_complete(&operation); |
| 7226 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7227 | |
| 7228 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7229 | } |
| 7230 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7231 | exit: |
| 7232 | psa_reset_key_attributes(&attributes); |
| 7233 | psa_destroy_key(key); |
| 7234 | PSA_DONE(); |
| 7235 | } |
| 7236 | /* END_CASE */ |
| 7237 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7238 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7239 | void verify_hash_fail(int key_type_arg, data_t *key_data, |
| 7240 | int alg_arg, data_t *hash_data, |
| 7241 | data_t *signature_data, |
| 7242 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7243 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7244 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7245 | psa_key_type_t key_type = key_type_arg; |
| 7246 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7247 | psa_status_t actual_status; |
| 7248 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7249 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7250 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7251 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7252 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7253 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7254 | psa_set_key_algorithm(&attributes, alg); |
| 7255 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7256 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7257 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7258 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7259 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7260 | actual_status = psa_verify_hash(key, alg, |
| 7261 | hash_data->x, hash_data->len, |
| 7262 | signature_data->x, signature_data->len); |
| 7263 | TEST_EQUAL(actual_status, expected_status); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7264 | |
| 7265 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7266 | psa_reset_key_attributes(&attributes); |
| 7267 | psa_destroy_key(key); |
| 7268 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7269 | } |
| 7270 | /* END_CASE */ |
| 7271 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7272 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7273 | /** |
| 7274 | * verify_hash_fail_interruptible() test intentions: |
| 7275 | * |
| 7276 | * Note: This test can currently only handle ECDSA. |
| 7277 | * |
| 7278 | * 1. Test that various failure cases for interruptible verify hash fail with |
| 7279 | * the correct error codes, and at the correct point (at start or during |
| 7280 | * complete). |
| 7281 | * |
| 7282 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7283 | * expected for different max_ops values. |
| 7284 | * |
| 7285 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7286 | * and that each successful stage completes some ops (this is not mandated by |
| 7287 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7288 | * |
| 7289 | * 4. Check that calling complete() when start() fails and complete() |
| 7290 | * after completion results in a BAD_STATE error. |
| 7291 | * |
| 7292 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 7293 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7294 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7295 | void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 7296 | int alg_arg, data_t *hash_data, |
| 7297 | data_t *signature_data, |
| 7298 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7299 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7300 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7301 | { |
| 7302 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7303 | psa_key_type_t key_type = key_type_arg; |
| 7304 | psa_algorithm_t alg = alg_arg; |
| 7305 | psa_status_t actual_status; |
| 7306 | psa_status_t expected_start_status = expected_start_status_arg; |
| 7307 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 7308 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7309 | uint32_t num_ops = 0; |
| 7310 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7311 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7312 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7313 | size_t min_completes = 0; |
| 7314 | size_t max_completes = 0; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7315 | psa_verify_hash_interruptible_operation_t operation = |
| 7316 | psa_verify_hash_interruptible_operation_init(); |
| 7317 | |
| 7318 | PSA_ASSERT(psa_crypto_init()); |
| 7319 | |
| 7320 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7321 | psa_set_key_algorithm(&attributes, alg); |
| 7322 | psa_set_key_type(&attributes, key_type); |
| 7323 | |
| 7324 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7325 | &key)); |
| 7326 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7327 | psa_interruptible_set_max_ops(max_ops); |
| 7328 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7329 | interruptible_signverify_get_minmax_completes(max_ops, |
| 7330 | expected_complete_status, |
| 7331 | &min_completes, |
| 7332 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7333 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7334 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7335 | TEST_ASSERT(num_ops_prior == 0); |
| 7336 | |
| 7337 | /* Start verification. */ |
| 7338 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7339 | hash_data->x, hash_data->len, |
| 7340 | signature_data->x, |
| 7341 | signature_data->len); |
| 7342 | |
| 7343 | TEST_EQUAL(actual_status, expected_start_status); |
| 7344 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7345 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 7346 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7347 | * start failed. */ |
| 7348 | actual_status = psa_verify_hash_complete(&operation); |
| 7349 | |
| 7350 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7351 | |
| 7352 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7353 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7354 | hash_data->x, hash_data->len, |
| 7355 | signature_data->x, |
| 7356 | signature_data->len); |
| 7357 | |
| 7358 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7359 | } |
| 7360 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7361 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7362 | TEST_ASSERT(num_ops_prior == 0); |
| 7363 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7364 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7365 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7366 | actual_status = psa_verify_hash_complete(&operation); |
| 7367 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7368 | num_completes++; |
| 7369 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7370 | if (actual_status == PSA_SUCCESS || |
| 7371 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7372 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7373 | /* We are asserting here that every complete makes progress |
| 7374 | * (completes some ops), which is true of the internal |
| 7375 | * implementation and probably any implementation, however this is |
| 7376 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7377 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7378 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7379 | num_ops_prior = num_ops; |
| 7380 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7381 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7382 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7383 | TEST_EQUAL(actual_status, expected_complete_status); |
| 7384 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 7385 | /* Check that another complete returns BAD_STATE. */ |
| 7386 | actual_status = psa_verify_hash_complete(&operation); |
| 7387 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7388 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7389 | TEST_LE_U(min_completes, num_completes); |
| 7390 | TEST_LE_U(num_completes, max_completes); |
| 7391 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7392 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7393 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7394 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7395 | TEST_ASSERT(num_ops == 0); |
| 7396 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7397 | exit: |
| 7398 | psa_reset_key_attributes(&attributes); |
| 7399 | psa_destroy_key(key); |
| 7400 | PSA_DONE(); |
| 7401 | } |
| 7402 | /* END_CASE */ |
| 7403 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7404 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7405 | /** |
| 7406 | * interruptible_signverify_hash_state_test() test intentions: |
| 7407 | * |
| 7408 | * Note: This test can currently only handle ECDSA. |
| 7409 | * |
| 7410 | * 1. Test that calling the various interruptible sign and verify hash functions |
| 7411 | * in incorrect orders returns BAD_STATE errors. |
| 7412 | */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7413 | void interruptible_signverify_hash_state_test(int key_type_arg, |
| 7414 | data_t *key_data, int alg_arg, data_t *input_data) |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7415 | { |
| 7416 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7417 | psa_key_type_t key_type = key_type_arg; |
| 7418 | psa_algorithm_t alg = alg_arg; |
| 7419 | size_t key_bits; |
| 7420 | unsigned char *signature = NULL; |
| 7421 | size_t signature_size; |
| 7422 | size_t signature_length = 0xdeadbeef; |
| 7423 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7424 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7425 | psa_sign_hash_interruptible_operation_init(); |
| 7426 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7427 | psa_verify_hash_interruptible_operation_init(); |
| 7428 | |
| 7429 | PSA_ASSERT(psa_crypto_init()); |
| 7430 | |
| 7431 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7432 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7433 | psa_set_key_algorithm(&attributes, alg); |
| 7434 | psa_set_key_type(&attributes, key_type); |
| 7435 | |
| 7436 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7437 | &key)); |
| 7438 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7439 | key_bits = psa_get_key_bits(&attributes); |
| 7440 | |
| 7441 | /* Allocate a buffer which has the size advertised by the |
| 7442 | * library. */ |
| 7443 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7444 | key_bits, alg); |
| 7445 | TEST_ASSERT(signature_size != 0); |
| 7446 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 7447 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7448 | |
| 7449 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7450 | |
| 7451 | /* --- Attempt completes prior to starts --- */ |
| 7452 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7453 | signature_size, |
| 7454 | &signature_length), |
| 7455 | PSA_ERROR_BAD_STATE); |
| 7456 | |
| 7457 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7458 | |
| 7459 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7460 | PSA_ERROR_BAD_STATE); |
| 7461 | |
| 7462 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7463 | |
| 7464 | /* --- Aborts in all other places. --- */ |
| 7465 | psa_sign_hash_abort(&sign_operation); |
| 7466 | |
| 7467 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7468 | input_data->x, input_data->len)); |
| 7469 | |
| 7470 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7471 | |
| 7472 | psa_interruptible_set_max_ops(1); |
| 7473 | |
| 7474 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7475 | input_data->x, input_data->len)); |
| 7476 | |
| 7477 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7478 | signature_size, |
| 7479 | &signature_length), |
| 7480 | PSA_OPERATION_INCOMPLETE); |
| 7481 | |
| 7482 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7483 | |
| 7484 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7485 | |
| 7486 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7487 | input_data->x, input_data->len)); |
| 7488 | |
| 7489 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7490 | signature_size, |
| 7491 | &signature_length)); |
| 7492 | |
| 7493 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7494 | |
| 7495 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7496 | |
| 7497 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7498 | input_data->x, input_data->len, |
| 7499 | signature, signature_length)); |
| 7500 | |
| 7501 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7502 | |
| 7503 | psa_interruptible_set_max_ops(1); |
| 7504 | |
| 7505 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7506 | input_data->x, input_data->len, |
| 7507 | signature, signature_length)); |
| 7508 | |
| 7509 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7510 | PSA_OPERATION_INCOMPLETE); |
| 7511 | |
| 7512 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7513 | |
| 7514 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7515 | |
| 7516 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7517 | input_data->x, input_data->len, |
| 7518 | signature, signature_length)); |
| 7519 | |
| 7520 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7521 | |
| 7522 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7523 | |
| 7524 | /* --- Attempt double starts. --- */ |
| 7525 | |
| 7526 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7527 | input_data->x, input_data->len)); |
| 7528 | |
| 7529 | TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg, |
| 7530 | input_data->x, input_data->len), |
| 7531 | PSA_ERROR_BAD_STATE); |
| 7532 | |
| 7533 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7534 | |
| 7535 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7536 | input_data->x, input_data->len, |
| 7537 | signature, signature_length)); |
| 7538 | |
| 7539 | TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg, |
| 7540 | input_data->x, input_data->len, |
| 7541 | signature, signature_length), |
| 7542 | PSA_ERROR_BAD_STATE); |
| 7543 | |
| 7544 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7545 | |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7546 | exit: |
| 7547 | /* |
| 7548 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7549 | * thus reset them as required. |
| 7550 | */ |
| 7551 | psa_reset_key_attributes(&attributes); |
| 7552 | |
| 7553 | psa_destroy_key(key); |
| 7554 | mbedtls_free(signature); |
| 7555 | PSA_DONE(); |
| 7556 | } |
| 7557 | /* END_CASE */ |
| 7558 | |
| 7559 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7560 | /** |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7561 | * interruptible_signverify_hash_edgecase_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7562 | * |
| 7563 | * Note: This test can currently only handle ECDSA. |
| 7564 | * |
| 7565 | * 1. Test various edge cases in the interruptible sign and verify hash |
| 7566 | * interfaces. |
| 7567 | */ |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7568 | void interruptible_signverify_hash_edgecase_tests(int key_type_arg, |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7569 | data_t *key_data, int alg_arg, data_t *input_data) |
| 7570 | { |
| 7571 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7572 | psa_key_type_t key_type = key_type_arg; |
| 7573 | psa_algorithm_t alg = alg_arg; |
| 7574 | size_t key_bits; |
| 7575 | unsigned char *signature = NULL; |
| 7576 | size_t signature_size; |
| 7577 | size_t signature_length = 0xdeadbeef; |
| 7578 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7579 | uint8_t *input_buffer = NULL; |
| 7580 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7581 | psa_sign_hash_interruptible_operation_init(); |
| 7582 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7583 | psa_verify_hash_interruptible_operation_init(); |
| 7584 | |
| 7585 | PSA_ASSERT(psa_crypto_init()); |
| 7586 | |
| 7587 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7588 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7589 | psa_set_key_algorithm(&attributes, alg); |
| 7590 | psa_set_key_type(&attributes, key_type); |
| 7591 | |
| 7592 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7593 | &key)); |
| 7594 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7595 | key_bits = psa_get_key_bits(&attributes); |
| 7596 | |
| 7597 | /* Allocate a buffer which has the size advertised by the |
| 7598 | * library. */ |
| 7599 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7600 | key_bits, alg); |
| 7601 | TEST_ASSERT(signature_size != 0); |
| 7602 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 7603 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7604 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7605 | /* --- Change function inputs mid run, to cause an error (sign only, |
| 7606 | * verify passes all inputs to start. --- */ |
| 7607 | |
| 7608 | psa_interruptible_set_max_ops(1); |
| 7609 | |
| 7610 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7611 | input_data->x, input_data->len)); |
| 7612 | |
| 7613 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7614 | signature_size, |
| 7615 | &signature_length), |
| 7616 | PSA_OPERATION_INCOMPLETE); |
| 7617 | |
| 7618 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7619 | 0, |
| 7620 | &signature_length), |
| 7621 | PSA_ERROR_BUFFER_TOO_SMALL); |
| 7622 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7623 | /* And test that this invalidates the operation. */ |
| 7624 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7625 | 0, |
| 7626 | &signature_length), |
| 7627 | PSA_ERROR_BAD_STATE); |
| 7628 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7629 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7630 | |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7631 | /* Trash the hash buffer in between start and complete, to ensure |
| 7632 | * no reliance on external buffers. */ |
| 7633 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7634 | |
| 7635 | input_buffer = mbedtls_calloc(1, input_data->len); |
| 7636 | TEST_ASSERT(input_buffer != NULL); |
| 7637 | |
| 7638 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7639 | |
| 7640 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7641 | input_buffer, input_data->len)); |
| 7642 | |
| 7643 | memset(input_buffer, '!', input_data->len); |
| 7644 | mbedtls_free(input_buffer); |
| 7645 | input_buffer = NULL; |
| 7646 | |
| 7647 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7648 | signature_size, |
| 7649 | &signature_length)); |
| 7650 | |
| 7651 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7652 | |
| 7653 | input_buffer = mbedtls_calloc(1, input_data->len); |
| 7654 | TEST_ASSERT(input_buffer != NULL); |
| 7655 | |
| 7656 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7657 | |
| 7658 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7659 | input_buffer, input_data->len, |
| 7660 | signature, signature_length)); |
| 7661 | |
| 7662 | memset(input_buffer, '!', input_data->len); |
| 7663 | mbedtls_free(input_buffer); |
| 7664 | input_buffer = NULL; |
| 7665 | |
| 7666 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7667 | |
| 7668 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7669 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7670 | exit: |
| 7671 | /* |
| 7672 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7673 | * thus reset them as required. |
| 7674 | */ |
| 7675 | psa_reset_key_attributes(&attributes); |
| 7676 | |
| 7677 | psa_destroy_key(key); |
| 7678 | mbedtls_free(signature); |
| 7679 | PSA_DONE(); |
| 7680 | } |
| 7681 | /* END_CASE */ |
| 7682 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7683 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7684 | /** |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 7685 | * interruptible_signverify_hash_ops_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7686 | * |
| 7687 | * Note: This test can currently only handle ECDSA. |
| 7688 | * |
| 7689 | * 1. Test that setting max ops is reflected in both interruptible sign and |
| 7690 | * verify hash |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7691 | * 2. Test that changing the value of max_ops to unlimited during an operation |
| 7692 | * causes that operation to complete in the next call. |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7693 | * |
| 7694 | * 3. Test that calling get_num_ops() between complete calls gives the same |
| 7695 | * 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] | 7696 | */ |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 7697 | void interruptible_signverify_hash_ops_tests(int key_type_arg, |
| 7698 | data_t *key_data, int alg_arg, |
| 7699 | data_t *input_data) |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7700 | { |
| 7701 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7702 | psa_key_type_t key_type = key_type_arg; |
| 7703 | psa_algorithm_t alg = alg_arg; |
| 7704 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7705 | size_t key_bits; |
| 7706 | unsigned char *signature = NULL; |
| 7707 | size_t signature_size; |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7708 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7709 | uint32_t num_ops = 0; |
| 7710 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 7711 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7712 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7713 | psa_sign_hash_interruptible_operation_init(); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7714 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7715 | psa_verify_hash_interruptible_operation_init(); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7716 | |
| 7717 | PSA_ASSERT(psa_crypto_init()); |
| 7718 | |
| 7719 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7720 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7721 | psa_set_key_algorithm(&attributes, alg); |
| 7722 | psa_set_key_type(&attributes, key_type); |
| 7723 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7724 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key)); |
| 7725 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7726 | key_bits = psa_get_key_bits(&attributes); |
| 7727 | |
| 7728 | /* Allocate a buffer which has the size advertised by the |
| 7729 | * library. */ |
| 7730 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7731 | |
| 7732 | TEST_ASSERT(signature_size != 0); |
| 7733 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 7734 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7735 | |
| 7736 | /* Check that default max ops gets set if we don't set it. */ |
| 7737 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7738 | input_data->x, input_data->len)); |
| 7739 | |
| 7740 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 7741 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7742 | |
| 7743 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7744 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7745 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7746 | input_data->x, input_data->len, |
| 7747 | signature, signature_size)); |
| 7748 | |
| 7749 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 7750 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7751 | |
| 7752 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7753 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7754 | /* Check that max ops gets set properly. */ |
| 7755 | |
| 7756 | psa_interruptible_set_max_ops(0xbeef); |
| 7757 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7758 | TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7759 | |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7760 | /* --- Ensure changing the max ops mid operation works (operation should |
| 7761 | * complete successfully after setting max ops to unlimited --- */ |
| 7762 | psa_interruptible_set_max_ops(1); |
| 7763 | |
| 7764 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7765 | input_data->x, input_data->len)); |
| 7766 | |
| 7767 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7768 | signature_size, |
| 7769 | &signature_length), |
| 7770 | PSA_OPERATION_INCOMPLETE); |
| 7771 | |
| 7772 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7773 | |
| 7774 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7775 | signature_size, |
| 7776 | &signature_length)); |
| 7777 | |
| 7778 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7779 | |
| 7780 | psa_interruptible_set_max_ops(1); |
| 7781 | |
| 7782 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7783 | input_data->x, input_data->len, |
| 7784 | signature, signature_length)); |
| 7785 | |
| 7786 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7787 | PSA_OPERATION_INCOMPLETE); |
| 7788 | |
| 7789 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7790 | |
| 7791 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7792 | |
| 7793 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7794 | |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7795 | /* --- Test that not calling get_num_ops inbetween complete calls does not |
| 7796 | * result in lost ops. ---*/ |
| 7797 | |
| 7798 | psa_interruptible_set_max_ops(1); |
| 7799 | |
| 7800 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7801 | input_data->x, input_data->len)); |
| 7802 | |
| 7803 | /* Continue performing the signature until complete. */ |
| 7804 | do { |
| 7805 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7806 | signature_size, |
| 7807 | &signature_length); |
| 7808 | |
| 7809 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7810 | |
| 7811 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7812 | |
| 7813 | PSA_ASSERT(status); |
| 7814 | |
| 7815 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7816 | |
| 7817 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7818 | input_data->x, input_data->len)); |
| 7819 | |
| 7820 | /* Continue performing the signature until complete. */ |
| 7821 | do { |
| 7822 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7823 | signature_size, |
| 7824 | &signature_length); |
| 7825 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7826 | |
| 7827 | PSA_ASSERT(status); |
| 7828 | |
| 7829 | TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation)); |
| 7830 | |
| 7831 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7832 | |
| 7833 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7834 | input_data->x, input_data->len, |
| 7835 | signature, signature_length)); |
| 7836 | |
| 7837 | /* Continue performing the verification until complete. */ |
| 7838 | do { |
| 7839 | status = psa_verify_hash_complete(&verify_operation); |
| 7840 | |
| 7841 | num_ops = psa_verify_hash_get_num_ops(&verify_operation); |
| 7842 | |
| 7843 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7844 | |
| 7845 | PSA_ASSERT(status); |
| 7846 | |
| 7847 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7848 | |
| 7849 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7850 | input_data->x, input_data->len, |
| 7851 | signature, signature_length)); |
| 7852 | |
| 7853 | /* Continue performing the verification until complete. */ |
| 7854 | do { |
| 7855 | status = psa_verify_hash_complete(&verify_operation); |
| 7856 | |
| 7857 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7858 | |
| 7859 | PSA_ASSERT(status); |
| 7860 | |
| 7861 | TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation)); |
| 7862 | |
| 7863 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7864 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7865 | exit: |
| 7866 | /* |
| 7867 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7868 | * thus reset them as required. |
| 7869 | */ |
| 7870 | psa_reset_key_attributes(&attributes); |
| 7871 | |
| 7872 | psa_destroy_key(key); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7873 | mbedtls_free(signature); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7874 | PSA_DONE(); |
| 7875 | } |
| 7876 | /* END_CASE */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7877 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7878 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7879 | void sign_message_deterministic(int key_type_arg, |
| 7880 | data_t *key_data, |
| 7881 | int alg_arg, |
| 7882 | data_t *input_data, |
| 7883 | data_t *output_data) |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7884 | { |
| 7885 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7886 | psa_key_type_t key_type = key_type_arg; |
| 7887 | psa_algorithm_t alg = alg_arg; |
| 7888 | size_t key_bits; |
| 7889 | unsigned char *signature = NULL; |
| 7890 | size_t signature_size; |
| 7891 | size_t signature_length = 0xdeadbeef; |
| 7892 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7893 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7894 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7895 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7896 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 7897 | psa_set_key_algorithm(&attributes, alg); |
| 7898 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7899 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7900 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7901 | &key)); |
| 7902 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7903 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7904 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7905 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7906 | TEST_ASSERT(signature_size != 0); |
| 7907 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 7908 | TEST_CALLOC(signature, signature_size); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7909 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7910 | PSA_ASSERT(psa_sign_message(key, alg, |
| 7911 | input_data->x, input_data->len, |
| 7912 | signature, signature_size, |
| 7913 | &signature_length)); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7914 | |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 7915 | TEST_BUFFERS_EQUAL(output_data->x, output_data->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7916 | signature, signature_length); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7917 | |
| 7918 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7919 | psa_reset_key_attributes(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7920 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7921 | psa_destroy_key(key); |
| 7922 | mbedtls_free(signature); |
| 7923 | PSA_DONE(); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7924 | |
| 7925 | } |
| 7926 | /* END_CASE */ |
| 7927 | |
| 7928 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7929 | void sign_message_fail(int key_type_arg, |
| 7930 | data_t *key_data, |
| 7931 | int alg_arg, |
| 7932 | data_t *input_data, |
| 7933 | int signature_size_arg, |
| 7934 | int expected_status_arg) |
| 7935 | { |
| 7936 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7937 | psa_key_type_t key_type = key_type_arg; |
| 7938 | psa_algorithm_t alg = alg_arg; |
| 7939 | size_t signature_size = signature_size_arg; |
| 7940 | psa_status_t actual_status; |
| 7941 | psa_status_t expected_status = expected_status_arg; |
| 7942 | unsigned char *signature = NULL; |
| 7943 | size_t signature_length = 0xdeadbeef; |
| 7944 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7945 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 7946 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7947 | |
| 7948 | PSA_ASSERT(psa_crypto_init()); |
| 7949 | |
| 7950 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 7951 | psa_set_key_algorithm(&attributes, alg); |
| 7952 | psa_set_key_type(&attributes, key_type); |
| 7953 | |
| 7954 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7955 | &key)); |
| 7956 | |
| 7957 | actual_status = psa_sign_message(key, alg, |
| 7958 | input_data->x, input_data->len, |
| 7959 | signature, signature_size, |
| 7960 | &signature_length); |
| 7961 | TEST_EQUAL(actual_status, expected_status); |
| 7962 | /* The value of *signature_length is unspecified on error, but |
| 7963 | * whatever it is, it should be less than signature_size, so that |
| 7964 | * if the caller tries to read *signature_length bytes without |
| 7965 | * checking the error code then they don't overflow a buffer. */ |
| 7966 | TEST_LE_U(signature_length, signature_size); |
| 7967 | |
| 7968 | exit: |
| 7969 | psa_reset_key_attributes(&attributes); |
| 7970 | psa_destroy_key(key); |
| 7971 | mbedtls_free(signature); |
| 7972 | PSA_DONE(); |
| 7973 | } |
| 7974 | /* END_CASE */ |
| 7975 | |
| 7976 | /* BEGIN_CASE */ |
| 7977 | void sign_verify_message(int key_type_arg, |
| 7978 | data_t *key_data, |
| 7979 | int alg_arg, |
| 7980 | data_t *input_data) |
| 7981 | { |
| 7982 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7983 | psa_key_type_t key_type = key_type_arg; |
| 7984 | psa_algorithm_t alg = alg_arg; |
| 7985 | size_t key_bits; |
| 7986 | unsigned char *signature = NULL; |
| 7987 | size_t signature_size; |
| 7988 | size_t signature_length = 0xdeadbeef; |
| 7989 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7990 | |
| 7991 | PSA_ASSERT(psa_crypto_init()); |
| 7992 | |
| 7993 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 7994 | PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 7995 | psa_set_key_algorithm(&attributes, alg); |
| 7996 | psa_set_key_type(&attributes, key_type); |
| 7997 | |
| 7998 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7999 | &key)); |
| 8000 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8001 | key_bits = psa_get_key_bits(&attributes); |
| 8002 | |
| 8003 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8004 | TEST_ASSERT(signature_size != 0); |
| 8005 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 8006 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8007 | |
| 8008 | PSA_ASSERT(psa_sign_message(key, alg, |
| 8009 | input_data->x, input_data->len, |
| 8010 | signature, signature_size, |
| 8011 | &signature_length)); |
| 8012 | TEST_LE_U(signature_length, signature_size); |
| 8013 | TEST_ASSERT(signature_length > 0); |
| 8014 | |
| 8015 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8016 | input_data->x, input_data->len, |
| 8017 | signature, signature_length)); |
| 8018 | |
| 8019 | if (input_data->len != 0) { |
| 8020 | /* Flip a bit in the input and verify that the signature is now |
| 8021 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 8022 | * because ECDSA may ignore the last few bits of the input. */ |
| 8023 | input_data->x[0] ^= 1; |
| 8024 | TEST_EQUAL(psa_verify_message(key, alg, |
| 8025 | input_data->x, input_data->len, |
| 8026 | signature, signature_length), |
| 8027 | PSA_ERROR_INVALID_SIGNATURE); |
| 8028 | } |
| 8029 | |
| 8030 | exit: |
| 8031 | psa_reset_key_attributes(&attributes); |
| 8032 | |
| 8033 | psa_destroy_key(key); |
| 8034 | mbedtls_free(signature); |
| 8035 | PSA_DONE(); |
| 8036 | } |
| 8037 | /* END_CASE */ |
| 8038 | |
| 8039 | /* BEGIN_CASE */ |
| 8040 | void verify_message(int key_type_arg, |
| 8041 | data_t *key_data, |
| 8042 | int alg_arg, |
| 8043 | data_t *input_data, |
| 8044 | data_t *signature_data) |
| 8045 | { |
| 8046 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8047 | psa_key_type_t key_type = key_type_arg; |
| 8048 | psa_algorithm_t alg = alg_arg; |
| 8049 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8050 | |
| 8051 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 8052 | |
| 8053 | PSA_ASSERT(psa_crypto_init()); |
| 8054 | |
| 8055 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8056 | psa_set_key_algorithm(&attributes, alg); |
| 8057 | psa_set_key_type(&attributes, key_type); |
| 8058 | |
| 8059 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8060 | &key)); |
| 8061 | |
| 8062 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8063 | input_data->x, input_data->len, |
| 8064 | signature_data->x, signature_data->len)); |
| 8065 | |
| 8066 | exit: |
| 8067 | psa_reset_key_attributes(&attributes); |
| 8068 | psa_destroy_key(key); |
| 8069 | PSA_DONE(); |
| 8070 | } |
| 8071 | /* END_CASE */ |
| 8072 | |
| 8073 | /* BEGIN_CASE */ |
| 8074 | void verify_message_fail(int key_type_arg, |
| 8075 | data_t *key_data, |
| 8076 | int alg_arg, |
| 8077 | data_t *hash_data, |
| 8078 | data_t *signature_data, |
| 8079 | int expected_status_arg) |
| 8080 | { |
| 8081 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8082 | psa_key_type_t key_type = key_type_arg; |
| 8083 | psa_algorithm_t alg = alg_arg; |
| 8084 | psa_status_t actual_status; |
| 8085 | psa_status_t expected_status = expected_status_arg; |
| 8086 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8087 | |
| 8088 | PSA_ASSERT(psa_crypto_init()); |
| 8089 | |
| 8090 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8091 | psa_set_key_algorithm(&attributes, alg); |
| 8092 | psa_set_key_type(&attributes, key_type); |
| 8093 | |
| 8094 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8095 | &key)); |
| 8096 | |
| 8097 | actual_status = psa_verify_message(key, alg, |
| 8098 | hash_data->x, hash_data->len, |
| 8099 | signature_data->x, |
| 8100 | signature_data->len); |
| 8101 | TEST_EQUAL(actual_status, expected_status); |
| 8102 | |
| 8103 | exit: |
| 8104 | psa_reset_key_attributes(&attributes); |
| 8105 | psa_destroy_key(key); |
| 8106 | PSA_DONE(); |
| 8107 | } |
| 8108 | /* END_CASE */ |
| 8109 | |
| 8110 | /* BEGIN_CASE */ |
| 8111 | void asymmetric_encrypt(int key_type_arg, |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8112 | data_t *key_data, |
| 8113 | int alg_arg, |
| 8114 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8115 | data_t *label, |
| 8116 | int expected_output_length_arg, |
| 8117 | int expected_status_arg) |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8118 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8119 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8120 | psa_key_type_t key_type = key_type_arg; |
| 8121 | psa_algorithm_t alg = alg_arg; |
| 8122 | size_t expected_output_length = expected_output_length_arg; |
| 8123 | size_t key_bits; |
| 8124 | unsigned char *output = NULL; |
| 8125 | size_t output_size; |
| 8126 | size_t output_length = ~0; |
| 8127 | psa_status_t actual_status; |
| 8128 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8129 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8130 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8131 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 8132 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8133 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8134 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 8135 | psa_set_key_algorithm(&attributes, alg); |
| 8136 | psa_set_key_type(&attributes, key_type); |
| 8137 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8138 | &key)); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8139 | |
| 8140 | /* Determine the maximum output length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8141 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8142 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8143 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8144 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8145 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 8146 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8147 | |
| 8148 | /* Encrypt the input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8149 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8150 | input_data->x, input_data->len, |
| 8151 | label->x, label->len, |
| 8152 | output, output_size, |
| 8153 | &output_length); |
| 8154 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8155 | if (actual_status == PSA_SUCCESS) { |
| 8156 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8157 | } else { |
| 8158 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8159 | } |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8160 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8161 | /* If the label is empty, the test framework puts a non-null pointer |
| 8162 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8163 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8164 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8165 | if (output_size != 0) { |
| 8166 | memset(output, 0, output_size); |
| 8167 | } |
| 8168 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8169 | input_data->x, input_data->len, |
| 8170 | NULL, label->len, |
| 8171 | output, output_size, |
| 8172 | &output_length); |
| 8173 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8174 | if (actual_status == PSA_SUCCESS) { |
| 8175 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8176 | } else { |
| 8177 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8178 | } |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8179 | } |
| 8180 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8181 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8182 | /* |
| 8183 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8184 | * thus reset them as required. |
| 8185 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8186 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8187 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8188 | psa_destroy_key(key); |
| 8189 | mbedtls_free(output); |
| 8190 | PSA_DONE(); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8191 | } |
| 8192 | /* END_CASE */ |
| 8193 | |
| 8194 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8195 | void asymmetric_encrypt_decrypt(int key_type_arg, |
| 8196 | data_t *key_data, |
| 8197 | int alg_arg, |
| 8198 | data_t *input_data, |
| 8199 | data_t *label) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8200 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8201 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8202 | psa_key_type_t key_type = key_type_arg; |
| 8203 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8204 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8205 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8206 | size_t output_size; |
| 8207 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8208 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8209 | size_t output2_size; |
| 8210 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8211 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8212 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8213 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8215 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 8216 | psa_set_key_algorithm(&attributes, alg); |
| 8217 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8218 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8219 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8220 | &key)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8221 | |
| 8222 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8223 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8224 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8225 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8226 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8227 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 8228 | TEST_CALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8229 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8230 | output2_size = input_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8231 | TEST_LE_U(output2_size, |
| 8232 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)); |
| 8233 | TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 8234 | TEST_CALLOC(output2, output2_size); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8235 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 8236 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 8237 | * the original plaintext because of the non-optional random |
| 8238 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8239 | PSA_ASSERT(psa_asymmetric_encrypt(key, alg, |
| 8240 | input_data->x, input_data->len, |
| 8241 | label->x, label->len, |
| 8242 | output, output_size, |
| 8243 | &output_length)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8244 | /* We don't know what ciphertext length to expect, but check that |
| 8245 | * it looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8246 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8247 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8248 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8249 | output, output_length, |
| 8250 | label->x, label->len, |
| 8251 | output2, output2_size, |
| 8252 | &output2_length)); |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 8253 | TEST_BUFFERS_EQUAL(input_data->x, input_data->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8254 | output2, output2_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8255 | |
| 8256 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8257 | /* |
| 8258 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8259 | * thus reset them as required. |
| 8260 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8261 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8263 | psa_destroy_key(key); |
| 8264 | mbedtls_free(output); |
| 8265 | mbedtls_free(output2); |
| 8266 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8267 | } |
| 8268 | /* END_CASE */ |
| 8269 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8270 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8271 | void asymmetric_decrypt(int key_type_arg, |
| 8272 | data_t *key_data, |
| 8273 | int alg_arg, |
| 8274 | data_t *input_data, |
| 8275 | data_t *label, |
| 8276 | data_t *expected_data) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8277 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8278 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8279 | psa_key_type_t key_type = key_type_arg; |
| 8280 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8281 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8282 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 8283 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8284 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8285 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8286 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8287 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8288 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8289 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8290 | psa_set_key_algorithm(&attributes, alg); |
| 8291 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8292 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8293 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8294 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8295 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8296 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8297 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8298 | |
| 8299 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8300 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8301 | TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 8302 | TEST_CALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8303 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8304 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8305 | input_data->x, input_data->len, |
| 8306 | label->x, label->len, |
| 8307 | output, |
| 8308 | output_size, |
| 8309 | &output_length)); |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 8310 | TEST_BUFFERS_EQUAL(expected_data->x, expected_data->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8311 | output, output_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8312 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8313 | /* If the label is empty, the test framework puts a non-null pointer |
| 8314 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8315 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8316 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8317 | if (output_size != 0) { |
| 8318 | memset(output, 0, output_size); |
| 8319 | } |
| 8320 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8321 | input_data->x, input_data->len, |
| 8322 | NULL, label->len, |
| 8323 | output, |
| 8324 | output_size, |
| 8325 | &output_length)); |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 8326 | TEST_BUFFERS_EQUAL(expected_data->x, expected_data->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8327 | output, output_length); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8328 | } |
| 8329 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8330 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8331 | psa_reset_key_attributes(&attributes); |
| 8332 | psa_destroy_key(key); |
| 8333 | mbedtls_free(output); |
| 8334 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8335 | } |
| 8336 | /* END_CASE */ |
| 8337 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8338 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8339 | void asymmetric_decrypt_fail(int key_type_arg, |
| 8340 | data_t *key_data, |
| 8341 | int alg_arg, |
| 8342 | data_t *input_data, |
| 8343 | data_t *label, |
| 8344 | int output_size_arg, |
| 8345 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8346 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8347 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8348 | psa_key_type_t key_type = key_type_arg; |
| 8349 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8350 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 8351 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8352 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8353 | psa_status_t actual_status; |
| 8354 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8355 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8356 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 8357 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8358 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8359 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8360 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8361 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8362 | psa_set_key_algorithm(&attributes, alg); |
| 8363 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8364 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8365 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8366 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8367 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8368 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8369 | input_data->x, input_data->len, |
| 8370 | label->x, label->len, |
| 8371 | output, output_size, |
| 8372 | &output_length); |
| 8373 | TEST_EQUAL(actual_status, expected_status); |
| 8374 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8375 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8376 | /* If the label is empty, the test framework puts a non-null pointer |
| 8377 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8378 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8379 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8380 | if (output_size != 0) { |
| 8381 | memset(output, 0, output_size); |
| 8382 | } |
| 8383 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8384 | input_data->x, input_data->len, |
| 8385 | NULL, label->len, |
| 8386 | output, output_size, |
| 8387 | &output_length); |
| 8388 | TEST_EQUAL(actual_status, expected_status); |
| 8389 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8390 | } |
| 8391 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8392 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8393 | psa_reset_key_attributes(&attributes); |
| 8394 | psa_destroy_key(key); |
| 8395 | mbedtls_free(output); |
| 8396 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8397 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8398 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8399 | |
| 8400 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8401 | void key_derivation_init() |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8402 | { |
| 8403 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 8404 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 8405 | * 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] | 8406 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8407 | size_t capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8408 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init(); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 8409 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8410 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8411 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8412 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8413 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8414 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8415 | TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity), |
| 8416 | PSA_ERROR_BAD_STATE); |
| 8417 | TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity), |
| 8418 | PSA_ERROR_BAD_STATE); |
| 8419 | TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity), |
| 8420 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8421 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8422 | /* A default operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8423 | PSA_ASSERT(psa_key_derivation_abort(&func)); |
| 8424 | PSA_ASSERT(psa_key_derivation_abort(&init)); |
| 8425 | PSA_ASSERT(psa_key_derivation_abort(&zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8426 | } |
| 8427 | /* END_CASE */ |
| 8428 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 8429 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8430 | void derive_setup(int alg_arg, int expected_status_arg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8431 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8432 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8433 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8434 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8435 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8436 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8437 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8438 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8439 | expected_status); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8440 | |
| 8441 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8442 | psa_key_derivation_abort(&operation); |
| 8443 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8444 | } |
| 8445 | /* END_CASE */ |
| 8446 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8447 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8448 | void derive_set_capacity(int alg_arg, int capacity_arg, |
| 8449 | int expected_status_arg) |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8450 | { |
| 8451 | psa_algorithm_t alg = alg_arg; |
| 8452 | size_t capacity = capacity_arg; |
| 8453 | psa_status_t expected_status = expected_status_arg; |
| 8454 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8455 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8456 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8457 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8458 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8459 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8460 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 8461 | expected_status); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8462 | |
| 8463 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8464 | psa_key_derivation_abort(&operation); |
| 8465 | PSA_DONE(); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8466 | } |
| 8467 | /* END_CASE */ |
| 8468 | |
| 8469 | /* BEGIN_CASE */ |
Kusumit Ghoderao | d60dfc0 | 2023-05-01 17:39:27 +0530 | [diff] [blame] | 8470 | void parse_binary_string_test(data_t *input, int output) |
| 8471 | { |
| 8472 | uint64_t value; |
| 8473 | value = parse_binary_string(input); |
| 8474 | TEST_EQUAL(value, output); |
| 8475 | } |
| 8476 | /* END_CASE */ |
| 8477 | |
| 8478 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8479 | void derive_input(int alg_arg, |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8480 | int step_arg1, int key_type_arg1, data_t *input1, |
| 8481 | int expected_status_arg1, |
| 8482 | int step_arg2, int key_type_arg2, data_t *input2, |
| 8483 | int expected_status_arg2, |
| 8484 | int step_arg3, int key_type_arg3, data_t *input3, |
| 8485 | int expected_status_arg3, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8486 | int output_key_type_arg, int expected_output_status_arg) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8487 | { |
| 8488 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8489 | psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 }; |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8490 | 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] | 8491 | psa_status_t expected_statuses[] = { expected_status_arg1, |
| 8492 | expected_status_arg2, |
| 8493 | expected_status_arg3 }; |
| 8494 | data_t *inputs[] = { input1, input2, input3 }; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8495 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8496 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8497 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8498 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8499 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8500 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8501 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8502 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8503 | psa_status_t expected_output_status = expected_output_status_arg; |
| 8504 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8505 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8506 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8507 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8508 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8509 | psa_set_key_algorithm(&attributes, alg); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8510 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8511 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8512 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8513 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8514 | mbedtls_test_set_step(i); |
| 8515 | if (steps[i] == 0) { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 8516 | /* Skip this step */ |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8517 | } else if (((psa_key_type_t) key_types[i]) != PSA_KEY_TYPE_NONE && |
| 8518 | key_types[i] != INPUT_INTEGER) { |
| 8519 | psa_set_key_type(&attributes, ((psa_key_type_t) key_types[i])); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8520 | PSA_ASSERT(psa_import_key(&attributes, |
| 8521 | inputs[i]->x, inputs[i]->len, |
| 8522 | &keys[i])); |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8523 | 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] | 8524 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8525 | // When taking a private key as secret input, use key agreement |
| 8526 | // to add the shared secret to the derivation |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8527 | TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self( |
| 8528 | &operation, keys[i]), |
| 8529 | expected_statuses[i]); |
| 8530 | } else { |
| 8531 | TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i], |
| 8532 | keys[i]), |
| 8533 | expected_statuses[i]); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8534 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8535 | } else { |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8536 | if (key_types[i] == INPUT_INTEGER) { |
| 8537 | TEST_EQUAL(psa_key_derivation_input_integer( |
| 8538 | &operation, steps[i], |
| 8539 | parse_binary_string(inputs[i])), |
| 8540 | expected_statuses[i]); |
| 8541 | } else { |
Kusumit Ghoderao | 02326d5 | 2023-04-06 17:47:59 +0530 | [diff] [blame] | 8542 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8543 | &operation, steps[i], |
| 8544 | inputs[i]->x, inputs[i]->len), |
| 8545 | expected_statuses[i]); |
Kusumit Ghoderao | 02326d5 | 2023-04-06 17:47:59 +0530 | [diff] [blame] | 8546 | } |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8547 | } |
| 8548 | } |
| 8549 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8550 | if (output_key_type != PSA_KEY_TYPE_NONE) { |
| 8551 | psa_reset_key_attributes(&attributes); |
| 8552 | psa_set_key_type(&attributes, output_key_type); |
| 8553 | psa_set_key_bits(&attributes, 8); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8554 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8555 | psa_key_derivation_output_key(&attributes, &operation, |
| 8556 | &output_key); |
| 8557 | } else { |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8558 | uint8_t buffer[1]; |
| 8559 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8560 | psa_key_derivation_output_bytes(&operation, |
| 8561 | buffer, sizeof(buffer)); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8562 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8563 | TEST_EQUAL(actual_output_status, expected_output_status); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8564 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8565 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8566 | psa_key_derivation_abort(&operation); |
| 8567 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8568 | psa_destroy_key(keys[i]); |
| 8569 | } |
| 8570 | psa_destroy_key(output_key); |
| 8571 | PSA_DONE(); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8572 | } |
| 8573 | /* END_CASE */ |
| 8574 | |
Kusumit Ghoderao | 42b02b9 | 2023-06-06 16:48:46 +0530 | [diff] [blame] | 8575 | /* BEGIN_CASE*/ |
| 8576 | void derive_input_invalid_cost(int alg_arg, int64_t cost) |
| 8577 | { |
| 8578 | psa_algorithm_t alg = alg_arg; |
| 8579 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8580 | |
| 8581 | PSA_ASSERT(psa_crypto_init()); |
| 8582 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8583 | |
| 8584 | TEST_EQUAL(psa_key_derivation_input_integer(&operation, |
| 8585 | PSA_KEY_DERIVATION_INPUT_COST, |
| 8586 | cost), |
| 8587 | PSA_ERROR_NOT_SUPPORTED); |
| 8588 | |
| 8589 | exit: |
| 8590 | psa_key_derivation_abort(&operation); |
| 8591 | PSA_DONE(); |
| 8592 | } |
| 8593 | /* END_CASE*/ |
| 8594 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8595 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8596 | void derive_over_capacity(int alg_arg) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8597 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8598 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8599 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 8600 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8601 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8602 | unsigned char input1[] = "Input 1"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8603 | size_t input1_length = sizeof(input1); |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8604 | unsigned char input2[] = "Input 2"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8605 | size_t input2_length = sizeof(input2); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8606 | uint8_t buffer[42]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8607 | size_t capacity = sizeof(buffer); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 8608 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 8609 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8610 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8611 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8612 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8613 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8614 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8615 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8616 | psa_set_key_algorithm(&attributes, alg); |
| 8617 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8618 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8619 | PSA_ASSERT(psa_import_key(&attributes, |
| 8620 | key_data, sizeof(key_data), |
| 8621 | &key)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8622 | |
| 8623 | /* valid key derivation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8624 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8625 | input1, input1_length, |
| 8626 | input2, input2_length, |
| 8627 | capacity)) { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8628 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8629 | } |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8630 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8631 | /* state of operation shouldn't allow additional generation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8632 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8633 | PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8634 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8635 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8636 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8637 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity), |
| 8638 | PSA_ERROR_INSUFFICIENT_DATA); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8639 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8640 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8641 | psa_key_derivation_abort(&operation); |
| 8642 | psa_destroy_key(key); |
| 8643 | PSA_DONE(); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8644 | } |
| 8645 | /* END_CASE */ |
| 8646 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8647 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8648 | void derive_actions_without_setup() |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8649 | { |
| 8650 | uint8_t output_buffer[16]; |
| 8651 | size_t buffer_size = 16; |
| 8652 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8653 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8654 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8655 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8656 | output_buffer, buffer_size) |
| 8657 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8658 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8659 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8660 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8661 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8662 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8663 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8664 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8665 | output_buffer, buffer_size) |
| 8666 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8667 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8668 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8669 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8670 | |
| 8671 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8672 | psa_key_derivation_abort(&operation); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8673 | } |
| 8674 | /* END_CASE */ |
| 8675 | |
| 8676 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8677 | void derive_output(int alg_arg, |
| 8678 | int step1_arg, data_t *input1, int expected_status_arg1, |
| 8679 | int step2_arg, data_t *input2, int expected_status_arg2, |
| 8680 | int step3_arg, data_t *input3, int expected_status_arg3, |
| 8681 | int step4_arg, data_t *input4, int expected_status_arg4, |
| 8682 | data_t *key_agreement_peer_key, |
| 8683 | int requested_capacity_arg, |
| 8684 | data_t *expected_output1, |
| 8685 | data_t *expected_output2, |
| 8686 | int other_key_input_type, |
| 8687 | int key_input_type, |
| 8688 | int derive_type) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8689 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8690 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8691 | psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg }; |
| 8692 | data_t *inputs[] = { input1, input2, input3, input4 }; |
| 8693 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8694 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8695 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8696 | MBEDTLS_SVC_KEY_ID_INIT }; |
| 8697 | psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2, |
| 8698 | expected_status_arg3, expected_status_arg4 }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8699 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8700 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8701 | uint8_t *expected_outputs[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8702 | { expected_output1->x, expected_output2->x }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8703 | size_t output_sizes[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8704 | { expected_output1->len, expected_output2->len }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8705 | size_t output_buffer_size = 0; |
| 8706 | uint8_t *output_buffer = NULL; |
| 8707 | size_t expected_capacity; |
| 8708 | size_t current_capacity; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8709 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; |
| 8710 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; |
| 8711 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; |
| 8712 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8713 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8714 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8715 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8716 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8717 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
| 8718 | if (output_sizes[i] > output_buffer_size) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8719 | output_buffer_size = output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8720 | } |
| 8721 | if (output_sizes[i] == 0) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8722 | expected_outputs[i] = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8723 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8724 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 8725 | TEST_CALLOC(output_buffer, output_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8726 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8727 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8728 | /* Extraction phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8729 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8730 | PSA_ASSERT(psa_key_derivation_set_capacity(&operation, |
| 8731 | requested_capacity)); |
| 8732 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8733 | switch (steps[i]) { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8734 | case 0: |
| 8735 | break; |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 8736 | case PSA_KEY_DERIVATION_INPUT_COST: |
| 8737 | TEST_EQUAL(psa_key_derivation_input_integer( |
Kusumit Ghoderao | f28e0f5 | 2023-06-06 15:03:22 +0530 | [diff] [blame] | 8738 | &operation, steps[i], |
| 8739 | parse_binary_string(inputs[i])), |
| 8740 | statuses[i]); |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 8741 | if (statuses[i] != PSA_SUCCESS) { |
| 8742 | goto exit; |
| 8743 | } |
| 8744 | break; |
| 8745 | case PSA_KEY_DERIVATION_INPUT_PASSWORD: |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8746 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8747 | switch (key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8748 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8749 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8750 | &operation, steps[i], |
| 8751 | inputs[i]->x, inputs[i]->len), |
| 8752 | statuses[i]); |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8753 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8754 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8755 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8756 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8757 | break; |
| 8758 | case 1: // input key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8759 | psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE); |
| 8760 | psa_set_key_algorithm(&attributes1, alg); |
| 8761 | psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8762 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8763 | PSA_ASSERT(psa_import_key(&attributes1, |
| 8764 | inputs[i]->x, inputs[i]->len, |
| 8765 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8766 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8767 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 8768 | PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1)); |
| 8769 | TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)), |
| 8770 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8771 | } |
| 8772 | |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 8773 | TEST_EQUAL(psa_key_derivation_input_key(&operation, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8774 | steps[i], |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 8775 | keys[i]), |
| 8776 | statuses[i]); |
| 8777 | |
| 8778 | if (statuses[i] != PSA_SUCCESS) { |
| 8779 | goto exit; |
| 8780 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8781 | break; |
| 8782 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8783 | TEST_ASSERT(!"default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8784 | break; |
| 8785 | } |
| 8786 | break; |
| 8787 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8788 | switch (other_key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8789 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8790 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 8791 | steps[i], |
| 8792 | inputs[i]->x, |
| 8793 | inputs[i]->len), |
| 8794 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8795 | break; |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 8796 | case 1: // input key, type DERIVE |
| 8797 | case 11: // input key, type RAW |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8798 | psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE); |
| 8799 | psa_set_key_algorithm(&attributes2, alg); |
| 8800 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8801 | |
| 8802 | // other secret of type RAW_DATA passed with input_key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8803 | if (other_key_input_type == 11) { |
| 8804 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA); |
| 8805 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8806 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8807 | PSA_ASSERT(psa_import_key(&attributes2, |
| 8808 | inputs[i]->x, inputs[i]->len, |
| 8809 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8810 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8811 | TEST_EQUAL(psa_key_derivation_input_key(&operation, |
| 8812 | steps[i], |
| 8813 | keys[i]), |
| 8814 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8815 | break; |
| 8816 | case 2: // key agreement |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8817 | psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE); |
| 8818 | psa_set_key_algorithm(&attributes3, alg); |
| 8819 | psa_set_key_type(&attributes3, |
| 8820 | PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)); |
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(&attributes3, |
| 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_key_agreement( |
| 8827 | &operation, |
| 8828 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 8829 | keys[i], key_agreement_peer_key->x, |
| 8830 | key_agreement_peer_key->len), statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8831 | break; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8832 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8833 | TEST_ASSERT(!"default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8834 | break; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8835 | } |
| 8836 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8837 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8838 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8839 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8840 | break; |
| 8841 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8842 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8843 | &operation, steps[i], |
| 8844 | inputs[i]->x, inputs[i]->len), statuses[i]); |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 8845 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8846 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 8847 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8848 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8849 | break; |
| 8850 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 8851 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8852 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8853 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8854 | ¤t_capacity)); |
| 8855 | TEST_EQUAL(current_capacity, requested_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8856 | expected_capacity = requested_capacity; |
| 8857 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8858 | if (derive_type == 1) { // output key |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8859 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; |
| 8860 | |
| 8861 | /* For output key derivation secret must be provided using |
| 8862 | input key, otherwise operation is not permitted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8863 | if (key_input_type == 1) { |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8864 | expected_status = PSA_SUCCESS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8865 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8866 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8867 | psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT); |
| 8868 | psa_set_key_algorithm(&attributes4, alg); |
| 8869 | psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE); |
| 8870 | psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8871 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8872 | TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation, |
| 8873 | &derived_key), expected_status); |
| 8874 | } else { // output bytes |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8875 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8876 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8877 | /* Read some bytes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8878 | status = psa_key_derivation_output_bytes(&operation, |
| 8879 | output_buffer, output_sizes[i]); |
| 8880 | if (expected_capacity == 0 && output_sizes[i] == 0) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8881 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8882 | TEST_ASSERT(status == PSA_SUCCESS || |
| 8883 | status == PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8884 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8885 | } else if (expected_capacity == 0 || |
| 8886 | output_sizes[i] > expected_capacity) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8887 | /* Capacity exceeded. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8888 | TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8889 | expected_capacity = 0; |
| 8890 | continue; |
| 8891 | } |
| 8892 | /* Success. Check the read data. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8893 | PSA_ASSERT(status); |
| 8894 | if (output_sizes[i] != 0) { |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 8895 | TEST_BUFFERS_EQUAL(output_buffer, output_sizes[i], |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8896 | expected_outputs[i], output_sizes[i]); |
| 8897 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8898 | /* Check the operation status. */ |
| 8899 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8900 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8901 | ¤t_capacity)); |
| 8902 | TEST_EQUAL(expected_capacity, current_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8903 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8904 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8905 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8906 | |
| 8907 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8908 | mbedtls_free(output_buffer); |
| 8909 | psa_key_derivation_abort(&operation); |
| 8910 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8911 | psa_destroy_key(keys[i]); |
| 8912 | } |
| 8913 | psa_destroy_key(derived_key); |
| 8914 | PSA_DONE(); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8915 | } |
| 8916 | /* END_CASE */ |
| 8917 | |
| 8918 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8919 | void derive_full(int alg_arg, |
| 8920 | data_t *key_data, |
| 8921 | data_t *input1, |
| 8922 | data_t *input2, |
| 8923 | int requested_capacity_arg) |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8924 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8925 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8926 | psa_algorithm_t alg = alg_arg; |
| 8927 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8928 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8929 | unsigned char output_buffer[16]; |
| 8930 | size_t expected_capacity = requested_capacity; |
| 8931 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8932 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8933 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8934 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8935 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8936 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8937 | psa_set_key_algorithm(&attributes, alg); |
| 8938 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8939 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8940 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8941 | &key)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8942 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8943 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8944 | input1->x, input1->len, |
| 8945 | input2->x, input2->len, |
| 8946 | requested_capacity)) { |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 8947 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8948 | } |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 8949 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8950 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8951 | ¤t_capacity)); |
| 8952 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8953 | |
| 8954 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8955 | while (current_capacity > 0) { |
| 8956 | size_t read_size = sizeof(output_buffer); |
| 8957 | if (read_size > current_capacity) { |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8958 | read_size = current_capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8959 | } |
| 8960 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8961 | output_buffer, |
| 8962 | read_size)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8963 | expected_capacity -= read_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8964 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8965 | ¤t_capacity)); |
| 8966 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8967 | } |
| 8968 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8969 | /* Check that the operation refuses to go over capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8970 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1), |
| 8971 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8972 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8973 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8974 | |
| 8975 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8976 | psa_key_derivation_abort(&operation); |
| 8977 | psa_destroy_key(key); |
| 8978 | PSA_DONE(); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8979 | } |
| 8980 | /* END_CASE */ |
| 8981 | |
Stephan Koch | 78109f5 | 2023-04-12 14:19:36 +0200 | [diff] [blame] | 8982 | /* 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] | 8983 | void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg, |
| 8984 | int derivation_step, |
| 8985 | int capacity, int expected_capacity_status_arg, |
| 8986 | data_t *expected_output, |
| 8987 | int expected_output_status_arg) |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8988 | { |
| 8989 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 8990 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | d378504 | 2022-09-16 06:45:44 -0400 | [diff] [blame] | 8991 | 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] | 8992 | uint8_t *output_buffer = NULL; |
| 8993 | psa_status_t status; |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 8994 | psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg; |
| 8995 | psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg; |
| 8996 | 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] | 8997 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 8998 | TEST_CALLOC(output_buffer, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8999 | PSA_ASSERT(psa_crypto_init()); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9000 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9001 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9002 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 9003 | expected_capacity_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9004 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9005 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 9006 | step, input->x, input->len), |
| 9007 | expected_input_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9008 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9009 | if (((psa_status_t) expected_input_status) != PSA_SUCCESS) { |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9010 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9011 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9012 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9013 | status = psa_key_derivation_output_bytes(&operation, output_buffer, |
| 9014 | expected_output->len); |
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 | TEST_EQUAL(status, expected_output_status); |
| 9017 | if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) { |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 9018 | TEST_BUFFERS_EQUAL(output_buffer, expected_output->len, expected_output->x, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9019 | expected_output->len); |
| 9020 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9021 | |
| 9022 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9023 | mbedtls_free(output_buffer); |
| 9024 | psa_key_derivation_abort(&operation); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9025 | PSA_DONE(); |
| 9026 | } |
| 9027 | /* END_CASE */ |
| 9028 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9029 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9030 | void derive_key_exercise(int alg_arg, |
| 9031 | data_t *key_data, |
| 9032 | data_t *input1, |
| 9033 | data_t *input2, |
| 9034 | int derived_type_arg, |
| 9035 | int derived_bits_arg, |
| 9036 | int derived_usage_arg, |
| 9037 | int derived_alg_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9038 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9039 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9040 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9041 | psa_algorithm_t alg = alg_arg; |
| 9042 | psa_key_type_t derived_type = derived_type_arg; |
| 9043 | size_t derived_bits = derived_bits_arg; |
| 9044 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 9045 | psa_algorithm_t derived_alg = derived_alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9046 | size_t capacity = PSA_BITS_TO_BYTES(derived_bits); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9047 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9048 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 9049 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9050 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9051 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9052 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9053 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9054 | psa_set_key_algorithm(&attributes, alg); |
| 9055 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
| 9056 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 9057 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9058 | |
| 9059 | /* Derive a key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9060 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9061 | input1->x, input1->len, |
| 9062 | input2->x, input2->len, |
| 9063 | capacity)) { |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9064 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9065 | } |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9066 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9067 | psa_set_key_usage_flags(&attributes, derived_usage); |
| 9068 | psa_set_key_algorithm(&attributes, derived_alg); |
| 9069 | psa_set_key_type(&attributes, derived_type); |
| 9070 | psa_set_key_bits(&attributes, derived_bits); |
| 9071 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation, |
| 9072 | &derived_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9073 | |
| 9074 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9075 | PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes)); |
| 9076 | TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type); |
| 9077 | TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9078 | |
| 9079 | /* Exercise the derived key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9080 | if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) { |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9081 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9082 | } |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9083 | |
| 9084 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9085 | /* |
| 9086 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9087 | * thus reset them as required. |
| 9088 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9089 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9090 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9091 | psa_key_derivation_abort(&operation); |
| 9092 | psa_destroy_key(base_key); |
| 9093 | psa_destroy_key(derived_key); |
| 9094 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9095 | } |
| 9096 | /* END_CASE */ |
| 9097 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9098 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9099 | void derive_key_export(int alg_arg, |
| 9100 | data_t *key_data, |
| 9101 | data_t *input1, |
| 9102 | data_t *input2, |
| 9103 | int bytes1_arg, |
| 9104 | int bytes2_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9105 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9106 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9107 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9108 | psa_algorithm_t alg = alg_arg; |
| 9109 | size_t bytes1 = bytes1_arg; |
| 9110 | size_t bytes2 = bytes2_arg; |
| 9111 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9112 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9113 | uint8_t *output_buffer = NULL; |
| 9114 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9115 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9116 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9117 | size_t length; |
| 9118 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 9119 | TEST_CALLOC(output_buffer, capacity); |
| 9120 | TEST_CALLOC(export_buffer, capacity); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9121 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9122 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9123 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9124 | psa_set_key_algorithm(&base_attributes, alg); |
| 9125 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9126 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9127 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9128 | |
| 9129 | /* Derive some material and output it. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9130 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9131 | input1->x, input1->len, |
| 9132 | input2->x, input2->len, |
| 9133 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9134 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9135 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9136 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9137 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9138 | output_buffer, |
| 9139 | capacity)); |
| 9140 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9141 | |
| 9142 | /* 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] | 9143 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9144 | input1->x, input1->len, |
| 9145 | input2->x, input2->len, |
| 9146 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9147 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9148 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9150 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9151 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9152 | psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA); |
| 9153 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1)); |
| 9154 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9155 | &derived_key)); |
| 9156 | PSA_ASSERT(psa_export_key(derived_key, |
| 9157 | export_buffer, bytes1, |
| 9158 | &length)); |
| 9159 | TEST_EQUAL(length, bytes1); |
| 9160 | PSA_ASSERT(psa_destroy_key(derived_key)); |
| 9161 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2)); |
| 9162 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9163 | &derived_key)); |
| 9164 | PSA_ASSERT(psa_export_key(derived_key, |
| 9165 | export_buffer + bytes1, bytes2, |
| 9166 | &length)); |
| 9167 | TEST_EQUAL(length, bytes2); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9168 | |
| 9169 | /* Compare the outputs from the two runs. */ |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 9170 | TEST_BUFFERS_EQUAL(output_buffer, bytes1 + bytes2, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9171 | export_buffer, capacity); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9172 | |
| 9173 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9174 | mbedtls_free(output_buffer); |
| 9175 | mbedtls_free(export_buffer); |
| 9176 | psa_key_derivation_abort(&operation); |
| 9177 | psa_destroy_key(base_key); |
| 9178 | psa_destroy_key(derived_key); |
| 9179 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9180 | } |
| 9181 | /* END_CASE */ |
| 9182 | |
| 9183 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9184 | void derive_key_type(int alg_arg, |
| 9185 | data_t *key_data, |
| 9186 | data_t *input1, |
| 9187 | data_t *input2, |
| 9188 | int key_type_arg, int bits_arg, |
| 9189 | data_t *expected_export) |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9190 | { |
| 9191 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9192 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9193 | const psa_algorithm_t alg = alg_arg; |
| 9194 | const psa_key_type_t key_type = key_type_arg; |
| 9195 | const size_t bits = bits_arg; |
| 9196 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9197 | const size_t export_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9198 | PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9199 | uint8_t *export_buffer = NULL; |
| 9200 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9201 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9202 | size_t export_length; |
| 9203 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 9204 | TEST_CALLOC(export_buffer, export_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9205 | PSA_ASSERT(psa_crypto_init()); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9206 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9207 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9208 | psa_set_key_algorithm(&base_attributes, alg); |
| 9209 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9210 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9211 | &base_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9212 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9213 | if (mbedtls_test_psa_setup_key_derivation_wrap( |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9214 | &operation, base_key, alg, |
| 9215 | input1->x, input1->len, |
| 9216 | input2->x, input2->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9217 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) { |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9218 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9219 | } |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9221 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9222 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9223 | psa_set_key_type(&derived_attributes, key_type); |
| 9224 | psa_set_key_bits(&derived_attributes, bits); |
| 9225 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9226 | &derived_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 | PSA_ASSERT(psa_export_key(derived_key, |
| 9229 | export_buffer, export_buffer_size, |
| 9230 | &export_length)); |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 9231 | TEST_BUFFERS_EQUAL(export_buffer, export_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9232 | expected_export->x, expected_export->len); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9233 | |
| 9234 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9235 | mbedtls_free(export_buffer); |
| 9236 | psa_key_derivation_abort(&operation); |
| 9237 | psa_destroy_key(base_key); |
| 9238 | psa_destroy_key(derived_key); |
| 9239 | PSA_DONE(); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9240 | } |
| 9241 | /* END_CASE */ |
| 9242 | |
| 9243 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9244 | void derive_key(int alg_arg, |
| 9245 | data_t *key_data, data_t *input1, data_t *input2, |
| 9246 | int type_arg, int bits_arg, |
| 9247 | int expected_status_arg, |
| 9248 | int is_large_output) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9249 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9250 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9251 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9252 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 9253 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9254 | size_t bits = bits_arg; |
| 9255 | psa_status_t expected_status = expected_status_arg; |
| 9256 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9257 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9258 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9259 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9260 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9261 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9262 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9263 | psa_set_key_algorithm(&base_attributes, alg); |
| 9264 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9265 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9266 | &base_key)); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9267 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9268 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9269 | input1->x, input1->len, |
| 9270 | input2->x, input2->len, |
| 9271 | SIZE_MAX)) { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9272 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9273 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9274 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9275 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9276 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9277 | psa_set_key_type(&derived_attributes, type); |
| 9278 | psa_set_key_bits(&derived_attributes, bits); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9279 | |
| 9280 | psa_status_t status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9281 | psa_key_derivation_output_key(&derived_attributes, |
| 9282 | &operation, |
| 9283 | &derived_key); |
| 9284 | if (is_large_output > 0) { |
| 9285 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9286 | } |
| 9287 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9288 | |
| 9289 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9290 | psa_key_derivation_abort(&operation); |
| 9291 | psa_destroy_key(base_key); |
| 9292 | psa_destroy_key(derived_key); |
| 9293 | PSA_DONE(); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9294 | } |
| 9295 | /* END_CASE */ |
| 9296 | |
| 9297 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9298 | void key_agreement_setup(int alg_arg, |
| 9299 | int our_key_type_arg, int our_key_alg_arg, |
| 9300 | data_t *our_key_data, data_t *peer_key_data, |
| 9301 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9302 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9303 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9304 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 9305 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9306 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9307 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9308 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9309 | psa_status_t expected_status = expected_status_arg; |
| 9310 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9311 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9312 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9314 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9315 | psa_set_key_algorithm(&attributes, our_key_alg); |
| 9316 | psa_set_key_type(&attributes, our_key_type); |
| 9317 | PSA_ASSERT(psa_import_key(&attributes, |
| 9318 | our_key_data->x, our_key_data->len, |
| 9319 | &our_key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9320 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9321 | /* The tests currently include inputs that should fail at either step. |
| 9322 | * Test cases that fail at the setup step should be changed to call |
| 9323 | * key_derivation_setup instead, and this function should be renamed |
| 9324 | * to key_agreement_fail. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9325 | status = psa_key_derivation_setup(&operation, alg); |
| 9326 | if (status == PSA_SUCCESS) { |
| 9327 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 9328 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 9329 | our_key, |
| 9330 | peer_key_data->x, peer_key_data->len), |
| 9331 | expected_status); |
| 9332 | } else { |
| 9333 | TEST_ASSERT(status == expected_status); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9334 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9335 | |
| 9336 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9337 | psa_key_derivation_abort(&operation); |
| 9338 | psa_destroy_key(our_key); |
| 9339 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9340 | } |
| 9341 | /* END_CASE */ |
| 9342 | |
| 9343 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9344 | void raw_key_agreement(int alg_arg, |
| 9345 | int our_key_type_arg, data_t *our_key_data, |
| 9346 | data_t *peer_key_data, |
| 9347 | data_t *expected_output) |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9348 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9349 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9350 | psa_algorithm_t alg = alg_arg; |
| 9351 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9352 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9353 | unsigned char *output = NULL; |
| 9354 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9355 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9356 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9357 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9358 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9359 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9360 | psa_set_key_algorithm(&attributes, alg); |
| 9361 | psa_set_key_type(&attributes, our_key_type); |
| 9362 | PSA_ASSERT(psa_import_key(&attributes, |
| 9363 | our_key_data->x, our_key_data->len, |
| 9364 | &our_key)); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9365 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9366 | PSA_ASSERT(psa_get_key_attributes(our_key, &attributes)); |
| 9367 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9368 | |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9369 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9370 | TEST_LE_U(expected_output->len, |
| 9371 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits)); |
| 9372 | TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits), |
| 9373 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9374 | |
| 9375 | /* Good case with exact output size */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 9376 | TEST_CALLOC(output, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9377 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9378 | peer_key_data->x, peer_key_data->len, |
| 9379 | output, expected_output->len, |
| 9380 | &output_length)); |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 9381 | TEST_BUFFERS_EQUAL(output, output_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9382 | expected_output->x, expected_output->len); |
| 9383 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9384 | output = NULL; |
| 9385 | output_length = ~0; |
| 9386 | |
| 9387 | /* Larger buffer */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 9388 | TEST_CALLOC(output, expected_output->len + 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9389 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9390 | peer_key_data->x, peer_key_data->len, |
| 9391 | output, expected_output->len + 1, |
| 9392 | &output_length)); |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 9393 | TEST_BUFFERS_EQUAL(output, output_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9394 | expected_output->x, expected_output->len); |
| 9395 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9396 | output = NULL; |
| 9397 | output_length = ~0; |
| 9398 | |
| 9399 | /* Buffer too small */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 9400 | TEST_CALLOC(output, expected_output->len - 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9401 | TEST_EQUAL(psa_raw_key_agreement(alg, our_key, |
| 9402 | peer_key_data->x, peer_key_data->len, |
| 9403 | output, expected_output->len - 1, |
| 9404 | &output_length), |
| 9405 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9406 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9407 | TEST_LE_U(output_length, expected_output->len - 1); |
| 9408 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9409 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9410 | |
| 9411 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9412 | mbedtls_free(output); |
| 9413 | psa_destroy_key(our_key); |
| 9414 | PSA_DONE(); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9415 | } |
| 9416 | /* END_CASE */ |
| 9417 | |
| 9418 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9419 | void key_agreement_capacity(int alg_arg, |
| 9420 | int our_key_type_arg, data_t *our_key_data, |
| 9421 | data_t *peer_key_data, |
| 9422 | int expected_capacity_arg) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9423 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9424 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9425 | psa_algorithm_t alg = alg_arg; |
| 9426 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9427 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9428 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9429 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9430 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9431 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9432 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9433 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9434 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9435 | psa_set_key_algorithm(&attributes, alg); |
| 9436 | psa_set_key_type(&attributes, our_key_type); |
| 9437 | PSA_ASSERT(psa_import_key(&attributes, |
| 9438 | our_key_data->x, our_key_data->len, |
| 9439 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9440 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9441 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9442 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9443 | &operation, |
| 9444 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9445 | peer_key_data->x, peer_key_data->len)); |
| 9446 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9447 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9448 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9449 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9450 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9451 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9452 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 9453 | /* Test the advertised capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9454 | PSA_ASSERT(psa_key_derivation_get_capacity( |
| 9455 | &operation, &actual_capacity)); |
| 9456 | TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9457 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9458 | /* Test the actual capacity by reading the output. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9459 | while (actual_capacity > sizeof(output)) { |
| 9460 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9461 | output, sizeof(output))); |
| 9462 | actual_capacity -= sizeof(output); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9463 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9464 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9465 | output, actual_capacity)); |
| 9466 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1), |
| 9467 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9468 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9469 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9470 | psa_key_derivation_abort(&operation); |
| 9471 | psa_destroy_key(our_key); |
| 9472 | PSA_DONE(); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9473 | } |
| 9474 | /* END_CASE */ |
| 9475 | |
| 9476 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9477 | void key_agreement_output(int alg_arg, |
| 9478 | int our_key_type_arg, data_t *our_key_data, |
| 9479 | data_t *peer_key_data, |
| 9480 | data_t *expected_output1, data_t *expected_output2) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9481 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9482 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9483 | psa_algorithm_t alg = alg_arg; |
| 9484 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9485 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9486 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9487 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9488 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 9489 | TEST_CALLOC(actual_output, MAX(expected_output1->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9490 | expected_output2->len)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9491 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9492 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9493 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9494 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9495 | psa_set_key_algorithm(&attributes, alg); |
| 9496 | psa_set_key_type(&attributes, our_key_type); |
| 9497 | PSA_ASSERT(psa_import_key(&attributes, |
| 9498 | our_key_data->x, our_key_data->len, |
| 9499 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9500 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9501 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9502 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9503 | &operation, |
| 9504 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9505 | peer_key_data->x, peer_key_data->len)); |
| 9506 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9507 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9508 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9509 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9510 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9511 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9512 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9513 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9514 | actual_output, |
| 9515 | expected_output1->len)); |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 9516 | TEST_BUFFERS_EQUAL(actual_output, expected_output1->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9517 | expected_output1->x, expected_output1->len); |
| 9518 | if (expected_output2->len != 0) { |
| 9519 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9520 | actual_output, |
| 9521 | expected_output2->len)); |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 9522 | TEST_BUFFERS_EQUAL(actual_output, expected_output2->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9523 | expected_output2->x, expected_output2->len); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9524 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9525 | |
| 9526 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9527 | psa_key_derivation_abort(&operation); |
| 9528 | psa_destroy_key(our_key); |
| 9529 | PSA_DONE(); |
| 9530 | mbedtls_free(actual_output); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9531 | } |
| 9532 | /* END_CASE */ |
| 9533 | |
| 9534 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9535 | void generate_random(int bytes_arg) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9536 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9537 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9538 | unsigned char *output = NULL; |
| 9539 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9540 | size_t i; |
| 9541 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9542 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9543 | TEST_ASSERT(bytes_arg >= 0); |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 9544 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 9545 | TEST_CALLOC(output, bytes); |
| 9546 | TEST_CALLOC(changed, bytes); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9547 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9548 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9549 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9550 | /* Run several times, to ensure that every output byte will be |
| 9551 | * nonzero at least once with overwhelming probability |
| 9552 | * (2^(-8*number_of_runs)). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9553 | for (run = 0; run < 10; run++) { |
| 9554 | if (bytes != 0) { |
| 9555 | memset(output, 0, bytes); |
| 9556 | } |
| 9557 | PSA_ASSERT(psa_generate_random(output, bytes)); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9558 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9559 | for (i = 0; i < bytes; i++) { |
| 9560 | if (output[i] != 0) { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9561 | ++changed[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9562 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9563 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9564 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9565 | |
| 9566 | /* Check that every byte was changed to nonzero at least once. This |
| 9567 | * validates that psa_generate_random is overwriting every byte of |
| 9568 | * the output buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9569 | for (i = 0; i < bytes; i++) { |
| 9570 | TEST_ASSERT(changed[i] != 0); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9571 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9572 | |
| 9573 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9574 | PSA_DONE(); |
| 9575 | mbedtls_free(output); |
| 9576 | mbedtls_free(changed); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9577 | } |
| 9578 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9579 | |
| 9580 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9581 | void generate_key(int type_arg, |
| 9582 | int bits_arg, |
| 9583 | int usage_arg, |
| 9584 | int alg_arg, |
| 9585 | int expected_status_arg, |
| 9586 | int is_large_key) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9587 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9588 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9589 | psa_key_type_t type = type_arg; |
| 9590 | psa_key_usage_t usage = usage_arg; |
| 9591 | size_t bits = bits_arg; |
| 9592 | psa_algorithm_t alg = alg_arg; |
| 9593 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9594 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 9595 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9596 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9597 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9598 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9599 | psa_set_key_usage_flags(&attributes, usage); |
| 9600 | psa_set_key_algorithm(&attributes, alg); |
| 9601 | psa_set_key_type(&attributes, type); |
| 9602 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9603 | |
| 9604 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9605 | psa_status_t status = psa_generate_key(&attributes, &key); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9606 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9607 | if (is_large_key > 0) { |
| 9608 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9609 | } |
| 9610 | TEST_EQUAL(status, expected_status); |
| 9611 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9612 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9613 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9614 | |
| 9615 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9616 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 9617 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 9618 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9619 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 9620 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9621 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 9622 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9623 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9624 | |
| 9625 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9626 | /* |
| 9627 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9628 | * thus reset them as required. |
| 9629 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9630 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9631 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9632 | psa_destroy_key(key); |
| 9633 | PSA_DONE(); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9634 | } |
| 9635 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 9636 | |
Valerio Setti | ff7f861 | 2023-05-26 13:49:33 +0200 | [diff] [blame] | 9637 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_LEGACY:PSA_WANT_ALG_RSA_PKCS1V15_CRYPT:PSA_WANT_ALG_RSA_PKCS1V15_SIGN:MBEDTLS_GENPRIME */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9638 | void generate_key_rsa(int bits_arg, |
| 9639 | data_t *e_arg, |
| 9640 | int expected_status_arg) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +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 | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 9643 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9644 | size_t bits = bits_arg; |
| 9645 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 9646 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 9647 | psa_status_t expected_status = expected_status_arg; |
| 9648 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9649 | uint8_t *exported = NULL; |
| 9650 | size_t exported_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9651 | PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9652 | size_t exported_length = SIZE_MAX; |
| 9653 | uint8_t *e_read_buffer = NULL; |
| 9654 | int is_default_public_exponent = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9655 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9656 | size_t e_read_length = SIZE_MAX; |
| 9657 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9658 | if (e_arg->len == 0 || |
| 9659 | (e_arg->len == 3 && |
| 9660 | 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] | 9661 | is_default_public_exponent = 1; |
| 9662 | e_read_size = 0; |
| 9663 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 9664 | TEST_CALLOC(e_read_buffer, e_read_size); |
| 9665 | TEST_CALLOC(exported, exported_size); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9666 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9667 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9668 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9669 | psa_set_key_usage_flags(&attributes, usage); |
| 9670 | psa_set_key_algorithm(&attributes, alg); |
| 9671 | PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type, |
| 9672 | e_arg->x, e_arg->len)); |
| 9673 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9674 | |
| 9675 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9676 | TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status); |
| 9677 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9678 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9679 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9680 | |
| 9681 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9682 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 9683 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 9684 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 9685 | PSA_ASSERT(psa_get_key_domain_parameters(&attributes, |
| 9686 | e_read_buffer, e_read_size, |
| 9687 | &e_read_length)); |
| 9688 | if (is_default_public_exponent) { |
| 9689 | TEST_EQUAL(e_read_length, 0); |
| 9690 | } else { |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 9691 | TEST_BUFFERS_EQUAL(e_read_buffer, e_read_length, e_arg->x, e_arg->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9692 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9693 | |
| 9694 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9695 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9696 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9697 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9698 | |
| 9699 | /* Export the key and check the public exponent. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9700 | PSA_ASSERT(psa_export_public_key(key, |
| 9701 | exported, exported_size, |
| 9702 | &exported_length)); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9703 | { |
| 9704 | uint8_t *p = exported; |
| 9705 | uint8_t *end = exported + exported_length; |
| 9706 | size_t len; |
| 9707 | /* RSAPublicKey ::= SEQUENCE { |
| 9708 | * modulus INTEGER, -- n |
| 9709 | * publicExponent INTEGER } -- e |
| 9710 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9711 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 9712 | MBEDTLS_ASN1_SEQUENCE | |
| 9713 | MBEDTLS_ASN1_CONSTRUCTED)); |
| 9714 | TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1)); |
| 9715 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 9716 | MBEDTLS_ASN1_INTEGER)); |
| 9717 | if (len >= 1 && p[0] == 0) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9718 | ++p; |
| 9719 | --len; |
| 9720 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9721 | if (e_arg->len == 0) { |
| 9722 | TEST_EQUAL(len, 3); |
| 9723 | TEST_EQUAL(p[0], 1); |
| 9724 | TEST_EQUAL(p[1], 0); |
| 9725 | TEST_EQUAL(p[2], 1); |
| 9726 | } else { |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 9727 | TEST_BUFFERS_EQUAL(p, len, e_arg->x, e_arg->len); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9728 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9729 | } |
| 9730 | |
| 9731 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9732 | /* |
| 9733 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 9734 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 9735 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9736 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9737 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9738 | psa_destroy_key(key); |
| 9739 | PSA_DONE(); |
| 9740 | mbedtls_free(e_read_buffer); |
| 9741 | mbedtls_free(exported); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9742 | } |
| 9743 | /* END_CASE */ |
| 9744 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9745 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9746 | void persistent_key_load_key_from_storage(data_t *data, |
| 9747 | int type_arg, int bits_arg, |
| 9748 | int usage_flags_arg, int alg_arg, |
| 9749 | int generation_method) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9750 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9751 | 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] | 9752 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9753 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9754 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9755 | psa_key_type_t type = type_arg; |
| 9756 | size_t bits = bits_arg; |
| 9757 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 9758 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9759 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9760 | unsigned char *first_export = NULL; |
| 9761 | unsigned char *second_export = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9762 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9763 | size_t first_exported_length; |
| 9764 | size_t second_exported_length; |
| 9765 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9766 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 9767 | TEST_CALLOC(first_export, export_size); |
| 9768 | TEST_CALLOC(second_export, export_size); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9769 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9771 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9772 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9773 | psa_set_key_id(&attributes, key_id); |
| 9774 | psa_set_key_usage_flags(&attributes, usage_flags); |
| 9775 | psa_set_key_algorithm(&attributes, alg); |
| 9776 | psa_set_key_type(&attributes, type); |
| 9777 | psa_set_key_bits(&attributes, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9778 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9779 | switch (generation_method) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9780 | case IMPORT_KEY: |
| 9781 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9782 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, |
| 9783 | &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9784 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9785 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9786 | case GENERATE_KEY: |
| 9787 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9788 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9789 | break; |
| 9790 | |
| 9791 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 9792 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9793 | { |
| 9794 | /* Create base key */ |
| 9795 | psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256); |
| 9796 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9797 | psa_set_key_usage_flags(&base_attributes, |
| 9798 | PSA_KEY_USAGE_DERIVE); |
| 9799 | psa_set_key_algorithm(&base_attributes, derive_alg); |
| 9800 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9801 | PSA_ASSERT(psa_import_key(&base_attributes, |
| 9802 | data->x, data->len, |
| 9803 | &base_key)); |
| 9804 | /* Derive a key. */ |
| 9805 | PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg)); |
| 9806 | PSA_ASSERT(psa_key_derivation_input_key( |
| 9807 | &operation, |
| 9808 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key)); |
| 9809 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 9810 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
| 9811 | NULL, 0)); |
| 9812 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, |
| 9813 | &operation, |
| 9814 | &key)); |
| 9815 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
| 9816 | PSA_ASSERT(psa_destroy_key(base_key)); |
| 9817 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9818 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9819 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9820 | TEST_ASSUME(!"KDF not supported in this configuration"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9821 | #endif |
| 9822 | break; |
| 9823 | |
| 9824 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9825 | TEST_ASSERT(!"generation_method not implemented in test"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9826 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9827 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9828 | psa_reset_key_attributes(&attributes); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9829 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9830 | /* Export the key if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9831 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9832 | PSA_ASSERT(psa_export_key(key, |
| 9833 | first_export, export_size, |
| 9834 | &first_exported_length)); |
| 9835 | if (generation_method == IMPORT_KEY) { |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 9836 | TEST_BUFFERS_EQUAL(data->x, data->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9837 | first_export, first_exported_length); |
| 9838 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9839 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9840 | |
| 9841 | /* Shutdown and restart */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9842 | PSA_ASSERT(psa_purge_key(key)); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 9843 | PSA_DONE(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9844 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9845 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9846 | /* Check key slot still contains key data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9847 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 9848 | TEST_ASSERT(mbedtls_svc_key_id_equal( |
| 9849 | psa_get_key_id(&attributes), key_id)); |
| 9850 | TEST_EQUAL(psa_get_key_lifetime(&attributes), |
| 9851 | PSA_KEY_LIFETIME_PERSISTENT); |
| 9852 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 9853 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 9854 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 9855 | mbedtls_test_update_key_usage_flags(usage_flags)); |
| 9856 | TEST_EQUAL(psa_get_key_algorithm(&attributes), alg); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9857 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9858 | /* Export the key again if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9859 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9860 | PSA_ASSERT(psa_export_key(key, |
| 9861 | second_export, export_size, |
| 9862 | &second_exported_length)); |
Tom Cosgrove | 65cd851 | 2023-07-20 16:46:01 +0100 | [diff] [blame] | 9863 | TEST_BUFFERS_EQUAL(first_export, first_exported_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9864 | second_export, second_exported_length); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9865 | } |
| 9866 | |
| 9867 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9868 | if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9869 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9870 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9871 | |
| 9872 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9873 | /* |
| 9874 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9875 | * thus reset them as required. |
| 9876 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9877 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9878 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9879 | mbedtls_free(first_export); |
| 9880 | mbedtls_free(second_export); |
| 9881 | psa_key_derivation_abort(&operation); |
| 9882 | psa_destroy_key(base_key); |
| 9883 | psa_destroy_key(key); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 9884 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9885 | } |
| 9886 | /* END_CASE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9887 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 9888 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9889 | void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg, |
| 9890 | int primitive_arg, int hash_arg, int role_arg, |
| 9891 | int test_input, data_t *pw_data, |
| 9892 | int inj_err_type_arg, |
| 9893 | int expected_error_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9894 | { |
| 9895 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 9896 | psa_pake_operation_t operation = psa_pake_operation_init(); |
| 9897 | psa_algorithm_t alg = alg_arg; |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9898 | psa_pake_primitive_t primitive = primitive_arg; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 9899 | psa_key_type_t key_type_pw = key_type_pw_arg; |
| 9900 | psa_key_usage_t key_usage_pw = key_usage_pw_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9901 | psa_algorithm_t hash_alg = hash_arg; |
| 9902 | psa_pake_role_t role = role_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9903 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9904 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9905 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
| 9906 | psa_status_t expected_error = expected_error_arg; |
| 9907 | psa_status_t status; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9908 | unsigned char *output_buffer = NULL; |
| 9909 | size_t output_len = 0; |
| 9910 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9911 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9912 | |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9913 | size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9914 | PSA_PAKE_STEP_KEY_SHARE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame^] | 9915 | TEST_CALLOC(output_buffer, buf_size); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9916 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9917 | if (pw_data->len > 0) { |
| 9918 | psa_set_key_usage_flags(&attributes, key_usage_pw); |
| 9919 | psa_set_key_algorithm(&attributes, alg); |
| 9920 | psa_set_key_type(&attributes, key_type_pw); |
| 9921 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 9922 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9923 | } |
| 9924 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9925 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 9926 | psa_pake_cs_set_primitive(&cipher_suite, primitive); |
| 9927 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9928 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9929 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 9930 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9931 | if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) { |
| 9932 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 9933 | expected_error); |
| 9934 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9935 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 9936 | expected_error); |
| 9937 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9938 | TEST_EQUAL(psa_pake_set_password_key(&operation, key), |
| 9939 | expected_error); |
| 9940 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9941 | TEST_EQUAL(psa_pake_set_role(&operation, role), |
| 9942 | expected_error); |
| 9943 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9944 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 9945 | NULL, 0, NULL), |
| 9946 | expected_error); |
| 9947 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9948 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0), |
| 9949 | expected_error); |
| 9950 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9951 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9952 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9953 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9954 | status = psa_pake_setup(&operation, &cipher_suite); |
| 9955 | if (status != PSA_SUCCESS) { |
| 9956 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9957 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9958 | } |
| 9959 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9960 | if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) { |
| 9961 | TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite), |
| 9962 | expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9963 | goto exit; |
| 9964 | } |
| 9965 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9966 | status = psa_pake_set_role(&operation, role); |
| 9967 | if (status != PSA_SUCCESS) { |
| 9968 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9969 | goto exit; |
| 9970 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9971 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9972 | if (pw_data->len > 0) { |
| 9973 | status = psa_pake_set_password_key(&operation, key); |
| 9974 | if (status != PSA_SUCCESS) { |
| 9975 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9976 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9977 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9978 | } |
| 9979 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9980 | if (inj_err_type == INJECT_ERR_INVALID_USER) { |
| 9981 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 9982 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9983 | goto exit; |
| 9984 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9985 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9986 | if (inj_err_type == INJECT_ERR_INVALID_PEER) { |
| 9987 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 9988 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9989 | goto exit; |
| 9990 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9991 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9992 | if (inj_err_type == INJECT_ERR_SET_USER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9993 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9994 | TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4), |
| 9995 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9996 | goto exit; |
| 9997 | } |
| 9998 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9999 | if (inj_err_type == INJECT_ERR_SET_PEER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10000 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10001 | TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4), |
| 10002 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10003 | goto exit; |
| 10004 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 10005 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10006 | const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10007 | PSA_PAKE_STEP_KEY_SHARE); |
| 10008 | const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10009 | PSA_PAKE_STEP_ZK_PUBLIC); |
| 10010 | const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10011 | PSA_PAKE_STEP_ZK_PROOF); |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 10012 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10013 | if (test_input) { |
| 10014 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 10015 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0), |
| 10016 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10017 | goto exit; |
| 10018 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10019 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10020 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 10021 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 10022 | output_buffer, size_zk_proof), |
| 10023 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10024 | goto exit; |
| 10025 | } |
| 10026 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10027 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 10028 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10029 | output_buffer, size_zk_proof), |
| 10030 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10031 | goto exit; |
| 10032 | } |
| 10033 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10034 | status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10035 | output_buffer, size_key_share); |
| 10036 | if (status != PSA_SUCCESS) { |
| 10037 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10038 | goto exit; |
| 10039 | } |
| 10040 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10041 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 10042 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10043 | output_buffer, size_zk_public + 1), |
| 10044 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10045 | goto exit; |
| 10046 | } |
| 10047 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10048 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10049 | // 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] | 10050 | psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10051 | output_buffer, size_zk_public + 1); |
| 10052 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10053 | output_buffer, size_zk_public), |
| 10054 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10055 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10056 | } |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10057 | } else { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10058 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 10059 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10060 | NULL, 0, NULL), |
| 10061 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10062 | goto exit; |
| 10063 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10064 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10065 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 10066 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 10067 | output_buffer, buf_size, &output_len), |
| 10068 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10069 | goto exit; |
| 10070 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10071 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10072 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 10073 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10074 | output_buffer, buf_size, &output_len), |
| 10075 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10076 | goto exit; |
| 10077 | } |
| 10078 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10079 | status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10080 | output_buffer, buf_size, &output_len); |
| 10081 | if (status != PSA_SUCCESS) { |
| 10082 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10083 | goto exit; |
| 10084 | } |
| 10085 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10086 | TEST_ASSERT(output_len > 0); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10087 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10088 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 10089 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10090 | output_buffer, size_zk_public - 1, &output_len), |
| 10091 | PSA_ERROR_BUFFER_TOO_SMALL); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10092 | goto exit; |
| 10093 | } |
| 10094 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10095 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10096 | // 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] | 10097 | psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10098 | output_buffer, size_zk_public - 1, &output_len); |
| 10099 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10100 | output_buffer, buf_size, &output_len), |
| 10101 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10102 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10103 | } |
| 10104 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10105 | |
| 10106 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10107 | PSA_ASSERT(psa_destroy_key(key)); |
| 10108 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10109 | mbedtls_free(output_buffer); |
| 10110 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10111 | } |
| 10112 | /* END_CASE */ |
| 10113 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 10114 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10115 | void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg, |
| 10116 | int client_input_first, int inject_error, |
| 10117 | data_t *pw_data) |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10118 | { |
| 10119 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10120 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10121 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10122 | psa_algorithm_t alg = alg_arg; |
| 10123 | psa_algorithm_t hash_alg = hash_arg; |
| 10124 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10125 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10126 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10127 | PSA_INIT(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10128 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10129 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10130 | psa_set_key_algorithm(&attributes, alg); |
| 10131 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10132 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10133 | &key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10135 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10136 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10137 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10138 | |
| 10139 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10140 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10141 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10142 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10143 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10144 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10145 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10146 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10147 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10149 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10150 | client_input_first, 1, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10151 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10152 | if (inject_error == 1 || inject_error == 2) { |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10153 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10154 | } |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10155 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10156 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10157 | client_input_first, 2, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10158 | |
| 10159 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10160 | psa_destroy_key(key); |
| 10161 | psa_pake_abort(&server); |
| 10162 | psa_pake_abort(&client); |
| 10163 | PSA_DONE(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10164 | } |
| 10165 | /* END_CASE */ |
| 10166 | |
| 10167 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10168 | void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg, |
| 10169 | int derive_alg_arg, data_t *pw_data, |
| 10170 | int client_input_first, int inj_err_type_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10171 | { |
| 10172 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10173 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10174 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10175 | psa_algorithm_t alg = alg_arg; |
| 10176 | psa_algorithm_t hash_alg = hash_arg; |
| 10177 | psa_algorithm_t derive_alg = derive_alg_arg; |
| 10178 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10179 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10180 | psa_key_derivation_operation_t server_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10181 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10182 | psa_key_derivation_operation_t client_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10183 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10184 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10185 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10186 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10187 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10188 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10189 | psa_set_key_algorithm(&attributes, alg); |
| 10190 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10191 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10192 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10193 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10194 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10195 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10196 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10197 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10198 | /* Get shared key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10199 | PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg)); |
| 10200 | PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10201 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10202 | if (PSA_ALG_IS_TLS12_PRF(derive_alg) || |
| 10203 | PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) { |
| 10204 | PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive, |
| 10205 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10206 | (const uint8_t *) "", 0)); |
| 10207 | PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive, |
| 10208 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10209 | (const uint8_t *) "", 0)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10210 | } |
| 10211 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10212 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10213 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10215 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10216 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10217 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10218 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10219 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10221 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) { |
| 10222 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10223 | PSA_ERROR_BAD_STATE); |
| 10224 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10225 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10226 | goto exit; |
| 10227 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10228 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10229 | /* First round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10230 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10231 | client_input_first, 1, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10232 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10233 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) { |
| 10234 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10235 | PSA_ERROR_BAD_STATE); |
| 10236 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10237 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10238 | goto exit; |
| 10239 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10240 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10241 | /* Second round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10242 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10243 | client_input_first, 2, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10244 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10245 | PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive)); |
| 10246 | PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10247 | |
| 10248 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10249 | psa_key_derivation_abort(&server_derive); |
| 10250 | psa_key_derivation_abort(&client_derive); |
| 10251 | psa_destroy_key(key); |
| 10252 | psa_pake_abort(&server); |
| 10253 | psa_pake_abort(&client); |
| 10254 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10255 | } |
| 10256 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10257 | |
| 10258 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10259 | void ecjpake_size_macros() |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10260 | { |
| 10261 | const psa_algorithm_t alg = PSA_ALG_JPAKE; |
| 10262 | const size_t bits = 256; |
| 10263 | const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10264 | 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] | 10265 | 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] | 10266 | PSA_ECC_FAMILY_SECP_R1); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10267 | |
| 10268 | // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types |
| 10269 | /* 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] | 10270 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10271 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
| 10272 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10273 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10274 | /* The output for ZK_PROOF is the same bitsize as the curve */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10275 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10276 | PSA_BITS_TO_BYTES(bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10277 | |
| 10278 | /* Input sizes are the same as output sizes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10279 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10280 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE)); |
| 10281 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10282 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC)); |
| 10283 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10284 | 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] | 10285 | |
| 10286 | /* These inequalities will always hold even when other PAKEs are added */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10287 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10288 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10289 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10290 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10291 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10292 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10293 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10294 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10295 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10296 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10297 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10298 | PSA_PAKE_INPUT_MAX_SIZE); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10299 | } |
| 10300 | /* END_CASE */ |