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-Silabs | b6ad682 | 2023-04-27 22:42:19 +0530 | [diff] [blame^] | 304 | result = result << 64 | 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 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 432 | ASSERT_ALLOC(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 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 442 | ASSERT_ALLOC(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 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 505 | ASSERT_ALLOC(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 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 586 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 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 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 695 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 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 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 747 | ASSERT_ALLOC(buffer0, buffer_length); |
| 748 | ASSERT_ALLOC(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 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1413 | ASSERT_ALLOC_WEAK(buffer, buffer_size); |
| 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()); |
| 1475 | ASSERT_ALLOC(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; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1522 | ASSERT_ALLOC(exported, export_size); |
| 1523 | if (!canonical_input) { |
| 1524 | ASSERT_ALLOC(reexported, export_size); |
| 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 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1533 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1534 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1535 | |
| 1536 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1537 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1538 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1539 | TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits); |
| 1540 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1541 | |
| 1542 | /* Export the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1543 | status = psa_export_key(key, exported, export_size, &exported_length); |
| 1544 | TEST_EQUAL(status, expected_export_status); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1545 | |
| 1546 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1547 | * and export_size. On errors, the exported length must be 0. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1548 | TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH); |
| 1549 | TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0); |
| 1550 | TEST_LE_U(exported_length, export_size); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1551 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1552 | TEST_ASSERT(mem_is_char(exported + exported_length, 0, |
| 1553 | export_size - exported_length)); |
| 1554 | if (status != PSA_SUCCESS) { |
| 1555 | TEST_EQUAL(exported_length, 0); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1556 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1557 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1558 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1559 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1560 | * this validates the canonical representations. For canonical inputs, |
| 1561 | * this doesn't directly validate the implementation, but it still helps |
| 1562 | * by cross-validating the test data with the sanity check code. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1563 | if (!psa_key_lifetime_is_external(lifetime)) { |
| 1564 | if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) { |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1565 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1566 | } |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1567 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1568 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1569 | if (canonical_input) { |
| 1570 | ASSERT_COMPARE(data->x, data->len, exported, exported_length); |
| 1571 | } else { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1572 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1573 | PSA_ASSERT(psa_import_key(&attributes, exported, exported_length, |
| 1574 | &key2)); |
| 1575 | PSA_ASSERT(psa_export_key(key2, |
| 1576 | reexported, |
| 1577 | export_size, |
| 1578 | &reexported_length)); |
| 1579 | ASSERT_COMPARE(exported, exported_length, |
| 1580 | reexported, reexported_length); |
| 1581 | PSA_ASSERT(psa_destroy_key(key2)); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1582 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1583 | TEST_LE_U(exported_length, |
| 1584 | PSA_EXPORT_KEY_OUTPUT_SIZE(type, |
| 1585 | psa_get_key_bits(&got_attributes))); |
| 1586 | TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1587 | |
| 1588 | destroy: |
| 1589 | /* Destroy the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1590 | PSA_ASSERT(psa_destroy_key(key)); |
| 1591 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1592 | |
| 1593 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1594 | /* |
| 1595 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1596 | * thus reset them as required. |
| 1597 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1598 | psa_reset_key_attributes(&got_attributes); |
| 1599 | psa_destroy_key(key); |
| 1600 | mbedtls_free(exported); |
| 1601 | mbedtls_free(reexported); |
| 1602 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1603 | } |
| 1604 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1605 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1606 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1607 | void import_export_public_key(data_t *data, |
| 1608 | int type_arg, // key pair or public key |
| 1609 | int alg_arg, |
| 1610 | int lifetime_arg, |
| 1611 | int export_size_delta, |
| 1612 | int expected_export_status_arg, |
| 1613 | data_t *expected_public_key) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1614 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1615 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1616 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1617 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1618 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1619 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1620 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1621 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1622 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1623 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1624 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1625 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1626 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1627 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1628 | psa_set_key_lifetime(&attributes, lifetime); |
| 1629 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1630 | psa_set_key_algorithm(&attributes, alg); |
| 1631 | psa_set_key_type(&attributes, type); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1632 | |
| 1633 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1634 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1635 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1636 | /* Export the public key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1637 | ASSERT_ALLOC(exported, export_size); |
| 1638 | status = psa_export_public_key(key, |
| 1639 | exported, export_size, |
| 1640 | &exported_length); |
| 1641 | TEST_EQUAL(status, expected_export_status); |
| 1642 | if (status == PSA_SUCCESS) { |
| 1643 | 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] | 1644 | size_t bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1645 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1646 | bits = psa_get_key_bits(&attributes); |
| 1647 | TEST_LE_U(expected_public_key->len, |
| 1648 | PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1649 | TEST_LE_U(expected_public_key->len, |
| 1650 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1651 | TEST_LE_U(expected_public_key->len, |
| 1652 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
| 1653 | ASSERT_COMPARE(expected_public_key->x, expected_public_key->len, |
| 1654 | exported, exported_length); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1655 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1656 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1657 | /* |
| 1658 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1659 | * thus reset them as required. |
| 1660 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1661 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1662 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1663 | mbedtls_free(exported); |
| 1664 | psa_destroy_key(key); |
| 1665 | PSA_DONE(); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1666 | } |
| 1667 | /* END_CASE */ |
| 1668 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1669 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1670 | void import_and_exercise_key(data_t *data, |
| 1671 | int type_arg, |
| 1672 | int bits_arg, |
| 1673 | int alg_arg) |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1674 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1675 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1676 | psa_key_type_t type = type_arg; |
| 1677 | size_t bits = bits_arg; |
| 1678 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1679 | 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] | 1680 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1681 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1682 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1683 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1684 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1685 | psa_set_key_usage_flags(&attributes, usage); |
| 1686 | psa_set_key_algorithm(&attributes, alg); |
| 1687 | psa_set_key_type(&attributes, type); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1688 | |
| 1689 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1690 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1691 | |
| 1692 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1693 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1694 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1695 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1696 | |
| 1697 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1698 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1699 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1700 | } |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1701 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1702 | PSA_ASSERT(psa_destroy_key(key)); |
| 1703 | test_operations_on_invalid_key(key); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1704 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1705 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1706 | /* |
| 1707 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1708 | * thus reset them as required. |
| 1709 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1710 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1711 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1712 | psa_reset_key_attributes(&attributes); |
| 1713 | psa_destroy_key(key); |
| 1714 | PSA_DONE(); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1715 | } |
| 1716 | /* END_CASE */ |
| 1717 | |
| 1718 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1719 | void effective_key_attributes(int type_arg, int expected_type_arg, |
| 1720 | int bits_arg, int expected_bits_arg, |
| 1721 | int usage_arg, int expected_usage_arg, |
| 1722 | int alg_arg, int expected_alg_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1723 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1724 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1725 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1726 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1727 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1728 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1729 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1730 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1731 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1732 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1733 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1734 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1735 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1736 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1737 | psa_set_key_usage_flags(&attributes, usage); |
| 1738 | psa_set_key_algorithm(&attributes, alg); |
| 1739 | psa_set_key_type(&attributes, key_type); |
| 1740 | psa_set_key_bits(&attributes, bits); |
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_generate_key(&attributes, &key)); |
| 1743 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1744 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1745 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1746 | TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type); |
| 1747 | TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits); |
| 1748 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
| 1749 | TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1750 | |
| 1751 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1752 | /* |
| 1753 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1754 | * thus reset them as required. |
| 1755 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1756 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1757 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1758 | psa_destroy_key(key); |
| 1759 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1760 | } |
| 1761 | /* END_CASE */ |
| 1762 | |
| 1763 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1764 | void check_key_policy(int type_arg, int bits_arg, |
| 1765 | int usage_arg, int alg_arg) |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1766 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1767 | test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg, |
| 1768 | usage_arg, |
| 1769 | mbedtls_test_update_key_usage_flags(usage_arg), |
| 1770 | alg_arg, alg_arg); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1771 | goto exit; |
| 1772 | } |
| 1773 | /* END_CASE */ |
| 1774 | |
| 1775 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1776 | void key_attributes_init() |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1777 | { |
| 1778 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1779 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1780 | * 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] | 1781 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1782 | psa_key_attributes_t func = psa_key_attributes_init(); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1783 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1784 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1785 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1786 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1787 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1788 | TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE); |
| 1789 | TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE); |
| 1790 | TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1791 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1792 | TEST_EQUAL(psa_get_key_type(&func), 0); |
| 1793 | TEST_EQUAL(psa_get_key_type(&init), 0); |
| 1794 | TEST_EQUAL(psa_get_key_type(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1795 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1796 | TEST_EQUAL(psa_get_key_bits(&func), 0); |
| 1797 | TEST_EQUAL(psa_get_key_bits(&init), 0); |
| 1798 | TEST_EQUAL(psa_get_key_bits(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1799 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1800 | TEST_EQUAL(psa_get_key_usage_flags(&func), 0); |
| 1801 | TEST_EQUAL(psa_get_key_usage_flags(&init), 0); |
| 1802 | TEST_EQUAL(psa_get_key_usage_flags(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1803 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1804 | TEST_EQUAL(psa_get_key_algorithm(&func), 0); |
| 1805 | TEST_EQUAL(psa_get_key_algorithm(&init), 0); |
| 1806 | TEST_EQUAL(psa_get_key_algorithm(&zero), 0); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1807 | } |
| 1808 | /* END_CASE */ |
| 1809 | |
| 1810 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1811 | void mac_key_policy(int policy_usage_arg, |
| 1812 | int policy_alg_arg, |
| 1813 | int key_type_arg, |
| 1814 | data_t *key_data, |
| 1815 | int exercise_alg_arg, |
| 1816 | int expected_status_sign_arg, |
| 1817 | int expected_status_verify_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1818 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1819 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1820 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1821 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1822 | psa_key_type_t key_type = key_type_arg; |
| 1823 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 1824 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 1825 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1826 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1827 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 1828 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1829 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1830 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1831 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1832 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1833 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1834 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1835 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1836 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1837 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1838 | &key)); |
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 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 1841 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1842 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1843 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 1844 | TEST_EQUAL(status, expected_status_sign); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1845 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1846 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1847 | uint8_t input[128] = { 0 }; |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1848 | size_t mac_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1849 | TEST_EQUAL(psa_mac_compute(key, exercise_alg, |
| 1850 | input, 128, |
| 1851 | mac, PSA_MAC_MAX_SIZE, &mac_len), |
| 1852 | expected_status_sign); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1853 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1854 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1855 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 1856 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 1857 | if (status == PSA_SUCCESS) { |
| 1858 | status = psa_mac_update(&operation, input, 128); |
| 1859 | if (status == PSA_SUCCESS) { |
| 1860 | TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE, |
| 1861 | &mac_len), |
| 1862 | expected_status_sign); |
| 1863 | } else { |
| 1864 | TEST_EQUAL(status, expected_status_sign); |
| 1865 | } |
| 1866 | } else { |
| 1867 | TEST_EQUAL(status, expected_status_sign); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1868 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1869 | PSA_ASSERT(psa_mac_abort(&operation)); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1870 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1871 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1872 | status = psa_mac_verify(key, exercise_alg, input, 128, |
| 1873 | mac, mac_len); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1874 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1875 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 1876 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1877 | } else { |
| 1878 | TEST_EQUAL(status, expected_status_verify); |
| 1879 | } |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1880 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1881 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1882 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 1883 | if (status == PSA_SUCCESS) { |
| 1884 | status = psa_mac_update(&operation, input, 128); |
| 1885 | if (status == PSA_SUCCESS) { |
| 1886 | status = psa_mac_verify_finish(&operation, mac, mac_len); |
| 1887 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 1888 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1889 | } else { |
| 1890 | TEST_EQUAL(status, expected_status_verify); |
| 1891 | } |
| 1892 | } else { |
| 1893 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1894 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1895 | } else { |
| 1896 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1897 | } |
| 1898 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1899 | psa_mac_abort(&operation); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1900 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1901 | memset(mac, 0, sizeof(mac)); |
| 1902 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 1903 | TEST_EQUAL(status, expected_status_verify); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1904 | |
| 1905 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1906 | psa_mac_abort(&operation); |
| 1907 | psa_destroy_key(key); |
| 1908 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1909 | } |
| 1910 | /* END_CASE */ |
| 1911 | |
| 1912 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1913 | void cipher_key_policy(int policy_usage_arg, |
| 1914 | int policy_alg, |
| 1915 | int key_type, |
| 1916 | data_t *key_data, |
| 1917 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1918 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1919 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1920 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1921 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1922 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1923 | size_t output_buffer_size = 0; |
| 1924 | size_t input_buffer_size = 0; |
| 1925 | size_t output_length = 0; |
| 1926 | uint8_t *output = NULL; |
| 1927 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1928 | psa_status_t status; |
| 1929 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1930 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg); |
| 1931 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg, |
| 1932 | input_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1933 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1934 | ASSERT_ALLOC(input, input_buffer_size); |
| 1935 | ASSERT_ALLOC(output, output_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1936 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1937 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1938 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1939 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1940 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1941 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1942 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1943 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1944 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1945 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1946 | /* Check if no key usage flag implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1947 | TEST_EQUAL(policy_usage, |
| 1948 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1949 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1950 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1951 | status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size, |
| 1952 | output, output_buffer_size, |
| 1953 | &output_length); |
| 1954 | if (policy_alg == exercise_alg && |
| 1955 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1956 | PSA_ASSERT(status); |
| 1957 | } else { |
| 1958 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1959 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1960 | |
| 1961 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1962 | status = psa_cipher_encrypt_setup(&operation, key, exercise_alg); |
| 1963 | if (policy_alg == exercise_alg && |
| 1964 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1965 | PSA_ASSERT(status); |
| 1966 | } else { |
| 1967 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1968 | } |
| 1969 | psa_cipher_abort(&operation); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1970 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1971 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1972 | status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size, |
| 1973 | input, input_buffer_size, |
| 1974 | &output_length); |
| 1975 | if (policy_alg == exercise_alg && |
| 1976 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 1977 | PSA_ASSERT(status); |
| 1978 | } else { |
| 1979 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1980 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1981 | |
| 1982 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1983 | status = psa_cipher_decrypt_setup(&operation, key, exercise_alg); |
| 1984 | if (policy_alg == exercise_alg && |
| 1985 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 1986 | PSA_ASSERT(status); |
| 1987 | } else { |
| 1988 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1989 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1990 | |
| 1991 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1992 | psa_cipher_abort(&operation); |
| 1993 | mbedtls_free(input); |
| 1994 | mbedtls_free(output); |
| 1995 | psa_destroy_key(key); |
| 1996 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1997 | } |
| 1998 | /* END_CASE */ |
| 1999 | |
| 2000 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2001 | void aead_key_policy(int policy_usage_arg, |
| 2002 | int policy_alg, |
| 2003 | int key_type, |
| 2004 | data_t *key_data, |
| 2005 | int nonce_length_arg, |
| 2006 | int tag_length_arg, |
| 2007 | int exercise_alg, |
| 2008 | int expected_status_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2009 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2010 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2011 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2012 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2013 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2014 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2015 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2016 | unsigned char nonce[16] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2017 | size_t nonce_length = nonce_length_arg; |
| 2018 | unsigned char tag[16]; |
| 2019 | size_t tag_length = tag_length_arg; |
| 2020 | size_t output_length; |
| 2021 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2022 | TEST_LE_U(nonce_length, sizeof(nonce)); |
| 2023 | TEST_LE_U(tag_length, sizeof(tag)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2024 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2025 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2026 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2027 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2028 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2029 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2030 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2031 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2032 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2033 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2034 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2035 | TEST_EQUAL(policy_usage, |
| 2036 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2037 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2038 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2039 | status = psa_aead_encrypt(key, exercise_alg, |
| 2040 | nonce, nonce_length, |
| 2041 | NULL, 0, |
| 2042 | NULL, 0, |
| 2043 | tag, tag_length, |
| 2044 | &output_length); |
| 2045 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2046 | TEST_EQUAL(status, expected_status); |
| 2047 | } else { |
| 2048 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2049 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2050 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2051 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2052 | status = psa_aead_encrypt_setup(&operation, key, exercise_alg); |
| 2053 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2054 | TEST_EQUAL(status, expected_status); |
| 2055 | } else { |
| 2056 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2057 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2058 | |
| 2059 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2060 | memset(tag, 0, sizeof(tag)); |
| 2061 | status = psa_aead_decrypt(key, exercise_alg, |
| 2062 | nonce, nonce_length, |
| 2063 | NULL, 0, |
| 2064 | tag, tag_length, |
| 2065 | NULL, 0, |
| 2066 | &output_length); |
| 2067 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2068 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2069 | } else if (expected_status == PSA_SUCCESS) { |
| 2070 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2071 | } else { |
| 2072 | TEST_EQUAL(status, expected_status); |
| 2073 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2074 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2075 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2076 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2077 | status = psa_aead_decrypt_setup(&operation, key, exercise_alg); |
| 2078 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2079 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2080 | } else { |
| 2081 | TEST_EQUAL(status, expected_status); |
| 2082 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2083 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2084 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2085 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2086 | psa_destroy_key(key); |
| 2087 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2088 | } |
| 2089 | /* END_CASE */ |
| 2090 | |
| 2091 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2092 | void asymmetric_encryption_key_policy(int policy_usage_arg, |
| 2093 | int policy_alg, |
| 2094 | int key_type, |
| 2095 | data_t *key_data, |
| 2096 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2097 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2098 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2099 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2100 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2101 | psa_status_t status; |
| 2102 | size_t key_bits; |
| 2103 | size_t buffer_length; |
| 2104 | unsigned char *buffer = NULL; |
| 2105 | size_t output_length; |
| 2106 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2107 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2108 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2109 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2110 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2111 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2112 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2113 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2114 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2115 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2116 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2117 | TEST_EQUAL(policy_usage, |
| 2118 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2119 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2120 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 2121 | key_bits = psa_get_key_bits(&attributes); |
| 2122 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, |
| 2123 | exercise_alg); |
| 2124 | ASSERT_ALLOC(buffer, buffer_length); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2125 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2126 | status = psa_asymmetric_encrypt(key, exercise_alg, |
| 2127 | NULL, 0, |
| 2128 | NULL, 0, |
| 2129 | buffer, buffer_length, |
| 2130 | &output_length); |
| 2131 | if (policy_alg == exercise_alg && |
| 2132 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2133 | PSA_ASSERT(status); |
| 2134 | } else { |
| 2135 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2136 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2137 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2138 | if (buffer_length != 0) { |
| 2139 | memset(buffer, 0, buffer_length); |
| 2140 | } |
| 2141 | status = psa_asymmetric_decrypt(key, exercise_alg, |
| 2142 | buffer, buffer_length, |
| 2143 | NULL, 0, |
| 2144 | buffer, buffer_length, |
| 2145 | &output_length); |
| 2146 | if (policy_alg == exercise_alg && |
| 2147 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2148 | TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING); |
| 2149 | } else { |
| 2150 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2151 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2152 | |
| 2153 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2154 | /* |
| 2155 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2156 | * thus reset them as required. |
| 2157 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2158 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2159 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2160 | psa_destroy_key(key); |
| 2161 | PSA_DONE(); |
| 2162 | mbedtls_free(buffer); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2163 | } |
| 2164 | /* END_CASE */ |
| 2165 | |
| 2166 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2167 | void asymmetric_signature_key_policy(int policy_usage_arg, |
| 2168 | int policy_alg, |
| 2169 | int key_type, |
| 2170 | data_t *key_data, |
| 2171 | int exercise_alg, |
| 2172 | int payload_length_arg, |
| 2173 | int expected_usage_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2174 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2175 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2176 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2177 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 2178 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2179 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2180 | unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 }; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2181 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2182 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2183 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2184 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2185 | int compatible_alg = payload_length_arg > 0; |
| 2186 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2187 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2188 | size_t signature_length; |
| 2189 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2190 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2191 | in the expected usage flags. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2192 | TEST_EQUAL(expected_usage, |
| 2193 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2194 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2195 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2196 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2197 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2198 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2199 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2200 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2201 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2202 | &key)); |
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 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2205 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2206 | status = psa_sign_hash(key, exercise_alg, |
| 2207 | payload, payload_length, |
| 2208 | signature, sizeof(signature), |
| 2209 | &signature_length); |
| 2210 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) { |
| 2211 | PSA_ASSERT(status); |
| 2212 | } else { |
| 2213 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2214 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2215 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2216 | memset(signature, 0, sizeof(signature)); |
| 2217 | status = psa_verify_hash(key, exercise_alg, |
| 2218 | payload, payload_length, |
| 2219 | signature, sizeof(signature)); |
| 2220 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) { |
| 2221 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2222 | } else { |
| 2223 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2224 | } |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2225 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2226 | if (PSA_ALG_IS_SIGN_HASH(exercise_alg) && |
| 2227 | PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) { |
| 2228 | status = psa_sign_message(key, exercise_alg, |
| 2229 | payload, payload_length, |
| 2230 | signature, sizeof(signature), |
| 2231 | &signature_length); |
| 2232 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) { |
| 2233 | PSA_ASSERT(status); |
| 2234 | } else { |
| 2235 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2236 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2237 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2238 | memset(signature, 0, sizeof(signature)); |
| 2239 | status = psa_verify_message(key, exercise_alg, |
| 2240 | payload, payload_length, |
| 2241 | signature, sizeof(signature)); |
| 2242 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) { |
| 2243 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2244 | } else { |
| 2245 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2246 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2247 | } |
| 2248 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2249 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2250 | psa_destroy_key(key); |
| 2251 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2252 | } |
| 2253 | /* END_CASE */ |
| 2254 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2255 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2256 | void derive_key_policy(int policy_usage, |
| 2257 | int policy_alg, |
| 2258 | int key_type, |
| 2259 | data_t *key_data, |
| 2260 | int exercise_alg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2261 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2262 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2263 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2264 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2265 | psa_status_t status; |
| 2266 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2267 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2268 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2269 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2270 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2271 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2272 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2273 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2274 | &key)); |
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_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2277 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2278 | if (PSA_ALG_IS_TLS12_PRF(exercise_alg) || |
| 2279 | PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) { |
| 2280 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 2281 | &operation, |
| 2282 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2283 | (const uint8_t *) "", 0)); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2284 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2285 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2286 | status = psa_key_derivation_input_key(&operation, |
| 2287 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 2288 | key); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2289 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2290 | if (policy_alg == exercise_alg && |
| 2291 | (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) { |
| 2292 | PSA_ASSERT(status); |
| 2293 | } else { |
| 2294 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2295 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2296 | |
| 2297 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2298 | psa_key_derivation_abort(&operation); |
| 2299 | psa_destroy_key(key); |
| 2300 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2301 | } |
| 2302 | /* END_CASE */ |
| 2303 | |
| 2304 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2305 | void agreement_key_policy(int policy_usage, |
| 2306 | int policy_alg, |
| 2307 | int key_type_arg, |
| 2308 | data_t *key_data, |
| 2309 | int exercise_alg, |
| 2310 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2311 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2312 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2313 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2314 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2315 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2316 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2317 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2318 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2319 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2320 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2321 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2322 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2323 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2324 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2325 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2326 | &key)); |
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_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
| 2329 | status = mbedtls_test_psa_key_agreement_with_self(&operation, key); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2330 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2331 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2332 | |
| 2333 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2334 | psa_key_derivation_abort(&operation); |
| 2335 | psa_destroy_key(key); |
| 2336 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2337 | } |
| 2338 | /* END_CASE */ |
| 2339 | |
| 2340 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2341 | void key_policy_alg2(int key_type_arg, data_t *key_data, |
| 2342 | int usage_arg, int alg_arg, int alg2_arg) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2343 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2344 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2345 | psa_key_type_t key_type = key_type_arg; |
| 2346 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2347 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2348 | psa_key_usage_t usage = usage_arg; |
| 2349 | psa_algorithm_t alg = alg_arg; |
| 2350 | psa_algorithm_t alg2 = alg2_arg; |
| 2351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2352 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2354 | psa_set_key_usage_flags(&attributes, usage); |
| 2355 | psa_set_key_algorithm(&attributes, alg); |
| 2356 | psa_set_key_enrollment_algorithm(&attributes, alg2); |
| 2357 | psa_set_key_type(&attributes, key_type); |
| 2358 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2359 | &key)); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2360 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2361 | /* Update the usage flags to obtain implicit usage flags */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2362 | usage = mbedtls_test_update_key_usage_flags(usage); |
| 2363 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 2364 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage); |
| 2365 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 2366 | TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2367 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2368 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2369 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2370 | } |
| 2371 | if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2372 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2373 | } |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2374 | |
| 2375 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2376 | /* |
| 2377 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2378 | * thus reset them as required. |
| 2379 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2380 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2381 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2382 | psa_destroy_key(key); |
| 2383 | PSA_DONE(); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2384 | } |
| 2385 | /* END_CASE */ |
| 2386 | |
| 2387 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2388 | void raw_agreement_key_policy(int policy_usage, |
| 2389 | int policy_alg, |
| 2390 | int key_type_arg, |
| 2391 | data_t *key_data, |
| 2392 | int exercise_alg, |
| 2393 | int expected_status_arg) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2394 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2395 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2396 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2397 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2398 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2399 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2400 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2401 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2402 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2403 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2404 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2405 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2406 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2407 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2408 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2409 | &key)); |
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 | status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2412 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2413 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2414 | |
| 2415 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2416 | psa_key_derivation_abort(&operation); |
| 2417 | psa_destroy_key(key); |
| 2418 | PSA_DONE(); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2419 | } |
| 2420 | /* END_CASE */ |
| 2421 | |
| 2422 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2423 | void copy_success(int source_usage_arg, |
| 2424 | int source_alg_arg, int source_alg2_arg, |
| 2425 | unsigned int source_lifetime_arg, |
| 2426 | int type_arg, data_t *material, |
| 2427 | int copy_attributes, |
| 2428 | int target_usage_arg, |
| 2429 | int target_alg_arg, int target_alg2_arg, |
| 2430 | unsigned int target_lifetime_arg, |
| 2431 | int expected_usage_arg, |
| 2432 | int expected_alg_arg, int expected_alg2_arg) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2433 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2434 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2435 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2436 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2437 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2438 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2439 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 2440 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2441 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2442 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2443 | uint8_t *export_buffer = NULL; |
| 2444 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2445 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2446 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2447 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2448 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2449 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2450 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2451 | psa_set_key_type(&source_attributes, type_arg); |
| 2452 | psa_set_key_lifetime(&source_attributes, source_lifetime); |
| 2453 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2454 | material->x, material->len, |
| 2455 | &source_key)); |
| 2456 | PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2457 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2458 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2459 | if (copy_attributes) { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2460 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2461 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2462 | psa_set_key_lifetime(&target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2463 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2464 | if (target_usage_arg != -1) { |
| 2465 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2466 | } |
| 2467 | if (target_alg_arg != -1) { |
| 2468 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2469 | } |
| 2470 | if (target_alg2_arg != -1) { |
| 2471 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
| 2472 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2473 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2474 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2475 | /* Copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2476 | PSA_ASSERT(psa_copy_key(source_key, |
| 2477 | &target_attributes, &target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2478 | |
| 2479 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2480 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2481 | |
| 2482 | /* Test that the target slot has the expected content and policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2483 | PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes)); |
| 2484 | TEST_EQUAL(psa_get_key_type(&source_attributes), |
| 2485 | psa_get_key_type(&target_attributes)); |
| 2486 | TEST_EQUAL(psa_get_key_bits(&source_attributes), |
| 2487 | psa_get_key_bits(&target_attributes)); |
| 2488 | TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes)); |
| 2489 | TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes)); |
| 2490 | TEST_EQUAL(expected_alg2, |
| 2491 | psa_get_key_enrollment_algorithm(&target_attributes)); |
| 2492 | if (expected_usage & PSA_KEY_USAGE_EXPORT) { |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2493 | size_t length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2494 | ASSERT_ALLOC(export_buffer, material->len); |
| 2495 | PSA_ASSERT(psa_export_key(target_key, export_buffer, |
| 2496 | material->len, &length)); |
| 2497 | ASSERT_COMPARE(material->x, material->len, |
| 2498 | export_buffer, length); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2499 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2500 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2501 | if (!psa_key_lifetime_is_external(target_lifetime)) { |
| 2502 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2503 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2504 | } |
| 2505 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2506 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2507 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2508 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2509 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2510 | PSA_ASSERT(psa_destroy_key(target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2511 | |
| 2512 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2513 | /* |
| 2514 | * Source and target key attributes may have been returned by |
| 2515 | * psa_get_key_attributes() thus reset them as required. |
| 2516 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2517 | psa_reset_key_attributes(&source_attributes); |
| 2518 | psa_reset_key_attributes(&target_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2519 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2520 | PSA_DONE(); |
| 2521 | mbedtls_free(export_buffer); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2522 | } |
| 2523 | /* END_CASE */ |
| 2524 | |
| 2525 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2526 | void copy_fail(int source_usage_arg, |
| 2527 | int source_alg_arg, int source_alg2_arg, |
| 2528 | int source_lifetime_arg, |
| 2529 | int type_arg, data_t *material, |
| 2530 | int target_type_arg, int target_bits_arg, |
| 2531 | int target_usage_arg, |
| 2532 | int target_alg_arg, int target_alg2_arg, |
| 2533 | int target_id_arg, int target_lifetime_arg, |
| 2534 | int expected_status_arg) |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2535 | { |
| 2536 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2537 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2538 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2539 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2540 | 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] | 2541 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2542 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2543 | |
| 2544 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2545 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2546 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2547 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2548 | psa_set_key_type(&source_attributes, type_arg); |
| 2549 | psa_set_key_lifetime(&source_attributes, source_lifetime_arg); |
| 2550 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2551 | material->x, material->len, |
| 2552 | &source_key)); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2553 | |
| 2554 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2555 | psa_set_key_id(&target_attributes, key_id); |
| 2556 | psa_set_key_lifetime(&target_attributes, target_lifetime_arg); |
| 2557 | psa_set_key_type(&target_attributes, target_type_arg); |
| 2558 | psa_set_key_bits(&target_attributes, target_bits_arg); |
| 2559 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2560 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2561 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2562 | |
| 2563 | /* Try to copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2564 | TEST_EQUAL(psa_copy_key(source_key, |
| 2565 | &target_attributes, &target_key), |
| 2566 | expected_status_arg); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2567 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2568 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2569 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2570 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2571 | psa_reset_key_attributes(&source_attributes); |
| 2572 | psa_reset_key_attributes(&target_attributes); |
| 2573 | PSA_DONE(); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2574 | } |
| 2575 | /* END_CASE */ |
| 2576 | |
| 2577 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2578 | void hash_operation_init() |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2579 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2580 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2581 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2582 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2583 | * 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] | 2584 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2585 | psa_hash_operation_t func = psa_hash_operation_init(); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2586 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2587 | psa_hash_operation_t zero; |
| 2588 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2589 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2590 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2591 | /* A freshly-initialized hash operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2592 | TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)), |
| 2593 | PSA_ERROR_BAD_STATE); |
| 2594 | TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)), |
| 2595 | PSA_ERROR_BAD_STATE); |
| 2596 | TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)), |
| 2597 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2598 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2599 | /* A default hash operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2600 | PSA_ASSERT(psa_hash_abort(&func)); |
| 2601 | PSA_ASSERT(psa_hash_abort(&init)); |
| 2602 | PSA_ASSERT(psa_hash_abort(&zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2603 | } |
| 2604 | /* END_CASE */ |
| 2605 | |
| 2606 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2607 | void hash_setup(int alg_arg, |
| 2608 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2609 | { |
| 2610 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2611 | uint8_t *output = NULL; |
| 2612 | size_t output_size = 0; |
| 2613 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2614 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2615 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2616 | psa_status_t status; |
| 2617 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2618 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2619 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2620 | /* Hash Setup, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2621 | output_size = PSA_HASH_LENGTH(alg); |
| 2622 | ASSERT_ALLOC(output, output_size); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2623 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2624 | status = psa_hash_compute(alg, NULL, 0, |
| 2625 | output, output_size, &output_length); |
| 2626 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2627 | |
| 2628 | /* Hash Setup, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2629 | status = psa_hash_setup(&operation, alg); |
| 2630 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2631 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2632 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2633 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2634 | |
| 2635 | /* If setup failed, reproduce the failure, so as to |
| 2636 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2637 | if (status != PSA_SUCCESS) { |
| 2638 | TEST_EQUAL(psa_hash_setup(&operation, alg), status); |
| 2639 | } |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2640 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2641 | /* Now the operation object should be reusable. */ |
| 2642 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2643 | PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG)); |
| 2644 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2645 | #endif |
| 2646 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2647 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2648 | mbedtls_free(output); |
| 2649 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2650 | } |
| 2651 | /* END_CASE */ |
| 2652 | |
| 2653 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2654 | void hash_compute_fail(int alg_arg, data_t *input, |
| 2655 | int output_size_arg, int expected_status_arg) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2656 | { |
| 2657 | psa_algorithm_t alg = alg_arg; |
| 2658 | uint8_t *output = NULL; |
| 2659 | size_t output_size = output_size_arg; |
| 2660 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2661 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2662 | psa_status_t expected_status = expected_status_arg; |
| 2663 | psa_status_t status; |
| 2664 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2665 | ASSERT_ALLOC(output, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2666 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2667 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2668 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2669 | /* Hash Compute, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2670 | status = psa_hash_compute(alg, input->x, input->len, |
| 2671 | output, output_size, &output_length); |
| 2672 | TEST_EQUAL(status, expected_status); |
| 2673 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2674 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2675 | /* Hash Compute, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2676 | status = psa_hash_setup(&operation, alg); |
| 2677 | if (status == PSA_SUCCESS) { |
| 2678 | status = psa_hash_update(&operation, input->x, input->len); |
| 2679 | if (status == PSA_SUCCESS) { |
| 2680 | status = psa_hash_finish(&operation, output, output_size, |
| 2681 | &output_length); |
| 2682 | if (status == PSA_SUCCESS) { |
| 2683 | TEST_LE_U(output_length, output_size); |
| 2684 | } else { |
| 2685 | TEST_EQUAL(status, expected_status); |
| 2686 | } |
| 2687 | } else { |
| 2688 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2689 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2690 | } else { |
| 2691 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2692 | } |
| 2693 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2694 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2695 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2696 | mbedtls_free(output); |
| 2697 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2698 | } |
| 2699 | /* END_CASE */ |
| 2700 | |
| 2701 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2702 | void hash_compare_fail(int alg_arg, data_t *input, |
| 2703 | data_t *reference_hash, |
| 2704 | int expected_status_arg) |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2705 | { |
| 2706 | psa_algorithm_t alg = alg_arg; |
| 2707 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2708 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2709 | psa_status_t status; |
| 2710 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2711 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2712 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2713 | /* Hash Compare, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2714 | status = psa_hash_compare(alg, input->x, input->len, |
| 2715 | reference_hash->x, reference_hash->len); |
| 2716 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2717 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2718 | /* Hash Compare, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2719 | status = psa_hash_setup(&operation, alg); |
| 2720 | if (status == PSA_SUCCESS) { |
| 2721 | status = psa_hash_update(&operation, input->x, input->len); |
| 2722 | if (status == PSA_SUCCESS) { |
| 2723 | status = psa_hash_verify(&operation, reference_hash->x, |
| 2724 | reference_hash->len); |
| 2725 | TEST_EQUAL(status, expected_status); |
| 2726 | } else { |
| 2727 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2728 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2729 | } else { |
| 2730 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2731 | } |
| 2732 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2733 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2734 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2735 | PSA_DONE(); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2736 | } |
| 2737 | /* END_CASE */ |
| 2738 | |
| 2739 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2740 | void hash_compute_compare(int alg_arg, data_t *input, |
| 2741 | data_t *expected_output) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2742 | { |
| 2743 | psa_algorithm_t alg = alg_arg; |
| 2744 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2745 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2746 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2747 | size_t i; |
| 2748 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2749 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2750 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2751 | /* Compute with tight buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2752 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 2753 | output, PSA_HASH_LENGTH(alg), |
| 2754 | &output_length)); |
| 2755 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2756 | ASSERT_COMPARE(output, output_length, |
| 2757 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2758 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2759 | /* Compute with tight buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2760 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2761 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2762 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 2763 | PSA_HASH_LENGTH(alg), |
| 2764 | &output_length)); |
| 2765 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2766 | ASSERT_COMPARE(output, output_length, |
| 2767 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2768 | |
| 2769 | /* Compute with larger buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2770 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 2771 | output, sizeof(output), |
| 2772 | &output_length)); |
| 2773 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2774 | ASSERT_COMPARE(output, output_length, |
| 2775 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2776 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2777 | /* Compute with larger buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2778 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2779 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2780 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 2781 | sizeof(output), &output_length)); |
| 2782 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2783 | ASSERT_COMPARE(output, output_length, |
| 2784 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2785 | |
| 2786 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2787 | PSA_ASSERT(psa_hash_compare(alg, input->x, input->len, |
| 2788 | output, output_length)); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2789 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2790 | /* Compare with correct hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2791 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2792 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2793 | PSA_ASSERT(psa_hash_verify(&operation, output, |
| 2794 | output_length)); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2795 | |
| 2796 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2797 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2798 | output, output_length + 1), |
| 2799 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2800 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2801 | /* Compare with trailing garbage, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2802 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2803 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2804 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1), |
| 2805 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2806 | |
| 2807 | /* Compare with truncated hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2808 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2809 | output, output_length - 1), |
| 2810 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2811 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2812 | /* Compare with truncated hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2813 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2814 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2815 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1), |
| 2816 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2817 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2818 | /* Compare with corrupted value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2819 | for (i = 0; i < output_length; i++) { |
| 2820 | mbedtls_test_set_step(i); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2821 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2822 | |
| 2823 | /* One-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2824 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2825 | output, output_length), |
| 2826 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2827 | |
| 2828 | /* Multi-Part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2829 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2830 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2831 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length), |
| 2832 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2833 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2834 | output[i] ^= 1; |
| 2835 | } |
| 2836 | |
| 2837 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2838 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2839 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2840 | } |
| 2841 | /* END_CASE */ |
| 2842 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2843 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2844 | void hash_bad_order() |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2845 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2846 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2847 | unsigned char input[] = ""; |
| 2848 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2849 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2850 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2851 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2852 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 |
| 2853 | }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2854 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2855 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2856 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2857 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2858 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2859 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2860 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2861 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2862 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2863 | TEST_EQUAL(psa_hash_setup(&operation, alg), |
| 2864 | PSA_ERROR_BAD_STATE); |
| 2865 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2866 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2867 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2868 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2869 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2870 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2871 | PSA_ERROR_BAD_STATE); |
| 2872 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2873 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2874 | /* Check that update calls abort on error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2875 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 2876 | operation.id = UINT_MAX; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2877 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2878 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2879 | PSA_ERROR_BAD_STATE); |
| 2880 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2881 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2882 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2883 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2884 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2885 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2886 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2887 | hash, sizeof(hash), &hash_len)); |
| 2888 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2889 | PSA_ERROR_BAD_STATE); |
| 2890 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2891 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2892 | /* Call verify without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2893 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2894 | valid_hash, sizeof(valid_hash)), |
| 2895 | PSA_ERROR_BAD_STATE); |
| 2896 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2897 | |
| 2898 | /* Call verify after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2899 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2900 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2901 | hash, sizeof(hash), &hash_len)); |
| 2902 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2903 | valid_hash, sizeof(valid_hash)), |
| 2904 | PSA_ERROR_BAD_STATE); |
| 2905 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2906 | |
| 2907 | /* Call verify twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2908 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2909 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2910 | PSA_ASSERT(psa_hash_verify(&operation, |
| 2911 | valid_hash, sizeof(valid_hash))); |
| 2912 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2913 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2914 | valid_hash, sizeof(valid_hash)), |
| 2915 | PSA_ERROR_BAD_STATE); |
| 2916 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2917 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2918 | |
| 2919 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2920 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2921 | hash, sizeof(hash), &hash_len), |
| 2922 | PSA_ERROR_BAD_STATE); |
| 2923 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2924 | |
| 2925 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2926 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2927 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2928 | hash, sizeof(hash), &hash_len)); |
| 2929 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2930 | hash, sizeof(hash), &hash_len), |
| 2931 | PSA_ERROR_BAD_STATE); |
| 2932 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2933 | |
| 2934 | /* Call finish after calling verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2935 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2936 | PSA_ASSERT(psa_hash_verify(&operation, |
| 2937 | valid_hash, sizeof(valid_hash))); |
| 2938 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2939 | hash, sizeof(hash), &hash_len), |
| 2940 | PSA_ERROR_BAD_STATE); |
| 2941 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2942 | |
| 2943 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2944 | PSA_DONE(); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2945 | } |
| 2946 | /* END_CASE */ |
| 2947 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2948 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2949 | void hash_verify_bad_args() |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2950 | { |
| 2951 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2952 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2953 | * appended to it */ |
| 2954 | unsigned char hash[] = { |
| 2955 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2956 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2957 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb |
| 2958 | }; |
| 2959 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2960 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2961 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2962 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2963 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2964 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2965 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2966 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2967 | TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1), |
| 2968 | PSA_ERROR_INVALID_SIGNATURE); |
| 2969 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2970 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2971 | ASSERT_OPERATION_IS_INACTIVE(operation); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2972 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2973 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2974 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2975 | TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size), |
| 2976 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2977 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2978 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2979 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2980 | TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)), |
| 2981 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2982 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2983 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2984 | PSA_DONE(); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2985 | } |
| 2986 | /* END_CASE */ |
| 2987 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2988 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2989 | void hash_finish_bad_args() |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2990 | { |
| 2991 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2992 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2993 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2994 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2995 | size_t hash_len; |
| 2996 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2997 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2998 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2999 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3000 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3001 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3002 | hash, expected_size - 1, &hash_len), |
| 3003 | PSA_ERROR_BUFFER_TOO_SMALL); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3004 | |
| 3005 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3006 | PSA_DONE(); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3007 | } |
| 3008 | /* END_CASE */ |
| 3009 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3010 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3011 | void hash_clone_source_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3012 | { |
| 3013 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3014 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3015 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 3016 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3017 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3018 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3019 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3020 | size_t hash_len; |
| 3021 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3022 | PSA_ASSERT(psa_crypto_init()); |
| 3023 | PSA_ASSERT(psa_hash_setup(&op_source, alg)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3024 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3025 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3026 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3027 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3028 | hash, sizeof(hash), &hash_len)); |
| 3029 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3030 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
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 | TEST_EQUAL(psa_hash_clone(&op_source, &op_setup), |
| 3033 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3035 | PSA_ASSERT(psa_hash_clone(&op_source, &op_init)); |
| 3036 | PSA_ASSERT(psa_hash_finish(&op_init, |
| 3037 | hash, sizeof(hash), &hash_len)); |
| 3038 | PSA_ASSERT(psa_hash_clone(&op_source, &op_finished)); |
| 3039 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3040 | hash, sizeof(hash), &hash_len)); |
| 3041 | PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted)); |
| 3042 | PSA_ASSERT(psa_hash_finish(&op_aborted, |
| 3043 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3044 | |
| 3045 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3046 | psa_hash_abort(&op_source); |
| 3047 | psa_hash_abort(&op_init); |
| 3048 | psa_hash_abort(&op_setup); |
| 3049 | psa_hash_abort(&op_finished); |
| 3050 | psa_hash_abort(&op_aborted); |
| 3051 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3052 | } |
| 3053 | /* END_CASE */ |
| 3054 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3055 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3056 | void hash_clone_target_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3057 | { |
| 3058 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3059 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3060 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3061 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3062 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3063 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3064 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 3065 | size_t hash_len; |
| 3066 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3067 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3068 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3069 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3070 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3071 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3072 | hash, sizeof(hash), &hash_len)); |
| 3073 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3074 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
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_clone(&op_setup, &op_target)); |
| 3077 | PSA_ASSERT(psa_hash_finish(&op_target, |
| 3078 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3079 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3080 | TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE); |
| 3081 | TEST_EQUAL(psa_hash_clone(&op_finished, &op_target), |
| 3082 | PSA_ERROR_BAD_STATE); |
| 3083 | TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target), |
| 3084 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3085 | |
| 3086 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3087 | psa_hash_abort(&op_target); |
| 3088 | psa_hash_abort(&op_init); |
| 3089 | psa_hash_abort(&op_setup); |
| 3090 | psa_hash_abort(&op_finished); |
| 3091 | psa_hash_abort(&op_aborted); |
| 3092 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3093 | } |
| 3094 | /* END_CASE */ |
| 3095 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3096 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3097 | void mac_operation_init() |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3098 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3099 | const uint8_t input[1] = { 0 }; |
| 3100 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3101 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3102 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3103 | * 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] | 3104 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3105 | psa_mac_operation_t func = psa_mac_operation_init(); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3106 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 3107 | psa_mac_operation_t zero; |
| 3108 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3109 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3110 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3111 | /* A freshly-initialized MAC operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3112 | TEST_EQUAL(psa_mac_update(&func, |
| 3113 | input, sizeof(input)), |
| 3114 | PSA_ERROR_BAD_STATE); |
| 3115 | TEST_EQUAL(psa_mac_update(&init, |
| 3116 | input, sizeof(input)), |
| 3117 | PSA_ERROR_BAD_STATE); |
| 3118 | TEST_EQUAL(psa_mac_update(&zero, |
| 3119 | input, sizeof(input)), |
| 3120 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3121 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3122 | /* A default MAC operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3123 | PSA_ASSERT(psa_mac_abort(&func)); |
| 3124 | PSA_ASSERT(psa_mac_abort(&init)); |
| 3125 | PSA_ASSERT(psa_mac_abort(&zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3126 | } |
| 3127 | /* END_CASE */ |
| 3128 | |
| 3129 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3130 | void mac_setup(int key_type_arg, |
| 3131 | data_t *key, |
| 3132 | int alg_arg, |
| 3133 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3134 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3135 | psa_key_type_t key_type = key_type_arg; |
| 3136 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3137 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3138 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3139 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3140 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3141 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3142 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3143 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3144 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3145 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3146 | if (!exercise_mac_setup(key_type, key->x, key->len, alg, |
| 3147 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3148 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3149 | } |
| 3150 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3151 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3152 | /* The operation object should be reusable. */ |
| 3153 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3154 | if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 3155 | smoke_test_key_data, |
| 3156 | sizeof(smoke_test_key_data), |
| 3157 | KNOWN_SUPPORTED_MAC_ALG, |
| 3158 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3159 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3160 | } |
| 3161 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3162 | #endif |
| 3163 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3164 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3165 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3166 | } |
| 3167 | /* END_CASE */ |
| 3168 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3169 | /* 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] | 3170 | void mac_bad_order() |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3171 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3172 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3173 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 3174 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3175 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3176 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3177 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3178 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa |
| 3179 | }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3180 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3181 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3182 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 3183 | size_t sign_mac_length = 0; |
| 3184 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3185 | const uint8_t verify_mac[] = { |
| 3186 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 3187 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3188 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 |
| 3189 | }; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3190 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3191 | PSA_ASSERT(psa_crypto_init()); |
| 3192 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 3193 | psa_set_key_algorithm(&attributes, alg); |
| 3194 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3195 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3196 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3197 | &key)); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3198 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3199 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3200 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3201 | PSA_ERROR_BAD_STATE); |
| 3202 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3203 | |
| 3204 | /* Call sign finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3205 | TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac), |
| 3206 | &sign_mac_length), |
| 3207 | PSA_ERROR_BAD_STATE); |
| 3208 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3209 | |
| 3210 | /* Call verify finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3211 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3212 | verify_mac, sizeof(verify_mac)), |
| 3213 | PSA_ERROR_BAD_STATE); |
| 3214 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3215 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3216 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3217 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3218 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3219 | TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg), |
| 3220 | PSA_ERROR_BAD_STATE); |
| 3221 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3222 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3223 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3224 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3225 | /* Call update after sign finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3226 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3227 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3228 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3229 | sign_mac, sizeof(sign_mac), |
| 3230 | &sign_mac_length)); |
| 3231 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3232 | PSA_ERROR_BAD_STATE); |
| 3233 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3234 | |
| 3235 | /* Call update after verify finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3236 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3237 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3238 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3239 | verify_mac, sizeof(verify_mac))); |
| 3240 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3241 | PSA_ERROR_BAD_STATE); |
| 3242 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3243 | |
| 3244 | /* Call sign finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3245 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3246 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3247 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3248 | sign_mac, sizeof(sign_mac), |
| 3249 | &sign_mac_length)); |
| 3250 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3251 | sign_mac, sizeof(sign_mac), |
| 3252 | &sign_mac_length), |
| 3253 | PSA_ERROR_BAD_STATE); |
| 3254 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3255 | |
| 3256 | /* Call verify finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3257 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3258 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3259 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3260 | verify_mac, sizeof(verify_mac))); |
| 3261 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3262 | verify_mac, sizeof(verify_mac)), |
| 3263 | PSA_ERROR_BAD_STATE); |
| 3264 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3265 | |
| 3266 | /* Setup sign but try verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3267 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3268 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3269 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3270 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3271 | verify_mac, sizeof(verify_mac)), |
| 3272 | PSA_ERROR_BAD_STATE); |
| 3273 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3274 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3275 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3276 | |
| 3277 | /* Setup verify but try sign. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3278 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3279 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3280 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3281 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3282 | sign_mac, sizeof(sign_mac), |
| 3283 | &sign_mac_length), |
| 3284 | PSA_ERROR_BAD_STATE); |
| 3285 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3286 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3287 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3288 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3289 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3290 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3291 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3292 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3293 | } |
| 3294 | /* END_CASE */ |
| 3295 | |
| 3296 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3297 | void mac_sign_verify_multi(int key_type_arg, |
| 3298 | data_t *key_data, |
| 3299 | int alg_arg, |
| 3300 | data_t *input, |
| 3301 | int is_verify, |
| 3302 | data_t *expected_mac) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3303 | { |
| 3304 | size_t data_part_len = 0; |
| 3305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3306 | 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] | 3307 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3308 | mbedtls_test_set_step(2000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3309 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3310 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3311 | alg_arg, |
| 3312 | input, data_part_len, |
| 3313 | expected_mac, |
| 3314 | is_verify, 0) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3315 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3316 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3317 | |
| 3318 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3319 | mbedtls_test_set_step(3000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3320 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3321 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3322 | alg_arg, |
| 3323 | input, data_part_len, |
| 3324 | expected_mac, |
| 3325 | is_verify, 1) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3326 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3327 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3328 | } |
| 3329 | |
| 3330 | /* Goto is required to silence warnings about unused labels, as we |
| 3331 | * don't actually do any test assertions in this function. */ |
| 3332 | goto exit; |
| 3333 | } |
| 3334 | /* END_CASE */ |
| 3335 | |
| 3336 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3337 | void mac_sign(int key_type_arg, |
| 3338 | data_t *key_data, |
| 3339 | int alg_arg, |
| 3340 | data_t *input, |
| 3341 | data_t *expected_mac) |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3342 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3343 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3344 | psa_key_type_t key_type = key_type_arg; |
| 3345 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3346 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3347 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3348 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3349 | size_t mac_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3350 | 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] | 3351 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3352 | const size_t output_sizes_to_test[] = { |
| 3353 | 0, |
| 3354 | 1, |
| 3355 | expected_mac->len - 1, |
| 3356 | expected_mac->len, |
| 3357 | expected_mac->len + 1, |
| 3358 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3359 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3360 | TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3361 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3362 | TEST_ASSERT(expected_mac->len == mac_buffer_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3363 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3364 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3365 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3366 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 3367 | psa_set_key_algorithm(&attributes, alg); |
| 3368 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3370 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3371 | &key)); |
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 | 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] | 3374 | const size_t output_size = output_sizes_to_test[i]; |
| 3375 | psa_status_t expected_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3376 | (output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3377 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3378 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3379 | mbedtls_test_set_step(output_size); |
| 3380 | ASSERT_ALLOC(actual_mac, output_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3381 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3382 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3383 | TEST_EQUAL(psa_mac_compute(key, alg, |
| 3384 | input->x, input->len, |
| 3385 | actual_mac, output_size, &mac_length), |
| 3386 | expected_status); |
| 3387 | if (expected_status == PSA_SUCCESS) { |
| 3388 | ASSERT_COMPARE(expected_mac->x, expected_mac->len, |
| 3389 | actual_mac, mac_length); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3390 | } |
| 3391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3392 | if (output_size > 0) { |
| 3393 | memset(actual_mac, 0, output_size); |
| 3394 | } |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3395 | |
| 3396 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3397 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3398 | PSA_ASSERT(psa_mac_update(&operation, |
| 3399 | input->x, input->len)); |
| 3400 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3401 | actual_mac, output_size, |
| 3402 | &mac_length), |
| 3403 | expected_status); |
| 3404 | PSA_ASSERT(psa_mac_abort(&operation)); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3405 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3406 | if (expected_status == PSA_SUCCESS) { |
| 3407 | ASSERT_COMPARE(expected_mac->x, expected_mac->len, |
| 3408 | actual_mac, mac_length); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3409 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3410 | mbedtls_free(actual_mac); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3411 | actual_mac = NULL; |
| 3412 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3413 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3414 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3415 | psa_mac_abort(&operation); |
| 3416 | psa_destroy_key(key); |
| 3417 | PSA_DONE(); |
| 3418 | mbedtls_free(actual_mac); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3419 | } |
| 3420 | /* END_CASE */ |
| 3421 | |
| 3422 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3423 | void mac_verify(int key_type_arg, |
| 3424 | data_t *key_data, |
| 3425 | int alg_arg, |
| 3426 | data_t *input, |
| 3427 | data_t *expected_mac) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3428 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3429 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3430 | psa_key_type_t key_type = key_type_arg; |
| 3431 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3432 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3433 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3434 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3435 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3436 | TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3437 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3438 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3439 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3440 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 3441 | psa_set_key_algorithm(&attributes, alg); |
| 3442 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3444 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3445 | &key)); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3446 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3447 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3448 | PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len, |
| 3449 | expected_mac->x, expected_mac->len)); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3450 | |
| 3451 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3452 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3453 | PSA_ASSERT(psa_mac_update(&operation, |
| 3454 | input->x, input->len)); |
| 3455 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3456 | expected_mac->x, |
| 3457 | expected_mac->len)); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3458 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3459 | /* Test a MAC that's too short, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3460 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3461 | input->x, input->len, |
| 3462 | expected_mac->x, |
| 3463 | expected_mac->len - 1), |
| 3464 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3465 | |
| 3466 | /* Test a MAC that's too short, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3467 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3468 | PSA_ASSERT(psa_mac_update(&operation, |
| 3469 | input->x, input->len)); |
| 3470 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3471 | expected_mac->x, |
| 3472 | expected_mac->len - 1), |
| 3473 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3474 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3475 | /* Test a MAC that's too long, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3476 | ASSERT_ALLOC(perturbed_mac, expected_mac->len + 1); |
| 3477 | memcpy(perturbed_mac, expected_mac->x, expected_mac->len); |
| 3478 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3479 | input->x, input->len, |
| 3480 | perturbed_mac, expected_mac->len + 1), |
| 3481 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3482 | |
| 3483 | /* Test a MAC that's too long, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3484 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3485 | PSA_ASSERT(psa_mac_update(&operation, |
| 3486 | input->x, input->len)); |
| 3487 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3488 | perturbed_mac, |
| 3489 | expected_mac->len + 1), |
| 3490 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3491 | |
| 3492 | /* Test changing one byte. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3493 | for (size_t i = 0; i < expected_mac->len; i++) { |
| 3494 | mbedtls_test_set_step(i); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3495 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3496 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3497 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3498 | input->x, input->len, |
| 3499 | perturbed_mac, expected_mac->len), |
| 3500 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3501 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3502 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3503 | PSA_ASSERT(psa_mac_update(&operation, |
| 3504 | input->x, input->len)); |
| 3505 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3506 | perturbed_mac, |
| 3507 | expected_mac->len), |
| 3508 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3509 | perturbed_mac[i] ^= 1; |
| 3510 | } |
| 3511 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3512 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3513 | psa_mac_abort(&operation); |
| 3514 | psa_destroy_key(key); |
| 3515 | PSA_DONE(); |
| 3516 | mbedtls_free(perturbed_mac); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3517 | } |
| 3518 | /* END_CASE */ |
| 3519 | |
| 3520 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3521 | void cipher_operation_init() |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3522 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3523 | const uint8_t input[1] = { 0 }; |
| 3524 | unsigned char output[1] = { 0 }; |
| 3525 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3526 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3527 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3528 | * 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] | 3529 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3530 | psa_cipher_operation_t func = psa_cipher_operation_init(); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3531 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3532 | psa_cipher_operation_t zero; |
| 3533 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3534 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3535 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3536 | /* A freshly-initialized cipher operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3537 | TEST_EQUAL(psa_cipher_update(&func, |
| 3538 | input, sizeof(input), |
| 3539 | output, sizeof(output), |
| 3540 | &output_length), |
| 3541 | PSA_ERROR_BAD_STATE); |
| 3542 | TEST_EQUAL(psa_cipher_update(&init, |
| 3543 | input, sizeof(input), |
| 3544 | output, sizeof(output), |
| 3545 | &output_length), |
| 3546 | PSA_ERROR_BAD_STATE); |
| 3547 | TEST_EQUAL(psa_cipher_update(&zero, |
| 3548 | input, sizeof(input), |
| 3549 | output, sizeof(output), |
| 3550 | &output_length), |
| 3551 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3552 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3553 | /* A default cipher operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3554 | PSA_ASSERT(psa_cipher_abort(&func)); |
| 3555 | PSA_ASSERT(psa_cipher_abort(&init)); |
| 3556 | PSA_ASSERT(psa_cipher_abort(&zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3557 | } |
| 3558 | /* END_CASE */ |
| 3559 | |
| 3560 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3561 | void cipher_setup(int key_type_arg, |
| 3562 | data_t *key, |
| 3563 | int alg_arg, |
| 3564 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3565 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3566 | psa_key_type_t key_type = key_type_arg; |
| 3567 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3568 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3569 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3570 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3571 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3572 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3573 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3574 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3575 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3576 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3577 | if (!exercise_cipher_setup(key_type, key->x, key->len, alg, |
| 3578 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3579 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3580 | } |
| 3581 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3582 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3583 | /* The operation object should be reusable. */ |
| 3584 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3585 | if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3586 | smoke_test_key_data, |
| 3587 | sizeof(smoke_test_key_data), |
| 3588 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3589 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3590 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3591 | } |
| 3592 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3593 | #endif |
| 3594 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3595 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3596 | psa_cipher_abort(&operation); |
| 3597 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3598 | } |
| 3599 | /* END_CASE */ |
| 3600 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3601 | /* 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] | 3602 | void cipher_bad_order() |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3603 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3604 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3605 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3606 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3607 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3608 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3609 | 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] | 3610 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3611 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3612 | 0xaa, 0xaa, 0xaa, 0xaa |
| 3613 | }; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3614 | const uint8_t text[] = { |
| 3615 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3616 | 0xbb, 0xbb, 0xbb, 0xbb |
| 3617 | }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3618 | 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] | 3619 | size_t length = 0; |
| 3620 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3621 | PSA_ASSERT(psa_crypto_init()); |
| 3622 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3623 | psa_set_key_algorithm(&attributes, alg); |
| 3624 | psa_set_key_type(&attributes, key_type); |
| 3625 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3626 | &key)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3627 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3628 | /* Call encrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3629 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3630 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3631 | TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg), |
| 3632 | PSA_ERROR_BAD_STATE); |
| 3633 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3634 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3635 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3636 | |
| 3637 | /* Call decrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3638 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3639 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3640 | TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg), |
| 3641 | PSA_ERROR_BAD_STATE); |
| 3642 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3643 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3644 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3645 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3646 | /* Generate an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3647 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3648 | buffer, sizeof(buffer), |
| 3649 | &length), |
| 3650 | PSA_ERROR_BAD_STATE); |
| 3651 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3652 | |
| 3653 | /* Generate an IV twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3654 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3655 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3656 | buffer, sizeof(buffer), |
| 3657 | &length)); |
| 3658 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3659 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3660 | buffer, sizeof(buffer), |
| 3661 | &length), |
| 3662 | PSA_ERROR_BAD_STATE); |
| 3663 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3664 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3665 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3666 | |
| 3667 | /* Generate an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3668 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3669 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3670 | iv, sizeof(iv))); |
| 3671 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3672 | buffer, sizeof(buffer), |
| 3673 | &length), |
| 3674 | PSA_ERROR_BAD_STATE); |
| 3675 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3676 | |
| 3677 | /* Set an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3678 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3679 | iv, sizeof(iv)), |
| 3680 | PSA_ERROR_BAD_STATE); |
| 3681 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3682 | |
| 3683 | /* Set an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3684 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3685 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3686 | iv, sizeof(iv))); |
| 3687 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3688 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3689 | iv, sizeof(iv)), |
| 3690 | PSA_ERROR_BAD_STATE); |
| 3691 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3692 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3693 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3694 | |
| 3695 | /* Set an IV after it's already generated. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3696 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3697 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3698 | buffer, sizeof(buffer), |
| 3699 | &length)); |
| 3700 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3701 | iv, sizeof(iv)), |
| 3702 | PSA_ERROR_BAD_STATE); |
| 3703 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3704 | |
| 3705 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3706 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3707 | text, sizeof(text), |
| 3708 | buffer, sizeof(buffer), |
| 3709 | &length), |
| 3710 | PSA_ERROR_BAD_STATE); |
| 3711 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3712 | |
| 3713 | /* Call update without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3714 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3715 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3716 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3717 | text, sizeof(text), |
| 3718 | buffer, sizeof(buffer), |
| 3719 | &length), |
| 3720 | PSA_ERROR_BAD_STATE); |
| 3721 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3722 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3723 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3724 | |
| 3725 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3726 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3727 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3728 | iv, sizeof(iv))); |
| 3729 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3730 | buffer, sizeof(buffer), &length)); |
| 3731 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3732 | text, sizeof(text), |
| 3733 | buffer, sizeof(buffer), |
| 3734 | &length), |
| 3735 | PSA_ERROR_BAD_STATE); |
| 3736 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3737 | |
| 3738 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3739 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3740 | buffer, sizeof(buffer), &length), |
| 3741 | PSA_ERROR_BAD_STATE); |
| 3742 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3743 | |
| 3744 | /* Call finish without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3745 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3746 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3747 | * for cipher modes with padding. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3748 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3749 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3750 | buffer, sizeof(buffer), &length), |
| 3751 | PSA_ERROR_BAD_STATE); |
| 3752 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3753 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3754 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3755 | |
| 3756 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3757 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3758 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3759 | iv, sizeof(iv))); |
| 3760 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3761 | buffer, sizeof(buffer), &length)); |
| 3762 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3763 | buffer, sizeof(buffer), &length), |
| 3764 | PSA_ERROR_BAD_STATE); |
| 3765 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3766 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3767 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3768 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3769 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3770 | psa_cipher_abort(&operation); |
| 3771 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3772 | } |
| 3773 | /* END_CASE */ |
| 3774 | |
| 3775 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3776 | void cipher_encrypt_fail(int alg_arg, |
| 3777 | int key_type_arg, |
| 3778 | data_t *key_data, |
| 3779 | data_t *input, |
| 3780 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3781 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3782 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3783 | psa_status_t status; |
| 3784 | psa_key_type_t key_type = key_type_arg; |
| 3785 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3786 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3787 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 }; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3788 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; |
| 3789 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3790 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3791 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3792 | size_t output_length = 0; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3793 | size_t function_output_length; |
| 3794 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3795 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3796 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3797 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 3798 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3799 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3800 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3801 | psa_set_key_algorithm(&attributes, alg); |
| 3802 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3803 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3804 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 3805 | input->len); |
| 3806 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3807 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3808 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3809 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3810 | } |
| 3811 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3812 | /* Encrypt, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3813 | status = psa_cipher_encrypt(key, alg, input->x, input->len, output, |
| 3814 | output_buffer_size, &output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3815 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3816 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3817 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3818 | /* Encrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3819 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
| 3820 | if (status == PSA_SUCCESS) { |
| 3821 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 3822 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3823 | iv, iv_size, |
| 3824 | &iv_length)); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3825 | } |
| 3826 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3827 | status = psa_cipher_update(&operation, input->x, input->len, |
| 3828 | output, output_buffer_size, |
| 3829 | &function_output_length); |
| 3830 | if (status == PSA_SUCCESS) { |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3831 | output_length += function_output_length; |
| 3832 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3833 | status = psa_cipher_finish(&operation, output + output_length, |
| 3834 | output_buffer_size - output_length, |
| 3835 | &function_output_length); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3836 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3837 | TEST_EQUAL(status, expected_status); |
| 3838 | } else { |
| 3839 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3840 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3841 | } else { |
| 3842 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3843 | } |
| 3844 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3845 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3846 | psa_cipher_abort(&operation); |
| 3847 | mbedtls_free(output); |
| 3848 | psa_destroy_key(key); |
| 3849 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3850 | } |
| 3851 | /* END_CASE */ |
| 3852 | |
| 3853 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3854 | void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data, |
| 3855 | data_t *input, int iv_length, |
| 3856 | int expected_result) |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3857 | { |
| 3858 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3859 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3860 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3861 | size_t output_buffer_size = 0; |
| 3862 | unsigned char *output = NULL; |
| 3863 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3864 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 3865 | ASSERT_ALLOC(output, output_buffer_size); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3866 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3867 | PSA_ASSERT(psa_crypto_init()); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3868 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3869 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3870 | psa_set_key_algorithm(&attributes, alg); |
| 3871 | psa_set_key_type(&attributes, key_type); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3872 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3873 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3874 | &key)); |
| 3875 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3876 | TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output, |
| 3877 | iv_length)); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3878 | |
| 3879 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3880 | psa_cipher_abort(&operation); |
| 3881 | mbedtls_free(output); |
| 3882 | psa_destroy_key(key); |
| 3883 | PSA_DONE(); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3884 | } |
| 3885 | /* END_CASE */ |
| 3886 | |
| 3887 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3888 | void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data, |
| 3889 | data_t *plaintext, data_t *ciphertext) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3890 | { |
| 3891 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3892 | psa_key_type_t key_type = key_type_arg; |
| 3893 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3894 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3895 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3896 | unsigned char *output = NULL; |
| 3897 | size_t output_buffer_size = 0; |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3898 | size_t output_length, length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3899 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3900 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3901 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3902 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3903 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3904 | TEST_LE_U(ciphertext->len, |
| 3905 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len)); |
| 3906 | TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len), |
| 3907 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len)); |
| 3908 | TEST_LE_U(plaintext->len, |
| 3909 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len)); |
| 3910 | TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len), |
| 3911 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len)); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3912 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3913 | |
| 3914 | /* Set up key and output buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3915 | psa_set_key_usage_flags(&attributes, |
| 3916 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3917 | psa_set_key_algorithm(&attributes, alg); |
| 3918 | psa_set_key_type(&attributes, key_type); |
| 3919 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3920 | &key)); |
| 3921 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 3922 | plaintext->len); |
| 3923 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3924 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3925 | /* set_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3926 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3927 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 3928 | PSA_ERROR_BAD_STATE); |
| 3929 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3930 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 3931 | PSA_ERROR_BAD_STATE); |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3932 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3933 | /* generate_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3934 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3935 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 3936 | &length), |
| 3937 | PSA_ERROR_BAD_STATE); |
| 3938 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3939 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 3940 | &length), |
| 3941 | PSA_ERROR_BAD_STATE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3942 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3943 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3944 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3945 | output_length = 0; |
| 3946 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3947 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3948 | plaintext->x, plaintext->len, |
| 3949 | output, output_buffer_size, |
| 3950 | &length)); |
| 3951 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3952 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3953 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3954 | mbedtls_buffer_offset(output, output_length), |
| 3955 | output_buffer_size - output_length, |
| 3956 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3957 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3958 | ASSERT_COMPARE(ciphertext->x, ciphertext->len, |
| 3959 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3960 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3961 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3962 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3963 | output_length = 0; |
| 3964 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3965 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3966 | ciphertext->x, ciphertext->len, |
| 3967 | output, output_buffer_size, |
| 3968 | &length)); |
| 3969 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3970 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3971 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3972 | mbedtls_buffer_offset(output, output_length), |
| 3973 | output_buffer_size - output_length, |
| 3974 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3975 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3976 | ASSERT_COMPARE(plaintext->x, plaintext->len, |
| 3977 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3978 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3979 | /* One-shot encryption */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3980 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3981 | PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len, |
| 3982 | output, output_buffer_size, |
| 3983 | &output_length)); |
| 3984 | ASSERT_COMPARE(ciphertext->x, ciphertext->len, |
| 3985 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3986 | |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3987 | /* One-shot decryption */ |
| 3988 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3989 | PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len, |
| 3990 | output, output_buffer_size, |
| 3991 | &output_length)); |
| 3992 | ASSERT_COMPARE(plaintext->x, plaintext->len, |
| 3993 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3994 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3995 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3996 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3997 | mbedtls_free(output); |
| 3998 | psa_cipher_abort(&operation); |
| 3999 | psa_destroy_key(key); |
| 4000 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4001 | } |
| 4002 | /* END_CASE */ |
| 4003 | |
| 4004 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4005 | 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] | 4006 | { |
| 4007 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4008 | psa_algorithm_t alg = alg_arg; |
| 4009 | psa_key_type_t key_type = key_type_arg; |
| 4010 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4011 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4012 | psa_status_t status; |
| 4013 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4014 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4015 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4016 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4017 | psa_set_key_algorithm(&attributes, alg); |
| 4018 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4019 | |
| 4020 | /* Usage of either of these two size macros would cause divide by zero |
| 4021 | * with incorrect key types previously. Input length should be irrelevant |
| 4022 | * here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4023 | TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16), |
| 4024 | 0); |
| 4025 | TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4026 | |
| 4027 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4028 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4029 | &key)); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4030 | |
| 4031 | /* Should fail due to invalid alg type (to support invalid key type). |
| 4032 | * Encrypt or decrypt will end up in the same place. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4033 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4035 | TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4036 | |
| 4037 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4038 | psa_cipher_abort(&operation); |
| 4039 | psa_destroy_key(key); |
| 4040 | PSA_DONE(); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4041 | } |
| 4042 | /* END_CASE */ |
| 4043 | |
| 4044 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4045 | void cipher_encrypt_validation(int alg_arg, |
| 4046 | int key_type_arg, |
| 4047 | data_t *key_data, |
| 4048 | data_t *input) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4049 | { |
| 4050 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4051 | psa_key_type_t key_type = key_type_arg; |
| 4052 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4053 | size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4054 | unsigned char *output1 = NULL; |
| 4055 | size_t output1_buffer_size = 0; |
| 4056 | size_t output1_length = 0; |
| 4057 | unsigned char *output2 = NULL; |
| 4058 | size_t output2_buffer_size = 0; |
| 4059 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4060 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4061 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4062 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4063 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4064 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4065 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4066 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4067 | psa_set_key_algorithm(&attributes, alg); |
| 4068 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4069 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4070 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4071 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4072 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4073 | ASSERT_ALLOC(output1, output1_buffer_size); |
| 4074 | ASSERT_ALLOC(output2, output2_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4075 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4076 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4077 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4078 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 4079 | /* The one-shot cipher encryption uses generated iv so validating |
| 4080 | the output is not possible. Validating with multipart encryption. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4081 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1, |
| 4082 | output1_buffer_size, &output1_length)); |
| 4083 | TEST_LE_U(output1_length, |
| 4084 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4085 | TEST_LE_U(output1_length, |
| 4086 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4087 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4088 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4089 | PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4090 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4091 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4092 | input->x, input->len, |
| 4093 | output2, output2_buffer_size, |
| 4094 | &function_output_length)); |
| 4095 | TEST_LE_U(function_output_length, |
| 4096 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4097 | TEST_LE_U(function_output_length, |
| 4098 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4099 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4100 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4101 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4102 | output2 + output2_length, |
| 4103 | output2_buffer_size - output2_length, |
| 4104 | &function_output_length)); |
| 4105 | TEST_LE_U(function_output_length, |
| 4106 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4107 | TEST_LE_U(function_output_length, |
| 4108 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4109 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4110 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4111 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4112 | ASSERT_COMPARE(output1 + iv_size, output1_length - iv_size, |
| 4113 | output2, output2_length); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4114 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4115 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4116 | psa_cipher_abort(&operation); |
| 4117 | mbedtls_free(output1); |
| 4118 | mbedtls_free(output2); |
| 4119 | psa_destroy_key(key); |
| 4120 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4121 | } |
| 4122 | /* END_CASE */ |
| 4123 | |
| 4124 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4125 | void cipher_encrypt_multipart(int alg_arg, int key_type_arg, |
| 4126 | data_t *key_data, data_t *iv, |
| 4127 | data_t *input, |
| 4128 | int first_part_size_arg, |
| 4129 | int output1_length_arg, int output2_length_arg, |
| 4130 | data_t *expected_output, |
| 4131 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4132 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4133 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4134 | psa_key_type_t key_type = key_type_arg; |
| 4135 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4136 | psa_status_t status; |
| 4137 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4138 | size_t first_part_size = first_part_size_arg; |
| 4139 | size_t output1_length = output1_length_arg; |
| 4140 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4141 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4142 | size_t output_buffer_size = 0; |
| 4143 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4144 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4145 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4146 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4147 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4148 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4150 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4151 | psa_set_key_algorithm(&attributes, alg); |
| 4152 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4153 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4154 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4155 | &key)); |
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_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4159 | if (iv->len > 0) { |
| 4160 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4161 | } |
| 4162 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4163 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4164 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4165 | ASSERT_ALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4166 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4167 | TEST_LE_U(first_part_size, input->len); |
| 4168 | PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size, |
| 4169 | output, output_buffer_size, |
| 4170 | &function_output_length)); |
| 4171 | TEST_ASSERT(function_output_length == output1_length); |
| 4172 | TEST_LE_U(function_output_length, |
| 4173 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4174 | TEST_LE_U(function_output_length, |
| 4175 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4176 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4177 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4178 | if (first_part_size < input->len) { |
| 4179 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4180 | input->x + first_part_size, |
| 4181 | input->len - first_part_size, |
| 4182 | (output_buffer_size == 0 ? NULL : |
| 4183 | output + total_output_length), |
| 4184 | output_buffer_size - total_output_length, |
| 4185 | &function_output_length)); |
| 4186 | TEST_ASSERT(function_output_length == output2_length); |
| 4187 | TEST_LE_U(function_output_length, |
| 4188 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4189 | alg, |
| 4190 | input->len - first_part_size)); |
| 4191 | TEST_LE_U(function_output_length, |
| 4192 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4193 | total_output_length += function_output_length; |
| 4194 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4195 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4196 | status = psa_cipher_finish(&operation, |
| 4197 | (output_buffer_size == 0 ? NULL : |
| 4198 | output + total_output_length), |
| 4199 | output_buffer_size - total_output_length, |
| 4200 | &function_output_length); |
| 4201 | TEST_LE_U(function_output_length, |
| 4202 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4203 | TEST_LE_U(function_output_length, |
| 4204 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4205 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4206 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4207 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4208 | if (expected_status == PSA_SUCCESS) { |
| 4209 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4210 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4211 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 4212 | output, total_output_length); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4213 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4214 | |
| 4215 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4216 | psa_cipher_abort(&operation); |
| 4217 | mbedtls_free(output); |
| 4218 | psa_destroy_key(key); |
| 4219 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4220 | } |
| 4221 | /* END_CASE */ |
| 4222 | |
| 4223 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4224 | void cipher_decrypt_multipart(int alg_arg, int key_type_arg, |
| 4225 | data_t *key_data, data_t *iv, |
| 4226 | data_t *input, |
| 4227 | int first_part_size_arg, |
| 4228 | int output1_length_arg, int output2_length_arg, |
| 4229 | data_t *expected_output, |
| 4230 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4231 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4232 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4233 | psa_key_type_t key_type = key_type_arg; |
| 4234 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4235 | psa_status_t status; |
| 4236 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4237 | size_t first_part_size = first_part_size_arg; |
| 4238 | size_t output1_length = output1_length_arg; |
| 4239 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4240 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4241 | size_t output_buffer_size = 0; |
| 4242 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4243 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4244 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4245 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4246 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4247 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4248 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4249 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4250 | psa_set_key_algorithm(&attributes, alg); |
| 4251 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4252 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4253 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4254 | &key)); |
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_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4257 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4258 | if (iv->len > 0) { |
| 4259 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4260 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4261 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4262 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4263 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4264 | ASSERT_ALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4266 | TEST_LE_U(first_part_size, input->len); |
| 4267 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4268 | input->x, first_part_size, |
| 4269 | output, output_buffer_size, |
| 4270 | &function_output_length)); |
| 4271 | TEST_ASSERT(function_output_length == output1_length); |
| 4272 | TEST_LE_U(function_output_length, |
| 4273 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4274 | TEST_LE_U(function_output_length, |
| 4275 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4276 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4277 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4278 | if (first_part_size < input->len) { |
| 4279 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4280 | input->x + first_part_size, |
| 4281 | input->len - first_part_size, |
| 4282 | (output_buffer_size == 0 ? NULL : |
| 4283 | output + total_output_length), |
| 4284 | output_buffer_size - total_output_length, |
| 4285 | &function_output_length)); |
| 4286 | TEST_ASSERT(function_output_length == output2_length); |
| 4287 | TEST_LE_U(function_output_length, |
| 4288 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4289 | alg, |
| 4290 | input->len - first_part_size)); |
| 4291 | TEST_LE_U(function_output_length, |
| 4292 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4293 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4294 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4295 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4296 | status = psa_cipher_finish(&operation, |
| 4297 | (output_buffer_size == 0 ? NULL : |
| 4298 | output + total_output_length), |
| 4299 | output_buffer_size - total_output_length, |
| 4300 | &function_output_length); |
| 4301 | TEST_LE_U(function_output_length, |
| 4302 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4303 | TEST_LE_U(function_output_length, |
| 4304 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4305 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4306 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4307 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4308 | if (expected_status == PSA_SUCCESS) { |
| 4309 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4310 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4311 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 4312 | output, total_output_length); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4313 | } |
| 4314 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4315 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4316 | psa_cipher_abort(&operation); |
| 4317 | mbedtls_free(output); |
| 4318 | psa_destroy_key(key); |
| 4319 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4320 | } |
| 4321 | /* END_CASE */ |
| 4322 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4323 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4324 | void cipher_decrypt_fail(int alg_arg, |
| 4325 | int key_type_arg, |
| 4326 | data_t *key_data, |
| 4327 | data_t *iv, |
| 4328 | data_t *input_arg, |
| 4329 | int expected_status_arg) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4330 | { |
| 4331 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4332 | psa_status_t status; |
| 4333 | psa_key_type_t key_type = key_type_arg; |
| 4334 | psa_algorithm_t alg = alg_arg; |
| 4335 | psa_status_t expected_status = expected_status_arg; |
| 4336 | unsigned char *input = NULL; |
| 4337 | size_t input_buffer_size = 0; |
| 4338 | unsigned char *output = NULL; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4339 | unsigned char *output_multi = NULL; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4340 | size_t output_buffer_size = 0; |
| 4341 | size_t output_length = 0; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4342 | size_t function_output_length; |
| 4343 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4344 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4345 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4346 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 4347 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4349 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4350 | psa_set_key_algorithm(&attributes, alg); |
| 4351 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4353 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4354 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4355 | } |
| 4356 | |
| 4357 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4358 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4359 | if (input_buffer_size > 0) { |
| 4360 | ASSERT_ALLOC(input, input_buffer_size); |
| 4361 | memcpy(input, iv->x, iv->len); |
| 4362 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4363 | } |
| 4364 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4365 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size); |
| 4366 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4367 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4368 | /* Decrypt, one-short */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4369 | status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4370 | output_buffer_size, &output_length); |
| 4371 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4372 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4373 | /* Decrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4374 | status = psa_cipher_decrypt_setup(&operation, key, alg); |
| 4375 | if (status == PSA_SUCCESS) { |
| 4376 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 4377 | input_arg->len) + |
| 4378 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4379 | ASSERT_ALLOC(output_multi, output_buffer_size); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4380 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4381 | if (iv->len > 0) { |
| 4382 | status = psa_cipher_set_iv(&operation, iv->x, iv->len); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4383 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4384 | if (status != PSA_SUCCESS) { |
| 4385 | TEST_EQUAL(status, expected_status); |
| 4386 | } |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4387 | } |
| 4388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4389 | if (status == PSA_SUCCESS) { |
| 4390 | status = psa_cipher_update(&operation, |
| 4391 | input_arg->x, input_arg->len, |
| 4392 | output_multi, output_buffer_size, |
| 4393 | &function_output_length); |
| 4394 | if (status == PSA_SUCCESS) { |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4395 | output_length = function_output_length; |
| 4396 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4397 | status = psa_cipher_finish(&operation, |
| 4398 | output_multi + output_length, |
| 4399 | output_buffer_size - output_length, |
| 4400 | &function_output_length); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4401 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4402 | TEST_EQUAL(status, expected_status); |
| 4403 | } else { |
| 4404 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4405 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4406 | } else { |
| 4407 | TEST_EQUAL(status, expected_status); |
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 | } else { |
| 4410 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4411 | } |
| 4412 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4413 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4414 | psa_cipher_abort(&operation); |
| 4415 | mbedtls_free(input); |
| 4416 | mbedtls_free(output); |
| 4417 | mbedtls_free(output_multi); |
| 4418 | psa_destroy_key(key); |
| 4419 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4420 | } |
| 4421 | /* END_CASE */ |
| 4422 | |
| 4423 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4424 | void cipher_decrypt(int alg_arg, |
| 4425 | int key_type_arg, |
| 4426 | data_t *key_data, |
| 4427 | data_t *iv, |
| 4428 | data_t *input_arg, |
| 4429 | data_t *expected_output) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4430 | { |
| 4431 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4432 | psa_key_type_t key_type = key_type_arg; |
| 4433 | psa_algorithm_t alg = alg_arg; |
| 4434 | unsigned char *input = NULL; |
| 4435 | size_t input_buffer_size = 0; |
| 4436 | unsigned char *output = NULL; |
| 4437 | size_t output_buffer_size = 0; |
| 4438 | size_t output_length = 0; |
| 4439 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4440 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4441 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4442 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4443 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4444 | psa_set_key_algorithm(&attributes, alg); |
| 4445 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4446 | |
| 4447 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4448 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4449 | if (input_buffer_size > 0) { |
| 4450 | ASSERT_ALLOC(input, input_buffer_size); |
| 4451 | memcpy(input, iv->x, iv->len); |
| 4452 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4453 | } |
| 4454 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4455 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size); |
| 4456 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4457 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4458 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4459 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4460 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4461 | PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4462 | output_buffer_size, &output_length)); |
| 4463 | TEST_LE_U(output_length, |
| 4464 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size)); |
| 4465 | TEST_LE_U(output_length, |
| 4466 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size)); |
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 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 4469 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4470 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4471 | mbedtls_free(input); |
| 4472 | mbedtls_free(output); |
| 4473 | psa_destroy_key(key); |
| 4474 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4475 | } |
| 4476 | /* END_CASE */ |
| 4477 | |
| 4478 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4479 | void cipher_verify_output(int alg_arg, |
| 4480 | int key_type_arg, |
| 4481 | data_t *key_data, |
| 4482 | data_t *input) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4483 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4484 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4485 | psa_key_type_t key_type = key_type_arg; |
| 4486 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4487 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4488 | size_t output1_size = 0; |
| 4489 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4490 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4491 | size_t output2_size = 0; |
| 4492 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4493 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4494 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4495 | PSA_ASSERT(psa_crypto_init()); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4496 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4497 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4498 | psa_set_key_algorithm(&attributes, alg); |
| 4499 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4500 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4501 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4502 | &key)); |
| 4503 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4504 | ASSERT_ALLOC(output1, output1_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4505 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4506 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, |
| 4507 | output1, output1_size, |
| 4508 | &output1_length)); |
| 4509 | TEST_LE_U(output1_length, |
| 4510 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4511 | TEST_LE_U(output1_length, |
| 4512 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4513 | |
| 4514 | output2_size = output1_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4515 | ASSERT_ALLOC(output2, output2_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4516 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4517 | PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length, |
| 4518 | output2, output2_size, |
| 4519 | &output2_length)); |
| 4520 | TEST_LE_U(output2_length, |
| 4521 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4522 | TEST_LE_U(output2_length, |
| 4523 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4524 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4525 | ASSERT_COMPARE(input->x, input->len, output2, output2_length); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4526 | |
| 4527 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4528 | mbedtls_free(output1); |
| 4529 | mbedtls_free(output2); |
| 4530 | psa_destroy_key(key); |
| 4531 | PSA_DONE(); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4532 | } |
| 4533 | /* END_CASE */ |
| 4534 | |
| 4535 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4536 | void cipher_verify_output_multipart(int alg_arg, |
| 4537 | int key_type_arg, |
| 4538 | data_t *key_data, |
| 4539 | data_t *input, |
| 4540 | int first_part_size_arg) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4541 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4542 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4543 | psa_key_type_t key_type = key_type_arg; |
| 4544 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4545 | size_t first_part_size = first_part_size_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4546 | unsigned char iv[16] = { 0 }; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4547 | size_t iv_size = 16; |
| 4548 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4549 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4550 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4551 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4552 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4553 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4554 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4555 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4556 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 4557 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4558 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4559 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4560 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4562 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4563 | psa_set_key_algorithm(&attributes, alg); |
| 4564 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4565 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4566 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4567 | &key)); |
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_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg)); |
| 4570 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4571 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4572 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 4573 | PSA_ASSERT(psa_cipher_generate_iv(&operation1, |
| 4574 | iv, iv_size, |
| 4575 | &iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4576 | } |
| 4577 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4578 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4579 | TEST_LE_U(output1_buffer_size, |
| 4580 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
| 4581 | ASSERT_ALLOC(output1, output1_buffer_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4582 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4583 | TEST_LE_U(first_part_size, input->len); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4584 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4585 | PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size, |
| 4586 | output1, output1_buffer_size, |
| 4587 | &function_output_length)); |
| 4588 | TEST_LE_U(function_output_length, |
| 4589 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4590 | TEST_LE_U(function_output_length, |
| 4591 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4592 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4593 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4594 | PSA_ASSERT(psa_cipher_update(&operation1, |
| 4595 | input->x + first_part_size, |
| 4596 | input->len - first_part_size, |
| 4597 | output1, output1_buffer_size, |
| 4598 | &function_output_length)); |
| 4599 | TEST_LE_U(function_output_length, |
| 4600 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4601 | alg, |
| 4602 | input->len - first_part_size)); |
| 4603 | TEST_LE_U(function_output_length, |
| 4604 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4605 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4606 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4607 | PSA_ASSERT(psa_cipher_finish(&operation1, |
| 4608 | output1 + output1_length, |
| 4609 | output1_buffer_size - output1_length, |
| 4610 | &function_output_length)); |
| 4611 | TEST_LE_U(function_output_length, |
| 4612 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4613 | TEST_LE_U(function_output_length, |
| 4614 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4615 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4616 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4617 | PSA_ASSERT(psa_cipher_abort(&operation1)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4618 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4619 | output2_buffer_size = output1_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4620 | TEST_LE_U(output2_buffer_size, |
| 4621 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4622 | TEST_LE_U(output2_buffer_size, |
| 4623 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
| 4624 | ASSERT_ALLOC(output2, output2_buffer_size); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4625 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4626 | if (iv_length > 0) { |
| 4627 | PSA_ASSERT(psa_cipher_set_iv(&operation2, |
| 4628 | iv, iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4629 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4630 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4631 | PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size, |
| 4632 | output2, output2_buffer_size, |
| 4633 | &function_output_length)); |
| 4634 | TEST_LE_U(function_output_length, |
| 4635 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4636 | TEST_LE_U(function_output_length, |
| 4637 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4638 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4639 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4640 | PSA_ASSERT(psa_cipher_update(&operation2, |
| 4641 | output1 + first_part_size, |
| 4642 | output1_length - first_part_size, |
| 4643 | output2, output2_buffer_size, |
| 4644 | &function_output_length)); |
| 4645 | TEST_LE_U(function_output_length, |
| 4646 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4647 | alg, |
| 4648 | output1_length - first_part_size)); |
| 4649 | TEST_LE_U(function_output_length, |
| 4650 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4651 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4652 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4653 | PSA_ASSERT(psa_cipher_finish(&operation2, |
| 4654 | output2 + output2_length, |
| 4655 | output2_buffer_size - output2_length, |
| 4656 | &function_output_length)); |
| 4657 | TEST_LE_U(function_output_length, |
| 4658 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4659 | TEST_LE_U(function_output_length, |
| 4660 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4661 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4662 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4663 | PSA_ASSERT(psa_cipher_abort(&operation2)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4664 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4665 | ASSERT_COMPARE(input->x, input->len, output2, output2_length); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4666 | |
| 4667 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4668 | psa_cipher_abort(&operation1); |
| 4669 | psa_cipher_abort(&operation2); |
| 4670 | mbedtls_free(output1); |
| 4671 | mbedtls_free(output2); |
| 4672 | psa_destroy_key(key); |
| 4673 | PSA_DONE(); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4674 | } |
| 4675 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 4676 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4677 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4678 | void aead_encrypt_decrypt(int key_type_arg, data_t *key_data, |
| 4679 | int alg_arg, |
| 4680 | data_t *nonce, |
| 4681 | data_t *additional_data, |
| 4682 | data_t *input_data, |
| 4683 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4684 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4685 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4686 | psa_key_type_t key_type = key_type_arg; |
| 4687 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4688 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4689 | unsigned char *output_data = NULL; |
| 4690 | size_t output_size = 0; |
| 4691 | size_t output_length = 0; |
| 4692 | unsigned char *output_data2 = NULL; |
| 4693 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4694 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4695 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4696 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4697 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4698 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4699 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4700 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4701 | psa_set_key_algorithm(&attributes, alg); |
| 4702 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4703 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4704 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4705 | &key)); |
| 4706 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4707 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4708 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4709 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4710 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4711 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4712 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4713 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4714 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
| 4715 | TEST_EQUAL(output_size, |
| 4716 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4717 | TEST_LE_U(output_size, |
| 4718 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4719 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4720 | ASSERT_ALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4721 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4722 | status = psa_aead_encrypt(key, alg, |
| 4723 | nonce->x, nonce->len, |
| 4724 | additional_data->x, |
| 4725 | additional_data->len, |
| 4726 | input_data->x, input_data->len, |
| 4727 | output_data, output_size, |
| 4728 | &output_length); |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4729 | |
| 4730 | /* If the operation is not supported, just skip and not fail in case the |
| 4731 | * encryption involves a common limitation of cryptography hardwares and |
| 4732 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4733 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4734 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4735 | 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] | 4736 | } |
| 4737 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4738 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4739 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4740 | if (PSA_SUCCESS == expected_result) { |
| 4741 | ASSERT_ALLOC(output_data2, output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4742 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4743 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4744 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4745 | TEST_EQUAL(input_data->len, |
| 4746 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length)); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4747 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4748 | TEST_LE_U(input_data->len, |
| 4749 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length)); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4750 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4751 | TEST_EQUAL(psa_aead_decrypt(key, alg, |
| 4752 | nonce->x, nonce->len, |
| 4753 | additional_data->x, |
| 4754 | additional_data->len, |
| 4755 | output_data, output_length, |
| 4756 | output_data2, output_length, |
| 4757 | &output_length2), |
| 4758 | expected_result); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4759 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4760 | ASSERT_COMPARE(input_data->x, input_data->len, |
| 4761 | output_data2, output_length2); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4762 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4763 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4764 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4765 | psa_destroy_key(key); |
| 4766 | mbedtls_free(output_data); |
| 4767 | mbedtls_free(output_data2); |
| 4768 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4769 | } |
| 4770 | /* END_CASE */ |
| 4771 | |
| 4772 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4773 | void aead_encrypt(int key_type_arg, data_t *key_data, |
| 4774 | int alg_arg, |
| 4775 | data_t *nonce, |
| 4776 | data_t *additional_data, |
| 4777 | data_t *input_data, |
| 4778 | data_t *expected_result) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4779 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4780 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4781 | psa_key_type_t key_type = key_type_arg; |
| 4782 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4783 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4784 | unsigned char *output_data = NULL; |
| 4785 | size_t output_size = 0; |
| 4786 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4787 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4788 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4789 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4790 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4791 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4792 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4793 | psa_set_key_algorithm(&attributes, alg); |
| 4794 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4795 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4796 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4797 | &key)); |
| 4798 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4799 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4800 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4801 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4802 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4803 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4804 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4805 | TEST_EQUAL(output_size, |
| 4806 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4807 | TEST_LE_U(output_size, |
| 4808 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
| 4809 | ASSERT_ALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4810 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4811 | status = psa_aead_encrypt(key, alg, |
| 4812 | nonce->x, nonce->len, |
| 4813 | additional_data->x, additional_data->len, |
| 4814 | input_data->x, input_data->len, |
| 4815 | output_data, output_size, |
| 4816 | &output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4817 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4818 | /* If the operation is not supported, just skip and not fail in case the |
| 4819 | * encryption involves a common limitation of cryptography hardwares and |
| 4820 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4821 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4822 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4823 | 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] | 4824 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4825 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4826 | PSA_ASSERT(status); |
| 4827 | ASSERT_COMPARE(expected_result->x, expected_result->len, |
| 4828 | output_data, output_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4829 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4830 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4831 | psa_destroy_key(key); |
| 4832 | mbedtls_free(output_data); |
| 4833 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4834 | } |
| 4835 | /* END_CASE */ |
| 4836 | |
| 4837 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4838 | void aead_decrypt(int key_type_arg, data_t *key_data, |
| 4839 | int alg_arg, |
| 4840 | data_t *nonce, |
| 4841 | data_t *additional_data, |
| 4842 | data_t *input_data, |
| 4843 | data_t *expected_data, |
| 4844 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4845 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4846 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4847 | psa_key_type_t key_type = key_type_arg; |
| 4848 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4849 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4850 | unsigned char *output_data = NULL; |
| 4851 | size_t output_size = 0; |
| 4852 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4853 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4854 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4855 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4856 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4857 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4858 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4859 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4860 | psa_set_key_algorithm(&attributes, alg); |
| 4861 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4862 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4863 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4864 | &key)); |
| 4865 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4866 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4867 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4868 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4869 | alg); |
| 4870 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4871 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4872 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4873 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4874 | TEST_EQUAL(output_size, |
| 4875 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4876 | TEST_LE_U(output_size, |
| 4877 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4878 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4879 | ASSERT_ALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4880 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4881 | status = psa_aead_decrypt(key, alg, |
| 4882 | nonce->x, nonce->len, |
| 4883 | additional_data->x, |
| 4884 | additional_data->len, |
| 4885 | input_data->x, input_data->len, |
| 4886 | output_data, output_size, |
| 4887 | &output_length); |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4888 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4889 | /* If the operation is not supported, just skip and not fail in case the |
| 4890 | * decryption involves a common limitation of cryptography hardwares and |
| 4891 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4892 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4893 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4894 | 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] | 4895 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4896 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4897 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4898 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4899 | if (expected_result == PSA_SUCCESS) { |
| 4900 | ASSERT_COMPARE(expected_data->x, expected_data->len, |
| 4901 | output_data, output_length); |
| 4902 | } |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4903 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4904 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4905 | psa_destroy_key(key); |
| 4906 | mbedtls_free(output_data); |
| 4907 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4908 | } |
| 4909 | /* END_CASE */ |
| 4910 | |
| 4911 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4912 | void aead_multipart_encrypt(int key_type_arg, data_t *key_data, |
| 4913 | int alg_arg, |
| 4914 | data_t *nonce, |
| 4915 | data_t *additional_data, |
| 4916 | data_t *input_data, |
| 4917 | int do_set_lengths, |
| 4918 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4919 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4920 | size_t ad_part_len = 0; |
| 4921 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4922 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4923 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4924 | for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) { |
| 4925 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4926 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4927 | if (do_set_lengths) { |
| 4928 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4929 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4930 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4931 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4932 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4933 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4934 | |
| 4935 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4936 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4937 | alg_arg, nonce, |
| 4938 | additional_data, |
| 4939 | ad_part_len, |
| 4940 | input_data, -1, |
| 4941 | set_lengths_method, |
| 4942 | expected_output, |
| 4943 | 1, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4944 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4945 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4946 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4947 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 4948 | mbedtls_test_set_step(1000 + ad_part_len); |
| 4949 | |
| 4950 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4951 | alg_arg, nonce, |
| 4952 | additional_data, |
| 4953 | ad_part_len, |
| 4954 | input_data, -1, |
| 4955 | set_lengths_method, |
| 4956 | expected_output, |
| 4957 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4958 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4959 | } |
| 4960 | } |
| 4961 | |
| 4962 | for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) { |
| 4963 | /* Split data into length(data_part_len) parts. */ |
| 4964 | mbedtls_test_set_step(2000 + data_part_len); |
| 4965 | |
| 4966 | if (do_set_lengths) { |
| 4967 | if (data_part_len & 0x01) { |
| 4968 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4969 | } else { |
| 4970 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
| 4971 | } |
| 4972 | } |
| 4973 | |
| 4974 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4975 | alg_arg, nonce, |
| 4976 | additional_data, -1, |
| 4977 | input_data, data_part_len, |
| 4978 | set_lengths_method, |
| 4979 | expected_output, |
| 4980 | 1, 0)) { |
| 4981 | break; |
| 4982 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4983 | |
| 4984 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4985 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4986 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4987 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4988 | alg_arg, nonce, |
| 4989 | additional_data, -1, |
| 4990 | input_data, data_part_len, |
| 4991 | set_lengths_method, |
| 4992 | expected_output, |
| 4993 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4994 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4995 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4996 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4997 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 4998 | /* Goto is required to silence warnings about unused labels, as we |
| 4999 | * don't actually do any test assertions in this function. */ |
| 5000 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5001 | } |
| 5002 | /* END_CASE */ |
| 5003 | |
| 5004 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5005 | void aead_multipart_decrypt(int key_type_arg, data_t *key_data, |
| 5006 | int alg_arg, |
| 5007 | data_t *nonce, |
| 5008 | data_t *additional_data, |
| 5009 | data_t *input_data, |
| 5010 | int do_set_lengths, |
| 5011 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5012 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5013 | size_t ad_part_len = 0; |
| 5014 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5015 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5016 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5017 | 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] | 5018 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5019 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5020 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5021 | if (do_set_lengths) { |
| 5022 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5023 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5024 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5025 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5026 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5027 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5028 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5029 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5030 | alg_arg, nonce, |
| 5031 | additional_data, |
| 5032 | ad_part_len, |
| 5033 | input_data, -1, |
| 5034 | set_lengths_method, |
| 5035 | expected_output, |
| 5036 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5037 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5038 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5039 | |
| 5040 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5041 | mbedtls_test_set_step(1000 + ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5042 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5043 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5044 | alg_arg, nonce, |
| 5045 | additional_data, |
| 5046 | ad_part_len, |
| 5047 | input_data, -1, |
| 5048 | set_lengths_method, |
| 5049 | expected_output, |
| 5050 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5051 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5052 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5053 | } |
| 5054 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5055 | 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] | 5056 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5057 | mbedtls_test_set_step(2000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5058 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5059 | if (do_set_lengths) { |
| 5060 | if (data_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5061 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5062 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5063 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5064 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5065 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5066 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5067 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5068 | alg_arg, nonce, |
| 5069 | additional_data, -1, |
| 5070 | input_data, data_part_len, |
| 5071 | set_lengths_method, |
| 5072 | expected_output, |
| 5073 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5074 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5075 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5076 | |
| 5077 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5078 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5079 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5080 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5081 | alg_arg, nonce, |
| 5082 | additional_data, -1, |
| 5083 | input_data, data_part_len, |
| 5084 | set_lengths_method, |
| 5085 | expected_output, |
| 5086 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5087 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5088 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5089 | } |
| 5090 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5091 | /* Goto is required to silence warnings about unused labels, as we |
| 5092 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5093 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5094 | } |
| 5095 | /* END_CASE */ |
| 5096 | |
| 5097 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5098 | void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data, |
| 5099 | int alg_arg, |
| 5100 | int nonce_length, |
| 5101 | int expected_nonce_length_arg, |
| 5102 | data_t *additional_data, |
| 5103 | data_t *input_data, |
| 5104 | int expected_status_arg) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5105 | { |
| 5106 | |
| 5107 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5108 | psa_key_type_t key_type = key_type_arg; |
| 5109 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5110 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5111 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5112 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5113 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5114 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5115 | size_t actual_nonce_length = 0; |
| 5116 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 5117 | unsigned char *output = NULL; |
| 5118 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5119 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5120 | size_t ciphertext_size = 0; |
| 5121 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5122 | size_t tag_length = 0; |
| 5123 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5124 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5125 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5126 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5127 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5128 | psa_set_key_algorithm(&attributes, alg); |
| 5129 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5130 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5131 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5132 | &key)); |
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_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5135 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5136 | 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] | 5137 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5138 | ASSERT_ALLOC(output, output_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5139 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5140 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5141 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5142 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5143 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5144 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5145 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5146 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5147 | |
| 5148 | /* If the operation is not supported, just skip and not fail in case the |
| 5149 | * encryption involves a common limitation of cryptography hardwares and |
| 5150 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5151 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5152 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5153 | 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] | 5154 | } |
| 5155 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5156 | PSA_ASSERT(status); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5157 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5158 | status = psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5159 | nonce_length, |
| 5160 | &actual_nonce_length); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5161 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5162 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5164 | TEST_EQUAL(actual_nonce_length, expected_nonce_length); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 5165 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5166 | if (expected_status == PSA_SUCCESS) { |
| 5167 | TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type, |
| 5168 | alg)); |
| 5169 | } |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 5170 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5171 | TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 5172 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5173 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5174 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5175 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5176 | input_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5177 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5178 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5179 | additional_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5181 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5182 | output, output_size, |
| 5183 | &ciphertext_length)); |
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_finish(&operation, ciphertext, ciphertext_size, |
| 5186 | &ciphertext_length, tag_buffer, |
| 5187 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5188 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5189 | |
| 5190 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5191 | psa_destroy_key(key); |
| 5192 | mbedtls_free(output); |
| 5193 | mbedtls_free(ciphertext); |
| 5194 | psa_aead_abort(&operation); |
| 5195 | PSA_DONE(); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5196 | } |
| 5197 | /* END_CASE */ |
| 5198 | |
| 5199 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5200 | void aead_multipart_set_nonce(int key_type_arg, data_t *key_data, |
| 5201 | int alg_arg, |
| 5202 | int nonce_length_arg, |
| 5203 | int set_lengths_method_arg, |
| 5204 | data_t *additional_data, |
| 5205 | data_t *input_data, |
| 5206 | int expected_status_arg) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5207 | { |
| 5208 | |
| 5209 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5210 | psa_key_type_t key_type = key_type_arg; |
| 5211 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5212 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5213 | uint8_t *nonce_buffer = NULL; |
| 5214 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5215 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5216 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5217 | unsigned char *output = NULL; |
| 5218 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5219 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5220 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5221 | size_t ciphertext_size = 0; |
| 5222 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5223 | size_t tag_length = 0; |
| 5224 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5225 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5226 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5227 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5228 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5230 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5231 | psa_set_key_algorithm(&attributes, alg); |
| 5232 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5233 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5234 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5235 | &key)); |
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_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5238 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5239 | 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] | 5240 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5241 | ASSERT_ALLOC(output, output_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5242 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5243 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5244 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5245 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5246 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5247 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5248 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5249 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5250 | |
| 5251 | /* If the operation is not supported, just skip and not fail in case the |
| 5252 | * encryption involves a common limitation of cryptography hardwares and |
| 5253 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5254 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5255 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5256 | 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] | 5257 | } |
| 5258 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5259 | PSA_ASSERT(status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5260 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5261 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5262 | if (nonce_length_arg == -1) { |
| 5263 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
| 5264 | ASSERT_ALLOC(nonce_buffer, 4); |
| 5265 | nonce_length = 0; |
| 5266 | } else { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5267 | /* If length is zero, then this will return NULL. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5268 | nonce_length = (size_t) nonce_length_arg; |
| 5269 | ASSERT_ALLOC(nonce_buffer, nonce_length); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5270 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5271 | if (nonce_buffer) { |
| 5272 | for (index = 0; index < nonce_length - 1; ++index) { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5273 | nonce_buffer[index] = 'a' + index; |
| 5274 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5275 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5276 | } |
| 5277 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5278 | if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { |
| 5279 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5280 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5281 | } |
| 5282 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5283 | status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5284 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5285 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5286 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5287 | if (expected_status == PSA_SUCCESS) { |
| 5288 | if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) { |
| 5289 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5290 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5291 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5292 | 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] | 5293 | expected_status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5294 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5295 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5296 | /* 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] | 5297 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5298 | additional_data->len), |
| 5299 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5300 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5301 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5302 | output, output_size, |
| 5303 | &ciphertext_length), |
| 5304 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5306 | TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5307 | &ciphertext_length, tag_buffer, |
| 5308 | PSA_AEAD_TAG_MAX_SIZE, &tag_length), |
| 5309 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5310 | } |
| 5311 | |
| 5312 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5313 | psa_destroy_key(key); |
| 5314 | mbedtls_free(output); |
| 5315 | mbedtls_free(ciphertext); |
| 5316 | mbedtls_free(nonce_buffer); |
| 5317 | psa_aead_abort(&operation); |
| 5318 | PSA_DONE(); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5319 | } |
| 5320 | /* END_CASE */ |
| 5321 | |
| 5322 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5323 | 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] | 5324 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5325 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5326 | data_t *nonce, |
| 5327 | data_t *additional_data, |
| 5328 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5329 | int expected_status_arg) |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5330 | { |
| 5331 | |
| 5332 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5333 | psa_key_type_t key_type = key_type_arg; |
| 5334 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5335 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5336 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5337 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5338 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5339 | unsigned char *output = NULL; |
| 5340 | unsigned char *ciphertext = NULL; |
| 5341 | size_t output_size = output_size_arg; |
| 5342 | size_t ciphertext_size = 0; |
| 5343 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5344 | size_t tag_length = 0; |
| 5345 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5346 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5347 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5349 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5350 | psa_set_key_algorithm(&attributes, alg); |
| 5351 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5353 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5354 | &key)); |
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_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5358 | ASSERT_ALLOC(output, output_size); |
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 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5361 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5362 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5363 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5364 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5365 | |
| 5366 | /* If the operation is not supported, just skip and not fail in case the |
| 5367 | * encryption involves a common limitation of cryptography hardwares and |
| 5368 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5369 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5370 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5371 | 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] | 5372 | } |
| 5373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5374 | PSA_ASSERT(status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5375 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5376 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5377 | input_data->len)); |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5378 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5379 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5380 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5381 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5382 | additional_data->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5383 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5384 | status = psa_aead_update(&operation, input_data->x, input_data->len, |
| 5385 | output, output_size, &ciphertext_length); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5386 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5387 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5389 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5390 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5391 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5392 | &ciphertext_length, tag_buffer, |
| 5393 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5394 | } |
| 5395 | |
| 5396 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5397 | psa_destroy_key(key); |
| 5398 | mbedtls_free(output); |
| 5399 | mbedtls_free(ciphertext); |
| 5400 | psa_aead_abort(&operation); |
| 5401 | PSA_DONE(); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5402 | } |
| 5403 | /* END_CASE */ |
| 5404 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5405 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5406 | void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data, |
| 5407 | int alg_arg, |
| 5408 | int finish_ciphertext_size_arg, |
| 5409 | int tag_size_arg, |
| 5410 | data_t *nonce, |
| 5411 | data_t *additional_data, |
| 5412 | data_t *input_data, |
| 5413 | int expected_status_arg) |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5414 | { |
| 5415 | |
| 5416 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5417 | psa_key_type_t key_type = key_type_arg; |
| 5418 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5419 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5420 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5421 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5422 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5423 | unsigned char *ciphertext = NULL; |
| 5424 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5425 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5426 | size_t ciphertext_size = 0; |
| 5427 | size_t ciphertext_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5428 | size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg; |
| 5429 | size_t tag_size = (size_t) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5430 | size_t tag_length = 0; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5431 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5432 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5433 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5434 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5435 | psa_set_key_algorithm(&attributes, alg); |
| 5436 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5437 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5438 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5439 | &key)); |
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_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5442 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5443 | 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] | 5444 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5445 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5446 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5447 | ASSERT_ALLOC(finish_ciphertext, finish_ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5449 | ASSERT_ALLOC(tag_buffer, tag_size); |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5450 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5451 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5452 | |
| 5453 | /* If the operation is not supported, just skip and not fail in case the |
| 5454 | * encryption involves a common limitation of cryptography hardwares and |
| 5455 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5456 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5457 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5458 | 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] | 5459 | } |
| 5460 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5461 | PSA_ASSERT(status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5462 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5463 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5464 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5465 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5466 | input_data->len)); |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5467 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5468 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5469 | additional_data->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5470 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5471 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5472 | ciphertext, ciphertext_size, &ciphertext_length)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5473 | |
| 5474 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5475 | status = psa_aead_finish(&operation, finish_ciphertext, |
| 5476 | finish_ciphertext_size, |
| 5477 | &ciphertext_length, tag_buffer, |
| 5478 | tag_size, &tag_length); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5479 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5480 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5481 | |
| 5482 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5483 | psa_destroy_key(key); |
| 5484 | mbedtls_free(ciphertext); |
| 5485 | mbedtls_free(finish_ciphertext); |
| 5486 | mbedtls_free(tag_buffer); |
| 5487 | psa_aead_abort(&operation); |
| 5488 | PSA_DONE(); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5489 | } |
| 5490 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5491 | |
| 5492 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5493 | void aead_multipart_verify(int key_type_arg, data_t *key_data, |
| 5494 | int alg_arg, |
| 5495 | data_t *nonce, |
| 5496 | data_t *additional_data, |
| 5497 | data_t *input_data, |
| 5498 | data_t *tag, |
| 5499 | int tag_usage_arg, |
| 5500 | int expected_setup_status_arg, |
| 5501 | int expected_status_arg) |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5502 | { |
| 5503 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5504 | psa_key_type_t key_type = key_type_arg; |
| 5505 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5506 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5507 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5508 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5509 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5510 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5511 | unsigned char *plaintext = NULL; |
| 5512 | unsigned char *finish_plaintext = NULL; |
| 5513 | size_t plaintext_size = 0; |
| 5514 | size_t plaintext_length = 0; |
| 5515 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5516 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5517 | unsigned char *tag_buffer = NULL; |
| 5518 | size_t tag_size = 0; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5519 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5520 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5521 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5522 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 5523 | psa_set_key_algorithm(&attributes, alg); |
| 5524 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5525 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5526 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5527 | &key)); |
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_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5530 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5531 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 5532 | input_data->len); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5533 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5534 | ASSERT_ALLOC(plaintext, plaintext_size); |
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 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); |
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 | ASSERT_ALLOC(finish_plaintext, verify_plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5539 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5540 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5541 | |
| 5542 | /* If the operation is not supported, just skip and not fail in case the |
| 5543 | * encryption involves a common limitation of cryptography hardwares and |
| 5544 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5545 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5546 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5547 | 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] | 5548 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5549 | TEST_EQUAL(status, expected_setup_status); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5550 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5551 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5552 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5553 | } |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5554 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5555 | PSA_ASSERT(status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5556 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5557 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5558 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5559 | status = psa_aead_set_lengths(&operation, additional_data->len, |
| 5560 | input_data->len); |
| 5561 | PSA_ASSERT(status); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5562 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5563 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5564 | additional_data->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 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 5567 | input_data->len, |
| 5568 | plaintext, plaintext_size, |
| 5569 | &plaintext_length)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5570 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5571 | if (tag_usage == USE_GIVEN_TAG) { |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5572 | tag_buffer = tag->x; |
| 5573 | tag_size = tag->len; |
| 5574 | } |
| 5575 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5576 | status = psa_aead_verify(&operation, finish_plaintext, |
| 5577 | verify_plaintext_size, |
| 5578 | &plaintext_length, |
| 5579 | tag_buffer, tag_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5580 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5581 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5582 | |
| 5583 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5584 | psa_destroy_key(key); |
| 5585 | mbedtls_free(plaintext); |
| 5586 | mbedtls_free(finish_plaintext); |
| 5587 | psa_aead_abort(&operation); |
| 5588 | PSA_DONE(); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5589 | } |
| 5590 | /* END_CASE */ |
| 5591 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5592 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5593 | void aead_multipart_setup(int key_type_arg, data_t *key_data, |
| 5594 | int alg_arg, int expected_status_arg) |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5595 | { |
| 5596 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5597 | psa_key_type_t key_type = key_type_arg; |
| 5598 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5599 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5600 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5601 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5602 | psa_status_t expected_status = expected_status_arg; |
| 5603 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5604 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5605 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5606 | psa_set_key_usage_flags(&attributes, |
| 5607 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5608 | psa_set_key_algorithm(&attributes, alg); |
| 5609 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5610 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5611 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5612 | &key)); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5613 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5614 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5615 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5616 | TEST_EQUAL(status, expected_status); |
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_aead_abort(&operation); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5619 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5620 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5621 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5622 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5623 | |
| 5624 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5625 | psa_destroy_key(key); |
| 5626 | psa_aead_abort(&operation); |
| 5627 | PSA_DONE(); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5628 | } |
| 5629 | /* END_CASE */ |
| 5630 | |
| 5631 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5632 | void aead_multipart_state_test(int key_type_arg, data_t *key_data, |
| 5633 | int alg_arg, |
| 5634 | data_t *nonce, |
| 5635 | data_t *additional_data, |
| 5636 | data_t *input_data) |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5637 | { |
| 5638 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5639 | psa_key_type_t key_type = key_type_arg; |
| 5640 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5641 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5642 | unsigned char *output_data = NULL; |
| 5643 | unsigned char *final_data = NULL; |
| 5644 | size_t output_size = 0; |
| 5645 | size_t finish_output_size = 0; |
| 5646 | size_t output_length = 0; |
| 5647 | size_t key_bits = 0; |
| 5648 | size_t tag_length = 0; |
| 5649 | size_t tag_size = 0; |
| 5650 | size_t nonce_length = 0; |
| 5651 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5652 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5653 | size_t output_part_length = 0; |
| 5654 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5655 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5656 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5657 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5658 | psa_set_key_usage_flags(&attributes, |
| 5659 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5660 | psa_set_key_algorithm(&attributes, alg); |
| 5661 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5662 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5663 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5664 | &key)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5665 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5666 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5667 | key_bits = psa_get_key_bits(&attributes); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5668 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5669 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5670 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5671 | TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE); |
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 | 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] | 5674 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5675 | ASSERT_ALLOC(output_data, output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5676 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5677 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5678 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5679 | TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5680 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5681 | ASSERT_ALLOC(final_data, finish_output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5682 | |
| 5683 | /* Test all operations error without calling setup first. */ |
| 5684 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5685 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5686 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5687 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5688 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5689 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5690 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5691 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5692 | &nonce_length), |
| 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 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5697 | /* ------------------------------------------------------- */ |
| 5698 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5699 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 5700 | input_data->len), |
| 5701 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5702 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5703 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5704 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5705 | /* ------------------------------------------------------- */ |
| 5706 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5707 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5708 | additional_data->len), |
| 5709 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5710 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5711 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5712 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5713 | /* ------------------------------------------------------- */ |
| 5714 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5715 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 5716 | input_data->len, output_data, |
| 5717 | output_size, &output_length), |
| 5718 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5719 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5720 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5721 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5722 | /* ------------------------------------------------------- */ |
| 5723 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5724 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 5725 | finish_output_size, |
| 5726 | &output_part_length, |
| 5727 | tag_buffer, tag_length, |
| 5728 | &tag_size), |
| 5729 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5730 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5731 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5732 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5733 | /* ------------------------------------------------------- */ |
| 5734 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5735 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 5736 | finish_output_size, |
| 5737 | &output_part_length, |
| 5738 | tag_buffer, |
| 5739 | tag_length), |
| 5740 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5741 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5742 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5743 | |
| 5744 | /* Test for double setups. */ |
| 5745 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5746 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5747 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5748 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 5749 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5750 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5751 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5752 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5753 | /* ------------------------------------------------------- */ |
| 5754 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5755 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5756 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5757 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 5758 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5759 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5760 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5761 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5762 | /* ------------------------------------------------------- */ |
| 5763 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5764 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5765 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5766 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 5767 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5768 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5769 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5770 | |
| 5771 | /* ------------------------------------------------------- */ |
| 5772 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5773 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5774 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5775 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 5776 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5777 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5778 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5779 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5780 | /* Test for not setting a nonce. */ |
| 5781 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5782 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5783 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5784 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5785 | additional_data->len), |
| 5786 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5787 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5788 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5789 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5790 | /* ------------------------------------------------------- */ |
| 5791 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5792 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5793 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5794 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 5795 | input_data->len, output_data, |
| 5796 | output_size, &output_length), |
| 5797 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5798 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5799 | psa_aead_abort(&operation); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5800 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5801 | /* ------------------------------------------------------- */ |
| 5802 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5803 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5804 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5805 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 5806 | finish_output_size, |
| 5807 | &output_part_length, |
| 5808 | tag_buffer, tag_length, |
| 5809 | &tag_size), |
| 5810 | PSA_ERROR_BAD_STATE); |
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 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5813 | |
| 5814 | /* ------------------------------------------------------- */ |
| 5815 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5816 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5817 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5818 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 5819 | finish_output_size, |
| 5820 | &output_part_length, |
| 5821 | tag_buffer, |
| 5822 | tag_length), |
| 5823 | PSA_ERROR_BAD_STATE); |
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 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5826 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5827 | /* Test for double setting nonce. */ |
| 5828 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5829 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5830 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5831 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5832 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5833 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5834 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5835 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5836 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5837 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5838 | /* Test for double generating nonce. */ |
| 5839 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5840 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5841 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5842 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5843 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5844 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5845 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5846 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5847 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5848 | &nonce_length), |
| 5849 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5850 | |
| 5851 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5852 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5853 | |
| 5854 | /* Test for generate nonce then set and vice versa */ |
| 5855 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5856 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5857 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5858 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5859 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5860 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5861 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5862 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5863 | PSA_ERROR_BAD_STATE); |
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_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5866 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5867 | /* Test for generating nonce after calling set lengths */ |
| 5868 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5869 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5870 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5871 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5872 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5873 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5874 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5875 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5876 | &nonce_length)); |
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_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5879 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5880 | /* 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] | 5881 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5882 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5883 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5884 | if (operation.alg == PSA_ALG_CCM) { |
| 5885 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5886 | input_data->len), |
| 5887 | PSA_ERROR_INVALID_ARGUMENT); |
| 5888 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5889 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5890 | &nonce_length), |
| 5891 | PSA_ERROR_BAD_STATE); |
| 5892 | } else { |
| 5893 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5894 | input_data->len)); |
| 5895 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5896 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5897 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5898 | } |
| 5899 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5900 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5901 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5902 | /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */ |
Dave Rodgman | d26d744 | 2023-02-11 17:14:54 +0000 | [diff] [blame] | 5903 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5904 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5905 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5906 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 5907 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5908 | input_data->len), |
| 5909 | PSA_ERROR_INVALID_ARGUMENT); |
| 5910 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5911 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5912 | &nonce_length), |
| 5913 | PSA_ERROR_BAD_STATE); |
| 5914 | } else { |
| 5915 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5916 | input_data->len)); |
| 5917 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5918 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5919 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5920 | } |
| 5921 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5922 | psa_aead_abort(&operation); |
Dave Rodgman | d26d744 | 2023-02-11 17:14:54 +0000 | [diff] [blame] | 5923 | #endif |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5924 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5925 | /* Test for 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] | 5926 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5927 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5928 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5929 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5930 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5931 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5932 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5933 | if (operation.alg == PSA_ALG_CCM) { |
| 5934 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5935 | input_data->len), |
| 5936 | PSA_ERROR_INVALID_ARGUMENT); |
| 5937 | } else { |
| 5938 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5939 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5940 | } |
| 5941 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5942 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5943 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5944 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5945 | /* Test for setting nonce after calling set lengths */ |
| 5946 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5947 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5948 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5949 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5950 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5951 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5952 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5953 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5954 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5955 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5956 | /* 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] | 5957 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5958 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5959 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5960 | if (operation.alg == PSA_ALG_CCM) { |
| 5961 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5962 | input_data->len), |
| 5963 | PSA_ERROR_INVALID_ARGUMENT); |
| 5964 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5965 | PSA_ERROR_BAD_STATE); |
| 5966 | } else { |
| 5967 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5968 | input_data->len)); |
| 5969 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5970 | } |
| 5971 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5972 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5973 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5974 | /* 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] | 5975 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5976 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5977 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5978 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 5979 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5980 | input_data->len), |
| 5981 | PSA_ERROR_INVALID_ARGUMENT); |
| 5982 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5983 | PSA_ERROR_BAD_STATE); |
| 5984 | } else { |
| 5985 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5986 | input_data->len)); |
| 5987 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5988 | } |
| 5989 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5990 | psa_aead_abort(&operation); |
Dave Rodgman | a476363 | 2023-02-11 18:36:23 +0000 | [diff] [blame] | 5991 | #endif |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5992 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5993 | /* 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] | 5994 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5995 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5996 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5997 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5998 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5999 | if (operation.alg == PSA_ALG_CCM) { |
| 6000 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6001 | input_data->len), |
| 6002 | PSA_ERROR_INVALID_ARGUMENT); |
| 6003 | } else { |
| 6004 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6005 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6006 | } |
| 6007 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6008 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6009 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6010 | /* 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] | 6011 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6012 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6013 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6014 | if (operation.alg == PSA_ALG_GCM) { |
| 6015 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6016 | SIZE_MAX), |
| 6017 | PSA_ERROR_INVALID_ARGUMENT); |
| 6018 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6019 | PSA_ERROR_BAD_STATE); |
| 6020 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6021 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6022 | SIZE_MAX)); |
| 6023 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6024 | } |
| 6025 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6026 | psa_aead_abort(&operation); |
Dave Rodgman | 91e8321 | 2023-02-11 20:07:43 +0000 | [diff] [blame] | 6027 | #endif |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6028 | |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 6029 | /* 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] | 6030 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6031 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6032 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6033 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6035 | if (operation.alg == PSA_ALG_GCM) { |
| 6036 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6037 | SIZE_MAX), |
| 6038 | PSA_ERROR_INVALID_ARGUMENT); |
| 6039 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6040 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6041 | SIZE_MAX)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6042 | } |
| 6043 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6044 | psa_aead_abort(&operation); |
Dave Rodgman | 641288b | 2023-02-11 22:02:04 +0000 | [diff] [blame] | 6045 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6046 | |
| 6047 | /* ------------------------------------------------------- */ |
| 6048 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6049 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6050 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6051 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6052 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6053 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6054 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6055 | &nonce_length), |
| 6056 | PSA_ERROR_BAD_STATE); |
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_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6059 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6060 | /* Test for generating nonce in decrypt setup. */ |
| 6061 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6062 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6063 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6064 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6065 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6066 | &nonce_length), |
| 6067 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6068 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6069 | psa_aead_abort(&operation); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6070 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6071 | /* Test for setting lengths twice. */ |
| 6072 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6073 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6074 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6075 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6076 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6077 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6078 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6079 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6080 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6081 | input_data->len), |
| 6082 | PSA_ERROR_BAD_STATE); |
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_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6085 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6086 | /* Test for setting lengths after setting nonce + already starting data. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6087 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6088 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6089 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6090 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6091 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6092 | if (operation.alg == PSA_ALG_CCM) { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6093 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6094 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6095 | additional_data->len), |
| 6096 | PSA_ERROR_BAD_STATE); |
| 6097 | } else { |
| 6098 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6099 | additional_data->len)); |
| 6100 | |
| 6101 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6102 | input_data->len), |
| 6103 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6104 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6105 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6106 | |
| 6107 | /* ------------------------------------------------------- */ |
| 6108 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6109 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6110 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6111 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6112 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6113 | if (operation.alg == PSA_ALG_CCM) { |
| 6114 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6115 | input_data->len, output_data, |
| 6116 | output_size, &output_length), |
| 6117 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6118 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6119 | } else { |
| 6120 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6121 | input_data->len, output_data, |
| 6122 | output_size, &output_length)); |
| 6123 | |
| 6124 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6125 | input_data->len), |
| 6126 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6127 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6128 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6129 | |
| 6130 | /* ------------------------------------------------------- */ |
| 6131 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6132 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6133 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6134 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6135 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6136 | if (operation.alg == PSA_ALG_CCM) { |
| 6137 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6138 | finish_output_size, |
| 6139 | &output_part_length, |
| 6140 | tag_buffer, tag_length, |
| 6141 | &tag_size)); |
| 6142 | } else { |
| 6143 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6144 | finish_output_size, |
| 6145 | &output_part_length, |
| 6146 | tag_buffer, tag_length, |
| 6147 | &tag_size)); |
| 6148 | |
| 6149 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6150 | input_data->len), |
| 6151 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6152 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6153 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6154 | |
| 6155 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 6156 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6157 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6159 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6160 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6161 | &nonce_length)); |
| 6162 | if (operation.alg == PSA_ALG_CCM) { |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6164 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6165 | additional_data->len), |
| 6166 | PSA_ERROR_BAD_STATE); |
| 6167 | } else { |
| 6168 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6169 | additional_data->len)); |
| 6170 | |
| 6171 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6172 | input_data->len), |
| 6173 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6174 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6175 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6176 | |
| 6177 | /* ------------------------------------------------------- */ |
| 6178 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6179 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6181 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6182 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6183 | &nonce_length)); |
| 6184 | if (operation.alg == PSA_ALG_CCM) { |
| 6185 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6186 | input_data->len, output_data, |
| 6187 | output_size, &output_length), |
| 6188 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6189 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6190 | } else { |
| 6191 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6192 | input_data->len, output_data, |
| 6193 | output_size, &output_length)); |
| 6194 | |
| 6195 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6196 | input_data->len), |
| 6197 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6198 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6199 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6200 | |
| 6201 | /* ------------------------------------------------------- */ |
| 6202 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6203 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6204 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6205 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6206 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6207 | &nonce_length)); |
| 6208 | if (operation.alg == PSA_ALG_CCM) { |
| 6209 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6210 | finish_output_size, |
| 6211 | &output_part_length, |
| 6212 | tag_buffer, tag_length, |
| 6213 | &tag_size)); |
| 6214 | } else { |
| 6215 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6216 | finish_output_size, |
| 6217 | &output_part_length, |
| 6218 | tag_buffer, tag_length, |
| 6219 | &tag_size)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6221 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6222 | input_data->len), |
| 6223 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6224 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6225 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6226 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6227 | /* Test for not sending any additional data or data after setting non zero |
| 6228 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6230 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6231 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6232 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6233 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6234 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6235 | input_data->len)); |
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 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6238 | finish_output_size, |
| 6239 | &output_part_length, |
| 6240 | tag_buffer, tag_length, |
| 6241 | &tag_size), |
| 6242 | PSA_ERROR_INVALID_ARGUMENT); |
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 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6245 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6246 | /* Test for not sending any additional data or data after setting non-zero |
| 6247 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6248 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6249 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
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_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6252 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6253 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6254 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6256 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6257 | finish_output_size, |
| 6258 | &output_part_length, |
| 6259 | tag_buffer, |
| 6260 | tag_length), |
| 6261 | PSA_ERROR_INVALID_ARGUMENT); |
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 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6264 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6265 | /* Test for not sending any additional data after setting a non-zero length |
| 6266 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6267 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6268 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6271 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6272 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6273 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6274 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6275 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6276 | input_data->len, output_data, |
| 6277 | output_size, &output_length), |
| 6278 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6279 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6280 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6281 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6282 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 6283 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6284 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6285 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6286 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6287 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6288 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6289 | input_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6290 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6291 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6292 | additional_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6293 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6294 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6295 | finish_output_size, |
| 6296 | &output_part_length, |
| 6297 | tag_buffer, tag_length, |
| 6298 | &tag_size), |
| 6299 | PSA_ERROR_INVALID_ARGUMENT); |
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 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6302 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6303 | /* Test for sending too much additional data after setting lengths. */ |
| 6304 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6305 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6306 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6307 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6308 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6309 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6310 | |
| 6311 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6312 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6313 | additional_data->len), |
| 6314 | PSA_ERROR_INVALID_ARGUMENT); |
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_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6317 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6318 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6319 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6320 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6321 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6322 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6323 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6324 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6325 | input_data->len)); |
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_update_ad(&operation, additional_data->x, |
| 6328 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6329 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6330 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6331 | 1), |
| 6332 | PSA_ERROR_INVALID_ARGUMENT); |
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_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6335 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6336 | /* Test for sending too much data after setting lengths. */ |
| 6337 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6338 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6339 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6340 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6341 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6342 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6343 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6344 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6345 | input_data->len, output_data, |
| 6346 | output_size, &output_length), |
| 6347 | PSA_ERROR_INVALID_ARGUMENT); |
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_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6350 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6351 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6353 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6354 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6355 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6356 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6357 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6358 | input_data->len)); |
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_update_ad(&operation, additional_data->x, |
| 6361 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6362 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6363 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6364 | input_data->len, output_data, |
| 6365 | output_size, &output_length)); |
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 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6368 | 1, output_data, |
| 6369 | output_size, &output_length), |
| 6370 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6371 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6372 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6373 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6374 | /* Test sending additional data after data. */ |
| 6375 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6376 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6377 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6378 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6379 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6380 | if (operation.alg != PSA_ALG_CCM) { |
| 6381 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6382 | input_data->len, output_data, |
| 6383 | output_size, &output_length)); |
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 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6386 | additional_data->len), |
| 6387 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6388 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6389 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6390 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6391 | /* Test calling finish on decryption. */ |
| 6392 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6393 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6394 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6395 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6396 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6397 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6398 | finish_output_size, |
| 6399 | &output_part_length, |
| 6400 | tag_buffer, tag_length, |
| 6401 | &tag_size), |
| 6402 | PSA_ERROR_BAD_STATE); |
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 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6405 | |
| 6406 | /* Test calling verify on encryption. */ |
| 6407 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6408 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6409 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6410 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6411 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6412 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6413 | finish_output_size, |
| 6414 | &output_part_length, |
| 6415 | tag_buffer, |
| 6416 | tag_length), |
| 6417 | PSA_ERROR_BAD_STATE); |
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 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6420 | |
| 6421 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6422 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6423 | psa_destroy_key(key); |
| 6424 | psa_aead_abort(&operation); |
| 6425 | mbedtls_free(output_data); |
| 6426 | mbedtls_free(final_data); |
| 6427 | PSA_DONE(); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6428 | } |
| 6429 | /* END_CASE */ |
| 6430 | |
| 6431 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6432 | void signature_size(int type_arg, |
| 6433 | int bits, |
| 6434 | int alg_arg, |
| 6435 | int expected_size_arg) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6436 | { |
| 6437 | psa_key_type_t type = type_arg; |
| 6438 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6439 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6440 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6441 | TEST_EQUAL(actual_size, (size_t) expected_size_arg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6442 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6443 | exit: |
| 6444 | ; |
| 6445 | } |
| 6446 | /* END_CASE */ |
| 6447 | |
| 6448 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6449 | void sign_hash_deterministic(int key_type_arg, data_t *key_data, |
| 6450 | int alg_arg, data_t *input_data, |
| 6451 | data_t *output_data) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6452 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6453 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6454 | psa_key_type_t key_type = key_type_arg; |
| 6455 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6456 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6457 | unsigned char *signature = NULL; |
| 6458 | size_t signature_size; |
| 6459 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6460 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6461 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6462 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6463 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6464 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6465 | psa_set_key_algorithm(&attributes, alg); |
| 6466 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6467 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6468 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6469 | &key)); |
| 6470 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6471 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6472 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6473 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6474 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6475 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6476 | key_bits, alg); |
| 6477 | TEST_ASSERT(signature_size != 0); |
| 6478 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6479 | ASSERT_ALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6480 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6481 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6482 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6483 | input_data->x, input_data->len, |
| 6484 | signature, signature_size, |
| 6485 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6486 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6487 | ASSERT_COMPARE(output_data->x, output_data->len, |
| 6488 | signature, signature_length); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6489 | |
| 6490 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6491 | /* |
| 6492 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6493 | * thus reset them as required. |
| 6494 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6495 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6496 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6497 | psa_destroy_key(key); |
| 6498 | mbedtls_free(signature); |
| 6499 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6500 | } |
| 6501 | /* END_CASE */ |
| 6502 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6503 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6504 | /** |
| 6505 | * sign_hash_interruptible() test intentions: |
| 6506 | * |
| 6507 | * Note: This test can currently only handle ECDSA. |
| 6508 | * |
| 6509 | * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 6510 | * and private keys / keypairs only). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6511 | * |
| 6512 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 6513 | * expected for different max_ops values. |
| 6514 | * |
| 6515 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 6516 | * and that each successful stage completes some ops (this is not mandated by |
| 6517 | * the PSA specification, but is currently the case). |
| 6518 | * |
| 6519 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 6520 | * complete() calls does not alter the number of ops returned. |
| 6521 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6522 | void sign_hash_interruptible(int key_type_arg, data_t *key_data, |
| 6523 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6524 | data_t *output_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6525 | { |
| 6526 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6527 | psa_key_type_t key_type = key_type_arg; |
| 6528 | psa_algorithm_t alg = alg_arg; |
| 6529 | size_t key_bits; |
| 6530 | unsigned char *signature = NULL; |
| 6531 | size_t signature_size; |
| 6532 | size_t signature_length = 0xdeadbeef; |
| 6533 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6534 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6535 | uint32_t num_ops = 0; |
| 6536 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6537 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6538 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6539 | size_t min_completes = 0; |
| 6540 | size_t max_completes = 0; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6541 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6542 | psa_sign_hash_interruptible_operation_t operation = |
| 6543 | psa_sign_hash_interruptible_operation_init(); |
| 6544 | |
| 6545 | PSA_ASSERT(psa_crypto_init()); |
| 6546 | |
| 6547 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6548 | psa_set_key_algorithm(&attributes, alg); |
| 6549 | psa_set_key_type(&attributes, key_type); |
| 6550 | |
| 6551 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6552 | &key)); |
| 6553 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6554 | key_bits = psa_get_key_bits(&attributes); |
| 6555 | |
| 6556 | /* Allocate a buffer which has the size advertised by the |
| 6557 | * library. */ |
| 6558 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6559 | key_bits, alg); |
| 6560 | TEST_ASSERT(signature_size != 0); |
| 6561 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6562 | ASSERT_ALLOC(signature, signature_size); |
| 6563 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6564 | psa_interruptible_set_max_ops(max_ops); |
| 6565 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6566 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6567 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6568 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6569 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6570 | TEST_ASSERT(num_ops_prior == 0); |
| 6571 | |
| 6572 | /* Start performing the signature. */ |
| 6573 | PSA_ASSERT(psa_sign_hash_start(&operation, key, alg, |
| 6574 | input_data->x, input_data->len)); |
| 6575 | |
| 6576 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6577 | TEST_ASSERT(num_ops_prior == 0); |
| 6578 | |
| 6579 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6580 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6581 | status = psa_sign_hash_complete(&operation, signature, signature_size, |
| 6582 | &signature_length); |
| 6583 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6584 | num_completes++; |
| 6585 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6586 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 6587 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6588 | /* We are asserting here that every complete makes progress |
| 6589 | * (completes some ops), which is true of the internal |
| 6590 | * implementation and probably any implementation, however this is |
| 6591 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6592 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6593 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6594 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 6595 | |
| 6596 | /* Ensure calling get_num_ops() twice still returns the same |
| 6597 | * number of ops as previously reported. */ |
| 6598 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6599 | |
| 6600 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6601 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6602 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6603 | |
| 6604 | TEST_ASSERT(status == PSA_SUCCESS); |
| 6605 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6606 | TEST_LE_U(min_completes, num_completes); |
| 6607 | TEST_LE_U(num_completes, max_completes); |
| 6608 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6609 | /* Verify that the signature is what is expected. */ |
| 6610 | ASSERT_COMPARE(output_data->x, output_data->len, |
| 6611 | signature, signature_length); |
| 6612 | |
| 6613 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6614 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6615 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6616 | TEST_ASSERT(num_ops == 0); |
| 6617 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6618 | exit: |
| 6619 | |
| 6620 | /* |
| 6621 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6622 | * thus reset them as required. |
| 6623 | */ |
| 6624 | psa_reset_key_attributes(&attributes); |
| 6625 | |
| 6626 | psa_destroy_key(key); |
| 6627 | mbedtls_free(signature); |
| 6628 | PSA_DONE(); |
| 6629 | } |
| 6630 | /* END_CASE */ |
| 6631 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6632 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6633 | void sign_hash_fail(int key_type_arg, data_t *key_data, |
| 6634 | int alg_arg, data_t *input_data, |
| 6635 | int signature_size_arg, int expected_status_arg) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6636 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6637 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6638 | psa_key_type_t key_type = key_type_arg; |
| 6639 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6640 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6641 | psa_status_t actual_status; |
| 6642 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 6643 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 6644 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6645 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6646 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6647 | ASSERT_ALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6648 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6649 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6650 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6651 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6652 | psa_set_key_algorithm(&attributes, alg); |
| 6653 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6654 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6655 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6656 | &key)); |
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 | actual_status = psa_sign_hash(key, alg, |
| 6659 | input_data->x, input_data->len, |
| 6660 | signature, signature_size, |
| 6661 | &signature_length); |
| 6662 | TEST_EQUAL(actual_status, expected_status); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6663 | /* The value of *signature_length is unspecified on error, but |
| 6664 | * whatever it is, it should be less than signature_size, so that |
| 6665 | * if the caller tries to read *signature_length bytes without |
| 6666 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6667 | TEST_LE_U(signature_length, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6668 | |
| 6669 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6670 | psa_reset_key_attributes(&attributes); |
| 6671 | psa_destroy_key(key); |
| 6672 | mbedtls_free(signature); |
| 6673 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6674 | } |
| 6675 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6676 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6677 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6678 | /** |
| 6679 | * sign_hash_fail_interruptible() test intentions: |
| 6680 | * |
| 6681 | * Note: This test can currently only handle ECDSA. |
| 6682 | * |
| 6683 | * 1. Test that various failure cases for interruptible sign hash fail with the |
| 6684 | * correct error codes, and at the correct point (at start or during |
| 6685 | * complete). |
| 6686 | * |
| 6687 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 6688 | * expected for different max_ops values. |
| 6689 | * |
| 6690 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 6691 | * and that each successful stage completes some ops (this is not mandated by |
| 6692 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 6693 | * |
| 6694 | * 4. Check that calling complete() when start() fails and complete() |
| 6695 | * after completion results in a BAD_STATE error. |
| 6696 | * |
| 6697 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 6698 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6699 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6700 | void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 6701 | int alg_arg, data_t *input_data, |
| 6702 | int signature_size_arg, |
| 6703 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6704 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6705 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6706 | { |
| 6707 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6708 | psa_key_type_t key_type = key_type_arg; |
| 6709 | psa_algorithm_t alg = alg_arg; |
| 6710 | size_t signature_size = signature_size_arg; |
| 6711 | psa_status_t actual_status; |
| 6712 | psa_status_t expected_start_status = expected_start_status_arg; |
| 6713 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 6714 | unsigned char *signature = NULL; |
| 6715 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6716 | uint32_t num_ops = 0; |
| 6717 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6718 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6719 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6720 | size_t min_completes = 0; |
| 6721 | size_t max_completes = 0; |
| 6722 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6723 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6724 | psa_sign_hash_interruptible_operation_t operation = |
| 6725 | psa_sign_hash_interruptible_operation_init(); |
| 6726 | |
| 6727 | ASSERT_ALLOC(signature, signature_size); |
| 6728 | |
| 6729 | PSA_ASSERT(psa_crypto_init()); |
| 6730 | |
| 6731 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6732 | psa_set_key_algorithm(&attributes, alg); |
| 6733 | psa_set_key_type(&attributes, key_type); |
| 6734 | |
| 6735 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6736 | &key)); |
| 6737 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6738 | psa_interruptible_set_max_ops(max_ops); |
| 6739 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6740 | interruptible_signverify_get_minmax_completes(max_ops, |
| 6741 | expected_complete_status, |
| 6742 | &min_completes, |
| 6743 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6744 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6745 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6746 | TEST_ASSERT(num_ops_prior == 0); |
| 6747 | |
| 6748 | /* Start performing the signature. */ |
| 6749 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 6750 | input_data->x, input_data->len); |
| 6751 | |
| 6752 | TEST_EQUAL(actual_status, expected_start_status); |
| 6753 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6754 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 6755 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 6756 | * start failed. */ |
| 6757 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6758 | signature_size, |
| 6759 | &signature_length); |
| 6760 | |
| 6761 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 6762 | |
| 6763 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6764 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 6765 | input_data->x, input_data->len); |
| 6766 | |
| 6767 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 6768 | } |
| 6769 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6770 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6771 | TEST_ASSERT(num_ops_prior == 0); |
| 6772 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6773 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6774 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6775 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6776 | signature_size, |
| 6777 | &signature_length); |
| 6778 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6779 | num_completes++; |
| 6780 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6781 | if (actual_status == PSA_SUCCESS || |
| 6782 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6783 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6784 | /* We are asserting here that every complete makes progress |
| 6785 | * (completes some ops), which is true of the internal |
| 6786 | * implementation and probably any implementation, however this is |
| 6787 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6788 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6789 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6790 | num_ops_prior = num_ops; |
| 6791 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6792 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6793 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6794 | TEST_EQUAL(actual_status, expected_complete_status); |
| 6795 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 6796 | /* Check that another complete returns BAD_STATE. */ |
| 6797 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6798 | signature_size, |
| 6799 | &signature_length); |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6800 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 6801 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6802 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6803 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6804 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6805 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6806 | TEST_ASSERT(num_ops == 0); |
| 6807 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6808 | /* The value of *signature_length is unspecified on error, but |
| 6809 | * whatever it is, it should be less than signature_size, so that |
| 6810 | * if the caller tries to read *signature_length bytes without |
| 6811 | * checking the error code then they don't overflow a buffer. */ |
| 6812 | TEST_LE_U(signature_length, signature_size); |
| 6813 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6814 | TEST_LE_U(min_completes, num_completes); |
| 6815 | TEST_LE_U(num_completes, max_completes); |
| 6816 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6817 | exit: |
| 6818 | psa_reset_key_attributes(&attributes); |
| 6819 | psa_destroy_key(key); |
| 6820 | mbedtls_free(signature); |
| 6821 | PSA_DONE(); |
| 6822 | } |
| 6823 | /* END_CASE */ |
| 6824 | |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6825 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6826 | void sign_verify_hash(int key_type_arg, data_t *key_data, |
| 6827 | int alg_arg, data_t *input_data) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6828 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6829 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6830 | psa_key_type_t key_type = key_type_arg; |
| 6831 | psa_algorithm_t alg = alg_arg; |
| 6832 | size_t key_bits; |
| 6833 | unsigned char *signature = NULL; |
| 6834 | size_t signature_size; |
| 6835 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6836 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6837 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6838 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6839 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6840 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 6841 | psa_set_key_algorithm(&attributes, alg); |
| 6842 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6843 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6844 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6845 | &key)); |
| 6846 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6847 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6848 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6849 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6850 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6851 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6852 | key_bits, alg); |
| 6853 | TEST_ASSERT(signature_size != 0); |
| 6854 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6855 | ASSERT_ALLOC(signature, signature_size); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6856 | |
| 6857 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6858 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6859 | input_data->x, input_data->len, |
| 6860 | signature, signature_size, |
| 6861 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6862 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6863 | TEST_LE_U(signature_length, signature_size); |
| 6864 | TEST_ASSERT(signature_length > 0); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6865 | |
| 6866 | /* Use the library to verify that the signature is correct. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6867 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 6868 | input_data->x, input_data->len, |
| 6869 | signature, signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6870 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6871 | if (input_data->len != 0) { |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6872 | /* Flip a bit in the input and verify that the signature is now |
| 6873 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6874 | * because ECDSA may ignore the last few bits of the input. */ |
| 6875 | input_data->x[0] ^= 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6876 | TEST_EQUAL(psa_verify_hash(key, alg, |
| 6877 | input_data->x, input_data->len, |
| 6878 | signature, signature_length), |
| 6879 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6880 | } |
| 6881 | |
| 6882 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6883 | /* |
| 6884 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6885 | * thus reset them as required. |
| 6886 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6887 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6888 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6889 | psa_destroy_key(key); |
| 6890 | mbedtls_free(signature); |
| 6891 | PSA_DONE(); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6892 | } |
| 6893 | /* END_CASE */ |
| 6894 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6895 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6896 | /** |
| 6897 | * sign_verify_hash_interruptible() test intentions: |
| 6898 | * |
| 6899 | * Note: This test can currently only handle ECDSA. |
| 6900 | * |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 6901 | * 1. Test that we can sign an input hash with the given keypair and then |
| 6902 | * afterwards verify that signature. This is currently the only way to test |
| 6903 | * non deterministic ECDSA, but this test can also handle deterministic. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6904 | * |
| 6905 | * 2. Test that after corrupting the hash, the verification detects an invalid |
| 6906 | * signature. |
| 6907 | * |
| 6908 | * 3. Test the number of calls to psa_sign_hash_complete() required are as |
| 6909 | * expected for different max_ops values. |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6910 | * |
| 6911 | * 4. Test that the number of ops done prior to starting signing and after abort |
| 6912 | * is zero and that each successful signing stage completes some ops (this is |
| 6913 | * not mandated by the PSA specification, but is currently the case). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6914 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6915 | 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] | 6916 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6917 | int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6918 | { |
| 6919 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6920 | psa_key_type_t key_type = key_type_arg; |
| 6921 | psa_algorithm_t alg = alg_arg; |
| 6922 | size_t key_bits; |
| 6923 | unsigned char *signature = NULL; |
| 6924 | size_t signature_size; |
| 6925 | size_t signature_length = 0xdeadbeef; |
| 6926 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6927 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6928 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6929 | uint32_t num_ops = 0; |
| 6930 | uint32_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6931 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6932 | size_t min_completes = 0; |
| 6933 | size_t max_completes = 0; |
| 6934 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6935 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 6936 | psa_sign_hash_interruptible_operation_init(); |
| 6937 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 6938 | psa_verify_hash_interruptible_operation_init(); |
| 6939 | |
| 6940 | PSA_ASSERT(psa_crypto_init()); |
| 6941 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6942 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 6943 | PSA_KEY_USAGE_VERIFY_HASH); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6944 | psa_set_key_algorithm(&attributes, alg); |
| 6945 | psa_set_key_type(&attributes, key_type); |
| 6946 | |
| 6947 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6948 | &key)); |
| 6949 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6950 | key_bits = psa_get_key_bits(&attributes); |
| 6951 | |
| 6952 | /* Allocate a buffer which has the size advertised by the |
| 6953 | * library. */ |
| 6954 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6955 | key_bits, alg); |
| 6956 | TEST_ASSERT(signature_size != 0); |
| 6957 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6958 | ASSERT_ALLOC(signature, signature_size); |
| 6959 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6960 | psa_interruptible_set_max_ops(max_ops); |
| 6961 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6962 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6963 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6964 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6965 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); |
| 6966 | TEST_ASSERT(num_ops_prior == 0); |
| 6967 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6968 | /* Start performing the signature. */ |
| 6969 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 6970 | input_data->x, input_data->len)); |
| 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 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6976 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6977 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6978 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 6979 | signature_size, |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6980 | &signature_length); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6981 | |
| 6982 | num_completes++; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6983 | |
| 6984 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 6985 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 6986 | /* We are asserting here that every complete makes progress |
| 6987 | * (completes some ops), which is true of the internal |
| 6988 | * implementation and probably any implementation, however this is |
| 6989 | * not mandated by the PSA specification. */ |
| 6990 | TEST_ASSERT(num_ops > num_ops_prior); |
| 6991 | |
| 6992 | num_ops_prior = num_ops; |
| 6993 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6994 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6995 | |
| 6996 | TEST_ASSERT(status == PSA_SUCCESS); |
| 6997 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6998 | TEST_LE_U(min_completes, num_completes); |
| 6999 | TEST_LE_U(num_completes, max_completes); |
| 7000 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7001 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7002 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7003 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7004 | TEST_ASSERT(num_ops == 0); |
| 7005 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7006 | /* Check that the signature length looks sensible. */ |
| 7007 | TEST_LE_U(signature_length, signature_size); |
| 7008 | TEST_ASSERT(signature_length > 0); |
| 7009 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7010 | num_completes = 0; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7011 | |
| 7012 | /* Start verification. */ |
| 7013 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7014 | input_data->x, input_data->len, |
| 7015 | signature, signature_length)); |
| 7016 | |
| 7017 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7018 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7019 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7020 | |
| 7021 | num_completes++; |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7022 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7023 | |
| 7024 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7025 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7026 | TEST_LE_U(min_completes, num_completes); |
| 7027 | TEST_LE_U(num_completes, max_completes); |
| 7028 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7029 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7030 | |
| 7031 | verify_operation = psa_verify_hash_interruptible_operation_init(); |
| 7032 | |
| 7033 | if (input_data->len != 0) { |
| 7034 | /* Flip a bit in the input and verify that the signature is now |
| 7035 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 7036 | * because ECDSA may ignore the last few bits of the input. */ |
| 7037 | input_data->x[0] ^= 1; |
| 7038 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7039 | /* Start verification. */ |
| 7040 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7041 | input_data->x, input_data->len, |
| 7042 | signature, signature_length)); |
| 7043 | |
| 7044 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7045 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7046 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7047 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7048 | |
| 7049 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7050 | } |
| 7051 | |
| 7052 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7053 | |
| 7054 | exit: |
| 7055 | /* |
| 7056 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7057 | * thus reset them as required. |
| 7058 | */ |
| 7059 | psa_reset_key_attributes(&attributes); |
| 7060 | |
| 7061 | psa_destroy_key(key); |
| 7062 | mbedtls_free(signature); |
| 7063 | PSA_DONE(); |
| 7064 | } |
| 7065 | /* END_CASE */ |
| 7066 | |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7067 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7068 | void verify_hash(int key_type_arg, data_t *key_data, |
| 7069 | int alg_arg, data_t *hash_data, |
| 7070 | data_t *signature_data) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7071 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7072 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7073 | psa_key_type_t key_type = key_type_arg; |
| 7074 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7075 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7076 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7077 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 7078 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7079 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7080 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7081 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7082 | psa_set_key_algorithm(&attributes, alg); |
| 7083 | psa_set_key_type(&attributes, key_type); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7084 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7085 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7086 | &key)); |
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_ASSERT(psa_verify_hash(key, alg, |
| 7089 | hash_data->x, hash_data->len, |
| 7090 | signature_data->x, signature_data->len)); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 7091 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7092 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7093 | psa_reset_key_attributes(&attributes); |
| 7094 | psa_destroy_key(key); |
| 7095 | PSA_DONE(); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7096 | } |
| 7097 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7098 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7099 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7100 | /** |
| 7101 | * verify_hash_interruptible() test intentions: |
| 7102 | * |
| 7103 | * Note: This test can currently only handle ECDSA. |
| 7104 | * |
| 7105 | * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 7106 | * only). Given this test only does verification it can accept public keys as |
| 7107 | * well as private keys / keypairs. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7108 | * |
| 7109 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7110 | * expected for different max_ops values. |
| 7111 | * |
| 7112 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7113 | * and that each successful stage completes some ops (this is not mandated by |
| 7114 | * the PSA specification, but is currently the case). |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7115 | * |
| 7116 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 7117 | * complete() calls does not alter the number of ops returned. |
| 7118 | * |
| 7119 | * 5. Test that after corrupting the hash, the verification detects an invalid |
| 7120 | * signature. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7121 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7122 | void verify_hash_interruptible(int key_type_arg, data_t *key_data, |
| 7123 | int alg_arg, data_t *hash_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7124 | data_t *signature_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7125 | { |
| 7126 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7127 | psa_key_type_t key_type = key_type_arg; |
| 7128 | psa_algorithm_t alg = alg_arg; |
| 7129 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7130 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7131 | uint32_t num_ops = 0; |
| 7132 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7133 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7134 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7135 | size_t min_completes = 0; |
| 7136 | size_t max_completes = 0; |
| 7137 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7138 | psa_verify_hash_interruptible_operation_t operation = |
| 7139 | psa_verify_hash_interruptible_operation_init(); |
| 7140 | |
| 7141 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 7142 | |
| 7143 | PSA_ASSERT(psa_crypto_init()); |
| 7144 | |
| 7145 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7146 | psa_set_key_algorithm(&attributes, alg); |
| 7147 | psa_set_key_type(&attributes, key_type); |
| 7148 | |
| 7149 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7150 | &key)); |
| 7151 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7152 | psa_interruptible_set_max_ops(max_ops); |
| 7153 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7154 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 7155 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7156 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7157 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7158 | |
| 7159 | TEST_ASSERT(num_ops_prior == 0); |
| 7160 | |
| 7161 | /* Start verification. */ |
| 7162 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7163 | hash_data->x, hash_data->len, |
| 7164 | signature_data->x, signature_data->len) |
| 7165 | ); |
| 7166 | |
| 7167 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7168 | |
| 7169 | TEST_ASSERT(num_ops_prior == 0); |
| 7170 | |
| 7171 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7172 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7173 | status = psa_verify_hash_complete(&operation); |
| 7174 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7175 | num_completes++; |
| 7176 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7177 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 7178 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7179 | /* We are asserting here that every complete makes progress |
| 7180 | * (completes some ops), which is true of the internal |
| 7181 | * implementation and probably any implementation, however this is |
| 7182 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7183 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7184 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7185 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 7186 | |
| 7187 | /* Ensure calling get_num_ops() twice still returns the same |
| 7188 | * number of ops as previously reported. */ |
| 7189 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7190 | |
| 7191 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7192 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7193 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7194 | |
| 7195 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7196 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7197 | TEST_LE_U(min_completes, num_completes); |
| 7198 | TEST_LE_U(num_completes, max_completes); |
| 7199 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7200 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7201 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7202 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7203 | TEST_ASSERT(num_ops == 0); |
| 7204 | |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7205 | if (hash_data->len != 0) { |
| 7206 | /* Flip a bit in the hash and verify that the signature is now detected |
| 7207 | * as invalid. Flip a bit at the beginning, not at the end, because |
| 7208 | * ECDSA may ignore the last few bits of the input. */ |
| 7209 | hash_data->x[0] ^= 1; |
| 7210 | |
| 7211 | /* Start verification. */ |
| 7212 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7213 | hash_data->x, hash_data->len, |
| 7214 | signature_data->x, signature_data->len)); |
| 7215 | |
| 7216 | /* Continue performing the signature until complete. */ |
| 7217 | do { |
| 7218 | status = psa_verify_hash_complete(&operation); |
| 7219 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7220 | |
| 7221 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7222 | } |
| 7223 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7224 | exit: |
| 7225 | psa_reset_key_attributes(&attributes); |
| 7226 | psa_destroy_key(key); |
| 7227 | PSA_DONE(); |
| 7228 | } |
| 7229 | /* END_CASE */ |
| 7230 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7231 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7232 | void verify_hash_fail(int key_type_arg, data_t *key_data, |
| 7233 | int alg_arg, data_t *hash_data, |
| 7234 | data_t *signature_data, |
| 7235 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7236 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7237 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7238 | psa_key_type_t key_type = key_type_arg; |
| 7239 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7240 | psa_status_t actual_status; |
| 7241 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7242 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7243 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7244 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7245 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7246 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7247 | psa_set_key_algorithm(&attributes, alg); |
| 7248 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7249 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7250 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7251 | &key)); |
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 | actual_status = psa_verify_hash(key, alg, |
| 7254 | hash_data->x, hash_data->len, |
| 7255 | signature_data->x, signature_data->len); |
| 7256 | TEST_EQUAL(actual_status, expected_status); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7257 | |
| 7258 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7259 | psa_reset_key_attributes(&attributes); |
| 7260 | psa_destroy_key(key); |
| 7261 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7262 | } |
| 7263 | /* END_CASE */ |
| 7264 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7265 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7266 | /** |
| 7267 | * verify_hash_fail_interruptible() test intentions: |
| 7268 | * |
| 7269 | * Note: This test can currently only handle ECDSA. |
| 7270 | * |
| 7271 | * 1. Test that various failure cases for interruptible verify hash fail with |
| 7272 | * the correct error codes, and at the correct point (at start or during |
| 7273 | * complete). |
| 7274 | * |
| 7275 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7276 | * expected for different max_ops values. |
| 7277 | * |
| 7278 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7279 | * and that each successful stage completes some ops (this is not mandated by |
| 7280 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7281 | * |
| 7282 | * 4. Check that calling complete() when start() fails and complete() |
| 7283 | * after completion results in a BAD_STATE error. |
| 7284 | * |
| 7285 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 7286 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7287 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7288 | void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 7289 | int alg_arg, data_t *hash_data, |
| 7290 | data_t *signature_data, |
| 7291 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7292 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7293 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7294 | { |
| 7295 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7296 | psa_key_type_t key_type = key_type_arg; |
| 7297 | psa_algorithm_t alg = alg_arg; |
| 7298 | psa_status_t actual_status; |
| 7299 | psa_status_t expected_start_status = expected_start_status_arg; |
| 7300 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 7301 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7302 | uint32_t num_ops = 0; |
| 7303 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7304 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7305 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7306 | size_t min_completes = 0; |
| 7307 | size_t max_completes = 0; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7308 | psa_verify_hash_interruptible_operation_t operation = |
| 7309 | psa_verify_hash_interruptible_operation_init(); |
| 7310 | |
| 7311 | PSA_ASSERT(psa_crypto_init()); |
| 7312 | |
| 7313 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7314 | psa_set_key_algorithm(&attributes, alg); |
| 7315 | psa_set_key_type(&attributes, key_type); |
| 7316 | |
| 7317 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7318 | &key)); |
| 7319 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7320 | psa_interruptible_set_max_ops(max_ops); |
| 7321 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7322 | interruptible_signverify_get_minmax_completes(max_ops, |
| 7323 | expected_complete_status, |
| 7324 | &min_completes, |
| 7325 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7326 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7327 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7328 | TEST_ASSERT(num_ops_prior == 0); |
| 7329 | |
| 7330 | /* Start verification. */ |
| 7331 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7332 | hash_data->x, hash_data->len, |
| 7333 | signature_data->x, |
| 7334 | signature_data->len); |
| 7335 | |
| 7336 | TEST_EQUAL(actual_status, expected_start_status); |
| 7337 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7338 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 7339 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7340 | * start failed. */ |
| 7341 | actual_status = psa_verify_hash_complete(&operation); |
| 7342 | |
| 7343 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7344 | |
| 7345 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7346 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7347 | hash_data->x, hash_data->len, |
| 7348 | signature_data->x, |
| 7349 | signature_data->len); |
| 7350 | |
| 7351 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7352 | } |
| 7353 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7354 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7355 | TEST_ASSERT(num_ops_prior == 0); |
| 7356 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7357 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7358 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7359 | actual_status = psa_verify_hash_complete(&operation); |
| 7360 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7361 | num_completes++; |
| 7362 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7363 | if (actual_status == PSA_SUCCESS || |
| 7364 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7365 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7366 | /* We are asserting here that every complete makes progress |
| 7367 | * (completes some ops), which is true of the internal |
| 7368 | * implementation and probably any implementation, however this is |
| 7369 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7370 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7371 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7372 | num_ops_prior = num_ops; |
| 7373 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7374 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7375 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7376 | TEST_EQUAL(actual_status, expected_complete_status); |
| 7377 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 7378 | /* Check that another complete returns BAD_STATE. */ |
| 7379 | actual_status = psa_verify_hash_complete(&operation); |
| 7380 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7381 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7382 | TEST_LE_U(min_completes, num_completes); |
| 7383 | TEST_LE_U(num_completes, max_completes); |
| 7384 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7385 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7386 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7387 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7388 | TEST_ASSERT(num_ops == 0); |
| 7389 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7390 | exit: |
| 7391 | psa_reset_key_attributes(&attributes); |
| 7392 | psa_destroy_key(key); |
| 7393 | PSA_DONE(); |
| 7394 | } |
| 7395 | /* END_CASE */ |
| 7396 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7397 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7398 | /** |
| 7399 | * interruptible_signverify_hash_state_test() test intentions: |
| 7400 | * |
| 7401 | * Note: This test can currently only handle ECDSA. |
| 7402 | * |
| 7403 | * 1. Test that calling the various interruptible sign and verify hash functions |
| 7404 | * in incorrect orders returns BAD_STATE errors. |
| 7405 | */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7406 | void interruptible_signverify_hash_state_test(int key_type_arg, |
| 7407 | data_t *key_data, int alg_arg, data_t *input_data) |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7408 | { |
| 7409 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7410 | psa_key_type_t key_type = key_type_arg; |
| 7411 | psa_algorithm_t alg = alg_arg; |
| 7412 | size_t key_bits; |
| 7413 | unsigned char *signature = NULL; |
| 7414 | size_t signature_size; |
| 7415 | size_t signature_length = 0xdeadbeef; |
| 7416 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7417 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7418 | psa_sign_hash_interruptible_operation_init(); |
| 7419 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7420 | psa_verify_hash_interruptible_operation_init(); |
| 7421 | |
| 7422 | PSA_ASSERT(psa_crypto_init()); |
| 7423 | |
| 7424 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7425 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7426 | psa_set_key_algorithm(&attributes, alg); |
| 7427 | psa_set_key_type(&attributes, key_type); |
| 7428 | |
| 7429 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7430 | &key)); |
| 7431 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7432 | key_bits = psa_get_key_bits(&attributes); |
| 7433 | |
| 7434 | /* Allocate a buffer which has the size advertised by the |
| 7435 | * library. */ |
| 7436 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7437 | key_bits, alg); |
| 7438 | TEST_ASSERT(signature_size != 0); |
| 7439 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7440 | ASSERT_ALLOC(signature, signature_size); |
| 7441 | |
| 7442 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7443 | |
| 7444 | /* --- Attempt completes prior to starts --- */ |
| 7445 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7446 | signature_size, |
| 7447 | &signature_length), |
| 7448 | PSA_ERROR_BAD_STATE); |
| 7449 | |
| 7450 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7451 | |
| 7452 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7453 | PSA_ERROR_BAD_STATE); |
| 7454 | |
| 7455 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7456 | |
| 7457 | /* --- Aborts in all other places. --- */ |
| 7458 | psa_sign_hash_abort(&sign_operation); |
| 7459 | |
| 7460 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7461 | input_data->x, input_data->len)); |
| 7462 | |
| 7463 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7464 | |
| 7465 | psa_interruptible_set_max_ops(1); |
| 7466 | |
| 7467 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7468 | input_data->x, input_data->len)); |
| 7469 | |
| 7470 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7471 | signature_size, |
| 7472 | &signature_length), |
| 7473 | PSA_OPERATION_INCOMPLETE); |
| 7474 | |
| 7475 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7476 | |
| 7477 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7478 | |
| 7479 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7480 | input_data->x, input_data->len)); |
| 7481 | |
| 7482 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7483 | signature_size, |
| 7484 | &signature_length)); |
| 7485 | |
| 7486 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7487 | |
| 7488 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7489 | |
| 7490 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7491 | input_data->x, input_data->len, |
| 7492 | signature, signature_length)); |
| 7493 | |
| 7494 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7495 | |
| 7496 | psa_interruptible_set_max_ops(1); |
| 7497 | |
| 7498 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7499 | input_data->x, input_data->len, |
| 7500 | signature, signature_length)); |
| 7501 | |
| 7502 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7503 | PSA_OPERATION_INCOMPLETE); |
| 7504 | |
| 7505 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7506 | |
| 7507 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7508 | |
| 7509 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7510 | input_data->x, input_data->len, |
| 7511 | signature, signature_length)); |
| 7512 | |
| 7513 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7514 | |
| 7515 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7516 | |
| 7517 | /* --- Attempt double starts. --- */ |
| 7518 | |
| 7519 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7520 | input_data->x, input_data->len)); |
| 7521 | |
| 7522 | TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg, |
| 7523 | input_data->x, input_data->len), |
| 7524 | PSA_ERROR_BAD_STATE); |
| 7525 | |
| 7526 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7527 | |
| 7528 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7529 | input_data->x, input_data->len, |
| 7530 | signature, signature_length)); |
| 7531 | |
| 7532 | TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg, |
| 7533 | input_data->x, input_data->len, |
| 7534 | signature, signature_length), |
| 7535 | PSA_ERROR_BAD_STATE); |
| 7536 | |
| 7537 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7538 | |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7539 | exit: |
| 7540 | /* |
| 7541 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7542 | * thus reset them as required. |
| 7543 | */ |
| 7544 | psa_reset_key_attributes(&attributes); |
| 7545 | |
| 7546 | psa_destroy_key(key); |
| 7547 | mbedtls_free(signature); |
| 7548 | PSA_DONE(); |
| 7549 | } |
| 7550 | /* END_CASE */ |
| 7551 | |
| 7552 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7553 | /** |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7554 | * interruptible_signverify_hash_edgecase_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7555 | * |
| 7556 | * Note: This test can currently only handle ECDSA. |
| 7557 | * |
| 7558 | * 1. Test various edge cases in the interruptible sign and verify hash |
| 7559 | * interfaces. |
| 7560 | */ |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7561 | void interruptible_signverify_hash_edgecase_tests(int key_type_arg, |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7562 | data_t *key_data, int alg_arg, data_t *input_data) |
| 7563 | { |
| 7564 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7565 | psa_key_type_t key_type = key_type_arg; |
| 7566 | psa_algorithm_t alg = alg_arg; |
| 7567 | size_t key_bits; |
| 7568 | unsigned char *signature = NULL; |
| 7569 | size_t signature_size; |
| 7570 | size_t signature_length = 0xdeadbeef; |
| 7571 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7572 | uint8_t *input_buffer = NULL; |
| 7573 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7574 | psa_sign_hash_interruptible_operation_init(); |
| 7575 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7576 | psa_verify_hash_interruptible_operation_init(); |
| 7577 | |
| 7578 | PSA_ASSERT(psa_crypto_init()); |
| 7579 | |
| 7580 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7581 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7582 | psa_set_key_algorithm(&attributes, alg); |
| 7583 | psa_set_key_type(&attributes, key_type); |
| 7584 | |
| 7585 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7586 | &key)); |
| 7587 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7588 | key_bits = psa_get_key_bits(&attributes); |
| 7589 | |
| 7590 | /* Allocate a buffer which has the size advertised by the |
| 7591 | * library. */ |
| 7592 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7593 | key_bits, alg); |
| 7594 | TEST_ASSERT(signature_size != 0); |
| 7595 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7596 | ASSERT_ALLOC(signature, signature_size); |
| 7597 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7598 | /* --- Change function inputs mid run, to cause an error (sign only, |
| 7599 | * verify passes all inputs to start. --- */ |
| 7600 | |
| 7601 | psa_interruptible_set_max_ops(1); |
| 7602 | |
| 7603 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7604 | input_data->x, input_data->len)); |
| 7605 | |
| 7606 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7607 | signature_size, |
| 7608 | &signature_length), |
| 7609 | PSA_OPERATION_INCOMPLETE); |
| 7610 | |
| 7611 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7612 | 0, |
| 7613 | &signature_length), |
| 7614 | PSA_ERROR_BUFFER_TOO_SMALL); |
| 7615 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7616 | /* And test that this invalidates the operation. */ |
| 7617 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7618 | 0, |
| 7619 | &signature_length), |
| 7620 | PSA_ERROR_BAD_STATE); |
| 7621 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7622 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7623 | |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7624 | /* Trash the hash buffer in between start and complete, to ensure |
| 7625 | * no reliance on external buffers. */ |
| 7626 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7627 | |
| 7628 | input_buffer = mbedtls_calloc(1, input_data->len); |
| 7629 | TEST_ASSERT(input_buffer != NULL); |
| 7630 | |
| 7631 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7632 | |
| 7633 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7634 | input_buffer, input_data->len)); |
| 7635 | |
| 7636 | memset(input_buffer, '!', input_data->len); |
| 7637 | mbedtls_free(input_buffer); |
| 7638 | input_buffer = NULL; |
| 7639 | |
| 7640 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7641 | signature_size, |
| 7642 | &signature_length)); |
| 7643 | |
| 7644 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7645 | |
| 7646 | input_buffer = mbedtls_calloc(1, input_data->len); |
| 7647 | TEST_ASSERT(input_buffer != NULL); |
| 7648 | |
| 7649 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7650 | |
| 7651 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7652 | input_buffer, input_data->len, |
| 7653 | signature, signature_length)); |
| 7654 | |
| 7655 | memset(input_buffer, '!', input_data->len); |
| 7656 | mbedtls_free(input_buffer); |
| 7657 | input_buffer = NULL; |
| 7658 | |
| 7659 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7660 | |
| 7661 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7662 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7663 | exit: |
| 7664 | /* |
| 7665 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7666 | * thus reset them as required. |
| 7667 | */ |
| 7668 | psa_reset_key_attributes(&attributes); |
| 7669 | |
| 7670 | psa_destroy_key(key); |
| 7671 | mbedtls_free(signature); |
| 7672 | PSA_DONE(); |
| 7673 | } |
| 7674 | /* END_CASE */ |
| 7675 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7676 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7677 | /** |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 7678 | * interruptible_signverify_hash_ops_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7679 | * |
| 7680 | * Note: This test can currently only handle ECDSA. |
| 7681 | * |
| 7682 | * 1. Test that setting max ops is reflected in both interruptible sign and |
| 7683 | * verify hash |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7684 | * 2. Test that changing the value of max_ops to unlimited during an operation |
| 7685 | * causes that operation to complete in the next call. |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7686 | * |
| 7687 | * 3. Test that calling get_num_ops() between complete calls gives the same |
| 7688 | * 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] | 7689 | */ |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 7690 | void interruptible_signverify_hash_ops_tests(int key_type_arg, |
| 7691 | data_t *key_data, int alg_arg, |
| 7692 | data_t *input_data) |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7693 | { |
| 7694 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7695 | psa_key_type_t key_type = key_type_arg; |
| 7696 | psa_algorithm_t alg = alg_arg; |
| 7697 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7698 | size_t key_bits; |
| 7699 | unsigned char *signature = NULL; |
| 7700 | size_t signature_size; |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7701 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7702 | uint32_t num_ops = 0; |
| 7703 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 7704 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7705 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7706 | psa_sign_hash_interruptible_operation_init(); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7707 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7708 | psa_verify_hash_interruptible_operation_init(); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7709 | |
| 7710 | PSA_ASSERT(psa_crypto_init()); |
| 7711 | |
| 7712 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7713 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7714 | psa_set_key_algorithm(&attributes, alg); |
| 7715 | psa_set_key_type(&attributes, key_type); |
| 7716 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7717 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key)); |
| 7718 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7719 | key_bits = psa_get_key_bits(&attributes); |
| 7720 | |
| 7721 | /* Allocate a buffer which has the size advertised by the |
| 7722 | * library. */ |
| 7723 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7724 | |
| 7725 | TEST_ASSERT(signature_size != 0); |
| 7726 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7727 | ASSERT_ALLOC(signature, signature_size); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7728 | |
| 7729 | /* Check that default max ops gets set if we don't set it. */ |
| 7730 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7731 | input_data->x, input_data->len)); |
| 7732 | |
| 7733 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 7734 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7735 | |
| 7736 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7737 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7738 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7739 | input_data->x, input_data->len, |
| 7740 | signature, signature_size)); |
| 7741 | |
| 7742 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 7743 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7744 | |
| 7745 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7746 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7747 | /* Check that max ops gets set properly. */ |
| 7748 | |
| 7749 | psa_interruptible_set_max_ops(0xbeef); |
| 7750 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7751 | TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7752 | |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7753 | /* --- Ensure changing the max ops mid operation works (operation should |
| 7754 | * complete successfully after setting max ops to unlimited --- */ |
| 7755 | psa_interruptible_set_max_ops(1); |
| 7756 | |
| 7757 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7758 | input_data->x, input_data->len)); |
| 7759 | |
| 7760 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7761 | signature_size, |
| 7762 | &signature_length), |
| 7763 | PSA_OPERATION_INCOMPLETE); |
| 7764 | |
| 7765 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7766 | |
| 7767 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7768 | signature_size, |
| 7769 | &signature_length)); |
| 7770 | |
| 7771 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7772 | |
| 7773 | psa_interruptible_set_max_ops(1); |
| 7774 | |
| 7775 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7776 | input_data->x, input_data->len, |
| 7777 | signature, signature_length)); |
| 7778 | |
| 7779 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7780 | PSA_OPERATION_INCOMPLETE); |
| 7781 | |
| 7782 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7783 | |
| 7784 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7785 | |
| 7786 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7787 | |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7788 | /* --- Test that not calling get_num_ops inbetween complete calls does not |
| 7789 | * result in lost ops. ---*/ |
| 7790 | |
| 7791 | psa_interruptible_set_max_ops(1); |
| 7792 | |
| 7793 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7794 | input_data->x, input_data->len)); |
| 7795 | |
| 7796 | /* Continue performing the signature until complete. */ |
| 7797 | do { |
| 7798 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7799 | signature_size, |
| 7800 | &signature_length); |
| 7801 | |
| 7802 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7803 | |
| 7804 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7805 | |
| 7806 | PSA_ASSERT(status); |
| 7807 | |
| 7808 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7809 | |
| 7810 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7811 | input_data->x, input_data->len)); |
| 7812 | |
| 7813 | /* Continue performing the signature until complete. */ |
| 7814 | do { |
| 7815 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7816 | signature_size, |
| 7817 | &signature_length); |
| 7818 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7819 | |
| 7820 | PSA_ASSERT(status); |
| 7821 | |
| 7822 | TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation)); |
| 7823 | |
| 7824 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7825 | |
| 7826 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7827 | input_data->x, input_data->len, |
| 7828 | signature, signature_length)); |
| 7829 | |
| 7830 | /* Continue performing the verification until complete. */ |
| 7831 | do { |
| 7832 | status = psa_verify_hash_complete(&verify_operation); |
| 7833 | |
| 7834 | num_ops = psa_verify_hash_get_num_ops(&verify_operation); |
| 7835 | |
| 7836 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7837 | |
| 7838 | PSA_ASSERT(status); |
| 7839 | |
| 7840 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7841 | |
| 7842 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7843 | input_data->x, input_data->len, |
| 7844 | signature, signature_length)); |
| 7845 | |
| 7846 | /* Continue performing the verification until complete. */ |
| 7847 | do { |
| 7848 | status = psa_verify_hash_complete(&verify_operation); |
| 7849 | |
| 7850 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7851 | |
| 7852 | PSA_ASSERT(status); |
| 7853 | |
| 7854 | TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation)); |
| 7855 | |
| 7856 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7857 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7858 | exit: |
| 7859 | /* |
| 7860 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7861 | * thus reset them as required. |
| 7862 | */ |
| 7863 | psa_reset_key_attributes(&attributes); |
| 7864 | |
| 7865 | psa_destroy_key(key); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7866 | mbedtls_free(signature); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7867 | PSA_DONE(); |
| 7868 | } |
| 7869 | /* END_CASE */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7870 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7871 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7872 | void sign_message_deterministic(int key_type_arg, |
| 7873 | data_t *key_data, |
| 7874 | int alg_arg, |
| 7875 | data_t *input_data, |
| 7876 | data_t *output_data) |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7877 | { |
| 7878 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7879 | psa_key_type_t key_type = key_type_arg; |
| 7880 | psa_algorithm_t alg = alg_arg; |
| 7881 | size_t key_bits; |
| 7882 | unsigned char *signature = NULL; |
| 7883 | size_t signature_size; |
| 7884 | size_t signature_length = 0xdeadbeef; |
| 7885 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7886 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7887 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7888 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7889 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 7890 | psa_set_key_algorithm(&attributes, alg); |
| 7891 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7892 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7893 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7894 | &key)); |
| 7895 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7896 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7897 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7898 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7899 | TEST_ASSERT(signature_size != 0); |
| 7900 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7901 | ASSERT_ALLOC(signature, signature_size); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7902 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7903 | PSA_ASSERT(psa_sign_message(key, alg, |
| 7904 | input_data->x, input_data->len, |
| 7905 | signature, signature_size, |
| 7906 | &signature_length)); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7907 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7908 | ASSERT_COMPARE(output_data->x, output_data->len, |
| 7909 | signature, signature_length); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7910 | |
| 7911 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7912 | psa_reset_key_attributes(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7913 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7914 | psa_destroy_key(key); |
| 7915 | mbedtls_free(signature); |
| 7916 | PSA_DONE(); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7917 | |
| 7918 | } |
| 7919 | /* END_CASE */ |
| 7920 | |
| 7921 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7922 | void sign_message_fail(int key_type_arg, |
| 7923 | data_t *key_data, |
| 7924 | int alg_arg, |
| 7925 | data_t *input_data, |
| 7926 | int signature_size_arg, |
| 7927 | int expected_status_arg) |
| 7928 | { |
| 7929 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7930 | psa_key_type_t key_type = key_type_arg; |
| 7931 | psa_algorithm_t alg = alg_arg; |
| 7932 | size_t signature_size = signature_size_arg; |
| 7933 | psa_status_t actual_status; |
| 7934 | psa_status_t expected_status = expected_status_arg; |
| 7935 | unsigned char *signature = NULL; |
| 7936 | size_t signature_length = 0xdeadbeef; |
| 7937 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7938 | |
| 7939 | ASSERT_ALLOC(signature, signature_size); |
| 7940 | |
| 7941 | PSA_ASSERT(psa_crypto_init()); |
| 7942 | |
| 7943 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 7944 | psa_set_key_algorithm(&attributes, alg); |
| 7945 | psa_set_key_type(&attributes, key_type); |
| 7946 | |
| 7947 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7948 | &key)); |
| 7949 | |
| 7950 | actual_status = psa_sign_message(key, alg, |
| 7951 | input_data->x, input_data->len, |
| 7952 | signature, signature_size, |
| 7953 | &signature_length); |
| 7954 | TEST_EQUAL(actual_status, expected_status); |
| 7955 | /* The value of *signature_length is unspecified on error, but |
| 7956 | * whatever it is, it should be less than signature_size, so that |
| 7957 | * if the caller tries to read *signature_length bytes without |
| 7958 | * checking the error code then they don't overflow a buffer. */ |
| 7959 | TEST_LE_U(signature_length, signature_size); |
| 7960 | |
| 7961 | exit: |
| 7962 | psa_reset_key_attributes(&attributes); |
| 7963 | psa_destroy_key(key); |
| 7964 | mbedtls_free(signature); |
| 7965 | PSA_DONE(); |
| 7966 | } |
| 7967 | /* END_CASE */ |
| 7968 | |
| 7969 | /* BEGIN_CASE */ |
| 7970 | void sign_verify_message(int key_type_arg, |
| 7971 | data_t *key_data, |
| 7972 | int alg_arg, |
| 7973 | data_t *input_data) |
| 7974 | { |
| 7975 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7976 | psa_key_type_t key_type = key_type_arg; |
| 7977 | psa_algorithm_t alg = alg_arg; |
| 7978 | size_t key_bits; |
| 7979 | unsigned char *signature = NULL; |
| 7980 | size_t signature_size; |
| 7981 | size_t signature_length = 0xdeadbeef; |
| 7982 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7983 | |
| 7984 | PSA_ASSERT(psa_crypto_init()); |
| 7985 | |
| 7986 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 7987 | PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 7988 | psa_set_key_algorithm(&attributes, alg); |
| 7989 | psa_set_key_type(&attributes, key_type); |
| 7990 | |
| 7991 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7992 | &key)); |
| 7993 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7994 | key_bits = psa_get_key_bits(&attributes); |
| 7995 | |
| 7996 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7997 | TEST_ASSERT(signature_size != 0); |
| 7998 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7999 | ASSERT_ALLOC(signature, signature_size); |
| 8000 | |
| 8001 | PSA_ASSERT(psa_sign_message(key, alg, |
| 8002 | input_data->x, input_data->len, |
| 8003 | signature, signature_size, |
| 8004 | &signature_length)); |
| 8005 | TEST_LE_U(signature_length, signature_size); |
| 8006 | TEST_ASSERT(signature_length > 0); |
| 8007 | |
| 8008 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8009 | input_data->x, input_data->len, |
| 8010 | signature, signature_length)); |
| 8011 | |
| 8012 | if (input_data->len != 0) { |
| 8013 | /* Flip a bit in the input and verify that the signature is now |
| 8014 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 8015 | * because ECDSA may ignore the last few bits of the input. */ |
| 8016 | input_data->x[0] ^= 1; |
| 8017 | TEST_EQUAL(psa_verify_message(key, alg, |
| 8018 | input_data->x, input_data->len, |
| 8019 | signature, signature_length), |
| 8020 | PSA_ERROR_INVALID_SIGNATURE); |
| 8021 | } |
| 8022 | |
| 8023 | exit: |
| 8024 | psa_reset_key_attributes(&attributes); |
| 8025 | |
| 8026 | psa_destroy_key(key); |
| 8027 | mbedtls_free(signature); |
| 8028 | PSA_DONE(); |
| 8029 | } |
| 8030 | /* END_CASE */ |
| 8031 | |
| 8032 | /* BEGIN_CASE */ |
| 8033 | void verify_message(int key_type_arg, |
| 8034 | data_t *key_data, |
| 8035 | int alg_arg, |
| 8036 | data_t *input_data, |
| 8037 | data_t *signature_data) |
| 8038 | { |
| 8039 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8040 | psa_key_type_t key_type = key_type_arg; |
| 8041 | psa_algorithm_t alg = alg_arg; |
| 8042 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8043 | |
| 8044 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 8045 | |
| 8046 | PSA_ASSERT(psa_crypto_init()); |
| 8047 | |
| 8048 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8049 | psa_set_key_algorithm(&attributes, alg); |
| 8050 | psa_set_key_type(&attributes, key_type); |
| 8051 | |
| 8052 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8053 | &key)); |
| 8054 | |
| 8055 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8056 | input_data->x, input_data->len, |
| 8057 | signature_data->x, signature_data->len)); |
| 8058 | |
| 8059 | exit: |
| 8060 | psa_reset_key_attributes(&attributes); |
| 8061 | psa_destroy_key(key); |
| 8062 | PSA_DONE(); |
| 8063 | } |
| 8064 | /* END_CASE */ |
| 8065 | |
| 8066 | /* BEGIN_CASE */ |
| 8067 | void verify_message_fail(int key_type_arg, |
| 8068 | data_t *key_data, |
| 8069 | int alg_arg, |
| 8070 | data_t *hash_data, |
| 8071 | data_t *signature_data, |
| 8072 | int expected_status_arg) |
| 8073 | { |
| 8074 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8075 | psa_key_type_t key_type = key_type_arg; |
| 8076 | psa_algorithm_t alg = alg_arg; |
| 8077 | psa_status_t actual_status; |
| 8078 | psa_status_t expected_status = expected_status_arg; |
| 8079 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8080 | |
| 8081 | PSA_ASSERT(psa_crypto_init()); |
| 8082 | |
| 8083 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8084 | psa_set_key_algorithm(&attributes, alg); |
| 8085 | psa_set_key_type(&attributes, key_type); |
| 8086 | |
| 8087 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8088 | &key)); |
| 8089 | |
| 8090 | actual_status = psa_verify_message(key, alg, |
| 8091 | hash_data->x, hash_data->len, |
| 8092 | signature_data->x, |
| 8093 | signature_data->len); |
| 8094 | TEST_EQUAL(actual_status, expected_status); |
| 8095 | |
| 8096 | exit: |
| 8097 | psa_reset_key_attributes(&attributes); |
| 8098 | psa_destroy_key(key); |
| 8099 | PSA_DONE(); |
| 8100 | } |
| 8101 | /* END_CASE */ |
| 8102 | |
| 8103 | /* BEGIN_CASE */ |
| 8104 | void asymmetric_encrypt(int key_type_arg, |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8105 | data_t *key_data, |
| 8106 | int alg_arg, |
| 8107 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8108 | data_t *label, |
| 8109 | int expected_output_length_arg, |
| 8110 | int expected_status_arg) |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8111 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8112 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8113 | psa_key_type_t key_type = key_type_arg; |
| 8114 | psa_algorithm_t alg = alg_arg; |
| 8115 | size_t expected_output_length = expected_output_length_arg; |
| 8116 | size_t key_bits; |
| 8117 | unsigned char *output = NULL; |
| 8118 | size_t output_size; |
| 8119 | size_t output_length = ~0; |
| 8120 | psa_status_t actual_status; |
| 8121 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8122 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8123 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8124 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 8125 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8126 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8127 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 8128 | psa_set_key_algorithm(&attributes, alg); |
| 8129 | psa_set_key_type(&attributes, key_type); |
| 8130 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8131 | &key)); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8132 | |
| 8133 | /* Determine the maximum output length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8134 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8135 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8136 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8137 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8138 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
| 8139 | ASSERT_ALLOC(output, output_size); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8140 | |
| 8141 | /* Encrypt the input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8142 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8143 | input_data->x, input_data->len, |
| 8144 | label->x, label->len, |
| 8145 | output, output_size, |
| 8146 | &output_length); |
| 8147 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8148 | if (actual_status == PSA_SUCCESS) { |
| 8149 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8150 | } else { |
| 8151 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8152 | } |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8153 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8154 | /* If the label is empty, the test framework puts a non-null pointer |
| 8155 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8156 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8157 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8158 | if (output_size != 0) { |
| 8159 | memset(output, 0, output_size); |
| 8160 | } |
| 8161 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8162 | input_data->x, input_data->len, |
| 8163 | NULL, label->len, |
| 8164 | output, output_size, |
| 8165 | &output_length); |
| 8166 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8167 | if (actual_status == PSA_SUCCESS) { |
| 8168 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8169 | } else { |
| 8170 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8171 | } |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8172 | } |
| 8173 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8174 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8175 | /* |
| 8176 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8177 | * thus reset them as required. |
| 8178 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8179 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8181 | psa_destroy_key(key); |
| 8182 | mbedtls_free(output); |
| 8183 | PSA_DONE(); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8184 | } |
| 8185 | /* END_CASE */ |
| 8186 | |
| 8187 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8188 | void asymmetric_encrypt_decrypt(int key_type_arg, |
| 8189 | data_t *key_data, |
| 8190 | int alg_arg, |
| 8191 | data_t *input_data, |
| 8192 | data_t *label) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8193 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8194 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8195 | psa_key_type_t key_type = key_type_arg; |
| 8196 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8197 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8198 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8199 | size_t output_size; |
| 8200 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8201 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8202 | size_t output2_size; |
| 8203 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8204 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8205 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8206 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8207 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8208 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 8209 | psa_set_key_algorithm(&attributes, alg); |
| 8210 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8211 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8212 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8213 | &key)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8214 | |
| 8215 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8216 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8217 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8218 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8219 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8220 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
| 8221 | ASSERT_ALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8222 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8223 | output2_size = input_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8224 | TEST_LE_U(output2_size, |
| 8225 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)); |
| 8226 | TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
| 8227 | ASSERT_ALLOC(output2, output2_size); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8228 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 8229 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 8230 | * the original plaintext because of the non-optional random |
| 8231 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8232 | PSA_ASSERT(psa_asymmetric_encrypt(key, alg, |
| 8233 | input_data->x, input_data->len, |
| 8234 | label->x, label->len, |
| 8235 | output, output_size, |
| 8236 | &output_length)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8237 | /* We don't know what ciphertext length to expect, but check that |
| 8238 | * it looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8239 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8240 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8241 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8242 | output, output_length, |
| 8243 | label->x, label->len, |
| 8244 | output2, output2_size, |
| 8245 | &output2_length)); |
| 8246 | ASSERT_COMPARE(input_data->x, input_data->len, |
| 8247 | output2, output2_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8248 | |
| 8249 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8250 | /* |
| 8251 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8252 | * thus reset them as required. |
| 8253 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8254 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8256 | psa_destroy_key(key); |
| 8257 | mbedtls_free(output); |
| 8258 | mbedtls_free(output2); |
| 8259 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8260 | } |
| 8261 | /* END_CASE */ |
| 8262 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8263 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8264 | void asymmetric_decrypt(int key_type_arg, |
| 8265 | data_t *key_data, |
| 8266 | int alg_arg, |
| 8267 | data_t *input_data, |
| 8268 | data_t *label, |
| 8269 | data_t *expected_data) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8270 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8271 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8272 | psa_key_type_t key_type = key_type_arg; |
| 8273 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8274 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8275 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 8276 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8277 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8278 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8279 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8280 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8282 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8283 | psa_set_key_algorithm(&attributes, alg); |
| 8284 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8285 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8286 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8287 | &key)); |
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_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8290 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8291 | |
| 8292 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8293 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8294 | TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
| 8295 | ASSERT_ALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8296 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8297 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8298 | input_data->x, input_data->len, |
| 8299 | label->x, label->len, |
| 8300 | output, |
| 8301 | output_size, |
| 8302 | &output_length)); |
| 8303 | ASSERT_COMPARE(expected_data->x, expected_data->len, |
| 8304 | output, output_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8305 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8306 | /* If the label is empty, the test framework puts a non-null pointer |
| 8307 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8308 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8309 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8310 | if (output_size != 0) { |
| 8311 | memset(output, 0, output_size); |
| 8312 | } |
| 8313 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8314 | input_data->x, input_data->len, |
| 8315 | NULL, label->len, |
| 8316 | output, |
| 8317 | output_size, |
| 8318 | &output_length)); |
| 8319 | ASSERT_COMPARE(expected_data->x, expected_data->len, |
| 8320 | output, output_length); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8321 | } |
| 8322 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8323 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8324 | psa_reset_key_attributes(&attributes); |
| 8325 | psa_destroy_key(key); |
| 8326 | mbedtls_free(output); |
| 8327 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8328 | } |
| 8329 | /* END_CASE */ |
| 8330 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8331 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8332 | void asymmetric_decrypt_fail(int key_type_arg, |
| 8333 | data_t *key_data, |
| 8334 | int alg_arg, |
| 8335 | data_t *input_data, |
| 8336 | data_t *label, |
| 8337 | int output_size_arg, |
| 8338 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8339 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8340 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8341 | psa_key_type_t key_type = key_type_arg; |
| 8342 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8343 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 8344 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8345 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8346 | psa_status_t actual_status; |
| 8347 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8348 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8349 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8350 | ASSERT_ALLOC(output, output_size); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8352 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8354 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8355 | psa_set_key_algorithm(&attributes, alg); |
| 8356 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8358 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8359 | &key)); |
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 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8362 | input_data->x, input_data->len, |
| 8363 | label->x, label->len, |
| 8364 | output, output_size, |
| 8365 | &output_length); |
| 8366 | TEST_EQUAL(actual_status, expected_status); |
| 8367 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8368 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8369 | /* If the label is empty, the test framework puts a non-null pointer |
| 8370 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8371 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8372 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8373 | if (output_size != 0) { |
| 8374 | memset(output, 0, output_size); |
| 8375 | } |
| 8376 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8377 | input_data->x, input_data->len, |
| 8378 | NULL, label->len, |
| 8379 | output, output_size, |
| 8380 | &output_length); |
| 8381 | TEST_EQUAL(actual_status, expected_status); |
| 8382 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8383 | } |
| 8384 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8385 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8386 | psa_reset_key_attributes(&attributes); |
| 8387 | psa_destroy_key(key); |
| 8388 | mbedtls_free(output); |
| 8389 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8390 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8391 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8392 | |
| 8393 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8394 | void key_derivation_init() |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8395 | { |
| 8396 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 8397 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 8398 | * 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] | 8399 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8400 | size_t capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8401 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init(); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 8402 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8403 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8404 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8405 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8406 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8407 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8408 | TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity), |
| 8409 | PSA_ERROR_BAD_STATE); |
| 8410 | TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity), |
| 8411 | PSA_ERROR_BAD_STATE); |
| 8412 | TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity), |
| 8413 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8414 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8415 | /* A default operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8416 | PSA_ASSERT(psa_key_derivation_abort(&func)); |
| 8417 | PSA_ASSERT(psa_key_derivation_abort(&init)); |
| 8418 | PSA_ASSERT(psa_key_derivation_abort(&zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8419 | } |
| 8420 | /* END_CASE */ |
| 8421 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 8422 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8423 | void derive_setup(int alg_arg, int expected_status_arg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8424 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8425 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8426 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8427 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8428 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8429 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8430 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8431 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8432 | expected_status); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8433 | |
| 8434 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8435 | psa_key_derivation_abort(&operation); |
| 8436 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8437 | } |
| 8438 | /* END_CASE */ |
| 8439 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8440 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8441 | void derive_set_capacity(int alg_arg, int capacity_arg, |
| 8442 | int expected_status_arg) |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8443 | { |
| 8444 | psa_algorithm_t alg = alg_arg; |
| 8445 | size_t capacity = capacity_arg; |
| 8446 | psa_status_t expected_status = expected_status_arg; |
| 8447 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8449 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8450 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8451 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8452 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8453 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 8454 | expected_status); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8455 | |
| 8456 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8457 | psa_key_derivation_abort(&operation); |
| 8458 | PSA_DONE(); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8459 | } |
| 8460 | /* END_CASE */ |
| 8461 | |
| 8462 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8463 | void derive_input(int alg_arg, |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8464 | int step_arg1, int key_type_arg1, data_t *input1, |
| 8465 | int expected_status_arg1, |
| 8466 | int step_arg2, int key_type_arg2, data_t *input2, |
| 8467 | int expected_status_arg2, |
| 8468 | int step_arg3, int key_type_arg3, data_t *input3, |
| 8469 | int expected_status_arg3, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8470 | int output_key_type_arg, int expected_output_status_arg) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8471 | { |
| 8472 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8473 | psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 }; |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8474 | 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] | 8475 | psa_status_t expected_statuses[] = { expected_status_arg1, |
| 8476 | expected_status_arg2, |
| 8477 | expected_status_arg3 }; |
| 8478 | data_t *inputs[] = { input1, input2, input3 }; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8479 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8480 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8481 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8482 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8483 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8484 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8485 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8486 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8487 | psa_status_t expected_output_status = expected_output_status_arg; |
| 8488 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8489 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8490 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8491 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8492 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8493 | psa_set_key_algorithm(&attributes, alg); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8494 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8495 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8496 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8497 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8498 | mbedtls_test_set_step(i); |
| 8499 | if (steps[i] == 0) { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 8500 | /* Skip this step */ |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8501 | } else if (((psa_key_type_t) key_types[i]) != PSA_KEY_TYPE_NONE && |
| 8502 | key_types[i] != INPUT_INTEGER) { |
| 8503 | psa_set_key_type(&attributes, ((psa_key_type_t) key_types[i])); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8504 | PSA_ASSERT(psa_import_key(&attributes, |
| 8505 | inputs[i]->x, inputs[i]->len, |
| 8506 | &keys[i])); |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8507 | 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] | 8508 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8509 | // When taking a private key as secret input, use key agreement |
| 8510 | // to add the shared secret to the derivation |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8511 | TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self( |
| 8512 | &operation, keys[i]), |
| 8513 | expected_statuses[i]); |
| 8514 | } else { |
| 8515 | TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i], |
| 8516 | keys[i]), |
| 8517 | expected_statuses[i]); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8518 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8519 | } else { |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8520 | if (key_types[i] == INPUT_INTEGER) { |
| 8521 | TEST_EQUAL(psa_key_derivation_input_integer( |
| 8522 | &operation, steps[i], |
| 8523 | parse_binary_string(inputs[i])), |
| 8524 | expected_statuses[i]); |
| 8525 | } else { |
Kusumit Ghoderao | 02326d5 | 2023-04-06 17:47:59 +0530 | [diff] [blame] | 8526 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8527 | &operation, steps[i], |
| 8528 | inputs[i]->x, inputs[i]->len), |
| 8529 | expected_statuses[i]); |
Kusumit Ghoderao | 02326d5 | 2023-04-06 17:47:59 +0530 | [diff] [blame] | 8530 | } |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8531 | } |
| 8532 | } |
| 8533 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8534 | if (output_key_type != PSA_KEY_TYPE_NONE) { |
| 8535 | psa_reset_key_attributes(&attributes); |
| 8536 | psa_set_key_type(&attributes, output_key_type); |
| 8537 | psa_set_key_bits(&attributes, 8); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8538 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8539 | psa_key_derivation_output_key(&attributes, &operation, |
| 8540 | &output_key); |
| 8541 | } else { |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8542 | uint8_t buffer[1]; |
| 8543 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8544 | psa_key_derivation_output_bytes(&operation, |
| 8545 | buffer, sizeof(buffer)); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8546 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8547 | TEST_EQUAL(actual_output_status, expected_output_status); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8548 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8549 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8550 | psa_key_derivation_abort(&operation); |
| 8551 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8552 | psa_destroy_key(keys[i]); |
| 8553 | } |
| 8554 | psa_destroy_key(output_key); |
| 8555 | PSA_DONE(); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8556 | } |
| 8557 | /* END_CASE */ |
| 8558 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8559 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8560 | void derive_over_capacity(int alg_arg) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8561 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8562 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8563 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 8564 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8565 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8566 | unsigned char input1[] = "Input 1"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8567 | size_t input1_length = sizeof(input1); |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8568 | unsigned char input2[] = "Input 2"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8569 | size_t input2_length = sizeof(input2); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8570 | uint8_t buffer[42]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8571 | size_t capacity = sizeof(buffer); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 8572 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 8573 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8574 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8575 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8576 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8577 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8578 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8579 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8580 | psa_set_key_algorithm(&attributes, alg); |
| 8581 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8582 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8583 | PSA_ASSERT(psa_import_key(&attributes, |
| 8584 | key_data, sizeof(key_data), |
| 8585 | &key)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8586 | |
| 8587 | /* valid key derivation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8588 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8589 | input1, input1_length, |
| 8590 | input2, input2_length, |
| 8591 | capacity)) { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8592 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8593 | } |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8594 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8595 | /* state of operation shouldn't allow additional generation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8596 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8597 | PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8598 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8599 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8600 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8601 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity), |
| 8602 | PSA_ERROR_INSUFFICIENT_DATA); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8603 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8604 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8605 | psa_key_derivation_abort(&operation); |
| 8606 | psa_destroy_key(key); |
| 8607 | PSA_DONE(); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8608 | } |
| 8609 | /* END_CASE */ |
| 8610 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8611 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8612 | void derive_actions_without_setup() |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8613 | { |
| 8614 | uint8_t output_buffer[16]; |
| 8615 | size_t buffer_size = 16; |
| 8616 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8617 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
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 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8620 | output_buffer, buffer_size) |
| 8621 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8622 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8623 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8624 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8625 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8626 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8627 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8628 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8629 | output_buffer, buffer_size) |
| 8630 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8631 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8632 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8633 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8634 | |
| 8635 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8636 | psa_key_derivation_abort(&operation); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8637 | } |
| 8638 | /* END_CASE */ |
| 8639 | |
| 8640 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8641 | void derive_output(int alg_arg, |
| 8642 | int step1_arg, data_t *input1, int expected_status_arg1, |
| 8643 | int step2_arg, data_t *input2, int expected_status_arg2, |
| 8644 | int step3_arg, data_t *input3, int expected_status_arg3, |
| 8645 | int step4_arg, data_t *input4, int expected_status_arg4, |
| 8646 | data_t *key_agreement_peer_key, |
| 8647 | int requested_capacity_arg, |
| 8648 | data_t *expected_output1, |
| 8649 | data_t *expected_output2, |
| 8650 | int other_key_input_type, |
| 8651 | int key_input_type, |
| 8652 | int derive_type) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8653 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8654 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8655 | psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg }; |
| 8656 | data_t *inputs[] = { input1, input2, input3, input4 }; |
| 8657 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8658 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8659 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8660 | MBEDTLS_SVC_KEY_ID_INIT }; |
| 8661 | psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2, |
| 8662 | expected_status_arg3, expected_status_arg4 }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8663 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8664 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8665 | uint8_t *expected_outputs[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8666 | { expected_output1->x, expected_output2->x }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8667 | size_t output_sizes[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8668 | { expected_output1->len, expected_output2->len }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8669 | size_t output_buffer_size = 0; |
| 8670 | uint8_t *output_buffer = NULL; |
| 8671 | size_t expected_capacity; |
| 8672 | size_t current_capacity; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8673 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; |
| 8674 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; |
| 8675 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; |
| 8676 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8677 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8678 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8679 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8680 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8681 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
| 8682 | if (output_sizes[i] > output_buffer_size) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8683 | output_buffer_size = output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8684 | } |
| 8685 | if (output_sizes[i] == 0) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8686 | expected_outputs[i] = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8687 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8688 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8689 | ASSERT_ALLOC(output_buffer, output_buffer_size); |
| 8690 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8691 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8692 | /* Extraction phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8693 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8694 | PSA_ASSERT(psa_key_derivation_set_capacity(&operation, |
| 8695 | requested_capacity)); |
| 8696 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8697 | switch (steps[i]) { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8698 | case 0: |
| 8699 | break; |
| 8700 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8701 | switch (key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8702 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8703 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8704 | &operation, steps[i], |
| 8705 | inputs[i]->x, inputs[i]->len), |
| 8706 | statuses[i]); |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8707 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8708 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8709 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8710 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8711 | break; |
| 8712 | case 1: // input key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8713 | psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE); |
| 8714 | psa_set_key_algorithm(&attributes1, alg); |
| 8715 | psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8716 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8717 | PSA_ASSERT(psa_import_key(&attributes1, |
| 8718 | inputs[i]->x, inputs[i]->len, |
| 8719 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8720 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8721 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 8722 | PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1)); |
| 8723 | TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)), |
| 8724 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8725 | } |
| 8726 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8727 | PSA_ASSERT(psa_key_derivation_input_key(&operation, |
| 8728 | steps[i], |
| 8729 | keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8730 | break; |
| 8731 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8732 | TEST_ASSERT(!"default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8733 | break; |
| 8734 | } |
| 8735 | break; |
| 8736 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8737 | switch (other_key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8738 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8739 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 8740 | steps[i], |
| 8741 | inputs[i]->x, |
| 8742 | inputs[i]->len), |
| 8743 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8744 | break; |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 8745 | case 1: // input key, type DERIVE |
| 8746 | case 11: // input key, type RAW |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8747 | psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE); |
| 8748 | psa_set_key_algorithm(&attributes2, alg); |
| 8749 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8750 | |
| 8751 | // other secret of type RAW_DATA passed with input_key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8752 | if (other_key_input_type == 11) { |
| 8753 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA); |
| 8754 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8755 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8756 | PSA_ASSERT(psa_import_key(&attributes2, |
| 8757 | inputs[i]->x, inputs[i]->len, |
| 8758 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8759 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8760 | TEST_EQUAL(psa_key_derivation_input_key(&operation, |
| 8761 | steps[i], |
| 8762 | keys[i]), |
| 8763 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8764 | break; |
| 8765 | case 2: // key agreement |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8766 | psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE); |
| 8767 | psa_set_key_algorithm(&attributes3, alg); |
| 8768 | psa_set_key_type(&attributes3, |
| 8769 | PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8771 | PSA_ASSERT(psa_import_key(&attributes3, |
| 8772 | inputs[i]->x, inputs[i]->len, |
| 8773 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8774 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8775 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 8776 | &operation, |
| 8777 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 8778 | keys[i], key_agreement_peer_key->x, |
| 8779 | key_agreement_peer_key->len), statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8780 | break; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8781 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8782 | TEST_ASSERT(!"default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8783 | break; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8784 | } |
| 8785 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8786 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8787 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8788 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8789 | break; |
| 8790 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8791 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8792 | &operation, steps[i], |
| 8793 | inputs[i]->x, inputs[i]->len), statuses[i]); |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 8794 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8795 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 8796 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8797 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8798 | break; |
| 8799 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 8800 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8801 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8802 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8803 | ¤t_capacity)); |
| 8804 | TEST_EQUAL(current_capacity, requested_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8805 | expected_capacity = requested_capacity; |
| 8806 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8807 | if (derive_type == 1) { // output key |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8808 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; |
| 8809 | |
| 8810 | /* For output key derivation secret must be provided using |
| 8811 | input key, otherwise operation is not permitted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8812 | if (key_input_type == 1) { |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8813 | expected_status = PSA_SUCCESS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8814 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8815 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8816 | psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT); |
| 8817 | psa_set_key_algorithm(&attributes4, alg); |
| 8818 | psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE); |
| 8819 | psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8820 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8821 | TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation, |
| 8822 | &derived_key), expected_status); |
| 8823 | } else { // output bytes |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8824 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8825 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8826 | /* Read some bytes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8827 | status = psa_key_derivation_output_bytes(&operation, |
| 8828 | output_buffer, output_sizes[i]); |
| 8829 | if (expected_capacity == 0 && output_sizes[i] == 0) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8830 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8831 | TEST_ASSERT(status == PSA_SUCCESS || |
| 8832 | status == PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8833 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8834 | } else if (expected_capacity == 0 || |
| 8835 | output_sizes[i] > expected_capacity) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8836 | /* Capacity exceeded. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8837 | TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8838 | expected_capacity = 0; |
| 8839 | continue; |
| 8840 | } |
| 8841 | /* Success. Check the read data. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8842 | PSA_ASSERT(status); |
| 8843 | if (output_sizes[i] != 0) { |
| 8844 | ASSERT_COMPARE(output_buffer, output_sizes[i], |
| 8845 | expected_outputs[i], output_sizes[i]); |
| 8846 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8847 | /* Check the operation status. */ |
| 8848 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8849 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8850 | ¤t_capacity)); |
| 8851 | TEST_EQUAL(expected_capacity, current_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8852 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8853 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8854 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8855 | |
| 8856 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8857 | mbedtls_free(output_buffer); |
| 8858 | psa_key_derivation_abort(&operation); |
| 8859 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8860 | psa_destroy_key(keys[i]); |
| 8861 | } |
| 8862 | psa_destroy_key(derived_key); |
| 8863 | PSA_DONE(); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8864 | } |
| 8865 | /* END_CASE */ |
| 8866 | |
| 8867 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8868 | void derive_full(int alg_arg, |
| 8869 | data_t *key_data, |
| 8870 | data_t *input1, |
| 8871 | data_t *input2, |
| 8872 | int requested_capacity_arg) |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8873 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8874 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8875 | psa_algorithm_t alg = alg_arg; |
| 8876 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8877 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8878 | unsigned char output_buffer[16]; |
| 8879 | size_t expected_capacity = requested_capacity; |
| 8880 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8881 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8882 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8883 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8884 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8885 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8886 | psa_set_key_algorithm(&attributes, alg); |
| 8887 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8888 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8889 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8890 | &key)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8891 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8892 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8893 | input1->x, input1->len, |
| 8894 | input2->x, input2->len, |
| 8895 | requested_capacity)) { |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 8896 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8897 | } |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 8898 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8899 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8900 | ¤t_capacity)); |
| 8901 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8902 | |
| 8903 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8904 | while (current_capacity > 0) { |
| 8905 | size_t read_size = sizeof(output_buffer); |
| 8906 | if (read_size > current_capacity) { |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8907 | read_size = current_capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8908 | } |
| 8909 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8910 | output_buffer, |
| 8911 | read_size)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8912 | expected_capacity -= read_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8913 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8914 | ¤t_capacity)); |
| 8915 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8916 | } |
| 8917 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8918 | /* Check that the operation refuses to go over capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8919 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1), |
| 8920 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8921 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8922 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8923 | |
| 8924 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8925 | psa_key_derivation_abort(&operation); |
| 8926 | psa_destroy_key(key); |
| 8927 | PSA_DONE(); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8928 | } |
| 8929 | /* END_CASE */ |
| 8930 | |
Przemek Stekiel | 8258ea7 | 2022-10-19 12:17:19 +0200 | [diff] [blame] | 8931 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8932 | void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg, |
| 8933 | int derivation_step, |
| 8934 | int capacity, int expected_capacity_status_arg, |
| 8935 | data_t *expected_output, |
| 8936 | int expected_output_status_arg) |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8937 | { |
| 8938 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 8939 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | d378504 | 2022-09-16 06:45:44 -0400 | [diff] [blame] | 8940 | 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] | 8941 | uint8_t *output_buffer = NULL; |
| 8942 | psa_status_t status; |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 8943 | psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg; |
| 8944 | psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg; |
| 8945 | 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] | 8946 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8947 | ASSERT_ALLOC(output_buffer, expected_output->len); |
| 8948 | PSA_ASSERT(psa_crypto_init()); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8949 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8950 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8951 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 8952 | expected_capacity_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8953 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8954 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 8955 | step, input->x, input->len), |
| 8956 | expected_input_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8957 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8958 | if (((psa_status_t) expected_input_status) != PSA_SUCCESS) { |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8959 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8960 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8961 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8962 | status = psa_key_derivation_output_bytes(&operation, output_buffer, |
| 8963 | expected_output->len); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8964 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8965 | TEST_EQUAL(status, expected_output_status); |
| 8966 | if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) { |
| 8967 | ASSERT_COMPARE(output_buffer, expected_output->len, expected_output->x, |
| 8968 | expected_output->len); |
| 8969 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8970 | |
| 8971 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8972 | mbedtls_free(output_buffer); |
| 8973 | psa_key_derivation_abort(&operation); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8974 | PSA_DONE(); |
| 8975 | } |
| 8976 | /* END_CASE */ |
| 8977 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 8978 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8979 | void derive_key_exercise(int alg_arg, |
| 8980 | data_t *key_data, |
| 8981 | data_t *input1, |
| 8982 | data_t *input2, |
| 8983 | int derived_type_arg, |
| 8984 | int derived_bits_arg, |
| 8985 | int derived_usage_arg, |
| 8986 | int derived_alg_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8987 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8988 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8989 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8990 | psa_algorithm_t alg = alg_arg; |
| 8991 | psa_key_type_t derived_type = derived_type_arg; |
| 8992 | size_t derived_bits = derived_bits_arg; |
| 8993 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 8994 | psa_algorithm_t derived_alg = derived_alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8995 | size_t capacity = PSA_BITS_TO_BYTES(derived_bits); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8996 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8997 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8998 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8999 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9000 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9001 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9002 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9003 | psa_set_key_algorithm(&attributes, alg); |
| 9004 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
| 9005 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 9006 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9007 | |
| 9008 | /* Derive a key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9009 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9010 | input1->x, input1->len, |
| 9011 | input2->x, input2->len, |
| 9012 | capacity)) { |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9013 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9014 | } |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9015 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9016 | psa_set_key_usage_flags(&attributes, derived_usage); |
| 9017 | psa_set_key_algorithm(&attributes, derived_alg); |
| 9018 | psa_set_key_type(&attributes, derived_type); |
| 9019 | psa_set_key_bits(&attributes, derived_bits); |
| 9020 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation, |
| 9021 | &derived_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9022 | |
| 9023 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9024 | PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes)); |
| 9025 | TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type); |
| 9026 | TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9027 | |
| 9028 | /* Exercise the derived key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9029 | if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) { |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9030 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9031 | } |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9032 | |
| 9033 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9034 | /* |
| 9035 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9036 | * thus reset them as required. |
| 9037 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9038 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9039 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9040 | psa_key_derivation_abort(&operation); |
| 9041 | psa_destroy_key(base_key); |
| 9042 | psa_destroy_key(derived_key); |
| 9043 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9044 | } |
| 9045 | /* END_CASE */ |
| 9046 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9047 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9048 | void derive_key_export(int alg_arg, |
| 9049 | data_t *key_data, |
| 9050 | data_t *input1, |
| 9051 | data_t *input2, |
| 9052 | int bytes1_arg, |
| 9053 | int bytes2_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9054 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9055 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9056 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9057 | psa_algorithm_t alg = alg_arg; |
| 9058 | size_t bytes1 = bytes1_arg; |
| 9059 | size_t bytes2 = bytes2_arg; |
| 9060 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9061 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9062 | uint8_t *output_buffer = NULL; |
| 9063 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9064 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9065 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9066 | size_t length; |
| 9067 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9068 | ASSERT_ALLOC(output_buffer, capacity); |
| 9069 | ASSERT_ALLOC(export_buffer, capacity); |
| 9070 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9071 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9072 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9073 | psa_set_key_algorithm(&base_attributes, alg); |
| 9074 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9075 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9076 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9077 | |
| 9078 | /* Derive some material and output it. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9079 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9080 | input1->x, input1->len, |
| 9081 | input2->x, input2->len, |
| 9082 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9083 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9084 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9085 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9086 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9087 | output_buffer, |
| 9088 | capacity)); |
| 9089 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9090 | |
| 9091 | /* 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] | 9092 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9093 | input1->x, input1->len, |
| 9094 | input2->x, input2->len, |
| 9095 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9096 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9097 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9098 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9099 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9100 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9101 | psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA); |
| 9102 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1)); |
| 9103 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9104 | &derived_key)); |
| 9105 | PSA_ASSERT(psa_export_key(derived_key, |
| 9106 | export_buffer, bytes1, |
| 9107 | &length)); |
| 9108 | TEST_EQUAL(length, bytes1); |
| 9109 | PSA_ASSERT(psa_destroy_key(derived_key)); |
| 9110 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2)); |
| 9111 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9112 | &derived_key)); |
| 9113 | PSA_ASSERT(psa_export_key(derived_key, |
| 9114 | export_buffer + bytes1, bytes2, |
| 9115 | &length)); |
| 9116 | TEST_EQUAL(length, bytes2); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9117 | |
| 9118 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9119 | ASSERT_COMPARE(output_buffer, bytes1 + bytes2, |
| 9120 | export_buffer, capacity); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9121 | |
| 9122 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9123 | mbedtls_free(output_buffer); |
| 9124 | mbedtls_free(export_buffer); |
| 9125 | psa_key_derivation_abort(&operation); |
| 9126 | psa_destroy_key(base_key); |
| 9127 | psa_destroy_key(derived_key); |
| 9128 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9129 | } |
| 9130 | /* END_CASE */ |
| 9131 | |
| 9132 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9133 | void derive_key_type(int alg_arg, |
| 9134 | data_t *key_data, |
| 9135 | data_t *input1, |
| 9136 | data_t *input2, |
| 9137 | int key_type_arg, int bits_arg, |
| 9138 | data_t *expected_export) |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9139 | { |
| 9140 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9141 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9142 | const psa_algorithm_t alg = alg_arg; |
| 9143 | const psa_key_type_t key_type = key_type_arg; |
| 9144 | const size_t bits = bits_arg; |
| 9145 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9146 | const size_t export_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9147 | PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9148 | uint8_t *export_buffer = NULL; |
| 9149 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9150 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9151 | size_t export_length; |
| 9152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9153 | ASSERT_ALLOC(export_buffer, export_buffer_size); |
| 9154 | PSA_ASSERT(psa_crypto_init()); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9155 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9156 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9157 | psa_set_key_algorithm(&base_attributes, alg); |
| 9158 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9159 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9160 | &base_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9161 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9162 | if (mbedtls_test_psa_setup_key_derivation_wrap( |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9163 | &operation, base_key, alg, |
| 9164 | input1->x, input1->len, |
| 9165 | input2->x, input2->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9166 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) { |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9167 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9168 | } |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9169 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9170 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9171 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9172 | psa_set_key_type(&derived_attributes, key_type); |
| 9173 | psa_set_key_bits(&derived_attributes, bits); |
| 9174 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9175 | &derived_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9176 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9177 | PSA_ASSERT(psa_export_key(derived_key, |
| 9178 | export_buffer, export_buffer_size, |
| 9179 | &export_length)); |
| 9180 | ASSERT_COMPARE(export_buffer, export_length, |
| 9181 | expected_export->x, expected_export->len); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9182 | |
| 9183 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9184 | mbedtls_free(export_buffer); |
| 9185 | psa_key_derivation_abort(&operation); |
| 9186 | psa_destroy_key(base_key); |
| 9187 | psa_destroy_key(derived_key); |
| 9188 | PSA_DONE(); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9189 | } |
| 9190 | /* END_CASE */ |
| 9191 | |
| 9192 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9193 | void derive_key(int alg_arg, |
| 9194 | data_t *key_data, data_t *input1, data_t *input2, |
| 9195 | int type_arg, int bits_arg, |
| 9196 | int expected_status_arg, |
| 9197 | int is_large_output) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9198 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9199 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9200 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9201 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 9202 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9203 | size_t bits = bits_arg; |
| 9204 | psa_status_t expected_status = expected_status_arg; |
| 9205 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9206 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9207 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9208 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9209 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9210 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9211 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9212 | psa_set_key_algorithm(&base_attributes, alg); |
| 9213 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9214 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9215 | &base_key)); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9216 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9217 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9218 | input1->x, input1->len, |
| 9219 | input2->x, input2->len, |
| 9220 | SIZE_MAX)) { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9221 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9222 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9223 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9224 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9225 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9226 | psa_set_key_type(&derived_attributes, type); |
| 9227 | psa_set_key_bits(&derived_attributes, bits); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9228 | |
| 9229 | psa_status_t status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9230 | psa_key_derivation_output_key(&derived_attributes, |
| 9231 | &operation, |
| 9232 | &derived_key); |
| 9233 | if (is_large_output > 0) { |
| 9234 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9235 | } |
| 9236 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9237 | |
| 9238 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9239 | psa_key_derivation_abort(&operation); |
| 9240 | psa_destroy_key(base_key); |
| 9241 | psa_destroy_key(derived_key); |
| 9242 | PSA_DONE(); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9243 | } |
| 9244 | /* END_CASE */ |
| 9245 | |
| 9246 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9247 | void key_agreement_setup(int alg_arg, |
| 9248 | int our_key_type_arg, int our_key_alg_arg, |
| 9249 | data_t *our_key_data, data_t *peer_key_data, |
| 9250 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9251 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9252 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9253 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 9254 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9255 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9256 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9257 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9258 | psa_status_t expected_status = expected_status_arg; |
| 9259 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9260 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9261 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9263 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9264 | psa_set_key_algorithm(&attributes, our_key_alg); |
| 9265 | psa_set_key_type(&attributes, our_key_type); |
| 9266 | PSA_ASSERT(psa_import_key(&attributes, |
| 9267 | our_key_data->x, our_key_data->len, |
| 9268 | &our_key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9269 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9270 | /* The tests currently include inputs that should fail at either step. |
| 9271 | * Test cases that fail at the setup step should be changed to call |
| 9272 | * key_derivation_setup instead, and this function should be renamed |
| 9273 | * to key_agreement_fail. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9274 | status = psa_key_derivation_setup(&operation, alg); |
| 9275 | if (status == PSA_SUCCESS) { |
| 9276 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 9277 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 9278 | our_key, |
| 9279 | peer_key_data->x, peer_key_data->len), |
| 9280 | expected_status); |
| 9281 | } else { |
| 9282 | TEST_ASSERT(status == expected_status); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9283 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9284 | |
| 9285 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9286 | psa_key_derivation_abort(&operation); |
| 9287 | psa_destroy_key(our_key); |
| 9288 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9289 | } |
| 9290 | /* END_CASE */ |
| 9291 | |
| 9292 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9293 | void raw_key_agreement(int alg_arg, |
| 9294 | int our_key_type_arg, data_t *our_key_data, |
| 9295 | data_t *peer_key_data, |
| 9296 | data_t *expected_output) |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9297 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9298 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9299 | psa_algorithm_t alg = alg_arg; |
| 9300 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9301 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9302 | unsigned char *output = NULL; |
| 9303 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9304 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9306 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9307 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9308 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9309 | psa_set_key_algorithm(&attributes, alg); |
| 9310 | psa_set_key_type(&attributes, our_key_type); |
| 9311 | PSA_ASSERT(psa_import_key(&attributes, |
| 9312 | our_key_data->x, our_key_data->len, |
| 9313 | &our_key)); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9314 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9315 | PSA_ASSERT(psa_get_key_attributes(our_key, &attributes)); |
| 9316 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9317 | |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9318 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9319 | TEST_LE_U(expected_output->len, |
| 9320 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits)); |
| 9321 | TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits), |
| 9322 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9323 | |
| 9324 | /* Good case with exact output size */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9325 | ASSERT_ALLOC(output, expected_output->len); |
| 9326 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9327 | peer_key_data->x, peer_key_data->len, |
| 9328 | output, expected_output->len, |
| 9329 | &output_length)); |
| 9330 | ASSERT_COMPARE(output, output_length, |
| 9331 | expected_output->x, expected_output->len); |
| 9332 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9333 | output = NULL; |
| 9334 | output_length = ~0; |
| 9335 | |
| 9336 | /* Larger buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9337 | ASSERT_ALLOC(output, expected_output->len + 1); |
| 9338 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9339 | peer_key_data->x, peer_key_data->len, |
| 9340 | output, expected_output->len + 1, |
| 9341 | &output_length)); |
| 9342 | ASSERT_COMPARE(output, output_length, |
| 9343 | expected_output->x, expected_output->len); |
| 9344 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9345 | output = NULL; |
| 9346 | output_length = ~0; |
| 9347 | |
| 9348 | /* Buffer too small */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9349 | ASSERT_ALLOC(output, expected_output->len - 1); |
| 9350 | TEST_EQUAL(psa_raw_key_agreement(alg, our_key, |
| 9351 | peer_key_data->x, peer_key_data->len, |
| 9352 | output, expected_output->len - 1, |
| 9353 | &output_length), |
| 9354 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9355 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9356 | TEST_LE_U(output_length, expected_output->len - 1); |
| 9357 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9358 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9359 | |
| 9360 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9361 | mbedtls_free(output); |
| 9362 | psa_destroy_key(our_key); |
| 9363 | PSA_DONE(); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9364 | } |
| 9365 | /* END_CASE */ |
| 9366 | |
| 9367 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9368 | void key_agreement_capacity(int alg_arg, |
| 9369 | int our_key_type_arg, data_t *our_key_data, |
| 9370 | data_t *peer_key_data, |
| 9371 | int expected_capacity_arg) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9372 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9373 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9374 | psa_algorithm_t alg = alg_arg; |
| 9375 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9376 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9377 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9378 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9379 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9380 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9381 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9382 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9383 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9384 | psa_set_key_algorithm(&attributes, alg); |
| 9385 | psa_set_key_type(&attributes, our_key_type); |
| 9386 | PSA_ASSERT(psa_import_key(&attributes, |
| 9387 | our_key_data->x, our_key_data->len, |
| 9388 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9389 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9390 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9391 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9392 | &operation, |
| 9393 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9394 | peer_key_data->x, peer_key_data->len)); |
| 9395 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9396 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9397 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9398 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9399 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9400 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9401 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 9402 | /* Test the advertised capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9403 | PSA_ASSERT(psa_key_derivation_get_capacity( |
| 9404 | &operation, &actual_capacity)); |
| 9405 | TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9406 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9407 | /* Test the actual capacity by reading the output. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9408 | while (actual_capacity > sizeof(output)) { |
| 9409 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9410 | output, sizeof(output))); |
| 9411 | actual_capacity -= sizeof(output); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9412 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9413 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9414 | output, actual_capacity)); |
| 9415 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1), |
| 9416 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9417 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9418 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9419 | psa_key_derivation_abort(&operation); |
| 9420 | psa_destroy_key(our_key); |
| 9421 | PSA_DONE(); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9422 | } |
| 9423 | /* END_CASE */ |
| 9424 | |
| 9425 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9426 | void key_agreement_output(int alg_arg, |
| 9427 | int our_key_type_arg, data_t *our_key_data, |
| 9428 | data_t *peer_key_data, |
| 9429 | data_t *expected_output1, data_t *expected_output2) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9430 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9431 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9432 | psa_algorithm_t alg = alg_arg; |
| 9433 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9434 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9435 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9436 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9437 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9438 | ASSERT_ALLOC(actual_output, MAX(expected_output1->len, |
| 9439 | expected_output2->len)); |
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_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9442 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9443 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9444 | psa_set_key_algorithm(&attributes, alg); |
| 9445 | psa_set_key_type(&attributes, our_key_type); |
| 9446 | PSA_ASSERT(psa_import_key(&attributes, |
| 9447 | our_key_data->x, our_key_data->len, |
| 9448 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9449 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9450 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9451 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9452 | &operation, |
| 9453 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9454 | peer_key_data->x, peer_key_data->len)); |
| 9455 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9456 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9457 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9458 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9459 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9460 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9461 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9462 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9463 | actual_output, |
| 9464 | expected_output1->len)); |
| 9465 | ASSERT_COMPARE(actual_output, expected_output1->len, |
| 9466 | expected_output1->x, expected_output1->len); |
| 9467 | if (expected_output2->len != 0) { |
| 9468 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9469 | actual_output, |
| 9470 | expected_output2->len)); |
| 9471 | ASSERT_COMPARE(actual_output, expected_output2->len, |
| 9472 | expected_output2->x, expected_output2->len); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9473 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9474 | |
| 9475 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9476 | psa_key_derivation_abort(&operation); |
| 9477 | psa_destroy_key(our_key); |
| 9478 | PSA_DONE(); |
| 9479 | mbedtls_free(actual_output); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9480 | } |
| 9481 | /* END_CASE */ |
| 9482 | |
| 9483 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9484 | void generate_random(int bytes_arg) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9485 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9486 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9487 | unsigned char *output = NULL; |
| 9488 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9489 | size_t i; |
| 9490 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9491 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9492 | TEST_ASSERT(bytes_arg >= 0); |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 9493 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9494 | ASSERT_ALLOC(output, bytes); |
| 9495 | ASSERT_ALLOC(changed, bytes); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9496 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9497 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9498 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9499 | /* Run several times, to ensure that every output byte will be |
| 9500 | * nonzero at least once with overwhelming probability |
| 9501 | * (2^(-8*number_of_runs)). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9502 | for (run = 0; run < 10; run++) { |
| 9503 | if (bytes != 0) { |
| 9504 | memset(output, 0, bytes); |
| 9505 | } |
| 9506 | PSA_ASSERT(psa_generate_random(output, bytes)); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9507 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9508 | for (i = 0; i < bytes; i++) { |
| 9509 | if (output[i] != 0) { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9510 | ++changed[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9511 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9512 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9513 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9514 | |
| 9515 | /* Check that every byte was changed to nonzero at least once. This |
| 9516 | * validates that psa_generate_random is overwriting every byte of |
| 9517 | * the output buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9518 | for (i = 0; i < bytes; i++) { |
| 9519 | TEST_ASSERT(changed[i] != 0); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9520 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9521 | |
| 9522 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9523 | PSA_DONE(); |
| 9524 | mbedtls_free(output); |
| 9525 | mbedtls_free(changed); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9526 | } |
| 9527 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9528 | |
| 9529 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9530 | void generate_key(int type_arg, |
| 9531 | int bits_arg, |
| 9532 | int usage_arg, |
| 9533 | int alg_arg, |
| 9534 | int expected_status_arg, |
| 9535 | int is_large_key) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9536 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9537 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9538 | psa_key_type_t type = type_arg; |
| 9539 | psa_key_usage_t usage = usage_arg; |
| 9540 | size_t bits = bits_arg; |
| 9541 | psa_algorithm_t alg = alg_arg; |
| 9542 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9543 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 9544 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9545 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9546 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9547 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9548 | psa_set_key_usage_flags(&attributes, usage); |
| 9549 | psa_set_key_algorithm(&attributes, alg); |
| 9550 | psa_set_key_type(&attributes, type); |
| 9551 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9552 | |
| 9553 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9554 | psa_status_t status = psa_generate_key(&attributes, &key); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9555 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9556 | if (is_large_key > 0) { |
| 9557 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9558 | } |
| 9559 | TEST_EQUAL(status, expected_status); |
| 9560 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9561 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9562 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9563 | |
| 9564 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9565 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 9566 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 9567 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9568 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 9569 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9570 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 9571 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9572 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9573 | |
| 9574 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9575 | /* |
| 9576 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9577 | * thus reset them as required. |
| 9578 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9579 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9580 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9581 | psa_destroy_key(key); |
| 9582 | PSA_DONE(); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9583 | } |
| 9584 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 9585 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 9586 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR: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] | 9587 | void generate_key_rsa(int bits_arg, |
| 9588 | data_t *e_arg, |
| 9589 | int expected_status_arg) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9590 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9591 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 9592 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9593 | size_t bits = bits_arg; |
| 9594 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 9595 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 9596 | psa_status_t expected_status = expected_status_arg; |
| 9597 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9598 | uint8_t *exported = NULL; |
| 9599 | size_t exported_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9600 | PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9601 | size_t exported_length = SIZE_MAX; |
| 9602 | uint8_t *e_read_buffer = NULL; |
| 9603 | int is_default_public_exponent = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9604 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9605 | size_t e_read_length = SIZE_MAX; |
| 9606 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9607 | if (e_arg->len == 0 || |
| 9608 | (e_arg->len == 3 && |
| 9609 | 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] | 9610 | is_default_public_exponent = 1; |
| 9611 | e_read_size = 0; |
| 9612 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9613 | ASSERT_ALLOC(e_read_buffer, e_read_size); |
| 9614 | ASSERT_ALLOC(exported, exported_size); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9615 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9616 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9617 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9618 | psa_set_key_usage_flags(&attributes, usage); |
| 9619 | psa_set_key_algorithm(&attributes, alg); |
| 9620 | PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type, |
| 9621 | e_arg->x, e_arg->len)); |
| 9622 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9623 | |
| 9624 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9625 | TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status); |
| 9626 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9627 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9628 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9629 | |
| 9630 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9631 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 9632 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 9633 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 9634 | PSA_ASSERT(psa_get_key_domain_parameters(&attributes, |
| 9635 | e_read_buffer, e_read_size, |
| 9636 | &e_read_length)); |
| 9637 | if (is_default_public_exponent) { |
| 9638 | TEST_EQUAL(e_read_length, 0); |
| 9639 | } else { |
| 9640 | ASSERT_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len); |
| 9641 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9642 | |
| 9643 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9644 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9645 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9646 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9647 | |
| 9648 | /* Export the key and check the public exponent. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9649 | PSA_ASSERT(psa_export_public_key(key, |
| 9650 | exported, exported_size, |
| 9651 | &exported_length)); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9652 | { |
| 9653 | uint8_t *p = exported; |
| 9654 | uint8_t *end = exported + exported_length; |
| 9655 | size_t len; |
| 9656 | /* RSAPublicKey ::= SEQUENCE { |
| 9657 | * modulus INTEGER, -- n |
| 9658 | * publicExponent INTEGER } -- e |
| 9659 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9660 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 9661 | MBEDTLS_ASN1_SEQUENCE | |
| 9662 | MBEDTLS_ASN1_CONSTRUCTED)); |
| 9663 | TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1)); |
| 9664 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 9665 | MBEDTLS_ASN1_INTEGER)); |
| 9666 | if (len >= 1 && p[0] == 0) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9667 | ++p; |
| 9668 | --len; |
| 9669 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9670 | if (e_arg->len == 0) { |
| 9671 | TEST_EQUAL(len, 3); |
| 9672 | TEST_EQUAL(p[0], 1); |
| 9673 | TEST_EQUAL(p[1], 0); |
| 9674 | TEST_EQUAL(p[2], 1); |
| 9675 | } else { |
| 9676 | ASSERT_COMPARE(p, len, e_arg->x, e_arg->len); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9677 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9678 | } |
| 9679 | |
| 9680 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9681 | /* |
| 9682 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 9683 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 9684 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9685 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9686 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9687 | psa_destroy_key(key); |
| 9688 | PSA_DONE(); |
| 9689 | mbedtls_free(e_read_buffer); |
| 9690 | mbedtls_free(exported); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9691 | } |
| 9692 | /* END_CASE */ |
| 9693 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9694 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9695 | void persistent_key_load_key_from_storage(data_t *data, |
| 9696 | int type_arg, int bits_arg, |
| 9697 | int usage_flags_arg, int alg_arg, |
| 9698 | int generation_method) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9699 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9700 | 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] | 9701 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9702 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9703 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9704 | psa_key_type_t type = type_arg; |
| 9705 | size_t bits = bits_arg; |
| 9706 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 9707 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9708 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9709 | unsigned char *first_export = NULL; |
| 9710 | unsigned char *second_export = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9711 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9712 | size_t first_exported_length; |
| 9713 | size_t second_exported_length; |
| 9714 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9715 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9716 | ASSERT_ALLOC(first_export, export_size); |
| 9717 | ASSERT_ALLOC(second_export, export_size); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9718 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9719 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9720 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9721 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9722 | psa_set_key_id(&attributes, key_id); |
| 9723 | psa_set_key_usage_flags(&attributes, usage_flags); |
| 9724 | psa_set_key_algorithm(&attributes, alg); |
| 9725 | psa_set_key_type(&attributes, type); |
| 9726 | psa_set_key_bits(&attributes, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9727 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9728 | switch (generation_method) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9729 | case IMPORT_KEY: |
| 9730 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9731 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, |
| 9732 | &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9733 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9734 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9735 | case GENERATE_KEY: |
| 9736 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9737 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9738 | break; |
| 9739 | |
| 9740 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 9741 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9742 | { |
| 9743 | /* Create base key */ |
| 9744 | psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256); |
| 9745 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9746 | psa_set_key_usage_flags(&base_attributes, |
| 9747 | PSA_KEY_USAGE_DERIVE); |
| 9748 | psa_set_key_algorithm(&base_attributes, derive_alg); |
| 9749 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9750 | PSA_ASSERT(psa_import_key(&base_attributes, |
| 9751 | data->x, data->len, |
| 9752 | &base_key)); |
| 9753 | /* Derive a key. */ |
| 9754 | PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg)); |
| 9755 | PSA_ASSERT(psa_key_derivation_input_key( |
| 9756 | &operation, |
| 9757 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key)); |
| 9758 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 9759 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
| 9760 | NULL, 0)); |
| 9761 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, |
| 9762 | &operation, |
| 9763 | &key)); |
| 9764 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
| 9765 | PSA_ASSERT(psa_destroy_key(base_key)); |
| 9766 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9767 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9768 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9769 | TEST_ASSUME(!"KDF not supported in this configuration"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9770 | #endif |
| 9771 | break; |
| 9772 | |
| 9773 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9774 | TEST_ASSERT(!"generation_method not implemented in test"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9775 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9776 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9777 | psa_reset_key_attributes(&attributes); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9778 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9779 | /* Export the key if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9780 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9781 | PSA_ASSERT(psa_export_key(key, |
| 9782 | first_export, export_size, |
| 9783 | &first_exported_length)); |
| 9784 | if (generation_method == IMPORT_KEY) { |
| 9785 | ASSERT_COMPARE(data->x, data->len, |
| 9786 | first_export, first_exported_length); |
| 9787 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9788 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9789 | |
| 9790 | /* Shutdown and restart */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9791 | PSA_ASSERT(psa_purge_key(key)); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 9792 | PSA_DONE(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9793 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9794 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9795 | /* Check key slot still contains key data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9796 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 9797 | TEST_ASSERT(mbedtls_svc_key_id_equal( |
| 9798 | psa_get_key_id(&attributes), key_id)); |
| 9799 | TEST_EQUAL(psa_get_key_lifetime(&attributes), |
| 9800 | PSA_KEY_LIFETIME_PERSISTENT); |
| 9801 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 9802 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 9803 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 9804 | mbedtls_test_update_key_usage_flags(usage_flags)); |
| 9805 | TEST_EQUAL(psa_get_key_algorithm(&attributes), alg); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9806 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9807 | /* Export the key again if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9808 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9809 | PSA_ASSERT(psa_export_key(key, |
| 9810 | second_export, export_size, |
| 9811 | &second_exported_length)); |
| 9812 | ASSERT_COMPARE(first_export, first_exported_length, |
| 9813 | second_export, second_exported_length); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9814 | } |
| 9815 | |
| 9816 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9817 | if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9818 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9819 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9820 | |
| 9821 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9822 | /* |
| 9823 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9824 | * thus reset them as required. |
| 9825 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9826 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9827 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9828 | mbedtls_free(first_export); |
| 9829 | mbedtls_free(second_export); |
| 9830 | psa_key_derivation_abort(&operation); |
| 9831 | psa_destroy_key(base_key); |
| 9832 | psa_destroy_key(key); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 9833 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9834 | } |
| 9835 | /* END_CASE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9836 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 9837 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9838 | void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg, |
| 9839 | int primitive_arg, int hash_arg, int role_arg, |
| 9840 | int test_input, data_t *pw_data, |
| 9841 | int inj_err_type_arg, |
| 9842 | int expected_error_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9843 | { |
| 9844 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 9845 | psa_pake_operation_t operation = psa_pake_operation_init(); |
| 9846 | psa_algorithm_t alg = alg_arg; |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9847 | psa_pake_primitive_t primitive = primitive_arg; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 9848 | psa_key_type_t key_type_pw = key_type_pw_arg; |
| 9849 | psa_key_usage_t key_usage_pw = key_usage_pw_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9850 | psa_algorithm_t hash_alg = hash_arg; |
| 9851 | psa_pake_role_t role = role_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9852 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9853 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9854 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
| 9855 | psa_status_t expected_error = expected_error_arg; |
| 9856 | psa_status_t status; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9857 | unsigned char *output_buffer = NULL; |
| 9858 | size_t output_len = 0; |
| 9859 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9860 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9861 | |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9862 | size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9863 | PSA_PAKE_STEP_KEY_SHARE); |
| 9864 | ASSERT_ALLOC(output_buffer, buf_size); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9865 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9866 | if (pw_data->len > 0) { |
| 9867 | psa_set_key_usage_flags(&attributes, key_usage_pw); |
| 9868 | psa_set_key_algorithm(&attributes, alg); |
| 9869 | psa_set_key_type(&attributes, key_type_pw); |
| 9870 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 9871 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9872 | } |
| 9873 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9874 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 9875 | psa_pake_cs_set_primitive(&cipher_suite, primitive); |
| 9876 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9877 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9878 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 9879 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9880 | if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) { |
| 9881 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 9882 | expected_error); |
| 9883 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9884 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 9885 | expected_error); |
| 9886 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9887 | TEST_EQUAL(psa_pake_set_password_key(&operation, key), |
| 9888 | expected_error); |
| 9889 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9890 | TEST_EQUAL(psa_pake_set_role(&operation, role), |
| 9891 | expected_error); |
| 9892 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9893 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 9894 | NULL, 0, NULL), |
| 9895 | expected_error); |
| 9896 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9897 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0), |
| 9898 | expected_error); |
| 9899 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9900 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9901 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9902 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9903 | status = psa_pake_setup(&operation, &cipher_suite); |
| 9904 | if (status != PSA_SUCCESS) { |
| 9905 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9906 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9907 | } |
| 9908 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9909 | if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) { |
| 9910 | TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite), |
| 9911 | expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9912 | goto exit; |
| 9913 | } |
| 9914 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9915 | status = psa_pake_set_role(&operation, role); |
| 9916 | if (status != PSA_SUCCESS) { |
| 9917 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9918 | goto exit; |
| 9919 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9920 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9921 | if (pw_data->len > 0) { |
| 9922 | status = psa_pake_set_password_key(&operation, key); |
| 9923 | if (status != PSA_SUCCESS) { |
| 9924 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9925 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9926 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9927 | } |
| 9928 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9929 | if (inj_err_type == INJECT_ERR_INVALID_USER) { |
| 9930 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 9931 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9932 | goto exit; |
| 9933 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9934 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9935 | if (inj_err_type == INJECT_ERR_INVALID_PEER) { |
| 9936 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 9937 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9938 | goto exit; |
| 9939 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9940 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9941 | if (inj_err_type == INJECT_ERR_SET_USER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9942 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9943 | TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4), |
| 9944 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9945 | goto exit; |
| 9946 | } |
| 9947 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9948 | if (inj_err_type == INJECT_ERR_SET_PEER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9949 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9950 | TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4), |
| 9951 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9952 | goto exit; |
| 9953 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9954 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9955 | const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 9956 | PSA_PAKE_STEP_KEY_SHARE); |
| 9957 | const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 9958 | PSA_PAKE_STEP_ZK_PUBLIC); |
| 9959 | const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 9960 | PSA_PAKE_STEP_ZK_PROOF); |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9961 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9962 | if (test_input) { |
| 9963 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 9964 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0), |
| 9965 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9966 | goto exit; |
| 9967 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 9968 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9969 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 9970 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 9971 | output_buffer, size_zk_proof), |
| 9972 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9973 | goto exit; |
| 9974 | } |
| 9975 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9976 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 9977 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 9978 | output_buffer, size_zk_proof), |
| 9979 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9980 | goto exit; |
| 9981 | } |
| 9982 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9983 | status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 9984 | output_buffer, size_key_share); |
| 9985 | if (status != PSA_SUCCESS) { |
| 9986 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9987 | goto exit; |
| 9988 | } |
| 9989 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9990 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 9991 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9992 | output_buffer, size_zk_public + 1), |
| 9993 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9994 | goto exit; |
| 9995 | } |
| 9996 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9997 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9998 | // 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] | 9999 | psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10000 | output_buffer, size_zk_public + 1); |
| 10001 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10002 | output_buffer, size_zk_public), |
| 10003 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10004 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10005 | } |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10006 | } else { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10007 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 10008 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10009 | NULL, 0, NULL), |
| 10010 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10011 | goto exit; |
| 10012 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10013 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10014 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 10015 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 10016 | output_buffer, buf_size, &output_len), |
| 10017 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10018 | goto exit; |
| 10019 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10020 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10021 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 10022 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10023 | output_buffer, buf_size, &output_len), |
| 10024 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10025 | goto exit; |
| 10026 | } |
| 10027 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10028 | status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10029 | output_buffer, buf_size, &output_len); |
| 10030 | if (status != PSA_SUCCESS) { |
| 10031 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10032 | goto exit; |
| 10033 | } |
| 10034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10035 | TEST_ASSERT(output_len > 0); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10036 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10037 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 10038 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10039 | output_buffer, size_zk_public - 1, &output_len), |
| 10040 | PSA_ERROR_BUFFER_TOO_SMALL); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10041 | goto exit; |
| 10042 | } |
| 10043 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10044 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10045 | // 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] | 10046 | psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10047 | output_buffer, size_zk_public - 1, &output_len); |
| 10048 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10049 | output_buffer, buf_size, &output_len), |
| 10050 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10051 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10052 | } |
| 10053 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10054 | |
| 10055 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10056 | PSA_ASSERT(psa_destroy_key(key)); |
| 10057 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10058 | mbedtls_free(output_buffer); |
| 10059 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10060 | } |
| 10061 | /* END_CASE */ |
| 10062 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 10063 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10064 | void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg, |
| 10065 | int client_input_first, int inject_error, |
| 10066 | data_t *pw_data) |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10067 | { |
| 10068 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10069 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10070 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10071 | psa_algorithm_t alg = alg_arg; |
| 10072 | psa_algorithm_t hash_alg = hash_arg; |
| 10073 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10074 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10075 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10076 | PSA_INIT(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10077 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10078 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10079 | psa_set_key_algorithm(&attributes, alg); |
| 10080 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10081 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10082 | &key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10083 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10084 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10085 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10086 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10087 | |
| 10088 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10089 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10090 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10091 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10092 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10093 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10094 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10095 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10096 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10097 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10098 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10099 | client_input_first, 1, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10100 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10101 | if (inject_error == 1 || inject_error == 2) { |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10102 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10103 | } |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10104 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10105 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10106 | client_input_first, 2, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10107 | |
| 10108 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10109 | psa_destroy_key(key); |
| 10110 | psa_pake_abort(&server); |
| 10111 | psa_pake_abort(&client); |
| 10112 | PSA_DONE(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10113 | } |
| 10114 | /* END_CASE */ |
| 10115 | |
| 10116 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10117 | void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg, |
| 10118 | int derive_alg_arg, data_t *pw_data, |
| 10119 | int client_input_first, int inj_err_type_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10120 | { |
| 10121 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10122 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10123 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10124 | psa_algorithm_t alg = alg_arg; |
| 10125 | psa_algorithm_t hash_alg = hash_arg; |
| 10126 | psa_algorithm_t derive_alg = derive_alg_arg; |
| 10127 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10128 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10129 | psa_key_derivation_operation_t server_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10130 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10131 | psa_key_derivation_operation_t client_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10132 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10133 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10135 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10136 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10137 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10138 | psa_set_key_algorithm(&attributes, alg); |
| 10139 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10140 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10141 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10142 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10143 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10144 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10145 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10146 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10147 | /* Get shared key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10148 | PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg)); |
| 10149 | PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10151 | if (PSA_ALG_IS_TLS12_PRF(derive_alg) || |
| 10152 | PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) { |
| 10153 | PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive, |
| 10154 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10155 | (const uint8_t *) "", 0)); |
| 10156 | PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive, |
| 10157 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10158 | (const uint8_t *) "", 0)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10159 | } |
| 10160 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10161 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10162 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10164 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10165 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10166 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10167 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10168 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10169 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10170 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) { |
| 10171 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10172 | PSA_ERROR_BAD_STATE); |
| 10173 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10174 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10175 | goto exit; |
| 10176 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10177 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10178 | /* First round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10179 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10180 | client_input_first, 1, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10181 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10182 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) { |
| 10183 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10184 | PSA_ERROR_BAD_STATE); |
| 10185 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10186 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10187 | goto exit; |
| 10188 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10189 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10190 | /* Second round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10191 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10192 | client_input_first, 2, 0); |
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_ASSERT(psa_pake_get_implicit_key(&server, &server_derive)); |
| 10195 | PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10196 | |
| 10197 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10198 | psa_key_derivation_abort(&server_derive); |
| 10199 | psa_key_derivation_abort(&client_derive); |
| 10200 | psa_destroy_key(key); |
| 10201 | psa_pake_abort(&server); |
| 10202 | psa_pake_abort(&client); |
| 10203 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10204 | } |
| 10205 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10206 | |
| 10207 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10208 | void ecjpake_size_macros() |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10209 | { |
| 10210 | const psa_algorithm_t alg = PSA_ALG_JPAKE; |
| 10211 | const size_t bits = 256; |
| 10212 | const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10213 | 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] | 10214 | 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] | 10215 | PSA_ECC_FAMILY_SECP_R1); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10216 | |
| 10217 | // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types |
| 10218 | /* 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] | 10219 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10220 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
| 10221 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10222 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10223 | /* The output for ZK_PROOF is the same bitsize as the curve */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10224 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10225 | PSA_BITS_TO_BYTES(bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10226 | |
| 10227 | /* Input sizes are the same as output sizes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10228 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10229 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE)); |
| 10230 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10231 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC)); |
| 10232 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10233 | 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] | 10234 | |
| 10235 | /* These inequalities will always hold even when other PAKEs are added */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10236 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10237 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10238 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10239 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10240 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10241 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10242 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10243 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10244 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10245 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10246 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10247 | PSA_PAKE_INPUT_MAX_SIZE); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10248 | } |
| 10249 | /* END_CASE */ |