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 | |
Kusumit Ghoderao | a14ae5a | 2023-04-19 14:16:26 +0530 | [diff] [blame^] | 19 | #include "../library/alignment.h" |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 20 | #include "test/asn1_helpers.h" |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 21 | #include "test/psa_crypto_helpers.h" |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 22 | #include "test/psa_exercise_key.h" |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 23 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 24 | #include "test/drivers/test_driver.h" |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 25 | #define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION |
| 26 | #else |
| 27 | #define TEST_DRIVER_LOCATION 0x7fffff |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 28 | #endif |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 29 | |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 30 | /* If this comes up, it's a bug in the test code or in the test data. */ |
| 31 | #define UNUSED 0xdeadbeef |
| 32 | |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 33 | /* Assert that an operation is (not) active. |
| 34 | * This serves as a proxy for checking if the operation is aborted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 35 | #define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0) |
| 36 | #define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 37 | |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 38 | #if defined(PSA_WANT_ALG_JPAKE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 39 | int ecjpake_operation_setup(psa_pake_operation_t *operation, |
| 40 | psa_pake_cipher_suite_t *cipher_suite, |
| 41 | psa_pake_role_t role, |
| 42 | mbedtls_svc_key_id_t key, |
| 43 | size_t key_available) |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 44 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 45 | PSA_ASSERT(psa_pake_abort(operation)); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 46 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 47 | PSA_ASSERT(psa_pake_setup(operation, cipher_suite)); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 48 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 49 | PSA_ASSERT(psa_pake_set_role(operation, role)); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 50 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 51 | if (key_available) { |
| 52 | PSA_ASSERT(psa_pake_set_password_key(operation, key)); |
| 53 | } |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 54 | return 0; |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 55 | exit: |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 56 | return 1; |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 57 | } |
| 58 | #endif |
| 59 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 60 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 61 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 62 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 63 | /** Test if a buffer contains a constant byte value. |
| 64 | * |
| 65 | * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 66 | * |
| 67 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 68 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 69 | * \param size Size of the buffer in bytes. |
| 70 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 71 | * \return 1 if the buffer is all-bits-zero. |
| 72 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 73 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 74 | static int mem_is_char(void *buffer, unsigned char c, size_t size) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 75 | { |
| 76 | size_t i; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 77 | for (i = 0; i < size; i++) { |
| 78 | if (((unsigned char *) buffer)[i] != c) { |
| 79 | return 0; |
| 80 | } |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 81 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 82 | return 1; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 83 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 84 | #if defined(MBEDTLS_ASN1_WRITE_C) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 85 | /* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 86 | static int asn1_write_10x(unsigned char **p, |
| 87 | unsigned char *start, |
| 88 | size_t bits, |
| 89 | unsigned char x) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 90 | { |
| 91 | int ret; |
| 92 | int len = bits / 8 + 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 93 | if (bits == 0) { |
| 94 | return MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 95 | } |
| 96 | if (bits <= 8 && x >= 1 << (bits - 1)) { |
| 97 | return MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 98 | } |
| 99 | if (*p < start || *p - start < (ptrdiff_t) len) { |
| 100 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 101 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 102 | *p -= len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 103 | (*p)[len-1] = x; |
| 104 | if (bits % 8 == 0) { |
| 105 | (*p)[1] |= 1; |
| 106 | } else { |
| 107 | (*p)[0] |= 1 << (bits % 8); |
| 108 | } |
| 109 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 110 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, |
| 111 | MBEDTLS_ASN1_INTEGER)); |
| 112 | return len; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 113 | } |
| 114 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 115 | static int construct_fake_rsa_key(unsigned char *buffer, |
| 116 | size_t buffer_size, |
| 117 | unsigned char **p, |
| 118 | size_t bits, |
| 119 | int keypair) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 120 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 121 | size_t half_bits = (bits + 1) / 2; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 122 | int ret; |
| 123 | int len = 0; |
| 124 | /* Construct something that looks like a DER encoding of |
| 125 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 126 | * RSAPrivateKey ::= SEQUENCE { |
| 127 | * version Version, |
| 128 | * modulus INTEGER, -- n |
| 129 | * publicExponent INTEGER, -- e |
| 130 | * privateExponent INTEGER, -- d |
| 131 | * prime1 INTEGER, -- p |
| 132 | * prime2 INTEGER, -- q |
| 133 | * exponent1 INTEGER, -- d mod (p-1) |
| 134 | * exponent2 INTEGER, -- d mod (q-1) |
| 135 | * coefficient INTEGER, -- (inverse of q) mod p |
| 136 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 137 | * } |
| 138 | * Or, for a public key, the same structure with only |
| 139 | * version, modulus and publicExponent. |
| 140 | */ |
| 141 | *p = buffer + buffer_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 142 | if (keypair) { |
| 143 | MBEDTLS_ASN1_CHK_ADD(len, /* pq */ |
| 144 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 145 | MBEDTLS_ASN1_CHK_ADD(len, /* dq */ |
| 146 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 147 | MBEDTLS_ASN1_CHK_ADD(len, /* dp */ |
| 148 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 149 | MBEDTLS_ASN1_CHK_ADD(len, /* q */ |
| 150 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 151 | MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */ |
| 152 | asn1_write_10x(p, buffer, half_bits, 3)); |
| 153 | MBEDTLS_ASN1_CHK_ADD(len, /* d */ |
| 154 | asn1_write_10x(p, buffer, bits, 1)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 155 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 156 | MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */ |
| 157 | asn1_write_10x(p, buffer, 17, 1)); |
| 158 | MBEDTLS_ASN1_CHK_ADD(len, /* n */ |
| 159 | asn1_write_10x(p, buffer, bits, 1)); |
| 160 | if (keypair) { |
| 161 | MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */ |
| 162 | mbedtls_asn1_write_int(p, buffer, 0)); |
| 163 | } |
| 164 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 165 | { |
| 166 | const unsigned char tag = |
| 167 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 168 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 169 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 170 | return len; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 171 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 172 | #endif /* MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 173 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 174 | int exercise_mac_setup(psa_key_type_t key_type, |
| 175 | const unsigned char *key_bytes, |
| 176 | size_t key_length, |
| 177 | psa_algorithm_t alg, |
| 178 | psa_mac_operation_t *operation, |
| 179 | psa_status_t *status) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 180 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 181 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 182 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 183 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 184 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 185 | psa_set_key_algorithm(&attributes, alg); |
| 186 | psa_set_key_type(&attributes, key_type); |
| 187 | PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 188 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 189 | *status = psa_mac_sign_setup(operation, key, alg); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 190 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 191 | PSA_ASSERT(psa_mac_abort(operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 192 | /* If setup failed, reproduce the failure, so that the caller can |
| 193 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 194 | if (*status != PSA_SUCCESS) { |
| 195 | TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 196 | } |
| 197 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 198 | psa_destroy_key(key); |
| 199 | return 1; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 200 | |
| 201 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 202 | psa_destroy_key(key); |
| 203 | return 0; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 204 | } |
| 205 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 206 | int exercise_cipher_setup(psa_key_type_t key_type, |
| 207 | const unsigned char *key_bytes, |
| 208 | size_t key_length, |
| 209 | psa_algorithm_t alg, |
| 210 | psa_cipher_operation_t *operation, |
| 211 | psa_status_t *status) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 212 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 213 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 214 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 215 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 216 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 217 | psa_set_key_algorithm(&attributes, alg); |
| 218 | psa_set_key_type(&attributes, key_type); |
| 219 | PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 221 | *status = psa_cipher_encrypt_setup(operation, key, alg); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 222 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 223 | PSA_ASSERT(psa_cipher_abort(operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 224 | /* If setup failed, reproduce the failure, so that the caller can |
| 225 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 226 | if (*status != PSA_SUCCESS) { |
| 227 | TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg), |
| 228 | *status); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 229 | } |
| 230 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 231 | psa_destroy_key(key); |
| 232 | return 1; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 233 | |
| 234 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 235 | psa_destroy_key(key); |
| 236 | return 0; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 237 | } |
| 238 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 239 | static int test_operations_on_invalid_key(mbedtls_svc_key_id_t key) |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 240 | { |
| 241 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 242 | mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 0x6964); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 243 | uint8_t buffer[1]; |
| 244 | size_t length; |
| 245 | int ok = 0; |
| 246 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 247 | psa_set_key_id(&attributes, key_id); |
| 248 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 249 | psa_set_key_algorithm(&attributes, PSA_ALG_CTR); |
| 250 | psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); |
| 251 | TEST_EQUAL(psa_get_key_attributes(key, &attributes), |
| 252 | PSA_ERROR_INVALID_HANDLE); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 253 | TEST_EQUAL( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 254 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID(psa_get_key_id(&attributes)), 0); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 255 | TEST_EQUAL( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 256 | MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0); |
| 257 | TEST_EQUAL(psa_get_key_lifetime(&attributes), 0); |
| 258 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0); |
| 259 | TEST_EQUAL(psa_get_key_algorithm(&attributes), 0); |
| 260 | TEST_EQUAL(psa_get_key_type(&attributes), 0); |
| 261 | TEST_EQUAL(psa_get_key_bits(&attributes), 0); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 263 | TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length), |
| 264 | PSA_ERROR_INVALID_HANDLE); |
| 265 | TEST_EQUAL(psa_export_public_key(key, |
| 266 | buffer, sizeof(buffer), &length), |
| 267 | PSA_ERROR_INVALID_HANDLE); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 268 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 269 | ok = 1; |
| 270 | |
| 271 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 272 | /* |
| 273 | * Key attributes may have been returned by psa_get_key_attributes() |
| 274 | * thus reset them as required. |
| 275 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 276 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 277 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 278 | return ok; |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 279 | } |
| 280 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 281 | /* Assert that a key isn't reported as having a slot number. */ |
| 282 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 283 | #define ASSERT_NO_SLOT_NUMBER(attributes) \ |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 284 | do \ |
| 285 | { \ |
| 286 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 287 | TEST_EQUAL(psa_get_key_slot_number( \ |
| 288 | attributes, \ |
| 289 | &ASSERT_NO_SLOT_NUMBER_slot_number), \ |
| 290 | PSA_ERROR_INVALID_ARGUMENT); \ |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 291 | } \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 292 | while (0) |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 293 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 294 | #define ASSERT_NO_SLOT_NUMBER(attributes) \ |
| 295 | ((void) 0) |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 296 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 297 | |
Kusumit Ghoderao | a14ae5a | 2023-04-19 14:16:26 +0530 | [diff] [blame^] | 298 | uint64_t parse_hex_string(data_t *hex_string) |
| 299 | { |
| 300 | uint64_t result = 0; |
| 301 | for (size_t i = 0; i < hex_string->len; i++) { |
| 302 | if (MBEDTLS_IS_BIG_ENDIAN) { |
| 303 | result |= ((hex_string->x)[i]) << (i * 8); |
| 304 | } else { |
| 305 | result |= ((hex_string->x)[i]) << ((hex_string->len - i - 1) * 8); |
| 306 | } |
| 307 | } |
| 308 | return result; |
| 309 | } |
| 310 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 311 | /* An overapproximation of the amount of storage needed for a key of the |
| 312 | * given type and with the given content. The API doesn't make it easy |
| 313 | * to find a good value for the size. The current implementation doesn't |
| 314 | * care about the value anyway. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 315 | #define KEY_BITS_FROM_DATA(type, data) \ |
| 316 | (data)->len |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 317 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 318 | typedef enum { |
| 319 | IMPORT_KEY = 0, |
| 320 | GENERATE_KEY = 1, |
| 321 | DERIVE_KEY = 2 |
| 322 | } generate_method; |
| 323 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 324 | typedef enum { |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 325 | DO_NOT_SET_LENGTHS = 0, |
| 326 | SET_LENGTHS_BEFORE_NONCE = 1, |
| 327 | SET_LENGTHS_AFTER_NONCE = 2 |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 328 | } set_lengths_method_t; |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 329 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 330 | typedef enum { |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 331 | USE_NULL_TAG = 0, |
| 332 | USE_GIVEN_TAG = 1, |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 333 | } tag_usage_method_t; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 334 | |
Kusumit Ghoderao | a14ae5a | 2023-04-19 14:16:26 +0530 | [diff] [blame^] | 335 | typedef enum { |
| 336 | INPUT_BYTES = 0, |
| 337 | INPUT_INTEGER = 1 |
| 338 | } key_derivation_input_method_t; |
| 339 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 340 | /*! |
| 341 | * \brief Internal Function for AEAD multipart tests. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 342 | * \param key_type_arg Type of key passed in |
| 343 | * \param key_data The encryption / decryption key data |
| 344 | * \param alg_arg The type of algorithm used |
| 345 | * \param nonce Nonce data |
| 346 | * \param additional_data Additional data |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 347 | * \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] | 348 | * feed additional data in to be encrypted / |
| 349 | * decrypted. If -1, no chunking. |
| 350 | * \param input_data Data to encrypt / decrypt |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 351 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 352 | * the data in to be encrypted / decrypted. If |
| 353 | * -1, no chunking |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 354 | * \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] | 355 | * expected here, this controls whether or not |
| 356 | * to set lengths, and in what order with |
| 357 | * respect to set nonce. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 358 | * \param expected_output Expected output |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 359 | * \param is_encrypt If non-zero this is an encryption operation. |
Paul Elliott | 41ffae1 | 2021-07-22 21:52:01 +0100 | [diff] [blame] | 360 | * \param do_zero_parts If non-zero, interleave zero length chunks |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 361 | * with normal length chunks. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 362 | * \return int Zero on failure, non-zero on success. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 363 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 364 | static int aead_multipart_internal_func(int key_type_arg, data_t *key_data, |
| 365 | int alg_arg, |
| 366 | data_t *nonce, |
| 367 | data_t *additional_data, |
| 368 | int ad_part_len_arg, |
| 369 | data_t *input_data, |
| 370 | int data_part_len_arg, |
| 371 | set_lengths_method_t set_lengths_method, |
| 372 | data_t *expected_output, |
| 373 | int is_encrypt, |
| 374 | int do_zero_parts) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 375 | { |
| 376 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 377 | psa_key_type_t key_type = key_type_arg; |
| 378 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 379 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 380 | unsigned char *output_data = NULL; |
| 381 | unsigned char *part_data = NULL; |
| 382 | unsigned char *final_data = NULL; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 383 | size_t data_true_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 384 | size_t part_data_size = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 385 | size_t output_size = 0; |
| 386 | size_t final_output_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 387 | size_t output_length = 0; |
| 388 | size_t key_bits = 0; |
| 389 | size_t tag_length = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 390 | size_t part_offset = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 391 | size_t part_length = 0; |
| 392 | size_t output_part_length = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 393 | size_t tag_size = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 394 | size_t ad_part_len = 0; |
| 395 | size_t data_part_len = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 396 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 397 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 398 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 399 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 400 | int test_ok = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 401 | size_t part_count = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 402 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 403 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 404 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 405 | if (is_encrypt) { |
| 406 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 407 | } else { |
| 408 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 409 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 410 | |
| 411 | psa_set_key_algorithm(&attributes, alg); |
| 412 | psa_set_key_type(&attributes, key_type); |
| 413 | |
| 414 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 415 | &key)); |
| 416 | |
| 417 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 418 | key_bits = psa_get_key_bits(&attributes); |
| 419 | |
| 420 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); |
| 421 | |
| 422 | if (is_encrypt) { |
| 423 | /* Tag gets written at end of buffer. */ |
| 424 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 425 | (input_data->len + |
| 426 | tag_length)); |
| 427 | data_true_size = input_data->len; |
| 428 | } else { |
| 429 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 430 | (input_data->len - |
| 431 | tag_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 432 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 433 | /* Do not want to attempt to decrypt tag. */ |
| 434 | data_true_size = input_data->len - tag_length; |
| 435 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 436 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 437 | ASSERT_ALLOC(output_data, output_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 438 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 439 | if (is_encrypt) { |
| 440 | final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
| 441 | TEST_LE_U(final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
| 442 | } else { |
| 443 | final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); |
| 444 | TEST_LE_U(final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 445 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 446 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 447 | ASSERT_ALLOC(final_data, final_output_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 449 | if (is_encrypt) { |
| 450 | status = psa_aead_encrypt_setup(&operation, key, alg); |
| 451 | } else { |
| 452 | status = psa_aead_decrypt_setup(&operation, key, alg); |
| 453 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 454 | |
| 455 | /* If the operation is not supported, just skip and not fail in case the |
| 456 | * encryption involves a common limitation of cryptography hardwares and |
| 457 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 458 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 459 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 460 | 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] | 461 | } |
| 462 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 463 | PSA_ASSERT(status); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 464 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 465 | if (set_lengths_method == DO_NOT_SET_LENGTHS) { |
| 466 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
| 467 | } else if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { |
| 468 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 469 | data_true_size)); |
| 470 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
| 471 | } else if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) { |
| 472 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 473 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 474 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 475 | data_true_size)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 476 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 477 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 478 | if (ad_part_len_arg != -1) { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 479 | /* Pass additional data in parts */ |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 480 | ad_part_len = (size_t) ad_part_len_arg; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 481 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 482 | for (part_offset = 0, part_count = 0; |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 483 | part_offset < additional_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 484 | part_offset += part_length, part_count++) { |
| 485 | if (do_zero_parts && (part_count & 0x01)) { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 486 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 487 | } else if (additional_data->len - part_offset < ad_part_len) { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 488 | part_length = additional_data->len - part_offset; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 489 | } else { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 490 | part_length = ad_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 491 | } |
| 492 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 493 | PSA_ASSERT(psa_aead_update_ad(&operation, |
| 494 | additional_data->x + part_offset, |
| 495 | part_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 496 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 497 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 498 | } else { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 499 | /* Pass additional data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 500 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 501 | additional_data->len)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 502 | } |
| 503 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 504 | if (data_part_len_arg != -1) { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 505 | /* Pass data in parts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 506 | data_part_len = (size_t) data_part_len_arg; |
| 507 | part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 508 | (size_t) data_part_len); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 509 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 510 | ASSERT_ALLOC(part_data, part_data_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 511 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 512 | for (part_offset = 0, part_count = 0; |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 513 | part_offset < data_true_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 514 | part_offset += part_length, part_count++) { |
| 515 | if (do_zero_parts && (part_count & 0x01)) { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 516 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 517 | } else if ((data_true_size - part_offset) < data_part_len) { |
| 518 | part_length = (data_true_size - part_offset); |
| 519 | } else { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 520 | part_length = data_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 521 | } |
| 522 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 523 | PSA_ASSERT(psa_aead_update(&operation, |
| 524 | (input_data->x + part_offset), |
| 525 | part_length, part_data, |
| 526 | part_data_size, |
| 527 | &output_part_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 528 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 529 | if (output_data && output_part_length) { |
| 530 | memcpy((output_data + output_length), part_data, |
| 531 | output_part_length); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 532 | } |
| 533 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 534 | output_length += output_part_length; |
| 535 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 536 | } else { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 537 | /* Pass all data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 538 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 539 | data_true_size, output_data, |
| 540 | output_size, &output_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 541 | } |
| 542 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 543 | if (is_encrypt) { |
| 544 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 545 | final_output_size, |
| 546 | &output_part_length, |
| 547 | tag_buffer, tag_length, |
| 548 | &tag_size)); |
| 549 | } else { |
| 550 | PSA_ASSERT(psa_aead_verify(&operation, final_data, |
| 551 | final_output_size, |
| 552 | &output_part_length, |
| 553 | (input_data->x + data_true_size), |
| 554 | tag_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 555 | } |
| 556 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 557 | if (output_data && output_part_length) { |
| 558 | memcpy((output_data + output_length), final_data, |
| 559 | output_part_length); |
| 560 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 561 | |
| 562 | output_length += output_part_length; |
| 563 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 564 | |
| 565 | /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE |
| 566 | * should be exact.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 567 | if (is_encrypt) { |
| 568 | TEST_EQUAL(tag_length, tag_size); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 569 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 570 | if (output_data && tag_length) { |
| 571 | memcpy((output_data + output_length), tag_buffer, |
| 572 | tag_length); |
| 573 | } |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 574 | |
| 575 | output_length += tag_length; |
| 576 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 577 | TEST_EQUAL(output_length, |
| 578 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 579 | input_data->len)); |
| 580 | TEST_LE_U(output_length, |
| 581 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
| 582 | } else { |
| 583 | TEST_EQUAL(output_length, |
| 584 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, |
| 585 | input_data->len)); |
| 586 | TEST_LE_U(output_length, |
| 587 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 588 | } |
| 589 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 590 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 591 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 592 | output_data, output_length); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 593 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 594 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 595 | test_ok = 1; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 596 | |
| 597 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 598 | psa_destroy_key(key); |
| 599 | psa_aead_abort(&operation); |
| 600 | mbedtls_free(output_data); |
| 601 | mbedtls_free(part_data); |
| 602 | mbedtls_free(final_data); |
| 603 | PSA_DONE(); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 604 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 605 | return test_ok; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 606 | } |
| 607 | |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 608 | /*! |
| 609 | * \brief Internal Function for MAC multipart tests. |
| 610 | * \param key_type_arg Type of key passed in |
| 611 | * \param key_data The encryption / decryption key data |
| 612 | * \param alg_arg The type of algorithm used |
| 613 | * \param input_data Data to encrypt / decrypt |
| 614 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 615 | * the data in to be encrypted / decrypted. If |
| 616 | * -1, no chunking |
| 617 | * \param expected_output Expected output |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 618 | * \param is_verify If non-zero this is a verify operation. |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 619 | * \param do_zero_parts If non-zero, interleave zero length chunks |
| 620 | * with normal length chunks. |
| 621 | * \return int Zero on failure, non-zero on success. |
| 622 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 623 | static int mac_multipart_internal_func(int key_type_arg, data_t *key_data, |
| 624 | int alg_arg, |
| 625 | data_t *input_data, |
| 626 | int data_part_len_arg, |
| 627 | data_t *expected_output, |
| 628 | int is_verify, |
| 629 | int do_zero_parts) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 630 | { |
| 631 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 632 | psa_key_type_t key_type = key_type_arg; |
| 633 | psa_algorithm_t alg = alg_arg; |
| 634 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 635 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
| 636 | size_t part_offset = 0; |
| 637 | size_t part_length = 0; |
| 638 | size_t data_part_len = 0; |
| 639 | size_t mac_len = 0; |
| 640 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 641 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 642 | |
| 643 | int test_ok = 0; |
| 644 | size_t part_count = 0; |
| 645 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 646 | PSA_INIT(); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 647 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 648 | if (is_verify) { |
| 649 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 650 | } else { |
| 651 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 652 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 653 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 654 | psa_set_key_algorithm(&attributes, alg); |
| 655 | psa_set_key_type(&attributes, key_type); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 656 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 657 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 658 | &key)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 659 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 660 | if (is_verify) { |
| 661 | status = psa_mac_verify_setup(&operation, key, alg); |
| 662 | } else { |
| 663 | status = psa_mac_sign_setup(&operation, key, alg); |
| 664 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 665 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 666 | PSA_ASSERT(status); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 667 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 668 | if (data_part_len_arg != -1) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 669 | /* Pass data in parts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 670 | data_part_len = (size_t) data_part_len_arg; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 671 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 672 | for (part_offset = 0, part_count = 0; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 673 | part_offset < input_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 674 | part_offset += part_length, part_count++) { |
| 675 | if (do_zero_parts && (part_count & 0x01)) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 676 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 677 | } else if ((input_data->len - part_offset) < data_part_len) { |
| 678 | part_length = (input_data->len - part_offset); |
| 679 | } else { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 680 | part_length = data_part_len; |
| 681 | } |
| 682 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 683 | PSA_ASSERT(psa_mac_update(&operation, |
| 684 | (input_data->x + part_offset), |
| 685 | part_length)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 686 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 687 | } else { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 688 | /* Pass all data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 689 | PSA_ASSERT(psa_mac_update(&operation, input_data->x, |
| 690 | input_data->len)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 691 | } |
| 692 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 693 | if (is_verify) { |
| 694 | PSA_ASSERT(psa_mac_verify_finish(&operation, expected_output->x, |
| 695 | expected_output->len)); |
| 696 | } else { |
| 697 | PSA_ASSERT(psa_mac_sign_finish(&operation, mac, |
| 698 | PSA_MAC_MAX_SIZE, &mac_len)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 699 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 700 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 701 | mac, mac_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | test_ok = 1; |
| 705 | |
| 706 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 707 | psa_destroy_key(key); |
| 708 | psa_mac_abort(&operation); |
| 709 | PSA_DONE(); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 710 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 711 | return test_ok; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 712 | } |
| 713 | |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 714 | #if defined(PSA_WANT_ALG_JPAKE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 715 | static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive, |
| 716 | psa_pake_operation_t *server, |
| 717 | psa_pake_operation_t *client, |
| 718 | int client_input_first, |
| 719 | int round, int inject_error) |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 720 | { |
| 721 | unsigned char *buffer0 = NULL, *buffer1 = NULL; |
| 722 | size_t buffer_length = ( |
| 723 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) + |
| 724 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) + |
| 725 | 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] | 726 | /* The output should be exactly this size according to the spec */ |
| 727 | const size_t expected_size_key_share = |
| 728 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE); |
| 729 | /* The output should be exactly this size according to the spec */ |
| 730 | const size_t expected_size_zk_public = |
| 731 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC); |
| 732 | /* The output can be smaller: the spec allows stripping leading zeroes */ |
| 733 | const size_t max_expected_size_zk_proof = |
| 734 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 735 | size_t buffer0_off = 0; |
| 736 | size_t buffer1_off = 0; |
| 737 | size_t s_g1_len, s_g2_len, s_a_len; |
| 738 | size_t s_g1_off, s_g2_off, s_a_off; |
| 739 | size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len; |
| 740 | size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off; |
| 741 | size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len; |
| 742 | size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off; |
| 743 | size_t c_g1_len, c_g2_len, c_a_len; |
| 744 | size_t c_g1_off, c_g2_off, c_a_off; |
| 745 | size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len; |
| 746 | size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off; |
| 747 | size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len; |
| 748 | size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off; |
| 749 | psa_status_t expected_status = PSA_SUCCESS; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 750 | psa_status_t status; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 751 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 752 | ASSERT_ALLOC(buffer0, buffer_length); |
| 753 | ASSERT_ALLOC(buffer1, buffer_length); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 754 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 755 | switch (round) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 756 | case 1: |
| 757 | /* Server first round Output */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 758 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 759 | buffer0 + buffer0_off, |
| 760 | 512 - buffer0_off, &s_g1_len)); |
| 761 | TEST_EQUAL(s_g1_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 762 | s_g1_off = buffer0_off; |
| 763 | buffer0_off += s_g1_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 764 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 765 | buffer0 + buffer0_off, |
| 766 | 512 - buffer0_off, &s_x1_pk_len)); |
| 767 | TEST_EQUAL(s_x1_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 768 | s_x1_pk_off = buffer0_off; |
| 769 | buffer0_off += s_x1_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 770 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 771 | buffer0 + buffer0_off, |
| 772 | 512 - buffer0_off, &s_x1_pr_len)); |
| 773 | TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 774 | s_x1_pr_off = buffer0_off; |
| 775 | buffer0_off += s_x1_pr_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 776 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 777 | buffer0 + buffer0_off, |
| 778 | 512 - buffer0_off, &s_g2_len)); |
| 779 | TEST_EQUAL(s_g2_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 780 | s_g2_off = buffer0_off; |
| 781 | buffer0_off += s_g2_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 782 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 783 | buffer0 + buffer0_off, |
| 784 | 512 - buffer0_off, &s_x2_pk_len)); |
| 785 | TEST_EQUAL(s_x2_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 786 | s_x2_pk_off = buffer0_off; |
| 787 | buffer0_off += s_x2_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 788 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 789 | buffer0 + buffer0_off, |
| 790 | 512 - buffer0_off, &s_x2_pr_len)); |
| 791 | TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 792 | s_x2_pr_off = buffer0_off; |
| 793 | buffer0_off += s_x2_pr_len; |
| 794 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 795 | if (inject_error == 1) { |
Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 796 | buffer0[s_x1_pr_off + 8] ^= 1; |
| 797 | buffer0[s_x2_pr_off + 7] ^= 1; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 798 | expected_status = PSA_ERROR_DATA_INVALID; |
| 799 | } |
| 800 | |
Neil Armstrong | 51009d7 | 2022-09-05 17:59:54 +0200 | [diff] [blame] | 801 | /* |
| 802 | * When injecting errors in inputs, the implementation is |
| 803 | * free to detect it right away of with a delay. |
| 804 | * This permits delaying the error until the end of the input |
| 805 | * sequence, if no error appears then, this will be treated |
| 806 | * as an error. |
| 807 | */ |
| 808 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 809 | if (client_input_first == 1) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 810 | /* Client first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 811 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 812 | buffer0 + s_g1_off, s_g1_len); |
| 813 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 814 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 815 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 816 | } else { |
| 817 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 818 | } |
| 819 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 820 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 821 | buffer0 + s_x1_pk_off, |
| 822 | s_x1_pk_len); |
| 823 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 824 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 825 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 826 | } else { |
| 827 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 828 | } |
| 829 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 830 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 831 | buffer0 + s_x1_pr_off, |
| 832 | s_x1_pr_len); |
| 833 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 834 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 835 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 836 | } else { |
| 837 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 838 | } |
| 839 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 840 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 841 | buffer0 + s_g2_off, |
| 842 | s_g2_len); |
| 843 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 844 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 845 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 846 | } else { |
| 847 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 848 | } |
| 849 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 850 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 851 | buffer0 + s_x2_pk_off, |
| 852 | s_x2_pk_len); |
| 853 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 854 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 855 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 856 | } else { |
| 857 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 858 | } |
| 859 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 860 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 861 | buffer0 + s_x2_pr_off, |
| 862 | s_x2_pr_len); |
| 863 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 864 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 865 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 866 | } else { |
| 867 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 868 | } |
| 869 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 870 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 871 | if (inject_error == 1) { |
| 872 | TEST_ASSERT( |
| 873 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 874 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | /* Client first round Output */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 878 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 879 | buffer1 + buffer1_off, |
| 880 | 512 - buffer1_off, &c_g1_len)); |
| 881 | TEST_EQUAL(c_g1_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 882 | c_g1_off = buffer1_off; |
| 883 | buffer1_off += c_g1_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 884 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 885 | buffer1 + buffer1_off, |
| 886 | 512 - buffer1_off, &c_x1_pk_len)); |
| 887 | TEST_EQUAL(c_x1_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 888 | c_x1_pk_off = buffer1_off; |
| 889 | buffer1_off += c_x1_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 890 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 891 | buffer1 + buffer1_off, |
| 892 | 512 - buffer1_off, &c_x1_pr_len)); |
| 893 | TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 894 | c_x1_pr_off = buffer1_off; |
| 895 | buffer1_off += c_x1_pr_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 896 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 897 | buffer1 + buffer1_off, |
| 898 | 512 - buffer1_off, &c_g2_len)); |
| 899 | TEST_EQUAL(c_g2_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 900 | c_g2_off = buffer1_off; |
| 901 | buffer1_off += c_g2_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 902 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 903 | buffer1 + buffer1_off, |
| 904 | 512 - buffer1_off, &c_x2_pk_len)); |
| 905 | TEST_EQUAL(c_x2_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 906 | c_x2_pk_off = buffer1_off; |
| 907 | buffer1_off += c_x2_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 908 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 909 | buffer1 + buffer1_off, |
| 910 | 512 - buffer1_off, &c_x2_pr_len)); |
| 911 | TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 912 | c_x2_pr_off = buffer1_off; |
| 913 | buffer1_off += c_x2_pr_len; |
| 914 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 915 | if (client_input_first == 0) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 916 | /* Client first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 917 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 918 | buffer0 + s_g1_off, s_g1_len); |
| 919 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 920 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 921 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 922 | } else { |
| 923 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 924 | } |
| 925 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 926 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 927 | buffer0 + s_x1_pk_off, |
| 928 | s_x1_pk_len); |
| 929 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 930 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 931 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 932 | } else { |
| 933 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 934 | } |
| 935 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 936 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 937 | buffer0 + s_x1_pr_off, |
| 938 | s_x1_pr_len); |
| 939 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 940 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 941 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 942 | } else { |
| 943 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 944 | } |
| 945 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 946 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 947 | buffer0 + s_g2_off, |
| 948 | s_g2_len); |
| 949 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 950 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 951 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 952 | } else { |
| 953 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 954 | } |
| 955 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 956 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 957 | buffer0 + s_x2_pk_off, |
| 958 | s_x2_pk_len); |
| 959 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 960 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 961 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 962 | } else { |
| 963 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 964 | } |
| 965 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 966 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 967 | buffer0 + s_x2_pr_off, |
| 968 | s_x2_pr_len); |
| 969 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 970 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 971 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 972 | } else { |
| 973 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 974 | } |
| 975 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 976 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 977 | if (inject_error == 1) { |
| 978 | TEST_ASSERT( |
| 979 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 980 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 981 | } |
| 982 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 983 | if (inject_error == 2) { |
Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 984 | buffer1[c_x1_pr_off + 12] ^= 1; |
| 985 | buffer1[c_x2_pr_off + 7] ^= 1; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 986 | expected_status = PSA_ERROR_DATA_INVALID; |
| 987 | } |
| 988 | |
| 989 | /* Server first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 990 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 991 | buffer1 + c_g1_off, c_g1_len); |
| 992 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 993 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 994 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 995 | } else { |
| 996 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 997 | } |
| 998 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 999 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1000 | buffer1 + c_x1_pk_off, c_x1_pk_len); |
| 1001 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1002 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1003 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1004 | } else { |
| 1005 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1006 | } |
| 1007 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1008 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1009 | buffer1 + c_x1_pr_off, c_x1_pr_len); |
| 1010 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1011 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1012 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1013 | } else { |
| 1014 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1015 | } |
| 1016 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1017 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 1018 | buffer1 + c_g2_off, c_g2_len); |
| 1019 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1020 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1021 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1022 | } else { |
| 1023 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1024 | } |
| 1025 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1026 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1027 | buffer1 + c_x2_pk_off, c_x2_pk_len); |
| 1028 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1029 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1030 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1031 | } else { |
| 1032 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1033 | } |
| 1034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1035 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1036 | buffer1 + c_x2_pr_off, c_x2_pr_len); |
| 1037 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1038 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1039 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1040 | } else { |
| 1041 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1042 | } |
| 1043 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1044 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1045 | if (inject_error == 2) { |
| 1046 | TEST_ASSERT( |
| 1047 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1048 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1049 | |
| 1050 | break; |
| 1051 | |
| 1052 | case 2: |
| 1053 | /* Server second round Output */ |
| 1054 | buffer0_off = 0; |
| 1055 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1056 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 1057 | buffer0 + buffer0_off, |
| 1058 | 512 - buffer0_off, &s_a_len)); |
| 1059 | TEST_EQUAL(s_a_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1060 | s_a_off = buffer0_off; |
| 1061 | buffer0_off += s_a_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1062 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1063 | buffer0 + buffer0_off, |
| 1064 | 512 - buffer0_off, &s_x2s_pk_len)); |
| 1065 | TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1066 | s_x2s_pk_off = buffer0_off; |
| 1067 | buffer0_off += s_x2s_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1068 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1069 | buffer0 + buffer0_off, |
| 1070 | 512 - buffer0_off, &s_x2s_pr_len)); |
| 1071 | TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1072 | s_x2s_pr_off = buffer0_off; |
| 1073 | buffer0_off += s_x2s_pr_len; |
| 1074 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1075 | if (inject_error == 3) { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1076 | buffer0[s_x2s_pk_off + 12] += 0x33; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1077 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1078 | } |
| 1079 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1080 | if (client_input_first == 1) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1081 | /* Client second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1082 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1083 | buffer0 + s_a_off, s_a_len); |
| 1084 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1085 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1086 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1087 | } else { |
| 1088 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1089 | } |
| 1090 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1091 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1092 | buffer0 + s_x2s_pk_off, |
| 1093 | s_x2s_pk_len); |
| 1094 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1095 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1096 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1097 | } else { |
| 1098 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1099 | } |
| 1100 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1101 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1102 | buffer0 + s_x2s_pr_off, |
| 1103 | s_x2s_pr_len); |
| 1104 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1105 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1106 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1107 | } else { |
| 1108 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1109 | } |
| 1110 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1111 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1112 | if (inject_error == 3) { |
| 1113 | TEST_ASSERT( |
| 1114 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1115 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | /* Client second round Output */ |
| 1119 | buffer1_off = 0; |
| 1120 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1121 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1122 | buffer1 + buffer1_off, |
| 1123 | 512 - buffer1_off, &c_a_len)); |
| 1124 | TEST_EQUAL(c_a_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1125 | c_a_off = buffer1_off; |
| 1126 | buffer1_off += c_a_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1127 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1128 | buffer1 + buffer1_off, |
| 1129 | 512 - buffer1_off, &c_x2s_pk_len)); |
| 1130 | TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1131 | c_x2s_pk_off = buffer1_off; |
| 1132 | buffer1_off += c_x2s_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1133 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1134 | buffer1 + buffer1_off, |
| 1135 | 512 - buffer1_off, &c_x2s_pr_len)); |
| 1136 | TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1137 | c_x2s_pr_off = buffer1_off; |
| 1138 | buffer1_off += c_x2s_pr_len; |
| 1139 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1140 | if (client_input_first == 0) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1141 | /* Client second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1142 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1143 | buffer0 + s_a_off, s_a_len); |
| 1144 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1145 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1146 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1147 | } else { |
| 1148 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1149 | } |
| 1150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1151 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1152 | buffer0 + s_x2s_pk_off, |
| 1153 | s_x2s_pk_len); |
| 1154 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1155 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1156 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1157 | } else { |
| 1158 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1159 | } |
| 1160 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1161 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1162 | buffer0 + s_x2s_pr_off, |
| 1163 | s_x2s_pr_len); |
| 1164 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1165 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1166 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1167 | } else { |
| 1168 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1169 | } |
| 1170 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1171 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1172 | if (inject_error == 3) { |
| 1173 | TEST_ASSERT( |
| 1174 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1175 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1176 | } |
| 1177 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1178 | if (inject_error == 4) { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1179 | buffer1[c_x2s_pk_off + 7] += 0x28; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1180 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1181 | } |
| 1182 | |
| 1183 | /* Server second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1184 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 1185 | buffer1 + c_a_off, c_a_len); |
| 1186 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1187 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1188 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1189 | } else { |
| 1190 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1191 | } |
| 1192 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1193 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1194 | buffer1 + c_x2s_pk_off, c_x2s_pk_len); |
| 1195 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1196 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1197 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1198 | } else { |
| 1199 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1200 | } |
| 1201 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1202 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1203 | buffer1 + c_x2s_pr_off, c_x2s_pr_len); |
| 1204 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1205 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1206 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1207 | } else { |
| 1208 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1209 | } |
| 1210 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1211 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1212 | if (inject_error == 4) { |
| 1213 | TEST_ASSERT( |
| 1214 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1215 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1216 | |
| 1217 | break; |
| 1218 | |
| 1219 | } |
| 1220 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1221 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1222 | mbedtls_free(buffer0); |
| 1223 | mbedtls_free(buffer1); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1224 | } |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 1225 | #endif /* PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1226 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1227 | typedef enum { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 1228 | INJECT_ERR_NONE = 0, |
| 1229 | INJECT_ERR_UNINITIALIZED_ACCESS, |
| 1230 | INJECT_ERR_DUPLICATE_SETUP, |
| 1231 | INJECT_ERR_INVALID_USER, |
| 1232 | INJECT_ERR_INVALID_PEER, |
| 1233 | INJECT_ERR_SET_USER, |
| 1234 | INJECT_ERR_SET_PEER, |
| 1235 | INJECT_EMPTY_IO_BUFFER, |
| 1236 | INJECT_UNKNOWN_STEP, |
| 1237 | INJECT_INVALID_FIRST_STEP, |
| 1238 | INJECT_WRONG_BUFFER_SIZE, |
| 1239 | INJECT_VALID_OPERATION_AFTER_FAILURE, |
| 1240 | INJECT_ANTICIPATE_KEY_DERIVATION_1, |
| 1241 | INJECT_ANTICIPATE_KEY_DERIVATION_2, |
| 1242 | } ecjpake_injected_failure_t; |
| 1243 | |
Paul Elliott | 01885fa | 2023-02-09 12:07:30 +0000 | [diff] [blame] | 1244 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Paul Elliott | 1243f93 | 2023-02-07 11:21:10 +0000 | [diff] [blame] | 1245 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1246 | static void interruptible_signverify_get_minmax_completes(uint32_t max_ops, |
| 1247 | psa_status_t expected_status, |
| 1248 | size_t *min_completes, |
| 1249 | size_t *max_completes) |
| 1250 | { |
| 1251 | |
| 1252 | /* This is slightly contrived, but we only really know that with a minimum |
| 1253 | value of max_ops that a successful operation should take more than one op |
| 1254 | to complete, and likewise that with a max_ops of |
| 1255 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */ |
| 1256 | if (max_ops == 0 || max_ops == 1) { |
Paul Elliott | c86d45e | 2023-02-15 17:38:05 +0000 | [diff] [blame] | 1257 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1258 | if (expected_status == PSA_SUCCESS) { |
| 1259 | *min_completes = 2; |
| 1260 | } else { |
| 1261 | *min_completes = 1; |
| 1262 | } |
| 1263 | |
| 1264 | *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED; |
| 1265 | } else { |
| 1266 | *min_completes = 1; |
| 1267 | *max_completes = 1; |
| 1268 | } |
| 1269 | } |
Paul Elliott | 01885fa | 2023-02-09 12:07:30 +0000 | [diff] [blame] | 1270 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1271 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1272 | /* END_HEADER */ |
| 1273 | |
| 1274 | /* BEGIN_DEPENDENCIES |
| 1275 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1276 | * END_DEPENDENCIES |
| 1277 | */ |
| 1278 | |
| 1279 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 7abdf7e | 2023-03-09 11:17:43 +0100 | [diff] [blame] | 1280 | void psa_can_do_hash() |
| 1281 | { |
| 1282 | /* We can't test that this is specific to drivers until partial init has |
| 1283 | * been implemented, but we can at least test before/after full init. */ |
| 1284 | TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE)); |
| 1285 | PSA_INIT(); |
| 1286 | TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE)); |
| 1287 | PSA_DONE(); |
| 1288 | } |
| 1289 | /* END_CASE */ |
| 1290 | |
| 1291 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1292 | void static_checks() |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1293 | { |
| 1294 | size_t max_truncated_mac_size = |
| 1295 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1296 | |
| 1297 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1298 | * encoding. The shifted mask is the maximum truncated value. The |
| 1299 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1300 | TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size); |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1301 | } |
| 1302 | /* END_CASE */ |
| 1303 | |
| 1304 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1305 | void import_with_policy(int type_arg, |
| 1306 | int usage_arg, int alg_arg, |
| 1307 | int expected_status_arg) |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1308 | { |
| 1309 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1310 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1311 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1312 | psa_key_type_t type = type_arg; |
| 1313 | psa_key_usage_t usage = usage_arg; |
| 1314 | psa_algorithm_t alg = alg_arg; |
| 1315 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1316 | const uint8_t key_material[16] = { 0 }; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1317 | psa_status_t status; |
| 1318 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1319 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1320 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1321 | psa_set_key_type(&attributes, type); |
| 1322 | psa_set_key_usage_flags(&attributes, usage); |
| 1323 | psa_set_key_algorithm(&attributes, alg); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1324 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1325 | status = psa_import_key(&attributes, |
| 1326 | key_material, sizeof(key_material), |
| 1327 | &key); |
| 1328 | TEST_EQUAL(status, expected_status); |
| 1329 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1330 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1331 | } |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1332 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1333 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1334 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1335 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), |
| 1336 | mbedtls_test_update_key_usage_flags(usage)); |
| 1337 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 1338 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1339 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1340 | PSA_ASSERT(psa_destroy_key(key)); |
| 1341 | test_operations_on_invalid_key(key); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1342 | |
| 1343 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1344 | /* |
| 1345 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1346 | * thus reset them as required. |
| 1347 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1348 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1349 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1350 | psa_destroy_key(key); |
| 1351 | PSA_DONE(); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1352 | } |
| 1353 | /* END_CASE */ |
| 1354 | |
| 1355 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1356 | void import_with_data(data_t *data, int type_arg, |
| 1357 | int attr_bits_arg, |
| 1358 | int expected_status_arg) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1359 | { |
| 1360 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1361 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1362 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1363 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1364 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1365 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1366 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1367 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1368 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1370 | psa_set_key_type(&attributes, type); |
| 1371 | psa_set_key_bits(&attributes, attr_bits); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1372 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1373 | status = psa_import_key(&attributes, data->x, data->len, &key); |
| 1374 | TEST_EQUAL(status, expected_status); |
| 1375 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1376 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1377 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1378 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1379 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1380 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1381 | if (attr_bits != 0) { |
| 1382 | TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes)); |
| 1383 | } |
| 1384 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1386 | PSA_ASSERT(psa_destroy_key(key)); |
| 1387 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1388 | |
| 1389 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1390 | /* |
| 1391 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1392 | * thus reset them as required. |
| 1393 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1394 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1395 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1396 | psa_destroy_key(key); |
| 1397 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1398 | } |
| 1399 | /* END_CASE */ |
| 1400 | |
| 1401 | /* BEGIN_CASE */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1402 | /* Construct and attempt to import a large unstructured key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1403 | void import_large_key(int type_arg, int byte_size_arg, |
| 1404 | int expected_status_arg) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1405 | { |
| 1406 | psa_key_type_t type = type_arg; |
| 1407 | size_t byte_size = byte_size_arg; |
| 1408 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1409 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1410 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1411 | psa_status_t status; |
| 1412 | uint8_t *buffer = NULL; |
| 1413 | size_t buffer_size = byte_size + 1; |
| 1414 | size_t n; |
| 1415 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1416 | /* Skip the test case if the target running the test cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1417 | * accommodate large keys due to heap size constraints */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1418 | ASSERT_ALLOC_WEAK(buffer, buffer_size); |
| 1419 | memset(buffer, 'K', byte_size); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1420 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1421 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1422 | |
| 1423 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1424 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1425 | psa_set_key_type(&attributes, type); |
| 1426 | status = psa_import_key(&attributes, buffer, byte_size, &key); |
| 1427 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 1428 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1429 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1430 | if (status == PSA_SUCCESS) { |
| 1431 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1432 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 1433 | TEST_EQUAL(psa_get_key_bits(&attributes), |
| 1434 | PSA_BYTES_TO_BITS(byte_size)); |
| 1435 | ASSERT_NO_SLOT_NUMBER(&attributes); |
| 1436 | memset(buffer, 0, byte_size + 1); |
| 1437 | PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n)); |
| 1438 | for (n = 0; n < byte_size; n++) { |
| 1439 | TEST_EQUAL(buffer[n], 'K'); |
| 1440 | } |
| 1441 | for (n = byte_size; n < buffer_size; n++) { |
| 1442 | TEST_EQUAL(buffer[n], 0); |
| 1443 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1447 | /* |
| 1448 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1449 | * thus reset them as required. |
| 1450 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1451 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1452 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1453 | psa_destroy_key(key); |
| 1454 | PSA_DONE(); |
| 1455 | mbedtls_free(buffer); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1456 | } |
| 1457 | /* END_CASE */ |
| 1458 | |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 1459 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1460 | /* Import an RSA key with a valid structure (but not valid numbers |
| 1461 | * inside, beyond having sensible size and parity). This is expected to |
| 1462 | * fail for large keys. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1463 | void import_rsa_made_up(int bits_arg, int keypair, int expected_status_arg) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1464 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1465 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1466 | size_t bits = bits_arg; |
| 1467 | psa_status_t expected_status = expected_status_arg; |
| 1468 | psa_status_t status; |
| 1469 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1470 | keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1471 | size_t buffer_size = /* Slight overapproximations */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1472 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1473 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1474 | unsigned char *p; |
| 1475 | int ret; |
| 1476 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1477 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1478 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1479 | PSA_ASSERT(psa_crypto_init()); |
| 1480 | ASSERT_ALLOC(buffer, buffer_size); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1481 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1482 | TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p, |
| 1483 | bits, keypair)) >= 0); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1484 | length = ret; |
| 1485 | |
| 1486 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1487 | psa_set_key_type(&attributes, type); |
| 1488 | status = psa_import_key(&attributes, p, length, &key); |
| 1489 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1490 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1491 | if (status == PSA_SUCCESS) { |
| 1492 | PSA_ASSERT(psa_destroy_key(key)); |
| 1493 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1494 | |
| 1495 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1496 | mbedtls_free(buffer); |
| 1497 | PSA_DONE(); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1498 | } |
| 1499 | /* END_CASE */ |
| 1500 | |
| 1501 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1502 | void import_export(data_t *data, |
| 1503 | int type_arg, |
| 1504 | int usage_arg, int alg_arg, |
| 1505 | int lifetime_arg, |
| 1506 | int expected_bits, |
| 1507 | int export_size_delta, |
| 1508 | int expected_export_status_arg, |
| 1509 | /*whether reexport must give the original input exactly*/ |
| 1510 | int canonical_input) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1511 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1512 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1513 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1514 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1515 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1516 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1517 | psa_key_lifetime_t lifetime = lifetime_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1518 | unsigned char *exported = NULL; |
| 1519 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1520 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1521 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1522 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1523 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1524 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1525 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1526 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1527 | ASSERT_ALLOC(exported, export_size); |
| 1528 | if (!canonical_input) { |
| 1529 | ASSERT_ALLOC(reexported, export_size); |
| 1530 | } |
| 1531 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1532 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1533 | psa_set_key_lifetime(&attributes, lifetime); |
| 1534 | psa_set_key_usage_flags(&attributes, usage_arg); |
| 1535 | psa_set_key_algorithm(&attributes, alg); |
| 1536 | psa_set_key_type(&attributes, type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1537 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1538 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1539 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1540 | |
| 1541 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1542 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1543 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1544 | TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits); |
| 1545 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1546 | |
| 1547 | /* Export the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1548 | status = psa_export_key(key, exported, export_size, &exported_length); |
| 1549 | TEST_EQUAL(status, expected_export_status); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1550 | |
| 1551 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1552 | * and export_size. On errors, the exported length must be 0. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1553 | TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH); |
| 1554 | TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0); |
| 1555 | TEST_LE_U(exported_length, export_size); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1556 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1557 | TEST_ASSERT(mem_is_char(exported + exported_length, 0, |
| 1558 | export_size - exported_length)); |
| 1559 | if (status != PSA_SUCCESS) { |
| 1560 | TEST_EQUAL(exported_length, 0); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1561 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1562 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1563 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1564 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1565 | * this validates the canonical representations. For canonical inputs, |
| 1566 | * this doesn't directly validate the implementation, but it still helps |
| 1567 | * by cross-validating the test data with the sanity check code. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1568 | if (!psa_key_lifetime_is_external(lifetime)) { |
| 1569 | if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) { |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1570 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1571 | } |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1572 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1573 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1574 | if (canonical_input) { |
| 1575 | ASSERT_COMPARE(data->x, data->len, exported, exported_length); |
| 1576 | } else { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1577 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1578 | PSA_ASSERT(psa_import_key(&attributes, exported, exported_length, |
| 1579 | &key2)); |
| 1580 | PSA_ASSERT(psa_export_key(key2, |
| 1581 | reexported, |
| 1582 | export_size, |
| 1583 | &reexported_length)); |
| 1584 | ASSERT_COMPARE(exported, exported_length, |
| 1585 | reexported, reexported_length); |
| 1586 | PSA_ASSERT(psa_destroy_key(key2)); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1587 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1588 | TEST_LE_U(exported_length, |
| 1589 | PSA_EXPORT_KEY_OUTPUT_SIZE(type, |
| 1590 | psa_get_key_bits(&got_attributes))); |
| 1591 | TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1592 | |
| 1593 | destroy: |
| 1594 | /* Destroy the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1595 | PSA_ASSERT(psa_destroy_key(key)); |
| 1596 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1597 | |
| 1598 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1599 | /* |
| 1600 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1601 | * thus reset them as required. |
| 1602 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1603 | psa_reset_key_attributes(&got_attributes); |
| 1604 | psa_destroy_key(key); |
| 1605 | mbedtls_free(exported); |
| 1606 | mbedtls_free(reexported); |
| 1607 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1608 | } |
| 1609 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1610 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1611 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1612 | void import_export_public_key(data_t *data, |
| 1613 | int type_arg, // key pair or public key |
| 1614 | int alg_arg, |
| 1615 | int lifetime_arg, |
| 1616 | int export_size_delta, |
| 1617 | int expected_export_status_arg, |
| 1618 | data_t *expected_public_key) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1619 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1620 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1621 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1622 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1623 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1624 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1625 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1626 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1627 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1628 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1629 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1630 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1631 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1632 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1633 | psa_set_key_lifetime(&attributes, lifetime); |
| 1634 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1635 | psa_set_key_algorithm(&attributes, alg); |
| 1636 | psa_set_key_type(&attributes, type); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1637 | |
| 1638 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1639 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1640 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1641 | /* Export the public key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1642 | ASSERT_ALLOC(exported, export_size); |
| 1643 | status = psa_export_public_key(key, |
| 1644 | exported, export_size, |
| 1645 | &exported_length); |
| 1646 | TEST_EQUAL(status, expected_export_status); |
| 1647 | if (status == PSA_SUCCESS) { |
| 1648 | 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] | 1649 | size_t bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1650 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1651 | bits = psa_get_key_bits(&attributes); |
| 1652 | TEST_LE_U(expected_public_key->len, |
| 1653 | PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1654 | TEST_LE_U(expected_public_key->len, |
| 1655 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1656 | TEST_LE_U(expected_public_key->len, |
| 1657 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
| 1658 | ASSERT_COMPARE(expected_public_key->x, expected_public_key->len, |
| 1659 | exported, exported_length); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1660 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1661 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1662 | /* |
| 1663 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1664 | * thus reset them as required. |
| 1665 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1666 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1667 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1668 | mbedtls_free(exported); |
| 1669 | psa_destroy_key(key); |
| 1670 | PSA_DONE(); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1671 | } |
| 1672 | /* END_CASE */ |
| 1673 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1674 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1675 | void import_and_exercise_key(data_t *data, |
| 1676 | int type_arg, |
| 1677 | int bits_arg, |
| 1678 | int alg_arg) |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1679 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1680 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1681 | psa_key_type_t type = type_arg; |
| 1682 | size_t bits = bits_arg; |
| 1683 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1684 | 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] | 1685 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1686 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1687 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1688 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1689 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1690 | psa_set_key_usage_flags(&attributes, usage); |
| 1691 | psa_set_key_algorithm(&attributes, alg); |
| 1692 | psa_set_key_type(&attributes, type); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1693 | |
| 1694 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1695 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1696 | |
| 1697 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1698 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1699 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1700 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1701 | |
| 1702 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1703 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1704 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1705 | } |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1706 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1707 | PSA_ASSERT(psa_destroy_key(key)); |
| 1708 | test_operations_on_invalid_key(key); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1709 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1710 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1711 | /* |
| 1712 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1713 | * thus reset them as required. |
| 1714 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1715 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1716 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1717 | psa_reset_key_attributes(&attributes); |
| 1718 | psa_destroy_key(key); |
| 1719 | PSA_DONE(); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1720 | } |
| 1721 | /* END_CASE */ |
| 1722 | |
| 1723 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1724 | void effective_key_attributes(int type_arg, int expected_type_arg, |
| 1725 | int bits_arg, int expected_bits_arg, |
| 1726 | int usage_arg, int expected_usage_arg, |
| 1727 | int alg_arg, int expected_alg_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1728 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1729 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1730 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1731 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1732 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1733 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1734 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1735 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1736 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1737 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1738 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1739 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1740 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1741 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1742 | psa_set_key_usage_flags(&attributes, usage); |
| 1743 | psa_set_key_algorithm(&attributes, alg); |
| 1744 | psa_set_key_type(&attributes, key_type); |
| 1745 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1746 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1747 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
| 1748 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1749 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1750 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1751 | TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type); |
| 1752 | TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits); |
| 1753 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
| 1754 | TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1755 | |
| 1756 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1757 | /* |
| 1758 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1759 | * thus reset them as required. |
| 1760 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1761 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1762 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1763 | psa_destroy_key(key); |
| 1764 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1765 | } |
| 1766 | /* END_CASE */ |
| 1767 | |
| 1768 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1769 | void check_key_policy(int type_arg, int bits_arg, |
| 1770 | int usage_arg, int alg_arg) |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1771 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1772 | test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg, |
| 1773 | usage_arg, |
| 1774 | mbedtls_test_update_key_usage_flags(usage_arg), |
| 1775 | alg_arg, alg_arg); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1776 | goto exit; |
| 1777 | } |
| 1778 | /* END_CASE */ |
| 1779 | |
| 1780 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1781 | void key_attributes_init() |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1782 | { |
| 1783 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1784 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1785 | * 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] | 1786 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1787 | psa_key_attributes_t func = psa_key_attributes_init(); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1788 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1789 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1790 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1791 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1792 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1793 | TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE); |
| 1794 | TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE); |
| 1795 | TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1796 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1797 | TEST_EQUAL(psa_get_key_type(&func), 0); |
| 1798 | TEST_EQUAL(psa_get_key_type(&init), 0); |
| 1799 | TEST_EQUAL(psa_get_key_type(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1800 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1801 | TEST_EQUAL(psa_get_key_bits(&func), 0); |
| 1802 | TEST_EQUAL(psa_get_key_bits(&init), 0); |
| 1803 | TEST_EQUAL(psa_get_key_bits(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1804 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1805 | TEST_EQUAL(psa_get_key_usage_flags(&func), 0); |
| 1806 | TEST_EQUAL(psa_get_key_usage_flags(&init), 0); |
| 1807 | TEST_EQUAL(psa_get_key_usage_flags(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1808 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1809 | TEST_EQUAL(psa_get_key_algorithm(&func), 0); |
| 1810 | TEST_EQUAL(psa_get_key_algorithm(&init), 0); |
| 1811 | TEST_EQUAL(psa_get_key_algorithm(&zero), 0); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1812 | } |
| 1813 | /* END_CASE */ |
| 1814 | |
| 1815 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1816 | void mac_key_policy(int policy_usage_arg, |
| 1817 | int policy_alg_arg, |
| 1818 | int key_type_arg, |
| 1819 | data_t *key_data, |
| 1820 | int exercise_alg_arg, |
| 1821 | int expected_status_sign_arg, |
| 1822 | int expected_status_verify_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1823 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1824 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1825 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1826 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1827 | psa_key_type_t key_type = key_type_arg; |
| 1828 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 1829 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 1830 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1831 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1832 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 1833 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1834 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1835 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1836 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1837 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1838 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1839 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1840 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1841 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1842 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1843 | &key)); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1844 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1845 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 1846 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1847 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1848 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 1849 | TEST_EQUAL(status, expected_status_sign); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1850 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1851 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1852 | uint8_t input[128] = { 0 }; |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1853 | size_t mac_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1854 | TEST_EQUAL(psa_mac_compute(key, exercise_alg, |
| 1855 | input, 128, |
| 1856 | mac, PSA_MAC_MAX_SIZE, &mac_len), |
| 1857 | expected_status_sign); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1858 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1859 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1860 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 1861 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 1862 | if (status == PSA_SUCCESS) { |
| 1863 | status = psa_mac_update(&operation, input, 128); |
| 1864 | if (status == PSA_SUCCESS) { |
| 1865 | TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE, |
| 1866 | &mac_len), |
| 1867 | expected_status_sign); |
| 1868 | } else { |
| 1869 | TEST_EQUAL(status, expected_status_sign); |
| 1870 | } |
| 1871 | } else { |
| 1872 | TEST_EQUAL(status, expected_status_sign); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1873 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1874 | PSA_ASSERT(psa_mac_abort(&operation)); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1875 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1876 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1877 | status = psa_mac_verify(key, exercise_alg, input, 128, |
| 1878 | mac, mac_len); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1879 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1880 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 1881 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1882 | } else { |
| 1883 | TEST_EQUAL(status, expected_status_verify); |
| 1884 | } |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1885 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1886 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1887 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 1888 | if (status == PSA_SUCCESS) { |
| 1889 | status = psa_mac_update(&operation, input, 128); |
| 1890 | if (status == PSA_SUCCESS) { |
| 1891 | status = psa_mac_verify_finish(&operation, mac, mac_len); |
| 1892 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 1893 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1894 | } else { |
| 1895 | TEST_EQUAL(status, expected_status_verify); |
| 1896 | } |
| 1897 | } else { |
| 1898 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1899 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1900 | } else { |
| 1901 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1902 | } |
| 1903 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1904 | psa_mac_abort(&operation); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1905 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1906 | memset(mac, 0, sizeof(mac)); |
| 1907 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 1908 | TEST_EQUAL(status, expected_status_verify); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1909 | |
| 1910 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1911 | psa_mac_abort(&operation); |
| 1912 | psa_destroy_key(key); |
| 1913 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1914 | } |
| 1915 | /* END_CASE */ |
| 1916 | |
| 1917 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1918 | void cipher_key_policy(int policy_usage_arg, |
| 1919 | int policy_alg, |
| 1920 | int key_type, |
| 1921 | data_t *key_data, |
| 1922 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1923 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1924 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1925 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1926 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1927 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1928 | size_t output_buffer_size = 0; |
| 1929 | size_t input_buffer_size = 0; |
| 1930 | size_t output_length = 0; |
| 1931 | uint8_t *output = NULL; |
| 1932 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1933 | psa_status_t status; |
| 1934 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1935 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg); |
| 1936 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg, |
| 1937 | input_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1938 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1939 | ASSERT_ALLOC(input, input_buffer_size); |
| 1940 | ASSERT_ALLOC(output, output_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1941 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1942 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1943 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1944 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1945 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1946 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1947 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1948 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1949 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1950 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1951 | /* Check if no key usage flag implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1952 | TEST_EQUAL(policy_usage, |
| 1953 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1954 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1955 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1956 | status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size, |
| 1957 | output, output_buffer_size, |
| 1958 | &output_length); |
| 1959 | if (policy_alg == exercise_alg && |
| 1960 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1961 | PSA_ASSERT(status); |
| 1962 | } else { |
| 1963 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1964 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1965 | |
| 1966 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1967 | status = psa_cipher_encrypt_setup(&operation, key, exercise_alg); |
| 1968 | if (policy_alg == exercise_alg && |
| 1969 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1970 | PSA_ASSERT(status); |
| 1971 | } else { |
| 1972 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1973 | } |
| 1974 | psa_cipher_abort(&operation); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1975 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1976 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1977 | status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size, |
| 1978 | input, input_buffer_size, |
| 1979 | &output_length); |
| 1980 | if (policy_alg == exercise_alg && |
| 1981 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 1982 | PSA_ASSERT(status); |
| 1983 | } else { |
| 1984 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1985 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1986 | |
| 1987 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1988 | status = psa_cipher_decrypt_setup(&operation, key, exercise_alg); |
| 1989 | if (policy_alg == exercise_alg && |
| 1990 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 1991 | PSA_ASSERT(status); |
| 1992 | } else { |
| 1993 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1994 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1995 | |
| 1996 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1997 | psa_cipher_abort(&operation); |
| 1998 | mbedtls_free(input); |
| 1999 | mbedtls_free(output); |
| 2000 | psa_destroy_key(key); |
| 2001 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2002 | } |
| 2003 | /* END_CASE */ |
| 2004 | |
| 2005 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2006 | void aead_key_policy(int policy_usage_arg, |
| 2007 | int policy_alg, |
| 2008 | int key_type, |
| 2009 | data_t *key_data, |
| 2010 | int nonce_length_arg, |
| 2011 | int tag_length_arg, |
| 2012 | int exercise_alg, |
| 2013 | int expected_status_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2014 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2015 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2016 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2017 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2018 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2019 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2020 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2021 | unsigned char nonce[16] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2022 | size_t nonce_length = nonce_length_arg; |
| 2023 | unsigned char tag[16]; |
| 2024 | size_t tag_length = tag_length_arg; |
| 2025 | size_t output_length; |
| 2026 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2027 | TEST_LE_U(nonce_length, sizeof(nonce)); |
| 2028 | TEST_LE_U(tag_length, sizeof(tag)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2029 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2030 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2031 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2032 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2033 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2034 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2035 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2036 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2037 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2038 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2039 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2040 | TEST_EQUAL(policy_usage, |
| 2041 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2042 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2043 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2044 | status = psa_aead_encrypt(key, exercise_alg, |
| 2045 | nonce, nonce_length, |
| 2046 | NULL, 0, |
| 2047 | NULL, 0, |
| 2048 | tag, tag_length, |
| 2049 | &output_length); |
| 2050 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2051 | TEST_EQUAL(status, expected_status); |
| 2052 | } else { |
| 2053 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2054 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2055 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2056 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2057 | status = psa_aead_encrypt_setup(&operation, key, exercise_alg); |
| 2058 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2059 | TEST_EQUAL(status, expected_status); |
| 2060 | } else { |
| 2061 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2062 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2063 | |
| 2064 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2065 | memset(tag, 0, sizeof(tag)); |
| 2066 | status = psa_aead_decrypt(key, exercise_alg, |
| 2067 | nonce, nonce_length, |
| 2068 | NULL, 0, |
| 2069 | tag, tag_length, |
| 2070 | NULL, 0, |
| 2071 | &output_length); |
| 2072 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2073 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2074 | } else if (expected_status == PSA_SUCCESS) { |
| 2075 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2076 | } else { |
| 2077 | TEST_EQUAL(status, expected_status); |
| 2078 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2079 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2080 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2081 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2082 | status = psa_aead_decrypt_setup(&operation, key, exercise_alg); |
| 2083 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2084 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2085 | } else { |
| 2086 | TEST_EQUAL(status, expected_status); |
| 2087 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2088 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2089 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2090 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2091 | psa_destroy_key(key); |
| 2092 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2093 | } |
| 2094 | /* END_CASE */ |
| 2095 | |
| 2096 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2097 | void asymmetric_encryption_key_policy(int policy_usage_arg, |
| 2098 | int policy_alg, |
| 2099 | int key_type, |
| 2100 | data_t *key_data, |
| 2101 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2102 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2103 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2104 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2105 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2106 | psa_status_t status; |
| 2107 | size_t key_bits; |
| 2108 | size_t buffer_length; |
| 2109 | unsigned char *buffer = NULL; |
| 2110 | size_t output_length; |
| 2111 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2112 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2114 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2115 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2116 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2117 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2118 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2119 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2120 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2121 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2122 | TEST_EQUAL(policy_usage, |
| 2123 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2124 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2125 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 2126 | key_bits = psa_get_key_bits(&attributes); |
| 2127 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, |
| 2128 | exercise_alg); |
| 2129 | ASSERT_ALLOC(buffer, buffer_length); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2130 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2131 | status = psa_asymmetric_encrypt(key, exercise_alg, |
| 2132 | NULL, 0, |
| 2133 | NULL, 0, |
| 2134 | buffer, buffer_length, |
| 2135 | &output_length); |
| 2136 | if (policy_alg == exercise_alg && |
| 2137 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2138 | PSA_ASSERT(status); |
| 2139 | } else { |
| 2140 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2141 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2142 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2143 | if (buffer_length != 0) { |
| 2144 | memset(buffer, 0, buffer_length); |
| 2145 | } |
| 2146 | status = psa_asymmetric_decrypt(key, exercise_alg, |
| 2147 | buffer, buffer_length, |
| 2148 | NULL, 0, |
| 2149 | buffer, buffer_length, |
| 2150 | &output_length); |
| 2151 | if (policy_alg == exercise_alg && |
| 2152 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2153 | TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING); |
| 2154 | } else { |
| 2155 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2156 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2157 | |
| 2158 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2159 | /* |
| 2160 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2161 | * thus reset them as required. |
| 2162 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2163 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2164 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2165 | psa_destroy_key(key); |
| 2166 | PSA_DONE(); |
| 2167 | mbedtls_free(buffer); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2168 | } |
| 2169 | /* END_CASE */ |
| 2170 | |
| 2171 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2172 | void asymmetric_signature_key_policy(int policy_usage_arg, |
| 2173 | int policy_alg, |
| 2174 | int key_type, |
| 2175 | data_t *key_data, |
| 2176 | int exercise_alg, |
| 2177 | int payload_length_arg, |
| 2178 | int expected_usage_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2179 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2180 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2181 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2182 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 2183 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2184 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2185 | unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 }; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2186 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2187 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2188 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2189 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2190 | int compatible_alg = payload_length_arg > 0; |
| 2191 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2192 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2193 | size_t signature_length; |
| 2194 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2195 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2196 | in the expected usage flags. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2197 | TEST_EQUAL(expected_usage, |
| 2198 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2199 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2200 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2201 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2202 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2203 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2204 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2205 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2206 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2207 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2208 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2209 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2210 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2211 | status = psa_sign_hash(key, exercise_alg, |
| 2212 | payload, payload_length, |
| 2213 | signature, sizeof(signature), |
| 2214 | &signature_length); |
| 2215 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) { |
| 2216 | PSA_ASSERT(status); |
| 2217 | } else { |
| 2218 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2219 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2221 | memset(signature, 0, sizeof(signature)); |
| 2222 | status = psa_verify_hash(key, exercise_alg, |
| 2223 | payload, payload_length, |
| 2224 | signature, sizeof(signature)); |
| 2225 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) { |
| 2226 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2227 | } else { |
| 2228 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2229 | } |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2230 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2231 | if (PSA_ALG_IS_SIGN_HASH(exercise_alg) && |
| 2232 | PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) { |
| 2233 | status = psa_sign_message(key, exercise_alg, |
| 2234 | payload, payload_length, |
| 2235 | signature, sizeof(signature), |
| 2236 | &signature_length); |
| 2237 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) { |
| 2238 | PSA_ASSERT(status); |
| 2239 | } else { |
| 2240 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2241 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2242 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2243 | memset(signature, 0, sizeof(signature)); |
| 2244 | status = psa_verify_message(key, exercise_alg, |
| 2245 | payload, payload_length, |
| 2246 | signature, sizeof(signature)); |
| 2247 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) { |
| 2248 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2249 | } else { |
| 2250 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2251 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2252 | } |
| 2253 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2254 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2255 | psa_destroy_key(key); |
| 2256 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2257 | } |
| 2258 | /* END_CASE */ |
| 2259 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2260 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2261 | void derive_key_policy(int policy_usage, |
| 2262 | int policy_alg, |
| 2263 | int key_type, |
| 2264 | data_t *key_data, |
| 2265 | int exercise_alg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2266 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2267 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2268 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2269 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2270 | psa_status_t status; |
| 2271 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2272 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2273 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2274 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2275 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2276 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2277 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2278 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2279 | &key)); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2280 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2281 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2282 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2283 | if (PSA_ALG_IS_TLS12_PRF(exercise_alg) || |
| 2284 | PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) { |
| 2285 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 2286 | &operation, |
| 2287 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2288 | (const uint8_t *) "", 0)); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2289 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2290 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2291 | status = psa_key_derivation_input_key(&operation, |
| 2292 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 2293 | key); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2294 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2295 | if (policy_alg == exercise_alg && |
| 2296 | (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) { |
| 2297 | PSA_ASSERT(status); |
| 2298 | } else { |
| 2299 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2300 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2301 | |
| 2302 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2303 | psa_key_derivation_abort(&operation); |
| 2304 | psa_destroy_key(key); |
| 2305 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2306 | } |
| 2307 | /* END_CASE */ |
| 2308 | |
| 2309 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2310 | void agreement_key_policy(int policy_usage, |
| 2311 | int policy_alg, |
| 2312 | int key_type_arg, |
| 2313 | data_t *key_data, |
| 2314 | int exercise_alg, |
| 2315 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2316 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2317 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2318 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2319 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2320 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2321 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2322 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2323 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2324 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2325 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2326 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2327 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2328 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2329 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2330 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2331 | &key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2332 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2333 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
| 2334 | status = mbedtls_test_psa_key_agreement_with_self(&operation, key); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2335 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2336 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2337 | |
| 2338 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2339 | psa_key_derivation_abort(&operation); |
| 2340 | psa_destroy_key(key); |
| 2341 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2342 | } |
| 2343 | /* END_CASE */ |
| 2344 | |
| 2345 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2346 | void key_policy_alg2(int key_type_arg, data_t *key_data, |
| 2347 | int usage_arg, int alg_arg, int alg2_arg) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2348 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2349 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2350 | psa_key_type_t key_type = key_type_arg; |
| 2351 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2352 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2353 | psa_key_usage_t usage = usage_arg; |
| 2354 | psa_algorithm_t alg = alg_arg; |
| 2355 | psa_algorithm_t alg2 = alg2_arg; |
| 2356 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2357 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2358 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2359 | psa_set_key_usage_flags(&attributes, usage); |
| 2360 | psa_set_key_algorithm(&attributes, alg); |
| 2361 | psa_set_key_enrollment_algorithm(&attributes, alg2); |
| 2362 | psa_set_key_type(&attributes, key_type); |
| 2363 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2364 | &key)); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2365 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2366 | /* Update the usage flags to obtain implicit usage flags */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2367 | usage = mbedtls_test_update_key_usage_flags(usage); |
| 2368 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 2369 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage); |
| 2370 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 2371 | TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2372 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2373 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2374 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2375 | } |
| 2376 | if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2377 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2378 | } |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2379 | |
| 2380 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2381 | /* |
| 2382 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2383 | * thus reset them as required. |
| 2384 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2385 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2386 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2387 | psa_destroy_key(key); |
| 2388 | PSA_DONE(); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2389 | } |
| 2390 | /* END_CASE */ |
| 2391 | |
| 2392 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2393 | void raw_agreement_key_policy(int policy_usage, |
| 2394 | int policy_alg, |
| 2395 | int key_type_arg, |
| 2396 | data_t *key_data, |
| 2397 | int exercise_alg, |
| 2398 | int expected_status_arg) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2399 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2400 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2401 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2402 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2403 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2404 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2405 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2406 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2407 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2408 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2409 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2410 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2411 | psa_set_key_type(&attributes, key_type); |
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 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2414 | &key)); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2415 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2416 | status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2417 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2418 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2419 | |
| 2420 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2421 | psa_key_derivation_abort(&operation); |
| 2422 | psa_destroy_key(key); |
| 2423 | PSA_DONE(); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2424 | } |
| 2425 | /* END_CASE */ |
| 2426 | |
| 2427 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2428 | void copy_success(int source_usage_arg, |
| 2429 | int source_alg_arg, int source_alg2_arg, |
| 2430 | unsigned int source_lifetime_arg, |
| 2431 | int type_arg, data_t *material, |
| 2432 | int copy_attributes, |
| 2433 | int target_usage_arg, |
| 2434 | int target_alg_arg, int target_alg2_arg, |
| 2435 | unsigned int target_lifetime_arg, |
| 2436 | int expected_usage_arg, |
| 2437 | int expected_alg_arg, int expected_alg2_arg) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2438 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2439 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2440 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2441 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2442 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2443 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2444 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 2445 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2446 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2447 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2448 | uint8_t *export_buffer = NULL; |
| 2449 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2450 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2451 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2452 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2453 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2454 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2455 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2456 | psa_set_key_type(&source_attributes, type_arg); |
| 2457 | psa_set_key_lifetime(&source_attributes, source_lifetime); |
| 2458 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2459 | material->x, material->len, |
| 2460 | &source_key)); |
| 2461 | PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2462 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2463 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2464 | if (copy_attributes) { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2465 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2466 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2467 | psa_set_key_lifetime(&target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2468 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2469 | if (target_usage_arg != -1) { |
| 2470 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2471 | } |
| 2472 | if (target_alg_arg != -1) { |
| 2473 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2474 | } |
| 2475 | if (target_alg2_arg != -1) { |
| 2476 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
| 2477 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2478 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2479 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2480 | /* Copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2481 | PSA_ASSERT(psa_copy_key(source_key, |
| 2482 | &target_attributes, &target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2483 | |
| 2484 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2485 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2486 | |
| 2487 | /* Test that the target slot has the expected content and policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2488 | PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes)); |
| 2489 | TEST_EQUAL(psa_get_key_type(&source_attributes), |
| 2490 | psa_get_key_type(&target_attributes)); |
| 2491 | TEST_EQUAL(psa_get_key_bits(&source_attributes), |
| 2492 | psa_get_key_bits(&target_attributes)); |
| 2493 | TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes)); |
| 2494 | TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes)); |
| 2495 | TEST_EQUAL(expected_alg2, |
| 2496 | psa_get_key_enrollment_algorithm(&target_attributes)); |
| 2497 | if (expected_usage & PSA_KEY_USAGE_EXPORT) { |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2498 | size_t length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2499 | ASSERT_ALLOC(export_buffer, material->len); |
| 2500 | PSA_ASSERT(psa_export_key(target_key, export_buffer, |
| 2501 | material->len, &length)); |
| 2502 | ASSERT_COMPARE(material->x, material->len, |
| 2503 | export_buffer, length); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2504 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2505 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2506 | if (!psa_key_lifetime_is_external(target_lifetime)) { |
| 2507 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2508 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2509 | } |
| 2510 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2511 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2512 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2513 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2514 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2515 | PSA_ASSERT(psa_destroy_key(target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2516 | |
| 2517 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2518 | /* |
| 2519 | * Source and target key attributes may have been returned by |
| 2520 | * psa_get_key_attributes() thus reset them as required. |
| 2521 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2522 | psa_reset_key_attributes(&source_attributes); |
| 2523 | psa_reset_key_attributes(&target_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2524 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2525 | PSA_DONE(); |
| 2526 | mbedtls_free(export_buffer); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2527 | } |
| 2528 | /* END_CASE */ |
| 2529 | |
| 2530 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2531 | void copy_fail(int source_usage_arg, |
| 2532 | int source_alg_arg, int source_alg2_arg, |
| 2533 | int source_lifetime_arg, |
| 2534 | int type_arg, data_t *material, |
| 2535 | int target_type_arg, int target_bits_arg, |
| 2536 | int target_usage_arg, |
| 2537 | int target_alg_arg, int target_alg2_arg, |
| 2538 | int target_id_arg, int target_lifetime_arg, |
| 2539 | int expected_status_arg) |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2540 | { |
| 2541 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2542 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2543 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2544 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2545 | 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] | 2546 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2547 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2548 | |
| 2549 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2550 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2551 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2552 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2553 | psa_set_key_type(&source_attributes, type_arg); |
| 2554 | psa_set_key_lifetime(&source_attributes, source_lifetime_arg); |
| 2555 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2556 | material->x, material->len, |
| 2557 | &source_key)); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2558 | |
| 2559 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2560 | psa_set_key_id(&target_attributes, key_id); |
| 2561 | psa_set_key_lifetime(&target_attributes, target_lifetime_arg); |
| 2562 | psa_set_key_type(&target_attributes, target_type_arg); |
| 2563 | psa_set_key_bits(&target_attributes, target_bits_arg); |
| 2564 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2565 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2566 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2567 | |
| 2568 | /* Try to copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2569 | TEST_EQUAL(psa_copy_key(source_key, |
| 2570 | &target_attributes, &target_key), |
| 2571 | expected_status_arg); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2572 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2573 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2574 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2575 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2576 | psa_reset_key_attributes(&source_attributes); |
| 2577 | psa_reset_key_attributes(&target_attributes); |
| 2578 | PSA_DONE(); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2579 | } |
| 2580 | /* END_CASE */ |
| 2581 | |
| 2582 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2583 | void hash_operation_init() |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2584 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2585 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2586 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2587 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2588 | * 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] | 2589 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2590 | psa_hash_operation_t func = psa_hash_operation_init(); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2591 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2592 | psa_hash_operation_t zero; |
| 2593 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2594 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2595 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2596 | /* A freshly-initialized hash operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2597 | TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)), |
| 2598 | PSA_ERROR_BAD_STATE); |
| 2599 | TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)), |
| 2600 | PSA_ERROR_BAD_STATE); |
| 2601 | TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)), |
| 2602 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2603 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2604 | /* A default hash operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2605 | PSA_ASSERT(psa_hash_abort(&func)); |
| 2606 | PSA_ASSERT(psa_hash_abort(&init)); |
| 2607 | PSA_ASSERT(psa_hash_abort(&zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2608 | } |
| 2609 | /* END_CASE */ |
| 2610 | |
| 2611 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2612 | void hash_setup(int alg_arg, |
| 2613 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2614 | { |
| 2615 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2616 | uint8_t *output = NULL; |
| 2617 | size_t output_size = 0; |
| 2618 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2619 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2620 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2621 | psa_status_t status; |
| 2622 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2623 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2624 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2625 | /* Hash Setup, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2626 | output_size = PSA_HASH_LENGTH(alg); |
| 2627 | ASSERT_ALLOC(output, output_size); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2628 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2629 | status = psa_hash_compute(alg, NULL, 0, |
| 2630 | output, output_size, &output_length); |
| 2631 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2632 | |
| 2633 | /* Hash Setup, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2634 | status = psa_hash_setup(&operation, alg); |
| 2635 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2636 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2637 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2638 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2639 | |
| 2640 | /* If setup failed, reproduce the failure, so as to |
| 2641 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2642 | if (status != PSA_SUCCESS) { |
| 2643 | TEST_EQUAL(psa_hash_setup(&operation, alg), status); |
| 2644 | } |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2645 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2646 | /* Now the operation object should be reusable. */ |
| 2647 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2648 | PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG)); |
| 2649 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2650 | #endif |
| 2651 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2652 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2653 | mbedtls_free(output); |
| 2654 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2655 | } |
| 2656 | /* END_CASE */ |
| 2657 | |
| 2658 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2659 | void hash_compute_fail(int alg_arg, data_t *input, |
| 2660 | int output_size_arg, int expected_status_arg) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2661 | { |
| 2662 | psa_algorithm_t alg = alg_arg; |
| 2663 | uint8_t *output = NULL; |
| 2664 | size_t output_size = output_size_arg; |
| 2665 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2666 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2667 | psa_status_t expected_status = expected_status_arg; |
| 2668 | psa_status_t status; |
| 2669 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2670 | ASSERT_ALLOC(output, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2671 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2672 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2673 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2674 | /* Hash Compute, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2675 | status = psa_hash_compute(alg, input->x, input->len, |
| 2676 | output, output_size, &output_length); |
| 2677 | TEST_EQUAL(status, expected_status); |
| 2678 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2679 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2680 | /* Hash Compute, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2681 | status = psa_hash_setup(&operation, alg); |
| 2682 | if (status == PSA_SUCCESS) { |
| 2683 | status = psa_hash_update(&operation, input->x, input->len); |
| 2684 | if (status == PSA_SUCCESS) { |
| 2685 | status = psa_hash_finish(&operation, output, output_size, |
| 2686 | &output_length); |
| 2687 | if (status == PSA_SUCCESS) { |
| 2688 | TEST_LE_U(output_length, output_size); |
| 2689 | } else { |
| 2690 | TEST_EQUAL(status, expected_status); |
| 2691 | } |
| 2692 | } else { |
| 2693 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2694 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2695 | } else { |
| 2696 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2697 | } |
| 2698 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2699 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2700 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2701 | mbedtls_free(output); |
| 2702 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2703 | } |
| 2704 | /* END_CASE */ |
| 2705 | |
| 2706 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2707 | void hash_compare_fail(int alg_arg, data_t *input, |
| 2708 | data_t *reference_hash, |
| 2709 | int expected_status_arg) |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2710 | { |
| 2711 | psa_algorithm_t alg = alg_arg; |
| 2712 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2713 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2714 | psa_status_t status; |
| 2715 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2716 | PSA_ASSERT(psa_crypto_init()); |
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, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2719 | status = psa_hash_compare(alg, input->x, input->len, |
| 2720 | reference_hash->x, reference_hash->len); |
| 2721 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2722 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2723 | /* Hash Compare, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2724 | status = psa_hash_setup(&operation, alg); |
| 2725 | if (status == PSA_SUCCESS) { |
| 2726 | status = psa_hash_update(&operation, input->x, input->len); |
| 2727 | if (status == PSA_SUCCESS) { |
| 2728 | status = psa_hash_verify(&operation, reference_hash->x, |
| 2729 | reference_hash->len); |
| 2730 | TEST_EQUAL(status, expected_status); |
| 2731 | } else { |
| 2732 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2733 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2734 | } else { |
| 2735 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2736 | } |
| 2737 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2738 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2739 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2740 | PSA_DONE(); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2741 | } |
| 2742 | /* END_CASE */ |
| 2743 | |
| 2744 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2745 | void hash_compute_compare(int alg_arg, data_t *input, |
| 2746 | data_t *expected_output) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2747 | { |
| 2748 | psa_algorithm_t alg = alg_arg; |
| 2749 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2750 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2751 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2752 | size_t i; |
| 2753 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2754 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2755 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2756 | /* Compute with tight buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2757 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 2758 | output, PSA_HASH_LENGTH(alg), |
| 2759 | &output_length)); |
| 2760 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2761 | ASSERT_COMPARE(output, output_length, |
| 2762 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2763 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2764 | /* Compute with tight buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2765 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2766 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2767 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 2768 | PSA_HASH_LENGTH(alg), |
| 2769 | &output_length)); |
| 2770 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2771 | ASSERT_COMPARE(output, output_length, |
| 2772 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2773 | |
| 2774 | /* Compute with larger buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2775 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 2776 | output, sizeof(output), |
| 2777 | &output_length)); |
| 2778 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2779 | ASSERT_COMPARE(output, output_length, |
| 2780 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2781 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2782 | /* Compute with larger buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2783 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2784 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2785 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 2786 | sizeof(output), &output_length)); |
| 2787 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2788 | ASSERT_COMPARE(output, output_length, |
| 2789 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2790 | |
| 2791 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2792 | PSA_ASSERT(psa_hash_compare(alg, input->x, input->len, |
| 2793 | output, output_length)); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2794 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2795 | /* Compare with correct hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2796 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2797 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2798 | PSA_ASSERT(psa_hash_verify(&operation, output, |
| 2799 | output_length)); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2800 | |
| 2801 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2802 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2803 | output, output_length + 1), |
| 2804 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2805 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2806 | /* Compare with trailing garbage, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2807 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2808 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2809 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1), |
| 2810 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2811 | |
| 2812 | /* Compare with truncated hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2813 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2814 | output, output_length - 1), |
| 2815 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2816 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2817 | /* Compare with truncated hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2818 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2819 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2820 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1), |
| 2821 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2822 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2823 | /* Compare with corrupted value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2824 | for (i = 0; i < output_length; i++) { |
| 2825 | mbedtls_test_set_step(i); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2826 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2827 | |
| 2828 | /* One-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2829 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2830 | output, output_length), |
| 2831 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2832 | |
| 2833 | /* Multi-Part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2834 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2835 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2836 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length), |
| 2837 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2838 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2839 | output[i] ^= 1; |
| 2840 | } |
| 2841 | |
| 2842 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2843 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2844 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2845 | } |
| 2846 | /* END_CASE */ |
| 2847 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2848 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2849 | void hash_bad_order() |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2850 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2851 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2852 | unsigned char input[] = ""; |
| 2853 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2854 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2855 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2856 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2857 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 |
| 2858 | }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2859 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2860 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2861 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2862 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2863 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2864 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2865 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2866 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2867 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2868 | TEST_EQUAL(psa_hash_setup(&operation, alg), |
| 2869 | PSA_ERROR_BAD_STATE); |
| 2870 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2871 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2872 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2873 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2874 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2875 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2876 | PSA_ERROR_BAD_STATE); |
| 2877 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2878 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2879 | /* Check that update calls abort on error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2880 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 2881 | operation.id = UINT_MAX; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2882 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2883 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2884 | PSA_ERROR_BAD_STATE); |
| 2885 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2886 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2887 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2888 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2889 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2890 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2891 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2892 | hash, sizeof(hash), &hash_len)); |
| 2893 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2894 | PSA_ERROR_BAD_STATE); |
| 2895 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2896 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2897 | /* Call verify without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2898 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2899 | valid_hash, sizeof(valid_hash)), |
| 2900 | PSA_ERROR_BAD_STATE); |
| 2901 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2902 | |
| 2903 | /* Call verify after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2904 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2905 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2906 | hash, sizeof(hash), &hash_len)); |
| 2907 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2908 | valid_hash, sizeof(valid_hash)), |
| 2909 | PSA_ERROR_BAD_STATE); |
| 2910 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2911 | |
| 2912 | /* Call verify twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2913 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2914 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2915 | PSA_ASSERT(psa_hash_verify(&operation, |
| 2916 | valid_hash, sizeof(valid_hash))); |
| 2917 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2918 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2919 | valid_hash, sizeof(valid_hash)), |
| 2920 | PSA_ERROR_BAD_STATE); |
| 2921 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2922 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2923 | |
| 2924 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2925 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2926 | hash, sizeof(hash), &hash_len), |
| 2927 | PSA_ERROR_BAD_STATE); |
| 2928 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2929 | |
| 2930 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2931 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2932 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2933 | hash, sizeof(hash), &hash_len)); |
| 2934 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2935 | hash, sizeof(hash), &hash_len), |
| 2936 | PSA_ERROR_BAD_STATE); |
| 2937 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2938 | |
| 2939 | /* Call finish after calling verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2940 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2941 | PSA_ASSERT(psa_hash_verify(&operation, |
| 2942 | valid_hash, sizeof(valid_hash))); |
| 2943 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2944 | hash, sizeof(hash), &hash_len), |
| 2945 | PSA_ERROR_BAD_STATE); |
| 2946 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2947 | |
| 2948 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2949 | PSA_DONE(); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2950 | } |
| 2951 | /* END_CASE */ |
| 2952 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2953 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2954 | void hash_verify_bad_args() |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2955 | { |
| 2956 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2957 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2958 | * appended to it */ |
| 2959 | unsigned char hash[] = { |
| 2960 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2961 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2962 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb |
| 2963 | }; |
| 2964 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2965 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2966 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2967 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2968 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2969 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2970 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2971 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2972 | TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1), |
| 2973 | PSA_ERROR_INVALID_SIGNATURE); |
| 2974 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2975 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2976 | ASSERT_OPERATION_IS_INACTIVE(operation); |
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 non-matching hash */ |
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 + 1, expected_size), |
| 2981 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2982 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2983 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2984 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2985 | TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)), |
| 2986 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2987 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2988 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2989 | PSA_DONE(); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2990 | } |
| 2991 | /* END_CASE */ |
| 2992 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2993 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2994 | void hash_finish_bad_args() |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2995 | { |
| 2996 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2997 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2998 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2999 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3000 | size_t hash_len; |
| 3001 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3002 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3003 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3004 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3005 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3006 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3007 | hash, expected_size - 1, &hash_len), |
| 3008 | PSA_ERROR_BUFFER_TOO_SMALL); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3009 | |
| 3010 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3011 | PSA_DONE(); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3012 | } |
| 3013 | /* END_CASE */ |
| 3014 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3015 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3016 | void hash_clone_source_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3017 | { |
| 3018 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3019 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3020 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 3021 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3022 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3023 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3024 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3025 | size_t hash_len; |
| 3026 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3027 | PSA_ASSERT(psa_crypto_init()); |
| 3028 | PSA_ASSERT(psa_hash_setup(&op_source, alg)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3029 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3030 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3031 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3032 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3033 | hash, sizeof(hash), &hash_len)); |
| 3034 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3035 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3036 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3037 | TEST_EQUAL(psa_hash_clone(&op_source, &op_setup), |
| 3038 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3039 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3040 | PSA_ASSERT(psa_hash_clone(&op_source, &op_init)); |
| 3041 | PSA_ASSERT(psa_hash_finish(&op_init, |
| 3042 | hash, sizeof(hash), &hash_len)); |
| 3043 | PSA_ASSERT(psa_hash_clone(&op_source, &op_finished)); |
| 3044 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3045 | hash, sizeof(hash), &hash_len)); |
| 3046 | PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted)); |
| 3047 | PSA_ASSERT(psa_hash_finish(&op_aborted, |
| 3048 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3049 | |
| 3050 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3051 | psa_hash_abort(&op_source); |
| 3052 | psa_hash_abort(&op_init); |
| 3053 | psa_hash_abort(&op_setup); |
| 3054 | psa_hash_abort(&op_finished); |
| 3055 | psa_hash_abort(&op_aborted); |
| 3056 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3057 | } |
| 3058 | /* END_CASE */ |
| 3059 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3060 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3061 | void hash_clone_target_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3062 | { |
| 3063 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3064 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3065 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3066 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3067 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3068 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3069 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 3070 | size_t hash_len; |
| 3071 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3072 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3073 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3074 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3075 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3076 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3077 | hash, sizeof(hash), &hash_len)); |
| 3078 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3079 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3080 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3081 | PSA_ASSERT(psa_hash_clone(&op_setup, &op_target)); |
| 3082 | PSA_ASSERT(psa_hash_finish(&op_target, |
| 3083 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3084 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3085 | TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE); |
| 3086 | TEST_EQUAL(psa_hash_clone(&op_finished, &op_target), |
| 3087 | PSA_ERROR_BAD_STATE); |
| 3088 | TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target), |
| 3089 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3090 | |
| 3091 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3092 | psa_hash_abort(&op_target); |
| 3093 | psa_hash_abort(&op_init); |
| 3094 | psa_hash_abort(&op_setup); |
| 3095 | psa_hash_abort(&op_finished); |
| 3096 | psa_hash_abort(&op_aborted); |
| 3097 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3098 | } |
| 3099 | /* END_CASE */ |
| 3100 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3101 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3102 | void mac_operation_init() |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3103 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3104 | const uint8_t input[1] = { 0 }; |
| 3105 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3106 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3107 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3108 | * 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] | 3109 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3110 | psa_mac_operation_t func = psa_mac_operation_init(); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3111 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 3112 | psa_mac_operation_t zero; |
| 3113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3114 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3115 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3116 | /* A freshly-initialized MAC operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3117 | TEST_EQUAL(psa_mac_update(&func, |
| 3118 | input, sizeof(input)), |
| 3119 | PSA_ERROR_BAD_STATE); |
| 3120 | TEST_EQUAL(psa_mac_update(&init, |
| 3121 | input, sizeof(input)), |
| 3122 | PSA_ERROR_BAD_STATE); |
| 3123 | TEST_EQUAL(psa_mac_update(&zero, |
| 3124 | input, sizeof(input)), |
| 3125 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3126 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3127 | /* A default MAC operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3128 | PSA_ASSERT(psa_mac_abort(&func)); |
| 3129 | PSA_ASSERT(psa_mac_abort(&init)); |
| 3130 | PSA_ASSERT(psa_mac_abort(&zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3131 | } |
| 3132 | /* END_CASE */ |
| 3133 | |
| 3134 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3135 | void mac_setup(int key_type_arg, |
| 3136 | data_t *key, |
| 3137 | int alg_arg, |
| 3138 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3139 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3140 | psa_key_type_t key_type = key_type_arg; |
| 3141 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3142 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3143 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3144 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3145 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3146 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3147 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3149 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3151 | if (!exercise_mac_setup(key_type, key->x, key->len, alg, |
| 3152 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3153 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3154 | } |
| 3155 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3156 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3157 | /* The operation object should be reusable. */ |
| 3158 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3159 | if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 3160 | smoke_test_key_data, |
| 3161 | sizeof(smoke_test_key_data), |
| 3162 | KNOWN_SUPPORTED_MAC_ALG, |
| 3163 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3164 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3165 | } |
| 3166 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3167 | #endif |
| 3168 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3169 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3170 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3171 | } |
| 3172 | /* END_CASE */ |
| 3173 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3174 | /* 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] | 3175 | void mac_bad_order() |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3176 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3177 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3178 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 3179 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3180 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3181 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3182 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3183 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa |
| 3184 | }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3185 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3186 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3187 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 3188 | size_t sign_mac_length = 0; |
| 3189 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3190 | const uint8_t verify_mac[] = { |
| 3191 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 3192 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3193 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 |
| 3194 | }; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3195 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3196 | PSA_ASSERT(psa_crypto_init()); |
| 3197 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 3198 | psa_set_key_algorithm(&attributes, alg); |
| 3199 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3200 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3201 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3202 | &key)); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3203 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3204 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3205 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3206 | PSA_ERROR_BAD_STATE); |
| 3207 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3208 | |
| 3209 | /* Call sign finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3210 | TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac), |
| 3211 | &sign_mac_length), |
| 3212 | PSA_ERROR_BAD_STATE); |
| 3213 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3214 | |
| 3215 | /* Call verify finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3216 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3217 | verify_mac, sizeof(verify_mac)), |
| 3218 | PSA_ERROR_BAD_STATE); |
| 3219 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3220 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3221 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3222 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3223 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3224 | TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg), |
| 3225 | PSA_ERROR_BAD_STATE); |
| 3226 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3227 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3228 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3229 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3230 | /* Call update after sign finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3231 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3232 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3233 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3234 | sign_mac, sizeof(sign_mac), |
| 3235 | &sign_mac_length)); |
| 3236 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3237 | PSA_ERROR_BAD_STATE); |
| 3238 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3239 | |
| 3240 | /* Call update after verify finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3241 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3242 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3243 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3244 | verify_mac, sizeof(verify_mac))); |
| 3245 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3246 | PSA_ERROR_BAD_STATE); |
| 3247 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3248 | |
| 3249 | /* Call sign finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3250 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3251 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3252 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3253 | sign_mac, sizeof(sign_mac), |
| 3254 | &sign_mac_length)); |
| 3255 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3256 | sign_mac, sizeof(sign_mac), |
| 3257 | &sign_mac_length), |
| 3258 | PSA_ERROR_BAD_STATE); |
| 3259 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3260 | |
| 3261 | /* Call verify finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3262 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3263 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3264 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3265 | verify_mac, sizeof(verify_mac))); |
| 3266 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3267 | verify_mac, sizeof(verify_mac)), |
| 3268 | PSA_ERROR_BAD_STATE); |
| 3269 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3270 | |
| 3271 | /* Setup sign but try verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3272 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3273 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3274 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3275 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3276 | verify_mac, sizeof(verify_mac)), |
| 3277 | PSA_ERROR_BAD_STATE); |
| 3278 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3279 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3280 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3281 | |
| 3282 | /* Setup verify but try sign. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3283 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3284 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3285 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3286 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3287 | sign_mac, sizeof(sign_mac), |
| 3288 | &sign_mac_length), |
| 3289 | PSA_ERROR_BAD_STATE); |
| 3290 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3291 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3292 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3293 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3294 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3295 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3296 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3297 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3298 | } |
| 3299 | /* END_CASE */ |
| 3300 | |
| 3301 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3302 | void mac_sign_verify_multi(int key_type_arg, |
| 3303 | data_t *key_data, |
| 3304 | int alg_arg, |
| 3305 | data_t *input, |
| 3306 | int is_verify, |
| 3307 | data_t *expected_mac) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3308 | { |
| 3309 | size_t data_part_len = 0; |
| 3310 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3311 | 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] | 3312 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3313 | mbedtls_test_set_step(2000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3314 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3315 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3316 | alg_arg, |
| 3317 | input, data_part_len, |
| 3318 | expected_mac, |
| 3319 | is_verify, 0) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3320 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3321 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3322 | |
| 3323 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3324 | mbedtls_test_set_step(3000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3325 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3326 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3327 | alg_arg, |
| 3328 | input, data_part_len, |
| 3329 | expected_mac, |
| 3330 | is_verify, 1) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3331 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3332 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3333 | } |
| 3334 | |
| 3335 | /* Goto is required to silence warnings about unused labels, as we |
| 3336 | * don't actually do any test assertions in this function. */ |
| 3337 | goto exit; |
| 3338 | } |
| 3339 | /* END_CASE */ |
| 3340 | |
| 3341 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3342 | void mac_sign(int key_type_arg, |
| 3343 | data_t *key_data, |
| 3344 | int alg_arg, |
| 3345 | data_t *input, |
| 3346 | data_t *expected_mac) |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3347 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3348 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3349 | psa_key_type_t key_type = key_type_arg; |
| 3350 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3351 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3352 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3353 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3354 | size_t mac_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3355 | 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] | 3356 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3357 | const size_t output_sizes_to_test[] = { |
| 3358 | 0, |
| 3359 | 1, |
| 3360 | expected_mac->len - 1, |
| 3361 | expected_mac->len, |
| 3362 | expected_mac->len + 1, |
| 3363 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3364 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3365 | TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3366 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3367 | TEST_ASSERT(expected_mac->len == mac_buffer_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3368 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3369 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3371 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 3372 | psa_set_key_algorithm(&attributes, alg); |
| 3373 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3374 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3375 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3376 | &key)); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3377 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3378 | 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] | 3379 | const size_t output_size = output_sizes_to_test[i]; |
| 3380 | psa_status_t expected_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3381 | (output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3382 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3383 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3384 | mbedtls_test_set_step(output_size); |
| 3385 | ASSERT_ALLOC(actual_mac, output_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3386 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3387 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3388 | TEST_EQUAL(psa_mac_compute(key, alg, |
| 3389 | input->x, input->len, |
| 3390 | actual_mac, output_size, &mac_length), |
| 3391 | expected_status); |
| 3392 | if (expected_status == PSA_SUCCESS) { |
| 3393 | ASSERT_COMPARE(expected_mac->x, expected_mac->len, |
| 3394 | actual_mac, mac_length); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3395 | } |
| 3396 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3397 | if (output_size > 0) { |
| 3398 | memset(actual_mac, 0, output_size); |
| 3399 | } |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3400 | |
| 3401 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3402 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3403 | PSA_ASSERT(psa_mac_update(&operation, |
| 3404 | input->x, input->len)); |
| 3405 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3406 | actual_mac, output_size, |
| 3407 | &mac_length), |
| 3408 | expected_status); |
| 3409 | PSA_ASSERT(psa_mac_abort(&operation)); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3410 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3411 | if (expected_status == PSA_SUCCESS) { |
| 3412 | ASSERT_COMPARE(expected_mac->x, expected_mac->len, |
| 3413 | actual_mac, mac_length); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3414 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3415 | mbedtls_free(actual_mac); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3416 | actual_mac = NULL; |
| 3417 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3418 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3419 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3420 | psa_mac_abort(&operation); |
| 3421 | psa_destroy_key(key); |
| 3422 | PSA_DONE(); |
| 3423 | mbedtls_free(actual_mac); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3424 | } |
| 3425 | /* END_CASE */ |
| 3426 | |
| 3427 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3428 | void mac_verify(int key_type_arg, |
| 3429 | data_t *key_data, |
| 3430 | int alg_arg, |
| 3431 | data_t *input, |
| 3432 | data_t *expected_mac) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3433 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3434 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3435 | psa_key_type_t key_type = key_type_arg; |
| 3436 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3437 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3438 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3439 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3440 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3441 | TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3442 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3443 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3444 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3445 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 3446 | psa_set_key_algorithm(&attributes, alg); |
| 3447 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3449 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3450 | &key)); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3451 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3452 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3453 | PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len, |
| 3454 | expected_mac->x, expected_mac->len)); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3455 | |
| 3456 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3457 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3458 | PSA_ASSERT(psa_mac_update(&operation, |
| 3459 | input->x, input->len)); |
| 3460 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3461 | expected_mac->x, |
| 3462 | expected_mac->len)); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3463 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3464 | /* Test a MAC that's too short, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3465 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3466 | input->x, input->len, |
| 3467 | expected_mac->x, |
| 3468 | expected_mac->len - 1), |
| 3469 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3470 | |
| 3471 | /* Test a MAC that's too short, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3472 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3473 | PSA_ASSERT(psa_mac_update(&operation, |
| 3474 | input->x, input->len)); |
| 3475 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3476 | expected_mac->x, |
| 3477 | expected_mac->len - 1), |
| 3478 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3479 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3480 | /* Test a MAC that's too long, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3481 | ASSERT_ALLOC(perturbed_mac, expected_mac->len + 1); |
| 3482 | memcpy(perturbed_mac, expected_mac->x, expected_mac->len); |
| 3483 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3484 | input->x, input->len, |
| 3485 | perturbed_mac, expected_mac->len + 1), |
| 3486 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3487 | |
| 3488 | /* Test a MAC that's too long, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3489 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3490 | PSA_ASSERT(psa_mac_update(&operation, |
| 3491 | input->x, input->len)); |
| 3492 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3493 | perturbed_mac, |
| 3494 | expected_mac->len + 1), |
| 3495 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3496 | |
| 3497 | /* Test changing one byte. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3498 | for (size_t i = 0; i < expected_mac->len; i++) { |
| 3499 | mbedtls_test_set_step(i); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3500 | perturbed_mac[i] ^= 1; |
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 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3503 | input->x, input->len, |
| 3504 | perturbed_mac, expected_mac->len), |
| 3505 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3506 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3507 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3508 | PSA_ASSERT(psa_mac_update(&operation, |
| 3509 | input->x, input->len)); |
| 3510 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3511 | perturbed_mac, |
| 3512 | expected_mac->len), |
| 3513 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3514 | perturbed_mac[i] ^= 1; |
| 3515 | } |
| 3516 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3517 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3518 | psa_mac_abort(&operation); |
| 3519 | psa_destroy_key(key); |
| 3520 | PSA_DONE(); |
| 3521 | mbedtls_free(perturbed_mac); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3522 | } |
| 3523 | /* END_CASE */ |
| 3524 | |
| 3525 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3526 | void cipher_operation_init() |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3527 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3528 | const uint8_t input[1] = { 0 }; |
| 3529 | unsigned char output[1] = { 0 }; |
| 3530 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3531 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3532 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3533 | * 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] | 3534 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3535 | psa_cipher_operation_t func = psa_cipher_operation_init(); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3536 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3537 | psa_cipher_operation_t zero; |
| 3538 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3539 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3540 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3541 | /* A freshly-initialized cipher operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3542 | TEST_EQUAL(psa_cipher_update(&func, |
| 3543 | input, sizeof(input), |
| 3544 | output, sizeof(output), |
| 3545 | &output_length), |
| 3546 | PSA_ERROR_BAD_STATE); |
| 3547 | TEST_EQUAL(psa_cipher_update(&init, |
| 3548 | input, sizeof(input), |
| 3549 | output, sizeof(output), |
| 3550 | &output_length), |
| 3551 | PSA_ERROR_BAD_STATE); |
| 3552 | TEST_EQUAL(psa_cipher_update(&zero, |
| 3553 | input, sizeof(input), |
| 3554 | output, sizeof(output), |
| 3555 | &output_length), |
| 3556 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3557 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3558 | /* A default cipher operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3559 | PSA_ASSERT(psa_cipher_abort(&func)); |
| 3560 | PSA_ASSERT(psa_cipher_abort(&init)); |
| 3561 | PSA_ASSERT(psa_cipher_abort(&zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3562 | } |
| 3563 | /* END_CASE */ |
| 3564 | |
| 3565 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3566 | void cipher_setup(int key_type_arg, |
| 3567 | data_t *key, |
| 3568 | int alg_arg, |
| 3569 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3570 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3571 | psa_key_type_t key_type = key_type_arg; |
| 3572 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3573 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3574 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3575 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3576 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3577 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3578 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3579 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3580 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3581 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3582 | if (!exercise_cipher_setup(key_type, key->x, key->len, alg, |
| 3583 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3584 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3585 | } |
| 3586 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3587 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3588 | /* The operation object should be reusable. */ |
| 3589 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3590 | if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3591 | smoke_test_key_data, |
| 3592 | sizeof(smoke_test_key_data), |
| 3593 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3594 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3595 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3596 | } |
| 3597 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3598 | #endif |
| 3599 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3600 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3601 | psa_cipher_abort(&operation); |
| 3602 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3603 | } |
| 3604 | /* END_CASE */ |
| 3605 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3606 | /* 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] | 3607 | void cipher_bad_order() |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3608 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3609 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3610 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3611 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3612 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3613 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3614 | 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] | 3615 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3616 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3617 | 0xaa, 0xaa, 0xaa, 0xaa |
| 3618 | }; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3619 | const uint8_t text[] = { |
| 3620 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3621 | 0xbb, 0xbb, 0xbb, 0xbb |
| 3622 | }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3623 | 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] | 3624 | size_t length = 0; |
| 3625 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3626 | PSA_ASSERT(psa_crypto_init()); |
| 3627 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3628 | psa_set_key_algorithm(&attributes, alg); |
| 3629 | psa_set_key_type(&attributes, key_type); |
| 3630 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3631 | &key)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3632 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3633 | /* Call encrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3634 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3635 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3636 | TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg), |
| 3637 | PSA_ERROR_BAD_STATE); |
| 3638 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3639 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3640 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3641 | |
| 3642 | /* Call decrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3643 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3644 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3645 | TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg), |
| 3646 | PSA_ERROR_BAD_STATE); |
| 3647 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3648 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3649 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3650 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3651 | /* Generate an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3652 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3653 | buffer, sizeof(buffer), |
| 3654 | &length), |
| 3655 | PSA_ERROR_BAD_STATE); |
| 3656 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3657 | |
| 3658 | /* Generate an IV twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3659 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3660 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3661 | buffer, sizeof(buffer), |
| 3662 | &length)); |
| 3663 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3664 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3665 | buffer, sizeof(buffer), |
| 3666 | &length), |
| 3667 | PSA_ERROR_BAD_STATE); |
| 3668 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3669 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3670 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3671 | |
| 3672 | /* Generate an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3673 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3674 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3675 | iv, sizeof(iv))); |
| 3676 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3677 | buffer, sizeof(buffer), |
| 3678 | &length), |
| 3679 | PSA_ERROR_BAD_STATE); |
| 3680 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3681 | |
| 3682 | /* Set an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3683 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3684 | iv, sizeof(iv)), |
| 3685 | PSA_ERROR_BAD_STATE); |
| 3686 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3687 | |
| 3688 | /* Set an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3689 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3690 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3691 | iv, sizeof(iv))); |
| 3692 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3693 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3694 | iv, sizeof(iv)), |
| 3695 | PSA_ERROR_BAD_STATE); |
| 3696 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3697 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3698 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3699 | |
| 3700 | /* Set an IV after it's already generated. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3701 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3702 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3703 | buffer, sizeof(buffer), |
| 3704 | &length)); |
| 3705 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3706 | iv, sizeof(iv)), |
| 3707 | PSA_ERROR_BAD_STATE); |
| 3708 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3709 | |
| 3710 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3711 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3712 | text, sizeof(text), |
| 3713 | buffer, sizeof(buffer), |
| 3714 | &length), |
| 3715 | PSA_ERROR_BAD_STATE); |
| 3716 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3717 | |
| 3718 | /* Call update without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3719 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3720 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3721 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3722 | text, sizeof(text), |
| 3723 | buffer, sizeof(buffer), |
| 3724 | &length), |
| 3725 | PSA_ERROR_BAD_STATE); |
| 3726 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3727 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3728 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3729 | |
| 3730 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3731 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3732 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3733 | iv, sizeof(iv))); |
| 3734 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3735 | buffer, sizeof(buffer), &length)); |
| 3736 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3737 | text, sizeof(text), |
| 3738 | buffer, sizeof(buffer), |
| 3739 | &length), |
| 3740 | PSA_ERROR_BAD_STATE); |
| 3741 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3742 | |
| 3743 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3744 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3745 | buffer, sizeof(buffer), &length), |
| 3746 | PSA_ERROR_BAD_STATE); |
| 3747 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3748 | |
| 3749 | /* Call finish without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3750 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3751 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3752 | * for cipher modes with padding. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3753 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3754 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3755 | buffer, sizeof(buffer), &length), |
| 3756 | PSA_ERROR_BAD_STATE); |
| 3757 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3758 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3759 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3760 | |
| 3761 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3762 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3763 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3764 | iv, sizeof(iv))); |
| 3765 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3766 | buffer, sizeof(buffer), &length)); |
| 3767 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3768 | buffer, sizeof(buffer), &length), |
| 3769 | PSA_ERROR_BAD_STATE); |
| 3770 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3771 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3772 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3773 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3774 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3775 | psa_cipher_abort(&operation); |
| 3776 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3777 | } |
| 3778 | /* END_CASE */ |
| 3779 | |
| 3780 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3781 | void cipher_encrypt_fail(int alg_arg, |
| 3782 | int key_type_arg, |
| 3783 | data_t *key_data, |
| 3784 | data_t *input, |
| 3785 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3786 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3787 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3788 | psa_status_t status; |
| 3789 | psa_key_type_t key_type = key_type_arg; |
| 3790 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3791 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3792 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 }; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3793 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; |
| 3794 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3795 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3796 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3797 | size_t output_length = 0; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3798 | size_t function_output_length; |
| 3799 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3800 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3801 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3802 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 3803 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3804 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3805 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3806 | psa_set_key_algorithm(&attributes, alg); |
| 3807 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3808 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3809 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 3810 | input->len); |
| 3811 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3812 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3813 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3814 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3815 | } |
| 3816 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3817 | /* Encrypt, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3818 | status = psa_cipher_encrypt(key, alg, input->x, input->len, output, |
| 3819 | output_buffer_size, &output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3820 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3821 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3822 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3823 | /* Encrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3824 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
| 3825 | if (status == PSA_SUCCESS) { |
| 3826 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 3827 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3828 | iv, iv_size, |
| 3829 | &iv_length)); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3830 | } |
| 3831 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3832 | status = psa_cipher_update(&operation, input->x, input->len, |
| 3833 | output, output_buffer_size, |
| 3834 | &function_output_length); |
| 3835 | if (status == PSA_SUCCESS) { |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3836 | output_length += function_output_length; |
| 3837 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3838 | status = psa_cipher_finish(&operation, output + output_length, |
| 3839 | output_buffer_size - output_length, |
| 3840 | &function_output_length); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3841 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3842 | TEST_EQUAL(status, expected_status); |
| 3843 | } else { |
| 3844 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3845 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3846 | } else { |
| 3847 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3848 | } |
| 3849 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3850 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3851 | psa_cipher_abort(&operation); |
| 3852 | mbedtls_free(output); |
| 3853 | psa_destroy_key(key); |
| 3854 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3855 | } |
| 3856 | /* END_CASE */ |
| 3857 | |
| 3858 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3859 | void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data, |
| 3860 | data_t *input, int iv_length, |
| 3861 | int expected_result) |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3862 | { |
| 3863 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3864 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3865 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3866 | size_t output_buffer_size = 0; |
| 3867 | unsigned char *output = NULL; |
| 3868 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3869 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 3870 | ASSERT_ALLOC(output, output_buffer_size); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3871 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3872 | PSA_ASSERT(psa_crypto_init()); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3873 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3874 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3875 | psa_set_key_algorithm(&attributes, alg); |
| 3876 | psa_set_key_type(&attributes, key_type); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3877 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3878 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3879 | &key)); |
| 3880 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3881 | TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output, |
| 3882 | iv_length)); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3883 | |
| 3884 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3885 | psa_cipher_abort(&operation); |
| 3886 | mbedtls_free(output); |
| 3887 | psa_destroy_key(key); |
| 3888 | PSA_DONE(); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3889 | } |
| 3890 | /* END_CASE */ |
| 3891 | |
| 3892 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3893 | void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data, |
| 3894 | data_t *plaintext, data_t *ciphertext) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3895 | { |
| 3896 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3897 | psa_key_type_t key_type = key_type_arg; |
| 3898 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3899 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3900 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3901 | unsigned char *output = NULL; |
| 3902 | size_t output_buffer_size = 0; |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3903 | size_t output_length, length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3904 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3905 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3906 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3907 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3908 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3909 | TEST_LE_U(ciphertext->len, |
| 3910 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len)); |
| 3911 | TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len), |
| 3912 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len)); |
| 3913 | TEST_LE_U(plaintext->len, |
| 3914 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len)); |
| 3915 | TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len), |
| 3916 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len)); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3917 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3918 | |
| 3919 | /* Set up key and output buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3920 | psa_set_key_usage_flags(&attributes, |
| 3921 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3922 | psa_set_key_algorithm(&attributes, alg); |
| 3923 | psa_set_key_type(&attributes, key_type); |
| 3924 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3925 | &key)); |
| 3926 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 3927 | plaintext->len); |
| 3928 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3929 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3930 | /* set_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3931 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3932 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 3933 | PSA_ERROR_BAD_STATE); |
| 3934 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3935 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 3936 | PSA_ERROR_BAD_STATE); |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3937 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3938 | /* generate_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3939 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3940 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 3941 | &length), |
| 3942 | PSA_ERROR_BAD_STATE); |
| 3943 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3944 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 3945 | &length), |
| 3946 | PSA_ERROR_BAD_STATE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3947 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3948 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3949 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3950 | output_length = 0; |
| 3951 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3952 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3953 | plaintext->x, plaintext->len, |
| 3954 | output, output_buffer_size, |
| 3955 | &length)); |
| 3956 | TEST_LE_U(length, output_buffer_size); |
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 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3959 | mbedtls_buffer_offset(output, output_length), |
| 3960 | output_buffer_size - output_length, |
| 3961 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3962 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3963 | ASSERT_COMPARE(ciphertext->x, ciphertext->len, |
| 3964 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3965 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3966 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3967 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3968 | output_length = 0; |
| 3969 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3970 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3971 | ciphertext->x, ciphertext->len, |
| 3972 | output, output_buffer_size, |
| 3973 | &length)); |
| 3974 | TEST_LE_U(length, output_buffer_size); |
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 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3977 | mbedtls_buffer_offset(output, output_length), |
| 3978 | output_buffer_size - output_length, |
| 3979 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3980 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3981 | ASSERT_COMPARE(plaintext->x, plaintext->len, |
| 3982 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3983 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3984 | /* One-shot encryption */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3985 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3986 | PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len, |
| 3987 | output, output_buffer_size, |
| 3988 | &output_length)); |
| 3989 | ASSERT_COMPARE(ciphertext->x, ciphertext->len, |
| 3990 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3991 | |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3992 | /* One-shot decryption */ |
| 3993 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3994 | PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len, |
| 3995 | output, output_buffer_size, |
| 3996 | &output_length)); |
| 3997 | ASSERT_COMPARE(plaintext->x, plaintext->len, |
| 3998 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3999 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4000 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4001 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4002 | mbedtls_free(output); |
| 4003 | psa_cipher_abort(&operation); |
| 4004 | psa_destroy_key(key); |
| 4005 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4006 | } |
| 4007 | /* END_CASE */ |
| 4008 | |
| 4009 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4010 | 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] | 4011 | { |
| 4012 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4013 | psa_algorithm_t alg = alg_arg; |
| 4014 | psa_key_type_t key_type = key_type_arg; |
| 4015 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4016 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4017 | psa_status_t status; |
| 4018 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4019 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4020 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4021 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4022 | psa_set_key_algorithm(&attributes, alg); |
| 4023 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4024 | |
| 4025 | /* Usage of either of these two size macros would cause divide by zero |
| 4026 | * with incorrect key types previously. Input length should be irrelevant |
| 4027 | * here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4028 | TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16), |
| 4029 | 0); |
| 4030 | TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4031 | |
| 4032 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4033 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4034 | &key)); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4035 | |
| 4036 | /* Should fail due to invalid alg type (to support invalid key type). |
| 4037 | * Encrypt or decrypt will end up in the same place. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4038 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4039 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4040 | TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4041 | |
| 4042 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4043 | psa_cipher_abort(&operation); |
| 4044 | psa_destroy_key(key); |
| 4045 | PSA_DONE(); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4046 | } |
| 4047 | /* END_CASE */ |
| 4048 | |
| 4049 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4050 | void cipher_encrypt_validation(int alg_arg, |
| 4051 | int key_type_arg, |
| 4052 | data_t *key_data, |
| 4053 | data_t *input) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4054 | { |
| 4055 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4056 | psa_key_type_t key_type = key_type_arg; |
| 4057 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4058 | size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4059 | unsigned char *output1 = NULL; |
| 4060 | size_t output1_buffer_size = 0; |
| 4061 | size_t output1_length = 0; |
| 4062 | unsigned char *output2 = NULL; |
| 4063 | size_t output2_buffer_size = 0; |
| 4064 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4065 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4066 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4067 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4068 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4069 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4070 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4071 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4072 | psa_set_key_algorithm(&attributes, alg); |
| 4073 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4074 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4075 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4076 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4077 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4078 | ASSERT_ALLOC(output1, output1_buffer_size); |
| 4079 | ASSERT_ALLOC(output2, output2_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4080 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4081 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4082 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4083 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 4084 | /* The one-shot cipher encryption uses generated iv so validating |
| 4085 | the output is not possible. Validating with multipart encryption. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4086 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1, |
| 4087 | output1_buffer_size, &output1_length)); |
| 4088 | TEST_LE_U(output1_length, |
| 4089 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4090 | TEST_LE_U(output1_length, |
| 4091 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4092 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4093 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4094 | PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4095 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4096 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4097 | input->x, input->len, |
| 4098 | output2, output2_buffer_size, |
| 4099 | &function_output_length)); |
| 4100 | TEST_LE_U(function_output_length, |
| 4101 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4102 | TEST_LE_U(function_output_length, |
| 4103 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4104 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4105 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4106 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4107 | output2 + output2_length, |
| 4108 | output2_buffer_size - output2_length, |
| 4109 | &function_output_length)); |
| 4110 | TEST_LE_U(function_output_length, |
| 4111 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4112 | TEST_LE_U(function_output_length, |
| 4113 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4114 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4115 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4116 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4117 | ASSERT_COMPARE(output1 + iv_size, output1_length - iv_size, |
| 4118 | output2, output2_length); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4119 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4120 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4121 | psa_cipher_abort(&operation); |
| 4122 | mbedtls_free(output1); |
| 4123 | mbedtls_free(output2); |
| 4124 | psa_destroy_key(key); |
| 4125 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4126 | } |
| 4127 | /* END_CASE */ |
| 4128 | |
| 4129 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4130 | void cipher_encrypt_multipart(int alg_arg, int key_type_arg, |
| 4131 | data_t *key_data, data_t *iv, |
| 4132 | data_t *input, |
| 4133 | int first_part_size_arg, |
| 4134 | int output1_length_arg, int output2_length_arg, |
| 4135 | data_t *expected_output, |
| 4136 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4137 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4138 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4139 | psa_key_type_t key_type = key_type_arg; |
| 4140 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4141 | psa_status_t status; |
| 4142 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4143 | size_t first_part_size = first_part_size_arg; |
| 4144 | size_t output1_length = output1_length_arg; |
| 4145 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4146 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4147 | size_t output_buffer_size = 0; |
| 4148 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4149 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4150 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4151 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4153 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4154 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4155 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4156 | psa_set_key_algorithm(&attributes, alg); |
| 4157 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4159 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4160 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4161 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4162 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4164 | if (iv->len > 0) { |
| 4165 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4166 | } |
| 4167 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4168 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4169 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4170 | ASSERT_ALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4172 | TEST_LE_U(first_part_size, input->len); |
| 4173 | PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size, |
| 4174 | output, output_buffer_size, |
| 4175 | &function_output_length)); |
| 4176 | TEST_ASSERT(function_output_length == output1_length); |
| 4177 | TEST_LE_U(function_output_length, |
| 4178 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4179 | TEST_LE_U(function_output_length, |
| 4180 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4181 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4183 | if (first_part_size < input->len) { |
| 4184 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4185 | input->x + first_part_size, |
| 4186 | input->len - first_part_size, |
| 4187 | (output_buffer_size == 0 ? NULL : |
| 4188 | output + total_output_length), |
| 4189 | output_buffer_size - total_output_length, |
| 4190 | &function_output_length)); |
| 4191 | TEST_ASSERT(function_output_length == output2_length); |
| 4192 | TEST_LE_U(function_output_length, |
| 4193 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4194 | alg, |
| 4195 | input->len - first_part_size)); |
| 4196 | TEST_LE_U(function_output_length, |
| 4197 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4198 | total_output_length += function_output_length; |
| 4199 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4200 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4201 | status = psa_cipher_finish(&operation, |
| 4202 | (output_buffer_size == 0 ? NULL : |
| 4203 | output + total_output_length), |
| 4204 | output_buffer_size - total_output_length, |
| 4205 | &function_output_length); |
| 4206 | TEST_LE_U(function_output_length, |
| 4207 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4208 | TEST_LE_U(function_output_length, |
| 4209 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4210 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4211 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4212 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4213 | if (expected_status == PSA_SUCCESS) { |
| 4214 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4215 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4216 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 4217 | output, total_output_length); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4218 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4219 | |
| 4220 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4221 | psa_cipher_abort(&operation); |
| 4222 | mbedtls_free(output); |
| 4223 | psa_destroy_key(key); |
| 4224 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4225 | } |
| 4226 | /* END_CASE */ |
| 4227 | |
| 4228 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4229 | void cipher_decrypt_multipart(int alg_arg, int key_type_arg, |
| 4230 | data_t *key_data, data_t *iv, |
| 4231 | data_t *input, |
| 4232 | int first_part_size_arg, |
| 4233 | int output1_length_arg, int output2_length_arg, |
| 4234 | data_t *expected_output, |
| 4235 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4236 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4237 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4238 | psa_key_type_t key_type = key_type_arg; |
| 4239 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4240 | psa_status_t status; |
| 4241 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4242 | size_t first_part_size = first_part_size_arg; |
| 4243 | size_t output1_length = output1_length_arg; |
| 4244 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4245 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4246 | size_t output_buffer_size = 0; |
| 4247 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4248 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4249 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4250 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4252 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4253 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4254 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4255 | psa_set_key_algorithm(&attributes, alg); |
| 4256 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4257 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4258 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4259 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4260 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4261 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4263 | if (iv->len > 0) { |
| 4264 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4265 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4266 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4267 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4268 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4269 | ASSERT_ALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4270 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4271 | TEST_LE_U(first_part_size, input->len); |
| 4272 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4273 | input->x, first_part_size, |
| 4274 | output, output_buffer_size, |
| 4275 | &function_output_length)); |
| 4276 | TEST_ASSERT(function_output_length == output1_length); |
| 4277 | TEST_LE_U(function_output_length, |
| 4278 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4279 | TEST_LE_U(function_output_length, |
| 4280 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4281 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4282 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4283 | if (first_part_size < input->len) { |
| 4284 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4285 | input->x + first_part_size, |
| 4286 | input->len - first_part_size, |
| 4287 | (output_buffer_size == 0 ? NULL : |
| 4288 | output + total_output_length), |
| 4289 | output_buffer_size - total_output_length, |
| 4290 | &function_output_length)); |
| 4291 | TEST_ASSERT(function_output_length == output2_length); |
| 4292 | TEST_LE_U(function_output_length, |
| 4293 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4294 | alg, |
| 4295 | input->len - first_part_size)); |
| 4296 | TEST_LE_U(function_output_length, |
| 4297 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4298 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4299 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4300 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4301 | status = psa_cipher_finish(&operation, |
| 4302 | (output_buffer_size == 0 ? NULL : |
| 4303 | output + total_output_length), |
| 4304 | output_buffer_size - total_output_length, |
| 4305 | &function_output_length); |
| 4306 | TEST_LE_U(function_output_length, |
| 4307 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4308 | TEST_LE_U(function_output_length, |
| 4309 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4310 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4311 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4312 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4313 | if (expected_status == PSA_SUCCESS) { |
| 4314 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4315 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4316 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 4317 | output, total_output_length); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4318 | } |
| 4319 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4320 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4321 | psa_cipher_abort(&operation); |
| 4322 | mbedtls_free(output); |
| 4323 | psa_destroy_key(key); |
| 4324 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4325 | } |
| 4326 | /* END_CASE */ |
| 4327 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4328 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4329 | void cipher_decrypt_fail(int alg_arg, |
| 4330 | int key_type_arg, |
| 4331 | data_t *key_data, |
| 4332 | data_t *iv, |
| 4333 | data_t *input_arg, |
| 4334 | int expected_status_arg) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4335 | { |
| 4336 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4337 | psa_status_t status; |
| 4338 | psa_key_type_t key_type = key_type_arg; |
| 4339 | psa_algorithm_t alg = alg_arg; |
| 4340 | psa_status_t expected_status = expected_status_arg; |
| 4341 | unsigned char *input = NULL; |
| 4342 | size_t input_buffer_size = 0; |
| 4343 | unsigned char *output = NULL; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4344 | unsigned char *output_multi = NULL; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4345 | size_t output_buffer_size = 0; |
| 4346 | size_t output_length = 0; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4347 | size_t function_output_length; |
| 4348 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4349 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4350 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4351 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 4352 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4354 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4355 | psa_set_key_algorithm(&attributes, alg); |
| 4356 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4358 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4359 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4360 | } |
| 4361 | |
| 4362 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4363 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4364 | if (input_buffer_size > 0) { |
| 4365 | ASSERT_ALLOC(input, input_buffer_size); |
| 4366 | memcpy(input, iv->x, iv->len); |
| 4367 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4368 | } |
| 4369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4370 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size); |
| 4371 | ASSERT_ALLOC(output, output_buffer_size); |
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, one-short */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4374 | status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4375 | output_buffer_size, &output_length); |
| 4376 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4377 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4378 | /* Decrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4379 | status = psa_cipher_decrypt_setup(&operation, key, alg); |
| 4380 | if (status == PSA_SUCCESS) { |
| 4381 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 4382 | input_arg->len) + |
| 4383 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4384 | ASSERT_ALLOC(output_multi, output_buffer_size); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4386 | if (iv->len > 0) { |
| 4387 | status = psa_cipher_set_iv(&operation, iv->x, iv->len); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4389 | if (status != PSA_SUCCESS) { |
| 4390 | TEST_EQUAL(status, expected_status); |
| 4391 | } |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4392 | } |
| 4393 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4394 | if (status == PSA_SUCCESS) { |
| 4395 | status = psa_cipher_update(&operation, |
| 4396 | input_arg->x, input_arg->len, |
| 4397 | output_multi, output_buffer_size, |
| 4398 | &function_output_length); |
| 4399 | if (status == PSA_SUCCESS) { |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4400 | output_length = function_output_length; |
| 4401 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4402 | status = psa_cipher_finish(&operation, |
| 4403 | output_multi + output_length, |
| 4404 | output_buffer_size - output_length, |
| 4405 | &function_output_length); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4406 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4407 | TEST_EQUAL(status, expected_status); |
| 4408 | } else { |
| 4409 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4410 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4411 | } else { |
| 4412 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4413 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4414 | } else { |
| 4415 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4416 | } |
| 4417 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4418 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4419 | psa_cipher_abort(&operation); |
| 4420 | mbedtls_free(input); |
| 4421 | mbedtls_free(output); |
| 4422 | mbedtls_free(output_multi); |
| 4423 | psa_destroy_key(key); |
| 4424 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4425 | } |
| 4426 | /* END_CASE */ |
| 4427 | |
| 4428 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4429 | void cipher_decrypt(int alg_arg, |
| 4430 | int key_type_arg, |
| 4431 | data_t *key_data, |
| 4432 | data_t *iv, |
| 4433 | data_t *input_arg, |
| 4434 | data_t *expected_output) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4435 | { |
| 4436 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4437 | psa_key_type_t key_type = key_type_arg; |
| 4438 | psa_algorithm_t alg = alg_arg; |
| 4439 | unsigned char *input = NULL; |
| 4440 | size_t input_buffer_size = 0; |
| 4441 | unsigned char *output = NULL; |
| 4442 | size_t output_buffer_size = 0; |
| 4443 | size_t output_length = 0; |
| 4444 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4445 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4446 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4447 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4448 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4449 | psa_set_key_algorithm(&attributes, alg); |
| 4450 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4451 | |
| 4452 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4453 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4454 | if (input_buffer_size > 0) { |
| 4455 | ASSERT_ALLOC(input, input_buffer_size); |
| 4456 | memcpy(input, iv->x, iv->len); |
| 4457 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4458 | } |
| 4459 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4460 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size); |
| 4461 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4462 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4463 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4464 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4465 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4466 | PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4467 | output_buffer_size, &output_length)); |
| 4468 | TEST_LE_U(output_length, |
| 4469 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size)); |
| 4470 | TEST_LE_U(output_length, |
| 4471 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4472 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4473 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 4474 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4475 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4476 | mbedtls_free(input); |
| 4477 | mbedtls_free(output); |
| 4478 | psa_destroy_key(key); |
| 4479 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4480 | } |
| 4481 | /* END_CASE */ |
| 4482 | |
| 4483 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4484 | void cipher_verify_output(int alg_arg, |
| 4485 | int key_type_arg, |
| 4486 | data_t *key_data, |
| 4487 | data_t *input) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4488 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4489 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4490 | psa_key_type_t key_type = key_type_arg; |
| 4491 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4492 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4493 | size_t output1_size = 0; |
| 4494 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4495 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4496 | size_t output2_size = 0; |
| 4497 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4498 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4499 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4500 | PSA_ASSERT(psa_crypto_init()); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4501 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4502 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4503 | psa_set_key_algorithm(&attributes, alg); |
| 4504 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4505 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4506 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4507 | &key)); |
| 4508 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4509 | ASSERT_ALLOC(output1, output1_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4510 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4511 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, |
| 4512 | output1, output1_size, |
| 4513 | &output1_length)); |
| 4514 | TEST_LE_U(output1_length, |
| 4515 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4516 | TEST_LE_U(output1_length, |
| 4517 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4518 | |
| 4519 | output2_size = output1_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4520 | ASSERT_ALLOC(output2, output2_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4521 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4522 | PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length, |
| 4523 | output2, output2_size, |
| 4524 | &output2_length)); |
| 4525 | TEST_LE_U(output2_length, |
| 4526 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4527 | TEST_LE_U(output2_length, |
| 4528 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4529 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4530 | ASSERT_COMPARE(input->x, input->len, output2, output2_length); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4531 | |
| 4532 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4533 | mbedtls_free(output1); |
| 4534 | mbedtls_free(output2); |
| 4535 | psa_destroy_key(key); |
| 4536 | PSA_DONE(); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4537 | } |
| 4538 | /* END_CASE */ |
| 4539 | |
| 4540 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4541 | void cipher_verify_output_multipart(int alg_arg, |
| 4542 | int key_type_arg, |
| 4543 | data_t *key_data, |
| 4544 | data_t *input, |
| 4545 | int first_part_size_arg) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4546 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4547 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4548 | psa_key_type_t key_type = key_type_arg; |
| 4549 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4550 | size_t first_part_size = first_part_size_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4551 | unsigned char iv[16] = { 0 }; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4552 | size_t iv_size = 16; |
| 4553 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4554 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4555 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4556 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4557 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4558 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4559 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4560 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4561 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 4562 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4563 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4564 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4565 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4566 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4567 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4568 | psa_set_key_algorithm(&attributes, alg); |
| 4569 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4570 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4571 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4572 | &key)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4573 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4574 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg)); |
| 4575 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4576 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4577 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 4578 | PSA_ASSERT(psa_cipher_generate_iv(&operation1, |
| 4579 | iv, iv_size, |
| 4580 | &iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4581 | } |
| 4582 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4583 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4584 | TEST_LE_U(output1_buffer_size, |
| 4585 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
| 4586 | ASSERT_ALLOC(output1, output1_buffer_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4587 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4588 | TEST_LE_U(first_part_size, input->len); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4589 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4590 | PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size, |
| 4591 | output1, output1_buffer_size, |
| 4592 | &function_output_length)); |
| 4593 | TEST_LE_U(function_output_length, |
| 4594 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4595 | TEST_LE_U(function_output_length, |
| 4596 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4597 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4598 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4599 | PSA_ASSERT(psa_cipher_update(&operation1, |
| 4600 | input->x + first_part_size, |
| 4601 | input->len - first_part_size, |
| 4602 | output1, output1_buffer_size, |
| 4603 | &function_output_length)); |
| 4604 | TEST_LE_U(function_output_length, |
| 4605 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4606 | alg, |
| 4607 | input->len - first_part_size)); |
| 4608 | TEST_LE_U(function_output_length, |
| 4609 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4610 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4611 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4612 | PSA_ASSERT(psa_cipher_finish(&operation1, |
| 4613 | output1 + output1_length, |
| 4614 | output1_buffer_size - output1_length, |
| 4615 | &function_output_length)); |
| 4616 | TEST_LE_U(function_output_length, |
| 4617 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4618 | TEST_LE_U(function_output_length, |
| 4619 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4620 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4621 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4622 | PSA_ASSERT(psa_cipher_abort(&operation1)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4623 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4624 | output2_buffer_size = output1_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4625 | TEST_LE_U(output2_buffer_size, |
| 4626 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4627 | TEST_LE_U(output2_buffer_size, |
| 4628 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
| 4629 | ASSERT_ALLOC(output2, output2_buffer_size); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4630 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4631 | if (iv_length > 0) { |
| 4632 | PSA_ASSERT(psa_cipher_set_iv(&operation2, |
| 4633 | iv, iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4634 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4635 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4636 | PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size, |
| 4637 | output2, output2_buffer_size, |
| 4638 | &function_output_length)); |
| 4639 | TEST_LE_U(function_output_length, |
| 4640 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4641 | TEST_LE_U(function_output_length, |
| 4642 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4643 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4644 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4645 | PSA_ASSERT(psa_cipher_update(&operation2, |
| 4646 | output1 + first_part_size, |
| 4647 | output1_length - first_part_size, |
| 4648 | output2, output2_buffer_size, |
| 4649 | &function_output_length)); |
| 4650 | TEST_LE_U(function_output_length, |
| 4651 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4652 | alg, |
| 4653 | output1_length - first_part_size)); |
| 4654 | TEST_LE_U(function_output_length, |
| 4655 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4656 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4657 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4658 | PSA_ASSERT(psa_cipher_finish(&operation2, |
| 4659 | output2 + output2_length, |
| 4660 | output2_buffer_size - output2_length, |
| 4661 | &function_output_length)); |
| 4662 | TEST_LE_U(function_output_length, |
| 4663 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4664 | TEST_LE_U(function_output_length, |
| 4665 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4666 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4667 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4668 | PSA_ASSERT(psa_cipher_abort(&operation2)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4669 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4670 | ASSERT_COMPARE(input->x, input->len, output2, output2_length); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4671 | |
| 4672 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4673 | psa_cipher_abort(&operation1); |
| 4674 | psa_cipher_abort(&operation2); |
| 4675 | mbedtls_free(output1); |
| 4676 | mbedtls_free(output2); |
| 4677 | psa_destroy_key(key); |
| 4678 | PSA_DONE(); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4679 | } |
| 4680 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 4681 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4682 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4683 | void aead_encrypt_decrypt(int key_type_arg, data_t *key_data, |
| 4684 | int alg_arg, |
| 4685 | data_t *nonce, |
| 4686 | data_t *additional_data, |
| 4687 | data_t *input_data, |
| 4688 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4689 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4690 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4691 | psa_key_type_t key_type = key_type_arg; |
| 4692 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4693 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4694 | unsigned char *output_data = NULL; |
| 4695 | size_t output_size = 0; |
| 4696 | size_t output_length = 0; |
| 4697 | unsigned char *output_data2 = NULL; |
| 4698 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4699 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4700 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4701 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4702 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4703 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4704 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4705 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4706 | psa_set_key_algorithm(&attributes, alg); |
| 4707 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4708 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4709 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4710 | &key)); |
| 4711 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4712 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4713 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4714 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4715 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4716 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4717 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4718 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4719 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
| 4720 | TEST_EQUAL(output_size, |
| 4721 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4722 | TEST_LE_U(output_size, |
| 4723 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4724 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4725 | ASSERT_ALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4726 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4727 | status = psa_aead_encrypt(key, alg, |
| 4728 | nonce->x, nonce->len, |
| 4729 | additional_data->x, |
| 4730 | additional_data->len, |
| 4731 | input_data->x, input_data->len, |
| 4732 | output_data, output_size, |
| 4733 | &output_length); |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4734 | |
| 4735 | /* If the operation is not supported, just skip and not fail in case the |
| 4736 | * encryption involves a common limitation of cryptography hardwares and |
| 4737 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4738 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4739 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4740 | 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] | 4741 | } |
| 4742 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4743 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4744 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4745 | if (PSA_SUCCESS == expected_result) { |
| 4746 | ASSERT_ALLOC(output_data2, output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4747 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4748 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4749 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4750 | TEST_EQUAL(input_data->len, |
| 4751 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length)); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4752 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4753 | TEST_LE_U(input_data->len, |
| 4754 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length)); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4755 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4756 | TEST_EQUAL(psa_aead_decrypt(key, alg, |
| 4757 | nonce->x, nonce->len, |
| 4758 | additional_data->x, |
| 4759 | additional_data->len, |
| 4760 | output_data, output_length, |
| 4761 | output_data2, output_length, |
| 4762 | &output_length2), |
| 4763 | expected_result); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4764 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4765 | ASSERT_COMPARE(input_data->x, input_data->len, |
| 4766 | output_data2, output_length2); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4767 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4768 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4769 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4770 | psa_destroy_key(key); |
| 4771 | mbedtls_free(output_data); |
| 4772 | mbedtls_free(output_data2); |
| 4773 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4774 | } |
| 4775 | /* END_CASE */ |
| 4776 | |
| 4777 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4778 | void aead_encrypt(int key_type_arg, data_t *key_data, |
| 4779 | int alg_arg, |
| 4780 | data_t *nonce, |
| 4781 | data_t *additional_data, |
| 4782 | data_t *input_data, |
| 4783 | data_t *expected_result) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4784 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4785 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4786 | psa_key_type_t key_type = key_type_arg; |
| 4787 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4788 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4789 | unsigned char *output_data = NULL; |
| 4790 | size_t output_size = 0; |
| 4791 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4792 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4793 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4794 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4795 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4796 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4797 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4798 | psa_set_key_algorithm(&attributes, alg); |
| 4799 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4800 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4801 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4802 | &key)); |
| 4803 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4804 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4805 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4806 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4807 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4808 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4809 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4810 | TEST_EQUAL(output_size, |
| 4811 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4812 | TEST_LE_U(output_size, |
| 4813 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
| 4814 | ASSERT_ALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4815 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4816 | status = psa_aead_encrypt(key, alg, |
| 4817 | nonce->x, nonce->len, |
| 4818 | additional_data->x, additional_data->len, |
| 4819 | input_data->x, input_data->len, |
| 4820 | output_data, output_size, |
| 4821 | &output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4822 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4823 | /* If the operation is not supported, just skip and not fail in case the |
| 4824 | * encryption involves a common limitation of cryptography hardwares and |
| 4825 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4826 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4827 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4828 | 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] | 4829 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4830 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4831 | PSA_ASSERT(status); |
| 4832 | ASSERT_COMPARE(expected_result->x, expected_result->len, |
| 4833 | output_data, output_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4834 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4835 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4836 | psa_destroy_key(key); |
| 4837 | mbedtls_free(output_data); |
| 4838 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4839 | } |
| 4840 | /* END_CASE */ |
| 4841 | |
| 4842 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4843 | void aead_decrypt(int key_type_arg, data_t *key_data, |
| 4844 | int alg_arg, |
| 4845 | data_t *nonce, |
| 4846 | data_t *additional_data, |
| 4847 | data_t *input_data, |
| 4848 | data_t *expected_data, |
| 4849 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4850 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4851 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4852 | psa_key_type_t key_type = key_type_arg; |
| 4853 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4854 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4855 | unsigned char *output_data = NULL; |
| 4856 | size_t output_size = 0; |
| 4857 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4858 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4859 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4860 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4861 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4862 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4863 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4864 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4865 | psa_set_key_algorithm(&attributes, alg); |
| 4866 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4867 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4868 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4869 | &key)); |
| 4870 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4871 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4872 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4873 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4874 | alg); |
| 4875 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4876 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4877 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4878 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4879 | TEST_EQUAL(output_size, |
| 4880 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4881 | TEST_LE_U(output_size, |
| 4882 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4883 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4884 | ASSERT_ALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4885 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4886 | status = psa_aead_decrypt(key, alg, |
| 4887 | nonce->x, nonce->len, |
| 4888 | additional_data->x, |
| 4889 | additional_data->len, |
| 4890 | input_data->x, input_data->len, |
| 4891 | output_data, output_size, |
| 4892 | &output_length); |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4893 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4894 | /* If the operation is not supported, just skip and not fail in case the |
| 4895 | * decryption involves a common limitation of cryptography hardwares and |
| 4896 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4897 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4898 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4899 | 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] | 4900 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4901 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4902 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4903 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4904 | if (expected_result == PSA_SUCCESS) { |
| 4905 | ASSERT_COMPARE(expected_data->x, expected_data->len, |
| 4906 | output_data, output_length); |
| 4907 | } |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4908 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4909 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4910 | psa_destroy_key(key); |
| 4911 | mbedtls_free(output_data); |
| 4912 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4913 | } |
| 4914 | /* END_CASE */ |
| 4915 | |
| 4916 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4917 | void aead_multipart_encrypt(int key_type_arg, data_t *key_data, |
| 4918 | int alg_arg, |
| 4919 | data_t *nonce, |
| 4920 | data_t *additional_data, |
| 4921 | data_t *input_data, |
| 4922 | int do_set_lengths, |
| 4923 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4924 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4925 | size_t ad_part_len = 0; |
| 4926 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4927 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4928 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4929 | for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) { |
| 4930 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4931 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4932 | if (do_set_lengths) { |
| 4933 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4934 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4935 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4936 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4937 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4938 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4939 | |
| 4940 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4941 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4942 | alg_arg, nonce, |
| 4943 | additional_data, |
| 4944 | ad_part_len, |
| 4945 | input_data, -1, |
| 4946 | set_lengths_method, |
| 4947 | expected_output, |
| 4948 | 1, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4949 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4950 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4951 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4952 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 4953 | mbedtls_test_set_step(1000 + ad_part_len); |
| 4954 | |
| 4955 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4956 | alg_arg, nonce, |
| 4957 | additional_data, |
| 4958 | ad_part_len, |
| 4959 | input_data, -1, |
| 4960 | set_lengths_method, |
| 4961 | expected_output, |
| 4962 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4963 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4964 | } |
| 4965 | } |
| 4966 | |
| 4967 | for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) { |
| 4968 | /* Split data into length(data_part_len) parts. */ |
| 4969 | mbedtls_test_set_step(2000 + data_part_len); |
| 4970 | |
| 4971 | if (do_set_lengths) { |
| 4972 | if (data_part_len & 0x01) { |
| 4973 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4974 | } else { |
| 4975 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
| 4976 | } |
| 4977 | } |
| 4978 | |
| 4979 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4980 | alg_arg, nonce, |
| 4981 | additional_data, -1, |
| 4982 | input_data, data_part_len, |
| 4983 | set_lengths_method, |
| 4984 | expected_output, |
| 4985 | 1, 0)) { |
| 4986 | break; |
| 4987 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4988 | |
| 4989 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4990 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4991 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4992 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4993 | alg_arg, nonce, |
| 4994 | additional_data, -1, |
| 4995 | input_data, data_part_len, |
| 4996 | set_lengths_method, |
| 4997 | expected_output, |
| 4998 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4999 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5000 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5001 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5002 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5003 | /* Goto is required to silence warnings about unused labels, as we |
| 5004 | * don't actually do any test assertions in this function. */ |
| 5005 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5006 | } |
| 5007 | /* END_CASE */ |
| 5008 | |
| 5009 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5010 | void aead_multipart_decrypt(int key_type_arg, data_t *key_data, |
| 5011 | int alg_arg, |
| 5012 | data_t *nonce, |
| 5013 | data_t *additional_data, |
| 5014 | data_t *input_data, |
| 5015 | int do_set_lengths, |
| 5016 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5017 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5018 | size_t ad_part_len = 0; |
| 5019 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5020 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5021 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5022 | 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] | 5023 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5024 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5025 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5026 | if (do_set_lengths) { |
| 5027 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5028 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5029 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5030 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5031 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5032 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5033 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5034 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5035 | alg_arg, nonce, |
| 5036 | additional_data, |
| 5037 | ad_part_len, |
| 5038 | input_data, -1, |
| 5039 | set_lengths_method, |
| 5040 | expected_output, |
| 5041 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5042 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5043 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5044 | |
| 5045 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5046 | mbedtls_test_set_step(1000 + ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5047 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5048 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5049 | alg_arg, nonce, |
| 5050 | additional_data, |
| 5051 | ad_part_len, |
| 5052 | input_data, -1, |
| 5053 | set_lengths_method, |
| 5054 | expected_output, |
| 5055 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5056 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5057 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5058 | } |
| 5059 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5060 | 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] | 5061 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5062 | mbedtls_test_set_step(2000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5063 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5064 | if (do_set_lengths) { |
| 5065 | if (data_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5066 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5067 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5068 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5069 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5070 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5071 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5072 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5073 | alg_arg, nonce, |
| 5074 | additional_data, -1, |
| 5075 | input_data, data_part_len, |
| 5076 | set_lengths_method, |
| 5077 | expected_output, |
| 5078 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5079 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5080 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5081 | |
| 5082 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5083 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5084 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5085 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5086 | alg_arg, nonce, |
| 5087 | additional_data, -1, |
| 5088 | input_data, data_part_len, |
| 5089 | set_lengths_method, |
| 5090 | expected_output, |
| 5091 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5092 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5093 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5094 | } |
| 5095 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5096 | /* Goto is required to silence warnings about unused labels, as we |
| 5097 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5098 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5099 | } |
| 5100 | /* END_CASE */ |
| 5101 | |
| 5102 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5103 | void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data, |
| 5104 | int alg_arg, |
| 5105 | int nonce_length, |
| 5106 | int expected_nonce_length_arg, |
| 5107 | data_t *additional_data, |
| 5108 | data_t *input_data, |
| 5109 | int expected_status_arg) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5110 | { |
| 5111 | |
| 5112 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5113 | psa_key_type_t key_type = key_type_arg; |
| 5114 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5115 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5116 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5117 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5118 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5119 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5120 | size_t actual_nonce_length = 0; |
| 5121 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 5122 | unsigned char *output = NULL; |
| 5123 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5124 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5125 | size_t ciphertext_size = 0; |
| 5126 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5127 | size_t tag_length = 0; |
| 5128 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5129 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5130 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5131 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5132 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5133 | psa_set_key_algorithm(&attributes, alg); |
| 5134 | psa_set_key_type(&attributes, key_type); |
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 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5137 | &key)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5138 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5139 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5140 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5141 | 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] | 5142 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5143 | ASSERT_ALLOC(output, output_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5144 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5145 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5146 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5147 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5149 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5151 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5152 | |
| 5153 | /* If the operation is not supported, just skip and not fail in case the |
| 5154 | * encryption involves a common limitation of cryptography hardwares and |
| 5155 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5156 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5157 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5158 | 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] | 5159 | } |
| 5160 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5161 | PSA_ASSERT(status); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5162 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5163 | status = psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5164 | nonce_length, |
| 5165 | &actual_nonce_length); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5166 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5167 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5168 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5169 | TEST_EQUAL(actual_nonce_length, expected_nonce_length); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 5170 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5171 | if (expected_status == PSA_SUCCESS) { |
| 5172 | TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type, |
| 5173 | alg)); |
| 5174 | } |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 5175 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5176 | TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 5177 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5178 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5179 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5180 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5181 | input_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5183 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5184 | additional_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5185 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5186 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5187 | output, output_size, |
| 5188 | &ciphertext_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5189 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5190 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5191 | &ciphertext_length, tag_buffer, |
| 5192 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5193 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5194 | |
| 5195 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5196 | psa_destroy_key(key); |
| 5197 | mbedtls_free(output); |
| 5198 | mbedtls_free(ciphertext); |
| 5199 | psa_aead_abort(&operation); |
| 5200 | PSA_DONE(); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5201 | } |
| 5202 | /* END_CASE */ |
| 5203 | |
| 5204 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5205 | void aead_multipart_set_nonce(int key_type_arg, data_t *key_data, |
| 5206 | int alg_arg, |
| 5207 | int nonce_length_arg, |
| 5208 | int set_lengths_method_arg, |
| 5209 | data_t *additional_data, |
| 5210 | data_t *input_data, |
| 5211 | int expected_status_arg) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5212 | { |
| 5213 | |
| 5214 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5215 | psa_key_type_t key_type = key_type_arg; |
| 5216 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5217 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5218 | uint8_t *nonce_buffer = NULL; |
| 5219 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5220 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5221 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5222 | unsigned char *output = NULL; |
| 5223 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5224 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5225 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5226 | size_t ciphertext_size = 0; |
| 5227 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5228 | size_t tag_length = 0; |
| 5229 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5230 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5231 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5232 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5233 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5234 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5235 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5236 | psa_set_key_algorithm(&attributes, alg); |
| 5237 | psa_set_key_type(&attributes, key_type); |
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 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5240 | &key)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5241 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5242 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5243 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5244 | 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] | 5245 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5246 | ASSERT_ALLOC(output, output_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5247 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5248 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5249 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5250 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5252 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5253 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5254 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5255 | |
| 5256 | /* If the operation is not supported, just skip and not fail in case the |
| 5257 | * encryption involves a common limitation of cryptography hardwares and |
| 5258 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5259 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5260 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5261 | 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] | 5262 | } |
| 5263 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5264 | PSA_ASSERT(status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5265 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5266 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5267 | if (nonce_length_arg == -1) { |
| 5268 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
| 5269 | ASSERT_ALLOC(nonce_buffer, 4); |
| 5270 | nonce_length = 0; |
| 5271 | } else { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5272 | /* If length is zero, then this will return NULL. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5273 | nonce_length = (size_t) nonce_length_arg; |
| 5274 | ASSERT_ALLOC(nonce_buffer, nonce_length); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5275 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5276 | if (nonce_buffer) { |
| 5277 | for (index = 0; index < nonce_length - 1; ++index) { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5278 | nonce_buffer[index] = 'a' + index; |
| 5279 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5280 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5281 | } |
| 5282 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5283 | if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { |
| 5284 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5285 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5286 | } |
| 5287 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5288 | status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5289 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5290 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5291 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5292 | if (expected_status == PSA_SUCCESS) { |
| 5293 | if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) { |
| 5294 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5295 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5296 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5297 | 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] | 5298 | expected_status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5299 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5300 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5301 | /* 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] | 5302 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5303 | additional_data->len), |
| 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_update(&operation, input_data->x, input_data->len, |
| 5307 | output, output_size, |
| 5308 | &ciphertext_length), |
| 5309 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5310 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5311 | TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5312 | &ciphertext_length, tag_buffer, |
| 5313 | PSA_AEAD_TAG_MAX_SIZE, &tag_length), |
| 5314 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5315 | } |
| 5316 | |
| 5317 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5318 | psa_destroy_key(key); |
| 5319 | mbedtls_free(output); |
| 5320 | mbedtls_free(ciphertext); |
| 5321 | mbedtls_free(nonce_buffer); |
| 5322 | psa_aead_abort(&operation); |
| 5323 | PSA_DONE(); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5324 | } |
| 5325 | /* END_CASE */ |
| 5326 | |
| 5327 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5328 | 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] | 5329 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5330 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5331 | data_t *nonce, |
| 5332 | data_t *additional_data, |
| 5333 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5334 | int expected_status_arg) |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5335 | { |
| 5336 | |
| 5337 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5338 | psa_key_type_t key_type = key_type_arg; |
| 5339 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5340 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5341 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5342 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5343 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5344 | unsigned char *output = NULL; |
| 5345 | unsigned char *ciphertext = NULL; |
| 5346 | size_t output_size = output_size_arg; |
| 5347 | size_t ciphertext_size = 0; |
| 5348 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5349 | size_t tag_length = 0; |
| 5350 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5352 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5354 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5355 | psa_set_key_algorithm(&attributes, alg); |
| 5356 | psa_set_key_type(&attributes, key_type); |
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 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5359 | &key)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5360 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5361 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5362 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5363 | ASSERT_ALLOC(output, output_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5364 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5365 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5367 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5368 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5369 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5370 | |
| 5371 | /* If the operation is not supported, just skip and not fail in case the |
| 5372 | * encryption involves a common limitation of cryptography hardwares and |
| 5373 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5374 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5375 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5376 | 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] | 5377 | } |
| 5378 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5379 | PSA_ASSERT(status); |
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_set_lengths(&operation, additional_data->len, |
| 5382 | input_data->len)); |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5383 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5384 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5386 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5387 | additional_data->len)); |
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 | status = psa_aead_update(&operation, input_data->x, input_data->len, |
| 5390 | output, output_size, &ciphertext_length); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5392 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5393 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5394 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5395 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5396 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5397 | &ciphertext_length, tag_buffer, |
| 5398 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5399 | } |
| 5400 | |
| 5401 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5402 | psa_destroy_key(key); |
| 5403 | mbedtls_free(output); |
| 5404 | mbedtls_free(ciphertext); |
| 5405 | psa_aead_abort(&operation); |
| 5406 | PSA_DONE(); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5407 | } |
| 5408 | /* END_CASE */ |
| 5409 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5410 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5411 | void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data, |
| 5412 | int alg_arg, |
| 5413 | int finish_ciphertext_size_arg, |
| 5414 | int tag_size_arg, |
| 5415 | data_t *nonce, |
| 5416 | data_t *additional_data, |
| 5417 | data_t *input_data, |
| 5418 | int expected_status_arg) |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5419 | { |
| 5420 | |
| 5421 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5422 | psa_key_type_t key_type = key_type_arg; |
| 5423 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5424 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5425 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5426 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5427 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5428 | unsigned char *ciphertext = NULL; |
| 5429 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5430 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5431 | size_t ciphertext_size = 0; |
| 5432 | size_t ciphertext_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5433 | size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg; |
| 5434 | size_t tag_size = (size_t) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5435 | size_t tag_length = 0; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5436 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5437 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5438 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5439 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5440 | psa_set_key_algorithm(&attributes, alg); |
| 5441 | psa_set_key_type(&attributes, key_type); |
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 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5444 | &key)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5445 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5446 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5447 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5448 | 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] | 5449 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5450 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5451 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5452 | ASSERT_ALLOC(finish_ciphertext, finish_ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5453 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5454 | ASSERT_ALLOC(tag_buffer, tag_size); |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5455 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5456 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5457 | |
| 5458 | /* If the operation is not supported, just skip and not fail in case the |
| 5459 | * encryption involves a common limitation of cryptography hardwares and |
| 5460 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5461 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5462 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5463 | 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] | 5464 | } |
| 5465 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5466 | PSA_ASSERT(status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5467 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5468 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5469 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5470 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5471 | input_data->len)); |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5472 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5473 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5474 | additional_data->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5475 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5476 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5477 | ciphertext, ciphertext_size, &ciphertext_length)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5478 | |
| 5479 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5480 | status = psa_aead_finish(&operation, finish_ciphertext, |
| 5481 | finish_ciphertext_size, |
| 5482 | &ciphertext_length, tag_buffer, |
| 5483 | tag_size, &tag_length); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5484 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5485 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5486 | |
| 5487 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5488 | psa_destroy_key(key); |
| 5489 | mbedtls_free(ciphertext); |
| 5490 | mbedtls_free(finish_ciphertext); |
| 5491 | mbedtls_free(tag_buffer); |
| 5492 | psa_aead_abort(&operation); |
| 5493 | PSA_DONE(); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5494 | } |
| 5495 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5496 | |
| 5497 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5498 | void aead_multipart_verify(int key_type_arg, data_t *key_data, |
| 5499 | int alg_arg, |
| 5500 | data_t *nonce, |
| 5501 | data_t *additional_data, |
| 5502 | data_t *input_data, |
| 5503 | data_t *tag, |
| 5504 | int tag_usage_arg, |
| 5505 | int expected_setup_status_arg, |
| 5506 | int expected_status_arg) |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5507 | { |
| 5508 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5509 | psa_key_type_t key_type = key_type_arg; |
| 5510 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5511 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5512 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5513 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5514 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5515 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5516 | unsigned char *plaintext = NULL; |
| 5517 | unsigned char *finish_plaintext = NULL; |
| 5518 | size_t plaintext_size = 0; |
| 5519 | size_t plaintext_length = 0; |
| 5520 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5521 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5522 | unsigned char *tag_buffer = NULL; |
| 5523 | size_t tag_size = 0; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5524 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5525 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5526 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5527 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 5528 | psa_set_key_algorithm(&attributes, alg); |
| 5529 | psa_set_key_type(&attributes, key_type); |
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 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5532 | &key)); |
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 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
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 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 5537 | input_data->len); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5538 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5539 | ASSERT_ALLOC(plaintext, plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5540 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5541 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5542 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5543 | ASSERT_ALLOC(finish_plaintext, verify_plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5544 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5545 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5546 | |
| 5547 | /* If the operation is not supported, just skip and not fail in case the |
| 5548 | * encryption involves a common limitation of cryptography hardwares and |
| 5549 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5550 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5551 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5552 | 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] | 5553 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5554 | TEST_EQUAL(status, expected_setup_status); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5555 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5556 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5557 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5558 | } |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5559 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5560 | PSA_ASSERT(status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5562 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5563 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5564 | status = psa_aead_set_lengths(&operation, additional_data->len, |
| 5565 | input_data->len); |
| 5566 | PSA_ASSERT(status); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5567 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5568 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5569 | additional_data->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5570 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5571 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 5572 | input_data->len, |
| 5573 | plaintext, plaintext_size, |
| 5574 | &plaintext_length)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5575 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5576 | if (tag_usage == USE_GIVEN_TAG) { |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5577 | tag_buffer = tag->x; |
| 5578 | tag_size = tag->len; |
| 5579 | } |
| 5580 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5581 | status = psa_aead_verify(&operation, finish_plaintext, |
| 5582 | verify_plaintext_size, |
| 5583 | &plaintext_length, |
| 5584 | tag_buffer, tag_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5585 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5586 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5587 | |
| 5588 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5589 | psa_destroy_key(key); |
| 5590 | mbedtls_free(plaintext); |
| 5591 | mbedtls_free(finish_plaintext); |
| 5592 | psa_aead_abort(&operation); |
| 5593 | PSA_DONE(); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5594 | } |
| 5595 | /* END_CASE */ |
| 5596 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5597 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5598 | void aead_multipart_setup(int key_type_arg, data_t *key_data, |
| 5599 | int alg_arg, int expected_status_arg) |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5600 | { |
| 5601 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5602 | psa_key_type_t key_type = key_type_arg; |
| 5603 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5604 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5605 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5606 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5607 | psa_status_t expected_status = expected_status_arg; |
| 5608 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5609 | PSA_ASSERT(psa_crypto_init()); |
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_set_key_usage_flags(&attributes, |
| 5612 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5613 | psa_set_key_algorithm(&attributes, alg); |
| 5614 | psa_set_key_type(&attributes, key_type); |
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 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5617 | &key)); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5618 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5619 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5620 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5621 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5622 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5623 | psa_aead_abort(&operation); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5624 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5625 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5626 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5627 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5628 | |
| 5629 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5630 | psa_destroy_key(key); |
| 5631 | psa_aead_abort(&operation); |
| 5632 | PSA_DONE(); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5633 | } |
| 5634 | /* END_CASE */ |
| 5635 | |
| 5636 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5637 | void aead_multipart_state_test(int key_type_arg, data_t *key_data, |
| 5638 | int alg_arg, |
| 5639 | data_t *nonce, |
| 5640 | data_t *additional_data, |
| 5641 | data_t *input_data) |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5642 | { |
| 5643 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5644 | psa_key_type_t key_type = key_type_arg; |
| 5645 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5646 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5647 | unsigned char *output_data = NULL; |
| 5648 | unsigned char *final_data = NULL; |
| 5649 | size_t output_size = 0; |
| 5650 | size_t finish_output_size = 0; |
| 5651 | size_t output_length = 0; |
| 5652 | size_t key_bits = 0; |
| 5653 | size_t tag_length = 0; |
| 5654 | size_t tag_size = 0; |
| 5655 | size_t nonce_length = 0; |
| 5656 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5657 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5658 | size_t output_part_length = 0; |
| 5659 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5660 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5661 | PSA_ASSERT(psa_crypto_init()); |
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_set_key_usage_flags(&attributes, |
| 5664 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5665 | psa_set_key_algorithm(&attributes, alg); |
| 5666 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5667 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5668 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5669 | &key)); |
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 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5672 | key_bits = psa_get_key_bits(&attributes); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5673 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5674 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5676 | TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5677 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5678 | 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] | 5679 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5680 | ASSERT_ALLOC(output_data, output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5681 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5682 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5683 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5684 | TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5685 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5686 | ASSERT_ALLOC(final_data, finish_output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5687 | |
| 5688 | /* Test all operations error without calling setup first. */ |
| 5689 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5690 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5691 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5692 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5693 | psa_aead_abort(&operation); |
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 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5696 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5697 | &nonce_length), |
| 5698 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5699 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5700 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5701 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5702 | /* ------------------------------------------------------- */ |
| 5703 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5704 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 5705 | input_data->len), |
| 5706 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5707 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5708 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5709 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5710 | /* ------------------------------------------------------- */ |
| 5711 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5712 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5713 | additional_data->len), |
| 5714 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5715 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5716 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5717 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5718 | /* ------------------------------------------------------- */ |
| 5719 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5720 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 5721 | input_data->len, output_data, |
| 5722 | output_size, &output_length), |
| 5723 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5724 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5725 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5726 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5727 | /* ------------------------------------------------------- */ |
| 5728 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5729 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 5730 | finish_output_size, |
| 5731 | &output_part_length, |
| 5732 | tag_buffer, tag_length, |
| 5733 | &tag_size), |
| 5734 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5735 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5736 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5737 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5738 | /* ------------------------------------------------------- */ |
| 5739 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5740 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 5741 | finish_output_size, |
| 5742 | &output_part_length, |
| 5743 | tag_buffer, |
| 5744 | tag_length), |
| 5745 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5746 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5747 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5748 | |
| 5749 | /* Test for double setups. */ |
| 5750 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5751 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5752 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5753 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 5754 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5755 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5756 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5757 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5758 | /* ------------------------------------------------------- */ |
| 5759 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5760 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5761 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5762 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 5763 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5764 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5765 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5766 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5767 | /* ------------------------------------------------------- */ |
| 5768 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5769 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5771 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 5772 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5773 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5774 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5775 | |
| 5776 | /* ------------------------------------------------------- */ |
| 5777 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5778 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5779 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5780 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 5781 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5782 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5783 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5784 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5785 | /* Test for not setting a nonce. */ |
| 5786 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5787 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5788 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5789 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5790 | additional_data->len), |
| 5791 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5792 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5793 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5794 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5795 | /* ------------------------------------------------------- */ |
| 5796 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5797 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 5800 | input_data->len, output_data, |
| 5801 | output_size, &output_length), |
| 5802 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5803 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5804 | psa_aead_abort(&operation); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5805 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5806 | /* ------------------------------------------------------- */ |
| 5807 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5808 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5809 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5810 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 5811 | finish_output_size, |
| 5812 | &output_part_length, |
| 5813 | tag_buffer, tag_length, |
| 5814 | &tag_size), |
| 5815 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5816 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5817 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5818 | |
| 5819 | /* ------------------------------------------------------- */ |
| 5820 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5821 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5822 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5823 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 5824 | finish_output_size, |
| 5825 | &output_part_length, |
| 5826 | tag_buffer, |
| 5827 | tag_length), |
| 5828 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5829 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5830 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5831 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5832 | /* Test for double setting nonce. */ |
| 5833 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5834 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5837 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5838 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5839 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5840 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5841 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5842 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5843 | /* Test for double generating nonce. */ |
| 5844 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5845 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5846 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5847 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5848 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5849 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5850 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5851 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5852 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5853 | &nonce_length), |
| 5854 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5855 | |
| 5856 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5857 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5858 | |
| 5859 | /* Test for generate nonce then set and vice versa */ |
| 5860 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5861 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5862 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5863 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5864 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5865 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5866 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5867 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5868 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5869 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5870 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5871 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5872 | /* Test for generating nonce after calling set lengths */ |
| 5873 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5874 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5875 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5876 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5877 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5878 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5879 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5880 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5881 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5882 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5883 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5884 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5885 | /* 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] | 5886 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5887 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5888 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5889 | if (operation.alg == PSA_ALG_CCM) { |
| 5890 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5891 | input_data->len), |
| 5892 | PSA_ERROR_INVALID_ARGUMENT); |
| 5893 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5894 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5895 | &nonce_length), |
| 5896 | PSA_ERROR_BAD_STATE); |
| 5897 | } else { |
| 5898 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5899 | input_data->len)); |
| 5900 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5901 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5902 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5903 | } |
| 5904 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5905 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5906 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5907 | /* 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] | 5908 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5909 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5910 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5911 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 5912 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5913 | input_data->len), |
| 5914 | PSA_ERROR_INVALID_ARGUMENT); |
| 5915 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5916 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5917 | &nonce_length), |
| 5918 | PSA_ERROR_BAD_STATE); |
| 5919 | } else { |
| 5920 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5921 | input_data->len)); |
| 5922 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5923 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5924 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5925 | } |
| 5926 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5927 | psa_aead_abort(&operation); |
Dave Rodgman | d26d744 | 2023-02-11 17:14:54 +0000 | [diff] [blame] | 5928 | #endif |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5929 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5930 | /* 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] | 5931 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5932 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5933 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5934 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5935 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5936 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5937 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5938 | if (operation.alg == PSA_ALG_CCM) { |
| 5939 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5940 | input_data->len), |
| 5941 | PSA_ERROR_INVALID_ARGUMENT); |
| 5942 | } else { |
| 5943 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5944 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5945 | } |
| 5946 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5947 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5948 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5949 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5950 | /* Test for setting nonce after calling set lengths */ |
| 5951 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5952 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5955 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5956 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5957 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5958 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5959 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5960 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5961 | /* 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] | 5962 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5963 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5964 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5965 | if (operation.alg == PSA_ALG_CCM) { |
| 5966 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5967 | input_data->len), |
| 5968 | PSA_ERROR_INVALID_ARGUMENT); |
| 5969 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5970 | PSA_ERROR_BAD_STATE); |
| 5971 | } else { |
| 5972 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5973 | input_data->len)); |
| 5974 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5975 | } |
| 5976 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5977 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5978 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5979 | /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */ |
Dave Rodgman | a476363 | 2023-02-11 18:36:23 +0000 | [diff] [blame] | 5980 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5981 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5982 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5983 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 5984 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5985 | input_data->len), |
| 5986 | PSA_ERROR_INVALID_ARGUMENT); |
| 5987 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5988 | PSA_ERROR_BAD_STATE); |
| 5989 | } else { |
| 5990 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5991 | input_data->len)); |
| 5992 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5993 | } |
| 5994 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5995 | psa_aead_abort(&operation); |
Dave Rodgman | a476363 | 2023-02-11 18:36:23 +0000 | [diff] [blame] | 5996 | #endif |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5997 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5998 | /* 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] | 5999 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6000 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6001 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6002 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6003 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6004 | if (operation.alg == PSA_ALG_CCM) { |
| 6005 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6006 | input_data->len), |
| 6007 | PSA_ERROR_INVALID_ARGUMENT); |
| 6008 | } else { |
| 6009 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6010 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6011 | } |
| 6012 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6013 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6014 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6015 | /* 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] | 6016 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6017 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6018 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6019 | if (operation.alg == PSA_ALG_GCM) { |
| 6020 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6021 | SIZE_MAX), |
| 6022 | PSA_ERROR_INVALID_ARGUMENT); |
| 6023 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6024 | PSA_ERROR_BAD_STATE); |
| 6025 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6026 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6027 | SIZE_MAX)); |
| 6028 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6029 | } |
| 6030 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6031 | psa_aead_abort(&operation); |
Dave Rodgman | 91e8321 | 2023-02-11 20:07:43 +0000 | [diff] [blame] | 6032 | #endif |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6033 | |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 6034 | /* 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] | 6035 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6036 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6037 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6038 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6039 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6040 | if (operation.alg == PSA_ALG_GCM) { |
| 6041 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6042 | SIZE_MAX), |
| 6043 | PSA_ERROR_INVALID_ARGUMENT); |
| 6044 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6045 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6046 | SIZE_MAX)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6047 | } |
| 6048 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6049 | psa_aead_abort(&operation); |
Dave Rodgman | 641288b | 2023-02-11 22:02:04 +0000 | [diff] [blame] | 6050 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6051 | |
| 6052 | /* ------------------------------------------------------- */ |
| 6053 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6054 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6055 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6056 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
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 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6059 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6060 | &nonce_length), |
| 6061 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6062 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6063 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6064 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6065 | /* Test for generating nonce in decrypt setup. */ |
| 6066 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6067 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
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 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6070 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6071 | &nonce_length), |
| 6072 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6073 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6074 | psa_aead_abort(&operation); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6075 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6076 | /* Test for setting lengths twice. */ |
| 6077 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6078 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6081 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6082 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6083 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6084 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6085 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6086 | input_data->len), |
| 6087 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6088 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6089 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6090 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6091 | /* Test for setting lengths after setting nonce + already starting data. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6092 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6093 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6094 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6095 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6096 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6097 | if (operation.alg == PSA_ALG_CCM) { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6098 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6099 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6100 | additional_data->len), |
| 6101 | PSA_ERROR_BAD_STATE); |
| 6102 | } else { |
| 6103 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6104 | additional_data->len)); |
| 6105 | |
| 6106 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6107 | input_data->len), |
| 6108 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6109 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6110 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6111 | |
| 6112 | /* ------------------------------------------------------- */ |
| 6113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6114 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6115 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6116 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6117 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6118 | if (operation.alg == PSA_ALG_CCM) { |
| 6119 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6120 | input_data->len, output_data, |
| 6121 | output_size, &output_length), |
| 6122 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6123 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6124 | } else { |
| 6125 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6126 | input_data->len, output_data, |
| 6127 | output_size, &output_length)); |
| 6128 | |
| 6129 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6130 | input_data->len), |
| 6131 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6132 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6133 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6134 | |
| 6135 | /* ------------------------------------------------------- */ |
| 6136 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6137 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6138 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6139 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6140 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6141 | if (operation.alg == PSA_ALG_CCM) { |
| 6142 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6143 | finish_output_size, |
| 6144 | &output_part_length, |
| 6145 | tag_buffer, tag_length, |
| 6146 | &tag_size)); |
| 6147 | } else { |
| 6148 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6149 | finish_output_size, |
| 6150 | &output_part_length, |
| 6151 | tag_buffer, tag_length, |
| 6152 | &tag_size)); |
| 6153 | |
| 6154 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6155 | input_data->len), |
| 6156 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6157 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6158 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6159 | |
| 6160 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 6161 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6162 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6165 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6166 | &nonce_length)); |
| 6167 | if (operation.alg == PSA_ALG_CCM) { |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6168 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6169 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6170 | additional_data->len), |
| 6171 | PSA_ERROR_BAD_STATE); |
| 6172 | } else { |
| 6173 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6174 | additional_data->len)); |
| 6175 | |
| 6176 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6177 | input_data->len), |
| 6178 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6179 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6180 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6181 | |
| 6182 | /* ------------------------------------------------------- */ |
| 6183 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6184 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6185 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6186 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6187 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6188 | &nonce_length)); |
| 6189 | if (operation.alg == PSA_ALG_CCM) { |
| 6190 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6191 | input_data->len, output_data, |
| 6192 | output_size, &output_length), |
| 6193 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6194 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6195 | } else { |
| 6196 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6197 | input_data->len, output_data, |
| 6198 | output_size, &output_length)); |
| 6199 | |
| 6200 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6201 | input_data->len), |
| 6202 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6203 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6204 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6205 | |
| 6206 | /* ------------------------------------------------------- */ |
| 6207 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6208 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6209 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6210 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6211 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6212 | &nonce_length)); |
| 6213 | if (operation.alg == PSA_ALG_CCM) { |
| 6214 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6215 | finish_output_size, |
| 6216 | &output_part_length, |
| 6217 | tag_buffer, tag_length, |
| 6218 | &tag_size)); |
| 6219 | } else { |
| 6220 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6221 | finish_output_size, |
| 6222 | &output_part_length, |
| 6223 | tag_buffer, tag_length, |
| 6224 | &tag_size)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6225 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6226 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6227 | input_data->len), |
| 6228 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6229 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6230 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6231 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6232 | /* Test for not sending any additional data or data after setting non zero |
| 6233 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6234 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6235 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6236 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6237 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6238 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6239 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6240 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6241 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6242 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6243 | finish_output_size, |
| 6244 | &output_part_length, |
| 6245 | tag_buffer, tag_length, |
| 6246 | &tag_size), |
| 6247 | PSA_ERROR_INVALID_ARGUMENT); |
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_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6250 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6251 | /* Test for not sending any additional data or data after setting non-zero |
| 6252 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6253 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6254 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6256 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6257 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6258 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6259 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6260 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6261 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6262 | finish_output_size, |
| 6263 | &output_part_length, |
| 6264 | tag_buffer, |
| 6265 | tag_length), |
| 6266 | PSA_ERROR_INVALID_ARGUMENT); |
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_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6269 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6270 | /* Test for not sending any additional data after setting a non-zero length |
| 6271 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6272 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6273 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6274 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6275 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6276 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6277 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6278 | input_data->len)); |
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 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6281 | input_data->len, output_data, |
| 6282 | output_size, &output_length), |
| 6283 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6284 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6285 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6286 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6287 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 6288 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6289 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6292 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6293 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6294 | input_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6295 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6296 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6297 | additional_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6298 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6299 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6300 | finish_output_size, |
| 6301 | &output_part_length, |
| 6302 | tag_buffer, tag_length, |
| 6303 | &tag_size), |
| 6304 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6306 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6307 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6308 | /* Test for sending too much additional data after setting lengths. */ |
| 6309 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6310 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6311 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6312 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6314 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6315 | |
| 6316 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6317 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6318 | additional_data->len), |
| 6319 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6320 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6321 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6322 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6323 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6324 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6325 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6328 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6329 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6330 | input_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6331 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6332 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6333 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6334 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6335 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6336 | 1), |
| 6337 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6338 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6339 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6340 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6341 | /* Test for sending too much data after setting lengths. */ |
| 6342 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6343 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6344 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6345 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6346 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6347 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
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 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6350 | input_data->len, output_data, |
| 6351 | output_size, &output_length), |
| 6352 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6354 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6355 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6356 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6358 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6361 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6362 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6363 | input_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6364 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6365 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6366 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6367 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6368 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6369 | input_data->len, output_data, |
| 6370 | output_size, &output_length)); |
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 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6373 | 1, output_data, |
| 6374 | output_size, &output_length), |
| 6375 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6377 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6378 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6379 | /* Test sending additional data after data. */ |
| 6380 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6381 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6382 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6383 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
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 | if (operation.alg != PSA_ALG_CCM) { |
| 6386 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6387 | input_data->len, output_data, |
| 6388 | output_size, &output_length)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6389 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6390 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6391 | additional_data->len), |
| 6392 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6393 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6394 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6395 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6396 | /* Test calling finish on decryption. */ |
| 6397 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6398 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6399 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6400 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6401 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6402 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6403 | finish_output_size, |
| 6404 | &output_part_length, |
| 6405 | tag_buffer, tag_length, |
| 6406 | &tag_size), |
| 6407 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6408 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6409 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6410 | |
| 6411 | /* Test calling verify on encryption. */ |
| 6412 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6413 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6414 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6415 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6416 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6417 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6418 | finish_output_size, |
| 6419 | &output_part_length, |
| 6420 | tag_buffer, |
| 6421 | tag_length), |
| 6422 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6423 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6424 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6425 | |
| 6426 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6427 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6428 | psa_destroy_key(key); |
| 6429 | psa_aead_abort(&operation); |
| 6430 | mbedtls_free(output_data); |
| 6431 | mbedtls_free(final_data); |
| 6432 | PSA_DONE(); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6433 | } |
| 6434 | /* END_CASE */ |
| 6435 | |
| 6436 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6437 | void signature_size(int type_arg, |
| 6438 | int bits, |
| 6439 | int alg_arg, |
| 6440 | int expected_size_arg) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6441 | { |
| 6442 | psa_key_type_t type = type_arg; |
| 6443 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6444 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6445 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6446 | TEST_EQUAL(actual_size, (size_t) expected_size_arg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6447 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6448 | exit: |
| 6449 | ; |
| 6450 | } |
| 6451 | /* END_CASE */ |
| 6452 | |
| 6453 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6454 | void sign_hash_deterministic(int key_type_arg, data_t *key_data, |
| 6455 | int alg_arg, data_t *input_data, |
| 6456 | data_t *output_data) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6457 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6458 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6459 | psa_key_type_t key_type = key_type_arg; |
| 6460 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6461 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6462 | unsigned char *signature = NULL; |
| 6463 | size_t signature_size; |
| 6464 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6465 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6466 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6467 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6468 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6469 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6470 | psa_set_key_algorithm(&attributes, alg); |
| 6471 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6472 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6473 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6474 | &key)); |
| 6475 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6476 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6477 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6478 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6479 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6480 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6481 | key_bits, alg); |
| 6482 | TEST_ASSERT(signature_size != 0); |
| 6483 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6484 | ASSERT_ALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6485 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6486 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6487 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6488 | input_data->x, input_data->len, |
| 6489 | signature, signature_size, |
| 6490 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6491 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6492 | ASSERT_COMPARE(output_data->x, output_data->len, |
| 6493 | signature, signature_length); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6494 | |
| 6495 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6496 | /* |
| 6497 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6498 | * thus reset them as required. |
| 6499 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6500 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6501 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6502 | psa_destroy_key(key); |
| 6503 | mbedtls_free(signature); |
| 6504 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6505 | } |
| 6506 | /* END_CASE */ |
| 6507 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6508 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6509 | /** |
| 6510 | * sign_hash_interruptible() test intentions: |
| 6511 | * |
| 6512 | * Note: This test can currently only handle ECDSA. |
| 6513 | * |
| 6514 | * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 6515 | * and private keys / keypairs only). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6516 | * |
| 6517 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 6518 | * expected for different max_ops values. |
| 6519 | * |
| 6520 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 6521 | * and that each successful stage completes some ops (this is not mandated by |
| 6522 | * the PSA specification, but is currently the case). |
| 6523 | * |
| 6524 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 6525 | * complete() calls does not alter the number of ops returned. |
| 6526 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6527 | void sign_hash_interruptible(int key_type_arg, data_t *key_data, |
| 6528 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6529 | data_t *output_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6530 | { |
| 6531 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6532 | psa_key_type_t key_type = key_type_arg; |
| 6533 | psa_algorithm_t alg = alg_arg; |
| 6534 | size_t key_bits; |
| 6535 | unsigned char *signature = NULL; |
| 6536 | size_t signature_size; |
| 6537 | size_t signature_length = 0xdeadbeef; |
| 6538 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6539 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6540 | uint32_t num_ops = 0; |
| 6541 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6542 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6543 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6544 | size_t min_completes = 0; |
| 6545 | size_t max_completes = 0; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6546 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6547 | psa_sign_hash_interruptible_operation_t operation = |
| 6548 | psa_sign_hash_interruptible_operation_init(); |
| 6549 | |
| 6550 | PSA_ASSERT(psa_crypto_init()); |
| 6551 | |
| 6552 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6553 | psa_set_key_algorithm(&attributes, alg); |
| 6554 | psa_set_key_type(&attributes, key_type); |
| 6555 | |
| 6556 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6557 | &key)); |
| 6558 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6559 | key_bits = psa_get_key_bits(&attributes); |
| 6560 | |
| 6561 | /* Allocate a buffer which has the size advertised by the |
| 6562 | * library. */ |
| 6563 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6564 | key_bits, alg); |
| 6565 | TEST_ASSERT(signature_size != 0); |
| 6566 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6567 | ASSERT_ALLOC(signature, signature_size); |
| 6568 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6569 | psa_interruptible_set_max_ops(max_ops); |
| 6570 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6571 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6572 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6573 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6574 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6575 | TEST_ASSERT(num_ops_prior == 0); |
| 6576 | |
| 6577 | /* Start performing the signature. */ |
| 6578 | PSA_ASSERT(psa_sign_hash_start(&operation, key, alg, |
| 6579 | input_data->x, input_data->len)); |
| 6580 | |
| 6581 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6582 | TEST_ASSERT(num_ops_prior == 0); |
| 6583 | |
| 6584 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6585 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6586 | status = psa_sign_hash_complete(&operation, signature, signature_size, |
| 6587 | &signature_length); |
| 6588 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6589 | num_completes++; |
| 6590 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6591 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 6592 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6593 | /* We are asserting here that every complete makes progress |
| 6594 | * (completes some ops), which is true of the internal |
| 6595 | * implementation and probably any implementation, however this is |
| 6596 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6597 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6598 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6599 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 6600 | |
| 6601 | /* Ensure calling get_num_ops() twice still returns the same |
| 6602 | * number of ops as previously reported. */ |
| 6603 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6604 | |
| 6605 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6606 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6607 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6608 | |
| 6609 | TEST_ASSERT(status == PSA_SUCCESS); |
| 6610 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6611 | TEST_LE_U(min_completes, num_completes); |
| 6612 | TEST_LE_U(num_completes, max_completes); |
| 6613 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6614 | /* Verify that the signature is what is expected. */ |
| 6615 | ASSERT_COMPARE(output_data->x, output_data->len, |
| 6616 | signature, signature_length); |
| 6617 | |
| 6618 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6619 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6620 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6621 | TEST_ASSERT(num_ops == 0); |
| 6622 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6623 | exit: |
| 6624 | |
| 6625 | /* |
| 6626 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6627 | * thus reset them as required. |
| 6628 | */ |
| 6629 | psa_reset_key_attributes(&attributes); |
| 6630 | |
| 6631 | psa_destroy_key(key); |
| 6632 | mbedtls_free(signature); |
| 6633 | PSA_DONE(); |
| 6634 | } |
| 6635 | /* END_CASE */ |
| 6636 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6637 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6638 | void sign_hash_fail(int key_type_arg, data_t *key_data, |
| 6639 | int alg_arg, data_t *input_data, |
| 6640 | int signature_size_arg, int expected_status_arg) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6641 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6642 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6643 | psa_key_type_t key_type = key_type_arg; |
| 6644 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6645 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6646 | psa_status_t actual_status; |
| 6647 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 6648 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 6649 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6650 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6651 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6652 | ASSERT_ALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6653 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6654 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6655 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6656 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6657 | psa_set_key_algorithm(&attributes, alg); |
| 6658 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6659 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6660 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6661 | &key)); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6662 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6663 | actual_status = psa_sign_hash(key, alg, |
| 6664 | input_data->x, input_data->len, |
| 6665 | signature, signature_size, |
| 6666 | &signature_length); |
| 6667 | TEST_EQUAL(actual_status, expected_status); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6668 | /* The value of *signature_length is unspecified on error, but |
| 6669 | * whatever it is, it should be less than signature_size, so that |
| 6670 | * if the caller tries to read *signature_length bytes without |
| 6671 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6672 | TEST_LE_U(signature_length, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6673 | |
| 6674 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6675 | psa_reset_key_attributes(&attributes); |
| 6676 | psa_destroy_key(key); |
| 6677 | mbedtls_free(signature); |
| 6678 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6679 | } |
| 6680 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6681 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6682 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6683 | /** |
| 6684 | * sign_hash_fail_interruptible() test intentions: |
| 6685 | * |
| 6686 | * Note: This test can currently only handle ECDSA. |
| 6687 | * |
| 6688 | * 1. Test that various failure cases for interruptible sign hash fail with the |
| 6689 | * correct error codes, and at the correct point (at start or during |
| 6690 | * complete). |
| 6691 | * |
| 6692 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 6693 | * expected for different max_ops values. |
| 6694 | * |
| 6695 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 6696 | * and that each successful stage completes some ops (this is not mandated by |
| 6697 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 6698 | * |
| 6699 | * 4. Check that calling complete() when start() fails and complete() |
| 6700 | * after completion results in a BAD_STATE error. |
| 6701 | * |
| 6702 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 6703 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6704 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6705 | void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 6706 | int alg_arg, data_t *input_data, |
| 6707 | int signature_size_arg, |
| 6708 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6709 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6710 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6711 | { |
| 6712 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6713 | psa_key_type_t key_type = key_type_arg; |
| 6714 | psa_algorithm_t alg = alg_arg; |
| 6715 | size_t signature_size = signature_size_arg; |
| 6716 | psa_status_t actual_status; |
| 6717 | psa_status_t expected_start_status = expected_start_status_arg; |
| 6718 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 6719 | unsigned char *signature = NULL; |
| 6720 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6721 | uint32_t num_ops = 0; |
| 6722 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6723 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6724 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6725 | size_t min_completes = 0; |
| 6726 | size_t max_completes = 0; |
| 6727 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6728 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6729 | psa_sign_hash_interruptible_operation_t operation = |
| 6730 | psa_sign_hash_interruptible_operation_init(); |
| 6731 | |
| 6732 | ASSERT_ALLOC(signature, signature_size); |
| 6733 | |
| 6734 | PSA_ASSERT(psa_crypto_init()); |
| 6735 | |
| 6736 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6737 | psa_set_key_algorithm(&attributes, alg); |
| 6738 | psa_set_key_type(&attributes, key_type); |
| 6739 | |
| 6740 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6741 | &key)); |
| 6742 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6743 | psa_interruptible_set_max_ops(max_ops); |
| 6744 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6745 | interruptible_signverify_get_minmax_completes(max_ops, |
| 6746 | expected_complete_status, |
| 6747 | &min_completes, |
| 6748 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6749 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6750 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6751 | TEST_ASSERT(num_ops_prior == 0); |
| 6752 | |
| 6753 | /* Start performing the signature. */ |
| 6754 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 6755 | input_data->x, input_data->len); |
| 6756 | |
| 6757 | TEST_EQUAL(actual_status, expected_start_status); |
| 6758 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6759 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 6760 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 6761 | * start failed. */ |
| 6762 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6763 | signature_size, |
| 6764 | &signature_length); |
| 6765 | |
| 6766 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 6767 | |
| 6768 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6769 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 6770 | input_data->x, input_data->len); |
| 6771 | |
| 6772 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 6773 | } |
| 6774 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6775 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6776 | TEST_ASSERT(num_ops_prior == 0); |
| 6777 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6778 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6779 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6780 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6781 | signature_size, |
| 6782 | &signature_length); |
| 6783 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6784 | num_completes++; |
| 6785 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6786 | if (actual_status == PSA_SUCCESS || |
| 6787 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6788 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6789 | /* We are asserting here that every complete makes progress |
| 6790 | * (completes some ops), which is true of the internal |
| 6791 | * implementation and probably any implementation, however this is |
| 6792 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6793 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6794 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6795 | num_ops_prior = num_ops; |
| 6796 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6797 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6798 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6799 | TEST_EQUAL(actual_status, expected_complete_status); |
| 6800 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 6801 | /* Check that another complete returns BAD_STATE. */ |
| 6802 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6803 | signature_size, |
| 6804 | &signature_length); |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6805 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 6806 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6807 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6808 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6809 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6810 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6811 | TEST_ASSERT(num_ops == 0); |
| 6812 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6813 | /* The value of *signature_length is unspecified on error, but |
| 6814 | * whatever it is, it should be less than signature_size, so that |
| 6815 | * if the caller tries to read *signature_length bytes without |
| 6816 | * checking the error code then they don't overflow a buffer. */ |
| 6817 | TEST_LE_U(signature_length, signature_size); |
| 6818 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6819 | TEST_LE_U(min_completes, num_completes); |
| 6820 | TEST_LE_U(num_completes, max_completes); |
| 6821 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6822 | exit: |
| 6823 | psa_reset_key_attributes(&attributes); |
| 6824 | psa_destroy_key(key); |
| 6825 | mbedtls_free(signature); |
| 6826 | PSA_DONE(); |
| 6827 | } |
| 6828 | /* END_CASE */ |
| 6829 | |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6830 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6831 | void sign_verify_hash(int key_type_arg, data_t *key_data, |
| 6832 | int alg_arg, data_t *input_data) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6833 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6834 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6835 | psa_key_type_t key_type = key_type_arg; |
| 6836 | psa_algorithm_t alg = alg_arg; |
| 6837 | size_t key_bits; |
| 6838 | unsigned char *signature = NULL; |
| 6839 | size_t signature_size; |
| 6840 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6841 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6842 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6843 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6844 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6845 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 6846 | psa_set_key_algorithm(&attributes, alg); |
| 6847 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6848 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6849 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6850 | &key)); |
| 6851 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6852 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6853 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6854 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6855 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6856 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6857 | key_bits, alg); |
| 6858 | TEST_ASSERT(signature_size != 0); |
| 6859 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6860 | ASSERT_ALLOC(signature, signature_size); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6861 | |
| 6862 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6863 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6864 | input_data->x, input_data->len, |
| 6865 | signature, signature_size, |
| 6866 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6867 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6868 | TEST_LE_U(signature_length, signature_size); |
| 6869 | TEST_ASSERT(signature_length > 0); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6870 | |
| 6871 | /* Use the library to verify that the signature is correct. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6872 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 6873 | input_data->x, input_data->len, |
| 6874 | signature, signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6875 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6876 | if (input_data->len != 0) { |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6877 | /* Flip a bit in the input and verify that the signature is now |
| 6878 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6879 | * because ECDSA may ignore the last few bits of the input. */ |
| 6880 | input_data->x[0] ^= 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6881 | TEST_EQUAL(psa_verify_hash(key, alg, |
| 6882 | input_data->x, input_data->len, |
| 6883 | signature, signature_length), |
| 6884 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6885 | } |
| 6886 | |
| 6887 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6888 | /* |
| 6889 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6890 | * thus reset them as required. |
| 6891 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6892 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6893 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6894 | psa_destroy_key(key); |
| 6895 | mbedtls_free(signature); |
| 6896 | PSA_DONE(); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6897 | } |
| 6898 | /* END_CASE */ |
| 6899 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6900 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6901 | /** |
| 6902 | * sign_verify_hash_interruptible() test intentions: |
| 6903 | * |
| 6904 | * Note: This test can currently only handle ECDSA. |
| 6905 | * |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 6906 | * 1. Test that we can sign an input hash with the given keypair and then |
| 6907 | * afterwards verify that signature. This is currently the only way to test |
| 6908 | * non deterministic ECDSA, but this test can also handle deterministic. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6909 | * |
| 6910 | * 2. Test that after corrupting the hash, the verification detects an invalid |
| 6911 | * signature. |
| 6912 | * |
| 6913 | * 3. Test the number of calls to psa_sign_hash_complete() required are as |
| 6914 | * expected for different max_ops values. |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6915 | * |
| 6916 | * 4. Test that the number of ops done prior to starting signing and after abort |
| 6917 | * is zero and that each successful signing stage completes some ops (this is |
| 6918 | * not mandated by the PSA specification, but is currently the case). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6919 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6920 | 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] | 6921 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6922 | int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6923 | { |
| 6924 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6925 | psa_key_type_t key_type = key_type_arg; |
| 6926 | psa_algorithm_t alg = alg_arg; |
| 6927 | size_t key_bits; |
| 6928 | unsigned char *signature = NULL; |
| 6929 | size_t signature_size; |
| 6930 | size_t signature_length = 0xdeadbeef; |
| 6931 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6932 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6933 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6934 | uint32_t num_ops = 0; |
| 6935 | uint32_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6936 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6937 | size_t min_completes = 0; |
| 6938 | size_t max_completes = 0; |
| 6939 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6940 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 6941 | psa_sign_hash_interruptible_operation_init(); |
| 6942 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 6943 | psa_verify_hash_interruptible_operation_init(); |
| 6944 | |
| 6945 | PSA_ASSERT(psa_crypto_init()); |
| 6946 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6947 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 6948 | PSA_KEY_USAGE_VERIFY_HASH); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6949 | psa_set_key_algorithm(&attributes, alg); |
| 6950 | psa_set_key_type(&attributes, key_type); |
| 6951 | |
| 6952 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6953 | &key)); |
| 6954 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6955 | key_bits = psa_get_key_bits(&attributes); |
| 6956 | |
| 6957 | /* Allocate a buffer which has the size advertised by the |
| 6958 | * library. */ |
| 6959 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6960 | key_bits, alg); |
| 6961 | TEST_ASSERT(signature_size != 0); |
| 6962 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6963 | ASSERT_ALLOC(signature, signature_size); |
| 6964 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6965 | psa_interruptible_set_max_ops(max_ops); |
| 6966 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6967 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6968 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6969 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6970 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); |
| 6971 | TEST_ASSERT(num_ops_prior == 0); |
| 6972 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6973 | /* Start performing the signature. */ |
| 6974 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 6975 | input_data->x, input_data->len)); |
| 6976 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6977 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); |
| 6978 | TEST_ASSERT(num_ops_prior == 0); |
| 6979 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6980 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6981 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6982 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6983 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 6984 | signature_size, |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6985 | &signature_length); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6986 | |
| 6987 | num_completes++; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6988 | |
| 6989 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 6990 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 6991 | /* We are asserting here that every complete makes progress |
| 6992 | * (completes some ops), which is true of the internal |
| 6993 | * implementation and probably any implementation, however this is |
| 6994 | * not mandated by the PSA specification. */ |
| 6995 | TEST_ASSERT(num_ops > num_ops_prior); |
| 6996 | |
| 6997 | num_ops_prior = num_ops; |
| 6998 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6999 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7000 | |
| 7001 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7002 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7003 | TEST_LE_U(min_completes, num_completes); |
| 7004 | TEST_LE_U(num_completes, max_completes); |
| 7005 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7006 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7007 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7008 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7009 | TEST_ASSERT(num_ops == 0); |
| 7010 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7011 | /* Check that the signature length looks sensible. */ |
| 7012 | TEST_LE_U(signature_length, signature_size); |
| 7013 | TEST_ASSERT(signature_length > 0); |
| 7014 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7015 | num_completes = 0; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7016 | |
| 7017 | /* Start verification. */ |
| 7018 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7019 | input_data->x, input_data->len, |
| 7020 | signature, signature_length)); |
| 7021 | |
| 7022 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7023 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7024 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7025 | |
| 7026 | num_completes++; |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7027 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7028 | |
| 7029 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7030 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7031 | TEST_LE_U(min_completes, num_completes); |
| 7032 | TEST_LE_U(num_completes, max_completes); |
| 7033 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7034 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7035 | |
| 7036 | verify_operation = psa_verify_hash_interruptible_operation_init(); |
| 7037 | |
| 7038 | if (input_data->len != 0) { |
| 7039 | /* Flip a bit in the input and verify that the signature is now |
| 7040 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 7041 | * because ECDSA may ignore the last few bits of the input. */ |
| 7042 | input_data->x[0] ^= 1; |
| 7043 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7044 | /* Start verification. */ |
| 7045 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7046 | input_data->x, input_data->len, |
| 7047 | signature, signature_length)); |
| 7048 | |
| 7049 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7050 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7051 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7052 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7053 | |
| 7054 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7055 | } |
| 7056 | |
| 7057 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7058 | |
| 7059 | exit: |
| 7060 | /* |
| 7061 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7062 | * thus reset them as required. |
| 7063 | */ |
| 7064 | psa_reset_key_attributes(&attributes); |
| 7065 | |
| 7066 | psa_destroy_key(key); |
| 7067 | mbedtls_free(signature); |
| 7068 | PSA_DONE(); |
| 7069 | } |
| 7070 | /* END_CASE */ |
| 7071 | |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7072 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7073 | void verify_hash(int key_type_arg, data_t *key_data, |
| 7074 | int alg_arg, data_t *hash_data, |
| 7075 | data_t *signature_data) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7076 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7077 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7078 | psa_key_type_t key_type = key_type_arg; |
| 7079 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7080 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7081 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7082 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 7083 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7084 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7085 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7086 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7087 | psa_set_key_algorithm(&attributes, alg); |
| 7088 | psa_set_key_type(&attributes, key_type); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7089 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7090 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7091 | &key)); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7092 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7093 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 7094 | hash_data->x, hash_data->len, |
| 7095 | signature_data->x, signature_data->len)); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 7096 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7097 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7098 | psa_reset_key_attributes(&attributes); |
| 7099 | psa_destroy_key(key); |
| 7100 | PSA_DONE(); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7101 | } |
| 7102 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7103 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7104 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7105 | /** |
| 7106 | * verify_hash_interruptible() test intentions: |
| 7107 | * |
| 7108 | * Note: This test can currently only handle ECDSA. |
| 7109 | * |
| 7110 | * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 7111 | * only). Given this test only does verification it can accept public keys as |
| 7112 | * well as private keys / keypairs. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7113 | * |
| 7114 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7115 | * expected for different max_ops values. |
| 7116 | * |
| 7117 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7118 | * and that each successful stage completes some ops (this is not mandated by |
| 7119 | * the PSA specification, but is currently the case). |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7120 | * |
| 7121 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 7122 | * complete() calls does not alter the number of ops returned. |
| 7123 | * |
| 7124 | * 5. Test that after corrupting the hash, the verification detects an invalid |
| 7125 | * signature. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7126 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7127 | void verify_hash_interruptible(int key_type_arg, data_t *key_data, |
| 7128 | int alg_arg, data_t *hash_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7129 | data_t *signature_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7130 | { |
| 7131 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7132 | psa_key_type_t key_type = key_type_arg; |
| 7133 | psa_algorithm_t alg = alg_arg; |
| 7134 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7135 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7136 | uint32_t num_ops = 0; |
| 7137 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7138 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7139 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7140 | size_t min_completes = 0; |
| 7141 | size_t max_completes = 0; |
| 7142 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7143 | psa_verify_hash_interruptible_operation_t operation = |
| 7144 | psa_verify_hash_interruptible_operation_init(); |
| 7145 | |
| 7146 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 7147 | |
| 7148 | PSA_ASSERT(psa_crypto_init()); |
| 7149 | |
| 7150 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7151 | psa_set_key_algorithm(&attributes, alg); |
| 7152 | psa_set_key_type(&attributes, key_type); |
| 7153 | |
| 7154 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7155 | &key)); |
| 7156 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7157 | psa_interruptible_set_max_ops(max_ops); |
| 7158 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7159 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 7160 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7161 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7162 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7163 | |
| 7164 | TEST_ASSERT(num_ops_prior == 0); |
| 7165 | |
| 7166 | /* Start verification. */ |
| 7167 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7168 | hash_data->x, hash_data->len, |
| 7169 | signature_data->x, signature_data->len) |
| 7170 | ); |
| 7171 | |
| 7172 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7173 | |
| 7174 | TEST_ASSERT(num_ops_prior == 0); |
| 7175 | |
| 7176 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7177 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7178 | status = psa_verify_hash_complete(&operation); |
| 7179 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7180 | num_completes++; |
| 7181 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7182 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 7183 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7184 | /* We are asserting here that every complete makes progress |
| 7185 | * (completes some ops), which is true of the internal |
| 7186 | * implementation and probably any implementation, however this is |
| 7187 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7188 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7189 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7190 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 7191 | |
| 7192 | /* Ensure calling get_num_ops() twice still returns the same |
| 7193 | * number of ops as previously reported. */ |
| 7194 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7195 | |
| 7196 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7197 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7198 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7199 | |
| 7200 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7201 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7202 | TEST_LE_U(min_completes, num_completes); |
| 7203 | TEST_LE_U(num_completes, max_completes); |
| 7204 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7205 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7206 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7207 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7208 | TEST_ASSERT(num_ops == 0); |
| 7209 | |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7210 | if (hash_data->len != 0) { |
| 7211 | /* Flip a bit in the hash and verify that the signature is now detected |
| 7212 | * as invalid. Flip a bit at the beginning, not at the end, because |
| 7213 | * ECDSA may ignore the last few bits of the input. */ |
| 7214 | hash_data->x[0] ^= 1; |
| 7215 | |
| 7216 | /* Start verification. */ |
| 7217 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7218 | hash_data->x, hash_data->len, |
| 7219 | signature_data->x, signature_data->len)); |
| 7220 | |
| 7221 | /* Continue performing the signature until complete. */ |
| 7222 | do { |
| 7223 | status = psa_verify_hash_complete(&operation); |
| 7224 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7225 | |
| 7226 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7227 | } |
| 7228 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7229 | exit: |
| 7230 | psa_reset_key_attributes(&attributes); |
| 7231 | psa_destroy_key(key); |
| 7232 | PSA_DONE(); |
| 7233 | } |
| 7234 | /* END_CASE */ |
| 7235 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7236 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7237 | void verify_hash_fail(int key_type_arg, data_t *key_data, |
| 7238 | int alg_arg, data_t *hash_data, |
| 7239 | data_t *signature_data, |
| 7240 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7241 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7242 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7243 | psa_key_type_t key_type = key_type_arg; |
| 7244 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7245 | psa_status_t actual_status; |
| 7246 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7247 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7248 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7249 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7250 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7251 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7252 | psa_set_key_algorithm(&attributes, alg); |
| 7253 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7254 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7255 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7256 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7257 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7258 | actual_status = psa_verify_hash(key, alg, |
| 7259 | hash_data->x, hash_data->len, |
| 7260 | signature_data->x, signature_data->len); |
| 7261 | TEST_EQUAL(actual_status, expected_status); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7262 | |
| 7263 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7264 | psa_reset_key_attributes(&attributes); |
| 7265 | psa_destroy_key(key); |
| 7266 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7267 | } |
| 7268 | /* END_CASE */ |
| 7269 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7270 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7271 | /** |
| 7272 | * verify_hash_fail_interruptible() test intentions: |
| 7273 | * |
| 7274 | * Note: This test can currently only handle ECDSA. |
| 7275 | * |
| 7276 | * 1. Test that various failure cases for interruptible verify hash fail with |
| 7277 | * the correct error codes, and at the correct point (at start or during |
| 7278 | * complete). |
| 7279 | * |
| 7280 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7281 | * expected for different max_ops values. |
| 7282 | * |
| 7283 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7284 | * and that each successful stage completes some ops (this is not mandated by |
| 7285 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7286 | * |
| 7287 | * 4. Check that calling complete() when start() fails and complete() |
| 7288 | * after completion results in a BAD_STATE error. |
| 7289 | * |
| 7290 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 7291 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7292 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7293 | void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 7294 | int alg_arg, data_t *hash_data, |
| 7295 | data_t *signature_data, |
| 7296 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7297 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7298 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7299 | { |
| 7300 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7301 | psa_key_type_t key_type = key_type_arg; |
| 7302 | psa_algorithm_t alg = alg_arg; |
| 7303 | psa_status_t actual_status; |
| 7304 | psa_status_t expected_start_status = expected_start_status_arg; |
| 7305 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 7306 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7307 | uint32_t num_ops = 0; |
| 7308 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7309 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7310 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7311 | size_t min_completes = 0; |
| 7312 | size_t max_completes = 0; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7313 | psa_verify_hash_interruptible_operation_t operation = |
| 7314 | psa_verify_hash_interruptible_operation_init(); |
| 7315 | |
| 7316 | PSA_ASSERT(psa_crypto_init()); |
| 7317 | |
| 7318 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7319 | psa_set_key_algorithm(&attributes, alg); |
| 7320 | psa_set_key_type(&attributes, key_type); |
| 7321 | |
| 7322 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7323 | &key)); |
| 7324 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7325 | psa_interruptible_set_max_ops(max_ops); |
| 7326 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7327 | interruptible_signverify_get_minmax_completes(max_ops, |
| 7328 | expected_complete_status, |
| 7329 | &min_completes, |
| 7330 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7331 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7332 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7333 | TEST_ASSERT(num_ops_prior == 0); |
| 7334 | |
| 7335 | /* Start verification. */ |
| 7336 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7337 | hash_data->x, hash_data->len, |
| 7338 | signature_data->x, |
| 7339 | signature_data->len); |
| 7340 | |
| 7341 | TEST_EQUAL(actual_status, expected_start_status); |
| 7342 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7343 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 7344 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7345 | * start failed. */ |
| 7346 | actual_status = psa_verify_hash_complete(&operation); |
| 7347 | |
| 7348 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7349 | |
| 7350 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7351 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7352 | hash_data->x, hash_data->len, |
| 7353 | signature_data->x, |
| 7354 | signature_data->len); |
| 7355 | |
| 7356 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7357 | } |
| 7358 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7359 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7360 | TEST_ASSERT(num_ops_prior == 0); |
| 7361 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7362 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7363 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7364 | actual_status = psa_verify_hash_complete(&operation); |
| 7365 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7366 | num_completes++; |
| 7367 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7368 | if (actual_status == PSA_SUCCESS || |
| 7369 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7370 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7371 | /* We are asserting here that every complete makes progress |
| 7372 | * (completes some ops), which is true of the internal |
| 7373 | * implementation and probably any implementation, however this is |
| 7374 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7375 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7376 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7377 | num_ops_prior = num_ops; |
| 7378 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7379 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7380 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7381 | TEST_EQUAL(actual_status, expected_complete_status); |
| 7382 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 7383 | /* Check that another complete returns BAD_STATE. */ |
| 7384 | actual_status = psa_verify_hash_complete(&operation); |
| 7385 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7386 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7387 | TEST_LE_U(min_completes, num_completes); |
| 7388 | TEST_LE_U(num_completes, max_completes); |
| 7389 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7390 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7391 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7392 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7393 | TEST_ASSERT(num_ops == 0); |
| 7394 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7395 | exit: |
| 7396 | psa_reset_key_attributes(&attributes); |
| 7397 | psa_destroy_key(key); |
| 7398 | PSA_DONE(); |
| 7399 | } |
| 7400 | /* END_CASE */ |
| 7401 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7402 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7403 | /** |
| 7404 | * interruptible_signverify_hash_state_test() test intentions: |
| 7405 | * |
| 7406 | * Note: This test can currently only handle ECDSA. |
| 7407 | * |
| 7408 | * 1. Test that calling the various interruptible sign and verify hash functions |
| 7409 | * in incorrect orders returns BAD_STATE errors. |
| 7410 | */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7411 | void interruptible_signverify_hash_state_test(int key_type_arg, |
| 7412 | data_t *key_data, int alg_arg, data_t *input_data) |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7413 | { |
| 7414 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7415 | psa_key_type_t key_type = key_type_arg; |
| 7416 | psa_algorithm_t alg = alg_arg; |
| 7417 | size_t key_bits; |
| 7418 | unsigned char *signature = NULL; |
| 7419 | size_t signature_size; |
| 7420 | size_t signature_length = 0xdeadbeef; |
| 7421 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7422 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7423 | psa_sign_hash_interruptible_operation_init(); |
| 7424 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7425 | psa_verify_hash_interruptible_operation_init(); |
| 7426 | |
| 7427 | PSA_ASSERT(psa_crypto_init()); |
| 7428 | |
| 7429 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7430 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7431 | psa_set_key_algorithm(&attributes, alg); |
| 7432 | psa_set_key_type(&attributes, key_type); |
| 7433 | |
| 7434 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7435 | &key)); |
| 7436 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7437 | key_bits = psa_get_key_bits(&attributes); |
| 7438 | |
| 7439 | /* Allocate a buffer which has the size advertised by the |
| 7440 | * library. */ |
| 7441 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7442 | key_bits, alg); |
| 7443 | TEST_ASSERT(signature_size != 0); |
| 7444 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7445 | ASSERT_ALLOC(signature, signature_size); |
| 7446 | |
| 7447 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7448 | |
| 7449 | /* --- Attempt completes prior to starts --- */ |
| 7450 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7451 | signature_size, |
| 7452 | &signature_length), |
| 7453 | PSA_ERROR_BAD_STATE); |
| 7454 | |
| 7455 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7456 | |
| 7457 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7458 | PSA_ERROR_BAD_STATE); |
| 7459 | |
| 7460 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7461 | |
| 7462 | /* --- Aborts in all other places. --- */ |
| 7463 | psa_sign_hash_abort(&sign_operation); |
| 7464 | |
| 7465 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7466 | input_data->x, input_data->len)); |
| 7467 | |
| 7468 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7469 | |
| 7470 | psa_interruptible_set_max_ops(1); |
| 7471 | |
| 7472 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7473 | input_data->x, input_data->len)); |
| 7474 | |
| 7475 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7476 | signature_size, |
| 7477 | &signature_length), |
| 7478 | PSA_OPERATION_INCOMPLETE); |
| 7479 | |
| 7480 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7481 | |
| 7482 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7483 | |
| 7484 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7485 | input_data->x, input_data->len)); |
| 7486 | |
| 7487 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7488 | signature_size, |
| 7489 | &signature_length)); |
| 7490 | |
| 7491 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7492 | |
| 7493 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7494 | |
| 7495 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7496 | input_data->x, input_data->len, |
| 7497 | signature, signature_length)); |
| 7498 | |
| 7499 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7500 | |
| 7501 | psa_interruptible_set_max_ops(1); |
| 7502 | |
| 7503 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7504 | input_data->x, input_data->len, |
| 7505 | signature, signature_length)); |
| 7506 | |
| 7507 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7508 | PSA_OPERATION_INCOMPLETE); |
| 7509 | |
| 7510 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7511 | |
| 7512 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7513 | |
| 7514 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7515 | input_data->x, input_data->len, |
| 7516 | signature, signature_length)); |
| 7517 | |
| 7518 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7519 | |
| 7520 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7521 | |
| 7522 | /* --- Attempt double starts. --- */ |
| 7523 | |
| 7524 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7525 | input_data->x, input_data->len)); |
| 7526 | |
| 7527 | TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg, |
| 7528 | input_data->x, input_data->len), |
| 7529 | PSA_ERROR_BAD_STATE); |
| 7530 | |
| 7531 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7532 | |
| 7533 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7534 | input_data->x, input_data->len, |
| 7535 | signature, signature_length)); |
| 7536 | |
| 7537 | TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg, |
| 7538 | input_data->x, input_data->len, |
| 7539 | signature, signature_length), |
| 7540 | PSA_ERROR_BAD_STATE); |
| 7541 | |
| 7542 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7543 | |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7544 | exit: |
| 7545 | /* |
| 7546 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7547 | * thus reset them as required. |
| 7548 | */ |
| 7549 | psa_reset_key_attributes(&attributes); |
| 7550 | |
| 7551 | psa_destroy_key(key); |
| 7552 | mbedtls_free(signature); |
| 7553 | PSA_DONE(); |
| 7554 | } |
| 7555 | /* END_CASE */ |
| 7556 | |
| 7557 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7558 | /** |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7559 | * interruptible_signverify_hash_edgecase_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7560 | * |
| 7561 | * Note: This test can currently only handle ECDSA. |
| 7562 | * |
| 7563 | * 1. Test various edge cases in the interruptible sign and verify hash |
| 7564 | * interfaces. |
| 7565 | */ |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7566 | void interruptible_signverify_hash_edgecase_tests(int key_type_arg, |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7567 | data_t *key_data, int alg_arg, data_t *input_data) |
| 7568 | { |
| 7569 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7570 | psa_key_type_t key_type = key_type_arg; |
| 7571 | psa_algorithm_t alg = alg_arg; |
| 7572 | size_t key_bits; |
| 7573 | unsigned char *signature = NULL; |
| 7574 | size_t signature_size; |
| 7575 | size_t signature_length = 0xdeadbeef; |
| 7576 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7577 | uint8_t *input_buffer = NULL; |
| 7578 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7579 | psa_sign_hash_interruptible_operation_init(); |
| 7580 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7581 | psa_verify_hash_interruptible_operation_init(); |
| 7582 | |
| 7583 | PSA_ASSERT(psa_crypto_init()); |
| 7584 | |
| 7585 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7586 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7587 | psa_set_key_algorithm(&attributes, alg); |
| 7588 | psa_set_key_type(&attributes, key_type); |
| 7589 | |
| 7590 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7591 | &key)); |
| 7592 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7593 | key_bits = psa_get_key_bits(&attributes); |
| 7594 | |
| 7595 | /* Allocate a buffer which has the size advertised by the |
| 7596 | * library. */ |
| 7597 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7598 | key_bits, alg); |
| 7599 | TEST_ASSERT(signature_size != 0); |
| 7600 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7601 | ASSERT_ALLOC(signature, signature_size); |
| 7602 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7603 | /* --- Change function inputs mid run, to cause an error (sign only, |
| 7604 | * verify passes all inputs to start. --- */ |
| 7605 | |
| 7606 | psa_interruptible_set_max_ops(1); |
| 7607 | |
| 7608 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7609 | input_data->x, input_data->len)); |
| 7610 | |
| 7611 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7612 | signature_size, |
| 7613 | &signature_length), |
| 7614 | PSA_OPERATION_INCOMPLETE); |
| 7615 | |
| 7616 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7617 | 0, |
| 7618 | &signature_length), |
| 7619 | PSA_ERROR_BUFFER_TOO_SMALL); |
| 7620 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7621 | /* And test that this invalidates the operation. */ |
| 7622 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7623 | 0, |
| 7624 | &signature_length), |
| 7625 | PSA_ERROR_BAD_STATE); |
| 7626 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7627 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7628 | |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7629 | /* Trash the hash buffer in between start and complete, to ensure |
| 7630 | * no reliance on external buffers. */ |
| 7631 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7632 | |
| 7633 | input_buffer = mbedtls_calloc(1, input_data->len); |
| 7634 | TEST_ASSERT(input_buffer != NULL); |
| 7635 | |
| 7636 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7637 | |
| 7638 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7639 | input_buffer, input_data->len)); |
| 7640 | |
| 7641 | memset(input_buffer, '!', input_data->len); |
| 7642 | mbedtls_free(input_buffer); |
| 7643 | input_buffer = NULL; |
| 7644 | |
| 7645 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7646 | signature_size, |
| 7647 | &signature_length)); |
| 7648 | |
| 7649 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7650 | |
| 7651 | input_buffer = mbedtls_calloc(1, input_data->len); |
| 7652 | TEST_ASSERT(input_buffer != NULL); |
| 7653 | |
| 7654 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7655 | |
| 7656 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7657 | input_buffer, input_data->len, |
| 7658 | signature, signature_length)); |
| 7659 | |
| 7660 | memset(input_buffer, '!', input_data->len); |
| 7661 | mbedtls_free(input_buffer); |
| 7662 | input_buffer = NULL; |
| 7663 | |
| 7664 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7665 | |
| 7666 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7667 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7668 | exit: |
| 7669 | /* |
| 7670 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7671 | * thus reset them as required. |
| 7672 | */ |
| 7673 | psa_reset_key_attributes(&attributes); |
| 7674 | |
| 7675 | psa_destroy_key(key); |
| 7676 | mbedtls_free(signature); |
| 7677 | PSA_DONE(); |
| 7678 | } |
| 7679 | /* END_CASE */ |
| 7680 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7681 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7682 | /** |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 7683 | * interruptible_signverify_hash_ops_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7684 | * |
| 7685 | * Note: This test can currently only handle ECDSA. |
| 7686 | * |
| 7687 | * 1. Test that setting max ops is reflected in both interruptible sign and |
| 7688 | * verify hash |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7689 | * 2. Test that changing the value of max_ops to unlimited during an operation |
| 7690 | * causes that operation to complete in the next call. |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7691 | * |
| 7692 | * 3. Test that calling get_num_ops() between complete calls gives the same |
| 7693 | * 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] | 7694 | */ |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 7695 | void interruptible_signverify_hash_ops_tests(int key_type_arg, |
| 7696 | data_t *key_data, int alg_arg, |
| 7697 | data_t *input_data) |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7698 | { |
| 7699 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7700 | psa_key_type_t key_type = key_type_arg; |
| 7701 | psa_algorithm_t alg = alg_arg; |
| 7702 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7703 | size_t key_bits; |
| 7704 | unsigned char *signature = NULL; |
| 7705 | size_t signature_size; |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7706 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7707 | uint32_t num_ops = 0; |
| 7708 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 7709 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7710 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7711 | psa_sign_hash_interruptible_operation_init(); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7712 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7713 | psa_verify_hash_interruptible_operation_init(); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7714 | |
| 7715 | PSA_ASSERT(psa_crypto_init()); |
| 7716 | |
| 7717 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7718 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7719 | psa_set_key_algorithm(&attributes, alg); |
| 7720 | psa_set_key_type(&attributes, key_type); |
| 7721 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7722 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key)); |
| 7723 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7724 | key_bits = psa_get_key_bits(&attributes); |
| 7725 | |
| 7726 | /* Allocate a buffer which has the size advertised by the |
| 7727 | * library. */ |
| 7728 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7729 | |
| 7730 | TEST_ASSERT(signature_size != 0); |
| 7731 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7732 | ASSERT_ALLOC(signature, signature_size); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7733 | |
| 7734 | /* Check that default max ops gets set if we don't set it. */ |
| 7735 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7736 | input_data->x, input_data->len)); |
| 7737 | |
| 7738 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 7739 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7740 | |
| 7741 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7742 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7743 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7744 | input_data->x, input_data->len, |
| 7745 | signature, signature_size)); |
| 7746 | |
| 7747 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 7748 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7749 | |
| 7750 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7751 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7752 | /* Check that max ops gets set properly. */ |
| 7753 | |
| 7754 | psa_interruptible_set_max_ops(0xbeef); |
| 7755 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7756 | TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7757 | |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7758 | /* --- Ensure changing the max ops mid operation works (operation should |
| 7759 | * complete successfully after setting max ops to unlimited --- */ |
| 7760 | psa_interruptible_set_max_ops(1); |
| 7761 | |
| 7762 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7763 | input_data->x, input_data->len)); |
| 7764 | |
| 7765 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7766 | signature_size, |
| 7767 | &signature_length), |
| 7768 | PSA_OPERATION_INCOMPLETE); |
| 7769 | |
| 7770 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7771 | |
| 7772 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7773 | signature_size, |
| 7774 | &signature_length)); |
| 7775 | |
| 7776 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7777 | |
| 7778 | psa_interruptible_set_max_ops(1); |
| 7779 | |
| 7780 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7781 | input_data->x, input_data->len, |
| 7782 | signature, signature_length)); |
| 7783 | |
| 7784 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7785 | PSA_OPERATION_INCOMPLETE); |
| 7786 | |
| 7787 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7788 | |
| 7789 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7790 | |
| 7791 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7792 | |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7793 | /* --- Test that not calling get_num_ops inbetween complete calls does not |
| 7794 | * result in lost ops. ---*/ |
| 7795 | |
| 7796 | psa_interruptible_set_max_ops(1); |
| 7797 | |
| 7798 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7799 | input_data->x, input_data->len)); |
| 7800 | |
| 7801 | /* Continue performing the signature until complete. */ |
| 7802 | do { |
| 7803 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7804 | signature_size, |
| 7805 | &signature_length); |
| 7806 | |
| 7807 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7808 | |
| 7809 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7810 | |
| 7811 | PSA_ASSERT(status); |
| 7812 | |
| 7813 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7814 | |
| 7815 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7816 | input_data->x, input_data->len)); |
| 7817 | |
| 7818 | /* Continue performing the signature until complete. */ |
| 7819 | do { |
| 7820 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7821 | signature_size, |
| 7822 | &signature_length); |
| 7823 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7824 | |
| 7825 | PSA_ASSERT(status); |
| 7826 | |
| 7827 | TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation)); |
| 7828 | |
| 7829 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7830 | |
| 7831 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7832 | input_data->x, input_data->len, |
| 7833 | signature, signature_length)); |
| 7834 | |
| 7835 | /* Continue performing the verification until complete. */ |
| 7836 | do { |
| 7837 | status = psa_verify_hash_complete(&verify_operation); |
| 7838 | |
| 7839 | num_ops = psa_verify_hash_get_num_ops(&verify_operation); |
| 7840 | |
| 7841 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7842 | |
| 7843 | PSA_ASSERT(status); |
| 7844 | |
| 7845 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7846 | |
| 7847 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7848 | input_data->x, input_data->len, |
| 7849 | signature, signature_length)); |
| 7850 | |
| 7851 | /* Continue performing the verification until complete. */ |
| 7852 | do { |
| 7853 | status = psa_verify_hash_complete(&verify_operation); |
| 7854 | |
| 7855 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7856 | |
| 7857 | PSA_ASSERT(status); |
| 7858 | |
| 7859 | TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation)); |
| 7860 | |
| 7861 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7862 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7863 | exit: |
| 7864 | /* |
| 7865 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7866 | * thus reset them as required. |
| 7867 | */ |
| 7868 | psa_reset_key_attributes(&attributes); |
| 7869 | |
| 7870 | psa_destroy_key(key); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7871 | mbedtls_free(signature); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7872 | PSA_DONE(); |
| 7873 | } |
| 7874 | /* END_CASE */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7875 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7876 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7877 | void sign_message_deterministic(int key_type_arg, |
| 7878 | data_t *key_data, |
| 7879 | int alg_arg, |
| 7880 | data_t *input_data, |
| 7881 | data_t *output_data) |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7882 | { |
| 7883 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7884 | psa_key_type_t key_type = key_type_arg; |
| 7885 | psa_algorithm_t alg = alg_arg; |
| 7886 | size_t key_bits; |
| 7887 | unsigned char *signature = NULL; |
| 7888 | size_t signature_size; |
| 7889 | size_t signature_length = 0xdeadbeef; |
| 7890 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7891 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7892 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7893 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7894 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 7895 | psa_set_key_algorithm(&attributes, alg); |
| 7896 | psa_set_key_type(&attributes, key_type); |
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 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7899 | &key)); |
| 7900 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7901 | key_bits = psa_get_key_bits(&attributes); |
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 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7904 | TEST_ASSERT(signature_size != 0); |
| 7905 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7906 | ASSERT_ALLOC(signature, signature_size); |
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 | PSA_ASSERT(psa_sign_message(key, alg, |
| 7909 | input_data->x, input_data->len, |
| 7910 | signature, signature_size, |
| 7911 | &signature_length)); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7912 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7913 | ASSERT_COMPARE(output_data->x, output_data->len, |
| 7914 | signature, signature_length); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7915 | |
| 7916 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7917 | psa_reset_key_attributes(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7918 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7919 | psa_destroy_key(key); |
| 7920 | mbedtls_free(signature); |
| 7921 | PSA_DONE(); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7922 | |
| 7923 | } |
| 7924 | /* END_CASE */ |
| 7925 | |
| 7926 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7927 | void sign_message_fail(int key_type_arg, |
| 7928 | data_t *key_data, |
| 7929 | int alg_arg, |
| 7930 | data_t *input_data, |
| 7931 | int signature_size_arg, |
| 7932 | int expected_status_arg) |
| 7933 | { |
| 7934 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7935 | psa_key_type_t key_type = key_type_arg; |
| 7936 | psa_algorithm_t alg = alg_arg; |
| 7937 | size_t signature_size = signature_size_arg; |
| 7938 | psa_status_t actual_status; |
| 7939 | psa_status_t expected_status = expected_status_arg; |
| 7940 | unsigned char *signature = NULL; |
| 7941 | size_t signature_length = 0xdeadbeef; |
| 7942 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7943 | |
| 7944 | ASSERT_ALLOC(signature, signature_size); |
| 7945 | |
| 7946 | PSA_ASSERT(psa_crypto_init()); |
| 7947 | |
| 7948 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 7949 | psa_set_key_algorithm(&attributes, alg); |
| 7950 | psa_set_key_type(&attributes, key_type); |
| 7951 | |
| 7952 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7953 | &key)); |
| 7954 | |
| 7955 | actual_status = psa_sign_message(key, alg, |
| 7956 | input_data->x, input_data->len, |
| 7957 | signature, signature_size, |
| 7958 | &signature_length); |
| 7959 | TEST_EQUAL(actual_status, expected_status); |
| 7960 | /* The value of *signature_length is unspecified on error, but |
| 7961 | * whatever it is, it should be less than signature_size, so that |
| 7962 | * if the caller tries to read *signature_length bytes without |
| 7963 | * checking the error code then they don't overflow a buffer. */ |
| 7964 | TEST_LE_U(signature_length, signature_size); |
| 7965 | |
| 7966 | exit: |
| 7967 | psa_reset_key_attributes(&attributes); |
| 7968 | psa_destroy_key(key); |
| 7969 | mbedtls_free(signature); |
| 7970 | PSA_DONE(); |
| 7971 | } |
| 7972 | /* END_CASE */ |
| 7973 | |
| 7974 | /* BEGIN_CASE */ |
| 7975 | void sign_verify_message(int key_type_arg, |
| 7976 | data_t *key_data, |
| 7977 | int alg_arg, |
| 7978 | data_t *input_data) |
| 7979 | { |
| 7980 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7981 | psa_key_type_t key_type = key_type_arg; |
| 7982 | psa_algorithm_t alg = alg_arg; |
| 7983 | size_t key_bits; |
| 7984 | unsigned char *signature = NULL; |
| 7985 | size_t signature_size; |
| 7986 | size_t signature_length = 0xdeadbeef; |
| 7987 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7988 | |
| 7989 | PSA_ASSERT(psa_crypto_init()); |
| 7990 | |
| 7991 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 7992 | PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 7993 | psa_set_key_algorithm(&attributes, alg); |
| 7994 | psa_set_key_type(&attributes, key_type); |
| 7995 | |
| 7996 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7997 | &key)); |
| 7998 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7999 | key_bits = psa_get_key_bits(&attributes); |
| 8000 | |
| 8001 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8002 | TEST_ASSERT(signature_size != 0); |
| 8003 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 8004 | ASSERT_ALLOC(signature, signature_size); |
| 8005 | |
| 8006 | PSA_ASSERT(psa_sign_message(key, alg, |
| 8007 | input_data->x, input_data->len, |
| 8008 | signature, signature_size, |
| 8009 | &signature_length)); |
| 8010 | TEST_LE_U(signature_length, signature_size); |
| 8011 | TEST_ASSERT(signature_length > 0); |
| 8012 | |
| 8013 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8014 | input_data->x, input_data->len, |
| 8015 | signature, signature_length)); |
| 8016 | |
| 8017 | if (input_data->len != 0) { |
| 8018 | /* Flip a bit in the input and verify that the signature is now |
| 8019 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 8020 | * because ECDSA may ignore the last few bits of the input. */ |
| 8021 | input_data->x[0] ^= 1; |
| 8022 | TEST_EQUAL(psa_verify_message(key, alg, |
| 8023 | input_data->x, input_data->len, |
| 8024 | signature, signature_length), |
| 8025 | PSA_ERROR_INVALID_SIGNATURE); |
| 8026 | } |
| 8027 | |
| 8028 | exit: |
| 8029 | psa_reset_key_attributes(&attributes); |
| 8030 | |
| 8031 | psa_destroy_key(key); |
| 8032 | mbedtls_free(signature); |
| 8033 | PSA_DONE(); |
| 8034 | } |
| 8035 | /* END_CASE */ |
| 8036 | |
| 8037 | /* BEGIN_CASE */ |
| 8038 | void verify_message(int key_type_arg, |
| 8039 | data_t *key_data, |
| 8040 | int alg_arg, |
| 8041 | data_t *input_data, |
| 8042 | data_t *signature_data) |
| 8043 | { |
| 8044 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8045 | psa_key_type_t key_type = key_type_arg; |
| 8046 | psa_algorithm_t alg = alg_arg; |
| 8047 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8048 | |
| 8049 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 8050 | |
| 8051 | PSA_ASSERT(psa_crypto_init()); |
| 8052 | |
| 8053 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8054 | psa_set_key_algorithm(&attributes, alg); |
| 8055 | psa_set_key_type(&attributes, key_type); |
| 8056 | |
| 8057 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8058 | &key)); |
| 8059 | |
| 8060 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8061 | input_data->x, input_data->len, |
| 8062 | signature_data->x, signature_data->len)); |
| 8063 | |
| 8064 | exit: |
| 8065 | psa_reset_key_attributes(&attributes); |
| 8066 | psa_destroy_key(key); |
| 8067 | PSA_DONE(); |
| 8068 | } |
| 8069 | /* END_CASE */ |
| 8070 | |
| 8071 | /* BEGIN_CASE */ |
| 8072 | void verify_message_fail(int key_type_arg, |
| 8073 | data_t *key_data, |
| 8074 | int alg_arg, |
| 8075 | data_t *hash_data, |
| 8076 | data_t *signature_data, |
| 8077 | int expected_status_arg) |
| 8078 | { |
| 8079 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8080 | psa_key_type_t key_type = key_type_arg; |
| 8081 | psa_algorithm_t alg = alg_arg; |
| 8082 | psa_status_t actual_status; |
| 8083 | psa_status_t expected_status = expected_status_arg; |
| 8084 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8085 | |
| 8086 | PSA_ASSERT(psa_crypto_init()); |
| 8087 | |
| 8088 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8089 | psa_set_key_algorithm(&attributes, alg); |
| 8090 | psa_set_key_type(&attributes, key_type); |
| 8091 | |
| 8092 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8093 | &key)); |
| 8094 | |
| 8095 | actual_status = psa_verify_message(key, alg, |
| 8096 | hash_data->x, hash_data->len, |
| 8097 | signature_data->x, |
| 8098 | signature_data->len); |
| 8099 | TEST_EQUAL(actual_status, expected_status); |
| 8100 | |
| 8101 | exit: |
| 8102 | psa_reset_key_attributes(&attributes); |
| 8103 | psa_destroy_key(key); |
| 8104 | PSA_DONE(); |
| 8105 | } |
| 8106 | /* END_CASE */ |
| 8107 | |
| 8108 | /* BEGIN_CASE */ |
| 8109 | void asymmetric_encrypt(int key_type_arg, |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8110 | data_t *key_data, |
| 8111 | int alg_arg, |
| 8112 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8113 | data_t *label, |
| 8114 | int expected_output_length_arg, |
| 8115 | int expected_status_arg) |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8116 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8117 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8118 | psa_key_type_t key_type = key_type_arg; |
| 8119 | psa_algorithm_t alg = alg_arg; |
| 8120 | size_t expected_output_length = expected_output_length_arg; |
| 8121 | size_t key_bits; |
| 8122 | unsigned char *output = NULL; |
| 8123 | size_t output_size; |
| 8124 | size_t output_length = ~0; |
| 8125 | psa_status_t actual_status; |
| 8126 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8127 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8128 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8129 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 8130 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8131 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8132 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 8133 | psa_set_key_algorithm(&attributes, alg); |
| 8134 | psa_set_key_type(&attributes, key_type); |
| 8135 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8136 | &key)); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8137 | |
| 8138 | /* Determine the maximum output length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8139 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8140 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8141 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8142 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8143 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
| 8144 | ASSERT_ALLOC(output, output_size); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8145 | |
| 8146 | /* Encrypt the input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8147 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8148 | input_data->x, input_data->len, |
| 8149 | label->x, label->len, |
| 8150 | output, output_size, |
| 8151 | &output_length); |
| 8152 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8153 | if (actual_status == PSA_SUCCESS) { |
| 8154 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8155 | } else { |
| 8156 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8157 | } |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8158 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8159 | /* If the label is empty, the test framework puts a non-null pointer |
| 8160 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8161 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8162 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8163 | if (output_size != 0) { |
| 8164 | memset(output, 0, output_size); |
| 8165 | } |
| 8166 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8167 | input_data->x, input_data->len, |
| 8168 | NULL, label->len, |
| 8169 | output, output_size, |
| 8170 | &output_length); |
| 8171 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8172 | if (actual_status == PSA_SUCCESS) { |
| 8173 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8174 | } else { |
| 8175 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8176 | } |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8177 | } |
| 8178 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8179 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8180 | /* |
| 8181 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8182 | * thus reset them as required. |
| 8183 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8184 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8185 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8186 | psa_destroy_key(key); |
| 8187 | mbedtls_free(output); |
| 8188 | PSA_DONE(); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8189 | } |
| 8190 | /* END_CASE */ |
| 8191 | |
| 8192 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8193 | void asymmetric_encrypt_decrypt(int key_type_arg, |
| 8194 | data_t *key_data, |
| 8195 | int alg_arg, |
| 8196 | data_t *input_data, |
| 8197 | data_t *label) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8198 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8199 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8200 | psa_key_type_t key_type = key_type_arg; |
| 8201 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8202 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8203 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8204 | size_t output_size; |
| 8205 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8206 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8207 | size_t output2_size; |
| 8208 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8209 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8210 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8211 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8212 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8213 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 8214 | psa_set_key_algorithm(&attributes, alg); |
| 8215 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8216 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8217 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8218 | &key)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8219 | |
| 8220 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8221 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8222 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8223 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8224 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8225 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
| 8226 | ASSERT_ALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8227 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8228 | output2_size = input_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8229 | TEST_LE_U(output2_size, |
| 8230 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)); |
| 8231 | TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
| 8232 | ASSERT_ALLOC(output2, output2_size); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8233 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 8234 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 8235 | * the original plaintext because of the non-optional random |
| 8236 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8237 | PSA_ASSERT(psa_asymmetric_encrypt(key, alg, |
| 8238 | input_data->x, input_data->len, |
| 8239 | label->x, label->len, |
| 8240 | output, output_size, |
| 8241 | &output_length)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8242 | /* We don't know what ciphertext length to expect, but check that |
| 8243 | * it looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8244 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8245 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8246 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8247 | output, output_length, |
| 8248 | label->x, label->len, |
| 8249 | output2, output2_size, |
| 8250 | &output2_length)); |
| 8251 | ASSERT_COMPARE(input_data->x, input_data->len, |
| 8252 | output2, output2_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8253 | |
| 8254 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8255 | /* |
| 8256 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8257 | * thus reset them as required. |
| 8258 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8259 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8260 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8261 | psa_destroy_key(key); |
| 8262 | mbedtls_free(output); |
| 8263 | mbedtls_free(output2); |
| 8264 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8265 | } |
| 8266 | /* END_CASE */ |
| 8267 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8268 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8269 | void asymmetric_decrypt(int key_type_arg, |
| 8270 | data_t *key_data, |
| 8271 | int alg_arg, |
| 8272 | data_t *input_data, |
| 8273 | data_t *label, |
| 8274 | data_t *expected_data) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8275 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8276 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8277 | psa_key_type_t key_type = key_type_arg; |
| 8278 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8279 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8280 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 8281 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8282 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8283 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8284 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8285 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8286 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8287 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8288 | psa_set_key_algorithm(&attributes, alg); |
| 8289 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8290 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8291 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8292 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8293 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8294 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8295 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8296 | |
| 8297 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8298 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8299 | TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
| 8300 | ASSERT_ALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8301 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8302 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8303 | input_data->x, input_data->len, |
| 8304 | label->x, label->len, |
| 8305 | output, |
| 8306 | output_size, |
| 8307 | &output_length)); |
| 8308 | ASSERT_COMPARE(expected_data->x, expected_data->len, |
| 8309 | output, output_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8310 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8311 | /* If the label is empty, the test framework puts a non-null pointer |
| 8312 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8313 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8314 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8315 | if (output_size != 0) { |
| 8316 | memset(output, 0, output_size); |
| 8317 | } |
| 8318 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8319 | input_data->x, input_data->len, |
| 8320 | NULL, label->len, |
| 8321 | output, |
| 8322 | output_size, |
| 8323 | &output_length)); |
| 8324 | ASSERT_COMPARE(expected_data->x, expected_data->len, |
| 8325 | output, output_length); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8326 | } |
| 8327 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8328 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8329 | psa_reset_key_attributes(&attributes); |
| 8330 | psa_destroy_key(key); |
| 8331 | mbedtls_free(output); |
| 8332 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8333 | } |
| 8334 | /* END_CASE */ |
| 8335 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8336 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8337 | void asymmetric_decrypt_fail(int key_type_arg, |
| 8338 | data_t *key_data, |
| 8339 | int alg_arg, |
| 8340 | data_t *input_data, |
| 8341 | data_t *label, |
| 8342 | int output_size_arg, |
| 8343 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8344 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8345 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8346 | psa_key_type_t key_type = key_type_arg; |
| 8347 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8348 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 8349 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8350 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8351 | psa_status_t actual_status; |
| 8352 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8353 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8354 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8355 | ASSERT_ALLOC(output, output_size); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8356 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8357 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8358 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8359 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8360 | psa_set_key_algorithm(&attributes, alg); |
| 8361 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8362 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8363 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8364 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8365 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8366 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8367 | input_data->x, input_data->len, |
| 8368 | label->x, label->len, |
| 8369 | output, output_size, |
| 8370 | &output_length); |
| 8371 | TEST_EQUAL(actual_status, expected_status); |
| 8372 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8373 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8374 | /* If the label is empty, the test framework puts a non-null pointer |
| 8375 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8376 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8377 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8378 | if (output_size != 0) { |
| 8379 | memset(output, 0, output_size); |
| 8380 | } |
| 8381 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8382 | input_data->x, input_data->len, |
| 8383 | NULL, label->len, |
| 8384 | output, output_size, |
| 8385 | &output_length); |
| 8386 | TEST_EQUAL(actual_status, expected_status); |
| 8387 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8388 | } |
| 8389 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8390 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8391 | psa_reset_key_attributes(&attributes); |
| 8392 | psa_destroy_key(key); |
| 8393 | mbedtls_free(output); |
| 8394 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8395 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8396 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8397 | |
| 8398 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8399 | void key_derivation_init() |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8400 | { |
| 8401 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 8402 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 8403 | * 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] | 8404 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8405 | size_t capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8406 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init(); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 8407 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8408 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8409 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8410 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8411 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8412 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8413 | TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity), |
| 8414 | PSA_ERROR_BAD_STATE); |
| 8415 | TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity), |
| 8416 | PSA_ERROR_BAD_STATE); |
| 8417 | TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity), |
| 8418 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8419 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8420 | /* A default operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8421 | PSA_ASSERT(psa_key_derivation_abort(&func)); |
| 8422 | PSA_ASSERT(psa_key_derivation_abort(&init)); |
| 8423 | PSA_ASSERT(psa_key_derivation_abort(&zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8424 | } |
| 8425 | /* END_CASE */ |
| 8426 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 8427 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8428 | void derive_setup(int alg_arg, int expected_status_arg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8429 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8430 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8431 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8432 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8433 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8434 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8435 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8436 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8437 | expected_status); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8438 | |
| 8439 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8440 | psa_key_derivation_abort(&operation); |
| 8441 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8442 | } |
| 8443 | /* END_CASE */ |
| 8444 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8445 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8446 | void derive_set_capacity(int alg_arg, int capacity_arg, |
| 8447 | int expected_status_arg) |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8448 | { |
| 8449 | psa_algorithm_t alg = alg_arg; |
| 8450 | size_t capacity = capacity_arg; |
| 8451 | psa_status_t expected_status = expected_status_arg; |
| 8452 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8453 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8454 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8455 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8456 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8457 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8458 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 8459 | expected_status); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8460 | |
| 8461 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8462 | psa_key_derivation_abort(&operation); |
| 8463 | PSA_DONE(); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8464 | } |
| 8465 | /* END_CASE */ |
| 8466 | |
| 8467 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8468 | void derive_input(int alg_arg, |
Kusumit Ghoderao | 02326d5 | 2023-04-06 17:47:59 +0530 | [diff] [blame] | 8469 | int step_arg1, int key_type_arg1, int input_type_arg1, |
| 8470 | data_t *input1, int expected_status_arg1, |
| 8471 | int step_arg2, int key_type_arg2, int input_type_arg2, |
| 8472 | data_t *input2, int expected_status_arg2, |
| 8473 | int step_arg3, int key_type_arg3, int input_type_arg3, |
| 8474 | data_t *input3, int expected_status_arg3, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8475 | int output_key_type_arg, int expected_output_status_arg) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8476 | { |
| 8477 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8478 | psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 }; |
| 8479 | psa_key_type_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 }; |
Kusumit Ghoderao | a14ae5a | 2023-04-19 14:16:26 +0530 | [diff] [blame^] | 8480 | key_derivation_input_method_t input_types[] = |
| 8481 | { input_type_arg1, input_type_arg2, input_type_arg3 }; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8482 | psa_status_t expected_statuses[] = { expected_status_arg1, |
| 8483 | expected_status_arg2, |
| 8484 | expected_status_arg3 }; |
| 8485 | data_t *inputs[] = { input1, input2, input3 }; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8486 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8487 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8488 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8489 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8490 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8491 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8492 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8493 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8494 | psa_status_t expected_output_status = expected_output_status_arg; |
| 8495 | psa_status_t actual_output_status; |
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 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8498 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8499 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8500 | psa_set_key_algorithm(&attributes, alg); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8501 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8502 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8503 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8504 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8505 | mbedtls_test_set_step(i); |
| 8506 | if (steps[i] == 0) { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 8507 | /* Skip this step */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8508 | } else if (key_types[i] != PSA_KEY_TYPE_NONE) { |
| 8509 | psa_set_key_type(&attributes, key_types[i]); |
| 8510 | PSA_ASSERT(psa_import_key(&attributes, |
| 8511 | inputs[i]->x, inputs[i]->len, |
| 8512 | &keys[i])); |
| 8513 | if (PSA_KEY_TYPE_IS_KEY_PAIR(key_types[i]) && |
| 8514 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8515 | // When taking a private key as secret input, use key agreement |
| 8516 | // to add the shared secret to the derivation |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8517 | TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self( |
| 8518 | &operation, keys[i]), |
| 8519 | expected_statuses[i]); |
| 8520 | } else { |
| 8521 | TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i], |
| 8522 | keys[i]), |
| 8523 | expected_statuses[i]); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8524 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8525 | } else { |
Kusumit Ghoderao | a14ae5a | 2023-04-19 14:16:26 +0530 | [diff] [blame^] | 8526 | if (input_types[i] == INPUT_BYTES) { |
Kusumit Ghoderao | 02326d5 | 2023-04-06 17:47:59 +0530 | [diff] [blame] | 8527 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8528 | &operation, steps[i], |
| 8529 | inputs[i]->x, inputs[i]->len), |
| 8530 | expected_statuses[i]); |
Kusumit Ghoderao | a14ae5a | 2023-04-19 14:16:26 +0530 | [diff] [blame^] | 8531 | } else if (input_types[i] == INPUT_INTEGER) { |
Kusumit Ghoderao | 02326d5 | 2023-04-06 17:47:59 +0530 | [diff] [blame] | 8532 | TEST_EQUAL(psa_key_derivation_input_integer( |
| 8533 | &operation, steps[i], |
Kusumit Ghoderao | a14ae5a | 2023-04-19 14:16:26 +0530 | [diff] [blame^] | 8534 | parse_hex_string(inputs[i])), |
Kusumit Ghoderao | 02326d5 | 2023-04-06 17:47:59 +0530 | [diff] [blame] | 8535 | expected_statuses[i]); |
| 8536 | } |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8537 | } |
| 8538 | } |
| 8539 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8540 | if (output_key_type != PSA_KEY_TYPE_NONE) { |
| 8541 | psa_reset_key_attributes(&attributes); |
| 8542 | psa_set_key_type(&attributes, output_key_type); |
| 8543 | psa_set_key_bits(&attributes, 8); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8544 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8545 | psa_key_derivation_output_key(&attributes, &operation, |
| 8546 | &output_key); |
| 8547 | } else { |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8548 | uint8_t buffer[1]; |
| 8549 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8550 | psa_key_derivation_output_bytes(&operation, |
| 8551 | buffer, sizeof(buffer)); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8552 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8553 | TEST_EQUAL(actual_output_status, expected_output_status); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8554 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8555 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8556 | psa_key_derivation_abort(&operation); |
| 8557 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8558 | psa_destroy_key(keys[i]); |
| 8559 | } |
| 8560 | psa_destroy_key(output_key); |
| 8561 | PSA_DONE(); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8562 | } |
| 8563 | /* END_CASE */ |
| 8564 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8565 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8566 | void derive_over_capacity(int alg_arg) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8567 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8568 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8569 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 8570 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8571 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8572 | unsigned char input1[] = "Input 1"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8573 | size_t input1_length = sizeof(input1); |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8574 | unsigned char input2[] = "Input 2"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8575 | size_t input2_length = sizeof(input2); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8576 | uint8_t buffer[42]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8577 | size_t capacity = sizeof(buffer); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 8578 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 8579 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8580 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8581 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8582 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8583 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8584 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8585 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8586 | psa_set_key_algorithm(&attributes, alg); |
| 8587 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8588 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8589 | PSA_ASSERT(psa_import_key(&attributes, |
| 8590 | key_data, sizeof(key_data), |
| 8591 | &key)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8592 | |
| 8593 | /* valid key derivation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8594 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8595 | input1, input1_length, |
| 8596 | input2, input2_length, |
| 8597 | capacity)) { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8598 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8599 | } |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8600 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8601 | /* state of operation shouldn't allow additional generation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8602 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8603 | PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8604 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8605 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8606 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8607 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity), |
| 8608 | PSA_ERROR_INSUFFICIENT_DATA); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8609 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8610 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8611 | psa_key_derivation_abort(&operation); |
| 8612 | psa_destroy_key(key); |
| 8613 | PSA_DONE(); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8614 | } |
| 8615 | /* END_CASE */ |
| 8616 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8617 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8618 | void derive_actions_without_setup() |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8619 | { |
| 8620 | uint8_t output_buffer[16]; |
| 8621 | size_t buffer_size = 16; |
| 8622 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8623 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8624 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8625 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8626 | output_buffer, buffer_size) |
| 8627 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8628 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8629 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 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 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8633 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8634 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8635 | output_buffer, buffer_size) |
| 8636 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8637 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8638 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8639 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8640 | |
| 8641 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8642 | psa_key_derivation_abort(&operation); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8643 | } |
| 8644 | /* END_CASE */ |
| 8645 | |
| 8646 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8647 | void derive_output(int alg_arg, |
| 8648 | int step1_arg, data_t *input1, int expected_status_arg1, |
| 8649 | int step2_arg, data_t *input2, int expected_status_arg2, |
| 8650 | int step3_arg, data_t *input3, int expected_status_arg3, |
| 8651 | int step4_arg, data_t *input4, int expected_status_arg4, |
| 8652 | data_t *key_agreement_peer_key, |
| 8653 | int requested_capacity_arg, |
| 8654 | data_t *expected_output1, |
| 8655 | data_t *expected_output2, |
| 8656 | int other_key_input_type, |
| 8657 | int key_input_type, |
| 8658 | int derive_type) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8659 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8660 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8661 | psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg }; |
| 8662 | data_t *inputs[] = { input1, input2, input3, input4 }; |
| 8663 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8664 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8665 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8666 | MBEDTLS_SVC_KEY_ID_INIT }; |
| 8667 | psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2, |
| 8668 | expected_status_arg3, expected_status_arg4 }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8669 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8670 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8671 | uint8_t *expected_outputs[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8672 | { expected_output1->x, expected_output2->x }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8673 | size_t output_sizes[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8674 | { expected_output1->len, expected_output2->len }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8675 | size_t output_buffer_size = 0; |
| 8676 | uint8_t *output_buffer = NULL; |
| 8677 | size_t expected_capacity; |
| 8678 | size_t current_capacity; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8679 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; |
| 8680 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; |
| 8681 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; |
| 8682 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8683 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8684 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8685 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8686 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8687 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
| 8688 | if (output_sizes[i] > output_buffer_size) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8689 | output_buffer_size = output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8690 | } |
| 8691 | if (output_sizes[i] == 0) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8692 | expected_outputs[i] = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8693 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8694 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8695 | ASSERT_ALLOC(output_buffer, output_buffer_size); |
| 8696 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8697 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8698 | /* Extraction phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8699 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8700 | PSA_ASSERT(psa_key_derivation_set_capacity(&operation, |
| 8701 | requested_capacity)); |
| 8702 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8703 | switch (steps[i]) { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8704 | case 0: |
| 8705 | break; |
| 8706 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8707 | switch (key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8708 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8709 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8710 | &operation, steps[i], |
| 8711 | inputs[i]->x, inputs[i]->len), |
| 8712 | statuses[i]); |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8713 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8714 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8715 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8716 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8717 | break; |
| 8718 | case 1: // input key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8719 | psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE); |
| 8720 | psa_set_key_algorithm(&attributes1, alg); |
| 8721 | psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8722 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8723 | PSA_ASSERT(psa_import_key(&attributes1, |
| 8724 | inputs[i]->x, inputs[i]->len, |
| 8725 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8726 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8727 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 8728 | PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1)); |
| 8729 | TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)), |
| 8730 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8731 | } |
| 8732 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8733 | PSA_ASSERT(psa_key_derivation_input_key(&operation, |
| 8734 | steps[i], |
| 8735 | keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8736 | break; |
| 8737 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8738 | TEST_ASSERT(!"default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8739 | break; |
| 8740 | } |
| 8741 | break; |
| 8742 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8743 | switch (other_key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8744 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8745 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 8746 | steps[i], |
| 8747 | inputs[i]->x, |
| 8748 | inputs[i]->len), |
| 8749 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8750 | break; |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 8751 | case 1: // input key, type DERIVE |
| 8752 | case 11: // input key, type RAW |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8753 | psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE); |
| 8754 | psa_set_key_algorithm(&attributes2, alg); |
| 8755 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8756 | |
| 8757 | // other secret of type RAW_DATA passed with input_key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8758 | if (other_key_input_type == 11) { |
| 8759 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA); |
| 8760 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8761 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8762 | PSA_ASSERT(psa_import_key(&attributes2, |
| 8763 | inputs[i]->x, inputs[i]->len, |
| 8764 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8765 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8766 | TEST_EQUAL(psa_key_derivation_input_key(&operation, |
| 8767 | steps[i], |
| 8768 | keys[i]), |
| 8769 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8770 | break; |
| 8771 | case 2: // key agreement |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8772 | psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE); |
| 8773 | psa_set_key_algorithm(&attributes3, alg); |
| 8774 | psa_set_key_type(&attributes3, |
| 8775 | PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8776 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8777 | PSA_ASSERT(psa_import_key(&attributes3, |
| 8778 | inputs[i]->x, inputs[i]->len, |
| 8779 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8780 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8781 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 8782 | &operation, |
| 8783 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 8784 | keys[i], key_agreement_peer_key->x, |
| 8785 | key_agreement_peer_key->len), statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8786 | break; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8787 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8788 | TEST_ASSERT(!"default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8789 | break; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8790 | } |
| 8791 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8792 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8793 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8794 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8795 | break; |
| 8796 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8797 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8798 | &operation, steps[i], |
| 8799 | inputs[i]->x, inputs[i]->len), statuses[i]); |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 8800 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8801 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 8802 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8803 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8804 | break; |
| 8805 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 8806 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8807 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8808 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8809 | ¤t_capacity)); |
| 8810 | TEST_EQUAL(current_capacity, requested_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8811 | expected_capacity = requested_capacity; |
| 8812 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8813 | if (derive_type == 1) { // output key |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8814 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; |
| 8815 | |
| 8816 | /* For output key derivation secret must be provided using |
| 8817 | input key, otherwise operation is not permitted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8818 | if (key_input_type == 1) { |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8819 | expected_status = PSA_SUCCESS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8820 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8821 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8822 | psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT); |
| 8823 | psa_set_key_algorithm(&attributes4, alg); |
| 8824 | psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE); |
| 8825 | psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8826 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8827 | TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation, |
| 8828 | &derived_key), expected_status); |
| 8829 | } else { // output bytes |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8830 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8831 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8832 | /* Read some bytes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8833 | status = psa_key_derivation_output_bytes(&operation, |
| 8834 | output_buffer, output_sizes[i]); |
| 8835 | if (expected_capacity == 0 && output_sizes[i] == 0) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8836 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8837 | TEST_ASSERT(status == PSA_SUCCESS || |
| 8838 | status == PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8839 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8840 | } else if (expected_capacity == 0 || |
| 8841 | output_sizes[i] > expected_capacity) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8842 | /* Capacity exceeded. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8843 | TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8844 | expected_capacity = 0; |
| 8845 | continue; |
| 8846 | } |
| 8847 | /* Success. Check the read data. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8848 | PSA_ASSERT(status); |
| 8849 | if (output_sizes[i] != 0) { |
| 8850 | ASSERT_COMPARE(output_buffer, output_sizes[i], |
| 8851 | expected_outputs[i], output_sizes[i]); |
| 8852 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8853 | /* Check the operation status. */ |
| 8854 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8855 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8856 | ¤t_capacity)); |
| 8857 | TEST_EQUAL(expected_capacity, current_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8858 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8859 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8860 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8861 | |
| 8862 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8863 | mbedtls_free(output_buffer); |
| 8864 | psa_key_derivation_abort(&operation); |
| 8865 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8866 | psa_destroy_key(keys[i]); |
| 8867 | } |
| 8868 | psa_destroy_key(derived_key); |
| 8869 | PSA_DONE(); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8870 | } |
| 8871 | /* END_CASE */ |
| 8872 | |
| 8873 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8874 | void derive_full(int alg_arg, |
| 8875 | data_t *key_data, |
| 8876 | data_t *input1, |
| 8877 | data_t *input2, |
| 8878 | int requested_capacity_arg) |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8879 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8880 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8881 | psa_algorithm_t alg = alg_arg; |
| 8882 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8883 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8884 | unsigned char output_buffer[16]; |
| 8885 | size_t expected_capacity = requested_capacity; |
| 8886 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8887 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
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_crypto_init()); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8890 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8891 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8892 | psa_set_key_algorithm(&attributes, alg); |
| 8893 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8894 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8895 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8896 | &key)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8897 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8898 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8899 | input1->x, input1->len, |
| 8900 | input2->x, input2->len, |
| 8901 | requested_capacity)) { |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 8902 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8903 | } |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 8904 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8905 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8906 | ¤t_capacity)); |
| 8907 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8908 | |
| 8909 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8910 | while (current_capacity > 0) { |
| 8911 | size_t read_size = sizeof(output_buffer); |
| 8912 | if (read_size > current_capacity) { |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8913 | read_size = current_capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8914 | } |
| 8915 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8916 | output_buffer, |
| 8917 | read_size)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8918 | expected_capacity -= read_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8919 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8920 | ¤t_capacity)); |
| 8921 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8922 | } |
| 8923 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8924 | /* Check that the operation refuses to go over capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8925 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1), |
| 8926 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8927 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8928 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8929 | |
| 8930 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8931 | psa_key_derivation_abort(&operation); |
| 8932 | psa_destroy_key(key); |
| 8933 | PSA_DONE(); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8934 | } |
| 8935 | /* END_CASE */ |
| 8936 | |
Przemek Stekiel | 8258ea7 | 2022-10-19 12:17:19 +0200 | [diff] [blame] | 8937 | /* 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] | 8938 | void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg, |
| 8939 | int derivation_step, |
| 8940 | int capacity, int expected_capacity_status_arg, |
| 8941 | data_t *expected_output, |
| 8942 | int expected_output_status_arg) |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8943 | { |
| 8944 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 8945 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | d378504 | 2022-09-16 06:45:44 -0400 | [diff] [blame] | 8946 | 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] | 8947 | uint8_t *output_buffer = NULL; |
| 8948 | psa_status_t status; |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 8949 | psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg; |
| 8950 | psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg; |
| 8951 | 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] | 8952 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8953 | ASSERT_ALLOC(output_buffer, expected_output->len); |
| 8954 | PSA_ASSERT(psa_crypto_init()); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8955 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8956 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8957 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 8958 | expected_capacity_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8959 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8960 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 8961 | step, input->x, input->len), |
| 8962 | expected_input_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8963 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8964 | if (((psa_status_t) expected_input_status) != PSA_SUCCESS) { |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8965 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8966 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8967 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8968 | status = psa_key_derivation_output_bytes(&operation, output_buffer, |
| 8969 | expected_output->len); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8970 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8971 | TEST_EQUAL(status, expected_output_status); |
| 8972 | if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) { |
| 8973 | ASSERT_COMPARE(output_buffer, expected_output->len, expected_output->x, |
| 8974 | expected_output->len); |
| 8975 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8976 | |
| 8977 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8978 | mbedtls_free(output_buffer); |
| 8979 | psa_key_derivation_abort(&operation); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8980 | PSA_DONE(); |
| 8981 | } |
| 8982 | /* END_CASE */ |
| 8983 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 8984 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8985 | void derive_key_exercise(int alg_arg, |
| 8986 | data_t *key_data, |
| 8987 | data_t *input1, |
| 8988 | data_t *input2, |
| 8989 | int derived_type_arg, |
| 8990 | int derived_bits_arg, |
| 8991 | int derived_usage_arg, |
| 8992 | int derived_alg_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8993 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8994 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8995 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8996 | psa_algorithm_t alg = alg_arg; |
| 8997 | psa_key_type_t derived_type = derived_type_arg; |
| 8998 | size_t derived_bits = derived_bits_arg; |
| 8999 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 9000 | psa_algorithm_t derived_alg = derived_alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9001 | size_t capacity = PSA_BITS_TO_BYTES(derived_bits); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9002 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9003 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 9004 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9005 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9006 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9007 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9008 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9009 | psa_set_key_algorithm(&attributes, alg); |
| 9010 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
| 9011 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 9012 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9013 | |
| 9014 | /* Derive a key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9015 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9016 | input1->x, input1->len, |
| 9017 | input2->x, input2->len, |
| 9018 | capacity)) { |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9019 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9020 | } |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9021 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9022 | psa_set_key_usage_flags(&attributes, derived_usage); |
| 9023 | psa_set_key_algorithm(&attributes, derived_alg); |
| 9024 | psa_set_key_type(&attributes, derived_type); |
| 9025 | psa_set_key_bits(&attributes, derived_bits); |
| 9026 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation, |
| 9027 | &derived_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9028 | |
| 9029 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9030 | PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes)); |
| 9031 | TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type); |
| 9032 | TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9033 | |
| 9034 | /* Exercise the derived key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9035 | if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) { |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9036 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9037 | } |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9038 | |
| 9039 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9040 | /* |
| 9041 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9042 | * thus reset them as required. |
| 9043 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9044 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9045 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9046 | psa_key_derivation_abort(&operation); |
| 9047 | psa_destroy_key(base_key); |
| 9048 | psa_destroy_key(derived_key); |
| 9049 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9050 | } |
| 9051 | /* END_CASE */ |
| 9052 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9053 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9054 | void derive_key_export(int alg_arg, |
| 9055 | data_t *key_data, |
| 9056 | data_t *input1, |
| 9057 | data_t *input2, |
| 9058 | int bytes1_arg, |
| 9059 | int bytes2_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9060 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9061 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9062 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9063 | psa_algorithm_t alg = alg_arg; |
| 9064 | size_t bytes1 = bytes1_arg; |
| 9065 | size_t bytes2 = bytes2_arg; |
| 9066 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9067 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9068 | uint8_t *output_buffer = NULL; |
| 9069 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9070 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9071 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9072 | size_t length; |
| 9073 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9074 | ASSERT_ALLOC(output_buffer, capacity); |
| 9075 | ASSERT_ALLOC(export_buffer, capacity); |
| 9076 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9077 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9078 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9079 | psa_set_key_algorithm(&base_attributes, alg); |
| 9080 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9081 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9082 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9083 | |
| 9084 | /* Derive some material and output it. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9085 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9086 | input1->x, input1->len, |
| 9087 | input2->x, input2->len, |
| 9088 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9089 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9090 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9091 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9092 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9093 | output_buffer, |
| 9094 | capacity)); |
| 9095 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9096 | |
| 9097 | /* 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] | 9098 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9099 | input1->x, input1->len, |
| 9100 | input2->x, input2->len, |
| 9101 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9102 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9103 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9104 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9105 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9106 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9107 | psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA); |
| 9108 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1)); |
| 9109 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9110 | &derived_key)); |
| 9111 | PSA_ASSERT(psa_export_key(derived_key, |
| 9112 | export_buffer, bytes1, |
| 9113 | &length)); |
| 9114 | TEST_EQUAL(length, bytes1); |
| 9115 | PSA_ASSERT(psa_destroy_key(derived_key)); |
| 9116 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2)); |
| 9117 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9118 | &derived_key)); |
| 9119 | PSA_ASSERT(psa_export_key(derived_key, |
| 9120 | export_buffer + bytes1, bytes2, |
| 9121 | &length)); |
| 9122 | TEST_EQUAL(length, bytes2); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9123 | |
| 9124 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9125 | ASSERT_COMPARE(output_buffer, bytes1 + bytes2, |
| 9126 | export_buffer, capacity); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9127 | |
| 9128 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9129 | mbedtls_free(output_buffer); |
| 9130 | mbedtls_free(export_buffer); |
| 9131 | psa_key_derivation_abort(&operation); |
| 9132 | psa_destroy_key(base_key); |
| 9133 | psa_destroy_key(derived_key); |
| 9134 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9135 | } |
| 9136 | /* END_CASE */ |
| 9137 | |
| 9138 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9139 | void derive_key_type(int alg_arg, |
| 9140 | data_t *key_data, |
| 9141 | data_t *input1, |
| 9142 | data_t *input2, |
| 9143 | int key_type_arg, int bits_arg, |
| 9144 | data_t *expected_export) |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9145 | { |
| 9146 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9147 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9148 | const psa_algorithm_t alg = alg_arg; |
| 9149 | const psa_key_type_t key_type = key_type_arg; |
| 9150 | const size_t bits = bits_arg; |
| 9151 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9152 | const size_t export_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9153 | PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9154 | uint8_t *export_buffer = NULL; |
| 9155 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9156 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9157 | size_t export_length; |
| 9158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9159 | ASSERT_ALLOC(export_buffer, export_buffer_size); |
| 9160 | PSA_ASSERT(psa_crypto_init()); |
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 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9163 | psa_set_key_algorithm(&base_attributes, alg); |
| 9164 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9165 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9166 | &base_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9167 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9168 | if (mbedtls_test_psa_setup_key_derivation_wrap( |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9169 | &operation, base_key, alg, |
| 9170 | input1->x, input1->len, |
| 9171 | input2->x, input2->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9172 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) { |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9173 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9174 | } |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9175 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9176 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9177 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9178 | psa_set_key_type(&derived_attributes, key_type); |
| 9179 | psa_set_key_bits(&derived_attributes, bits); |
| 9180 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9181 | &derived_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9183 | PSA_ASSERT(psa_export_key(derived_key, |
| 9184 | export_buffer, export_buffer_size, |
| 9185 | &export_length)); |
| 9186 | ASSERT_COMPARE(export_buffer, export_length, |
| 9187 | expected_export->x, expected_export->len); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9188 | |
| 9189 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9190 | mbedtls_free(export_buffer); |
| 9191 | psa_key_derivation_abort(&operation); |
| 9192 | psa_destroy_key(base_key); |
| 9193 | psa_destroy_key(derived_key); |
| 9194 | PSA_DONE(); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9195 | } |
| 9196 | /* END_CASE */ |
| 9197 | |
| 9198 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9199 | void derive_key(int alg_arg, |
| 9200 | data_t *key_data, data_t *input1, data_t *input2, |
| 9201 | int type_arg, int bits_arg, |
| 9202 | int expected_status_arg, |
| 9203 | int is_large_output) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9204 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9205 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9206 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9207 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 9208 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9209 | size_t bits = bits_arg; |
| 9210 | psa_status_t expected_status = expected_status_arg; |
| 9211 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9212 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9213 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9215 | PSA_ASSERT(psa_crypto_init()); |
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 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9218 | psa_set_key_algorithm(&base_attributes, alg); |
| 9219 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9220 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9221 | &base_key)); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9222 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9223 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9224 | input1->x, input1->len, |
| 9225 | input2->x, input2->len, |
| 9226 | SIZE_MAX)) { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9227 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9228 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9230 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9231 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9232 | psa_set_key_type(&derived_attributes, type); |
| 9233 | psa_set_key_bits(&derived_attributes, bits); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9234 | |
| 9235 | psa_status_t status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9236 | psa_key_derivation_output_key(&derived_attributes, |
| 9237 | &operation, |
| 9238 | &derived_key); |
| 9239 | if (is_large_output > 0) { |
| 9240 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9241 | } |
| 9242 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9243 | |
| 9244 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9245 | psa_key_derivation_abort(&operation); |
| 9246 | psa_destroy_key(base_key); |
| 9247 | psa_destroy_key(derived_key); |
| 9248 | PSA_DONE(); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9249 | } |
| 9250 | /* END_CASE */ |
| 9251 | |
| 9252 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9253 | void key_agreement_setup(int alg_arg, |
| 9254 | int our_key_type_arg, int our_key_alg_arg, |
| 9255 | data_t *our_key_data, data_t *peer_key_data, |
| 9256 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9257 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9258 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9259 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 9260 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9261 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9262 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9263 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9264 | psa_status_t expected_status = expected_status_arg; |
| 9265 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9266 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9267 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9268 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9269 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9270 | psa_set_key_algorithm(&attributes, our_key_alg); |
| 9271 | psa_set_key_type(&attributes, our_key_type); |
| 9272 | PSA_ASSERT(psa_import_key(&attributes, |
| 9273 | our_key_data->x, our_key_data->len, |
| 9274 | &our_key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9275 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9276 | /* The tests currently include inputs that should fail at either step. |
| 9277 | * Test cases that fail at the setup step should be changed to call |
| 9278 | * key_derivation_setup instead, and this function should be renamed |
| 9279 | * to key_agreement_fail. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9280 | status = psa_key_derivation_setup(&operation, alg); |
| 9281 | if (status == PSA_SUCCESS) { |
| 9282 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 9283 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 9284 | our_key, |
| 9285 | peer_key_data->x, peer_key_data->len), |
| 9286 | expected_status); |
| 9287 | } else { |
| 9288 | TEST_ASSERT(status == expected_status); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9289 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9290 | |
| 9291 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9292 | psa_key_derivation_abort(&operation); |
| 9293 | psa_destroy_key(our_key); |
| 9294 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9295 | } |
| 9296 | /* END_CASE */ |
| 9297 | |
| 9298 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9299 | void raw_key_agreement(int alg_arg, |
| 9300 | int our_key_type_arg, data_t *our_key_data, |
| 9301 | data_t *peer_key_data, |
| 9302 | data_t *expected_output) |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9303 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9304 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9305 | psa_algorithm_t alg = alg_arg; |
| 9306 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9307 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9308 | unsigned char *output = NULL; |
| 9309 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9310 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9311 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9312 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9314 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9315 | psa_set_key_algorithm(&attributes, alg); |
| 9316 | psa_set_key_type(&attributes, our_key_type); |
| 9317 | PSA_ASSERT(psa_import_key(&attributes, |
| 9318 | our_key_data->x, our_key_data->len, |
| 9319 | &our_key)); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9320 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9321 | PSA_ASSERT(psa_get_key_attributes(our_key, &attributes)); |
| 9322 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9323 | |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9324 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9325 | TEST_LE_U(expected_output->len, |
| 9326 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits)); |
| 9327 | TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits), |
| 9328 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9329 | |
| 9330 | /* Good case with exact output size */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9331 | ASSERT_ALLOC(output, expected_output->len); |
| 9332 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9333 | peer_key_data->x, peer_key_data->len, |
| 9334 | output, expected_output->len, |
| 9335 | &output_length)); |
| 9336 | ASSERT_COMPARE(output, output_length, |
| 9337 | expected_output->x, expected_output->len); |
| 9338 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9339 | output = NULL; |
| 9340 | output_length = ~0; |
| 9341 | |
| 9342 | /* Larger buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9343 | ASSERT_ALLOC(output, expected_output->len + 1); |
| 9344 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9345 | peer_key_data->x, peer_key_data->len, |
| 9346 | output, expected_output->len + 1, |
| 9347 | &output_length)); |
| 9348 | ASSERT_COMPARE(output, output_length, |
| 9349 | expected_output->x, expected_output->len); |
| 9350 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9351 | output = NULL; |
| 9352 | output_length = ~0; |
| 9353 | |
| 9354 | /* Buffer too small */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9355 | ASSERT_ALLOC(output, expected_output->len - 1); |
| 9356 | TEST_EQUAL(psa_raw_key_agreement(alg, our_key, |
| 9357 | peer_key_data->x, peer_key_data->len, |
| 9358 | output, expected_output->len - 1, |
| 9359 | &output_length), |
| 9360 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9361 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9362 | TEST_LE_U(output_length, expected_output->len - 1); |
| 9363 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9364 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9365 | |
| 9366 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9367 | mbedtls_free(output); |
| 9368 | psa_destroy_key(our_key); |
| 9369 | PSA_DONE(); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9370 | } |
| 9371 | /* END_CASE */ |
| 9372 | |
| 9373 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9374 | void key_agreement_capacity(int alg_arg, |
| 9375 | int our_key_type_arg, data_t *our_key_data, |
| 9376 | data_t *peer_key_data, |
| 9377 | int expected_capacity_arg) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9378 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9379 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9380 | psa_algorithm_t alg = alg_arg; |
| 9381 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9382 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9383 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9384 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9385 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9386 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9387 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9389 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9390 | psa_set_key_algorithm(&attributes, alg); |
| 9391 | psa_set_key_type(&attributes, our_key_type); |
| 9392 | PSA_ASSERT(psa_import_key(&attributes, |
| 9393 | our_key_data->x, our_key_data->len, |
| 9394 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9395 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9396 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9397 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9398 | &operation, |
| 9399 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9400 | peer_key_data->x, peer_key_data->len)); |
| 9401 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9402 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9403 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9404 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9405 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9406 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9407 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 9408 | /* Test the advertised capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9409 | PSA_ASSERT(psa_key_derivation_get_capacity( |
| 9410 | &operation, &actual_capacity)); |
| 9411 | TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9412 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9413 | /* Test the actual capacity by reading the output. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9414 | while (actual_capacity > sizeof(output)) { |
| 9415 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9416 | output, sizeof(output))); |
| 9417 | actual_capacity -= sizeof(output); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9418 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9419 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9420 | output, actual_capacity)); |
| 9421 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1), |
| 9422 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9423 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9424 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9425 | psa_key_derivation_abort(&operation); |
| 9426 | psa_destroy_key(our_key); |
| 9427 | PSA_DONE(); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9428 | } |
| 9429 | /* END_CASE */ |
| 9430 | |
| 9431 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9432 | void key_agreement_output(int alg_arg, |
| 9433 | int our_key_type_arg, data_t *our_key_data, |
| 9434 | data_t *peer_key_data, |
| 9435 | data_t *expected_output1, data_t *expected_output2) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9436 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9437 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9438 | psa_algorithm_t alg = alg_arg; |
| 9439 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9440 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9441 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9442 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9444 | ASSERT_ALLOC(actual_output, MAX(expected_output1->len, |
| 9445 | expected_output2->len)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9446 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9447 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9449 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9450 | psa_set_key_algorithm(&attributes, alg); |
| 9451 | psa_set_key_type(&attributes, our_key_type); |
| 9452 | PSA_ASSERT(psa_import_key(&attributes, |
| 9453 | our_key_data->x, our_key_data->len, |
| 9454 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9455 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9456 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9457 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9458 | &operation, |
| 9459 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9460 | peer_key_data->x, peer_key_data->len)); |
| 9461 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9462 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9463 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9464 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9465 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9466 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9467 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9468 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9469 | actual_output, |
| 9470 | expected_output1->len)); |
| 9471 | ASSERT_COMPARE(actual_output, expected_output1->len, |
| 9472 | expected_output1->x, expected_output1->len); |
| 9473 | if (expected_output2->len != 0) { |
| 9474 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9475 | actual_output, |
| 9476 | expected_output2->len)); |
| 9477 | ASSERT_COMPARE(actual_output, expected_output2->len, |
| 9478 | expected_output2->x, expected_output2->len); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9479 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9480 | |
| 9481 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9482 | psa_key_derivation_abort(&operation); |
| 9483 | psa_destroy_key(our_key); |
| 9484 | PSA_DONE(); |
| 9485 | mbedtls_free(actual_output); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9486 | } |
| 9487 | /* END_CASE */ |
| 9488 | |
| 9489 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9490 | void generate_random(int bytes_arg) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9491 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9492 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9493 | unsigned char *output = NULL; |
| 9494 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9495 | size_t i; |
| 9496 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9497 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9498 | TEST_ASSERT(bytes_arg >= 0); |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 9499 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9500 | ASSERT_ALLOC(output, bytes); |
| 9501 | ASSERT_ALLOC(changed, bytes); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9502 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9503 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9504 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9505 | /* Run several times, to ensure that every output byte will be |
| 9506 | * nonzero at least once with overwhelming probability |
| 9507 | * (2^(-8*number_of_runs)). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9508 | for (run = 0; run < 10; run++) { |
| 9509 | if (bytes != 0) { |
| 9510 | memset(output, 0, bytes); |
| 9511 | } |
| 9512 | PSA_ASSERT(psa_generate_random(output, bytes)); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9513 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9514 | for (i = 0; i < bytes; i++) { |
| 9515 | if (output[i] != 0) { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9516 | ++changed[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9517 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9518 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9519 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9520 | |
| 9521 | /* Check that every byte was changed to nonzero at least once. This |
| 9522 | * validates that psa_generate_random is overwriting every byte of |
| 9523 | * the output buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9524 | for (i = 0; i < bytes; i++) { |
| 9525 | TEST_ASSERT(changed[i] != 0); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9526 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9527 | |
| 9528 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9529 | PSA_DONE(); |
| 9530 | mbedtls_free(output); |
| 9531 | mbedtls_free(changed); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9532 | } |
| 9533 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9534 | |
| 9535 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9536 | void generate_key(int type_arg, |
| 9537 | int bits_arg, |
| 9538 | int usage_arg, |
| 9539 | int alg_arg, |
| 9540 | int expected_status_arg, |
| 9541 | int is_large_key) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9542 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9543 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9544 | psa_key_type_t type = type_arg; |
| 9545 | psa_key_usage_t usage = usage_arg; |
| 9546 | size_t bits = bits_arg; |
| 9547 | psa_algorithm_t alg = alg_arg; |
| 9548 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9549 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 9550 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9551 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9552 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9553 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9554 | psa_set_key_usage_flags(&attributes, usage); |
| 9555 | psa_set_key_algorithm(&attributes, alg); |
| 9556 | psa_set_key_type(&attributes, type); |
| 9557 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9558 | |
| 9559 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9560 | psa_status_t status = psa_generate_key(&attributes, &key); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9562 | if (is_large_key > 0) { |
| 9563 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9564 | } |
| 9565 | TEST_EQUAL(status, expected_status); |
| 9566 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9567 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9568 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9569 | |
| 9570 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9571 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 9572 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 9573 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9574 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 9575 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9576 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 9577 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9578 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9579 | |
| 9580 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9581 | /* |
| 9582 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9583 | * thus reset them as required. |
| 9584 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9585 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9586 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9587 | psa_destroy_key(key); |
| 9588 | PSA_DONE(); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9589 | } |
| 9590 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 9591 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 9592 | /* 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] | 9593 | void generate_key_rsa(int bits_arg, |
| 9594 | data_t *e_arg, |
| 9595 | int expected_status_arg) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9596 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9597 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 9598 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9599 | size_t bits = bits_arg; |
| 9600 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 9601 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 9602 | psa_status_t expected_status = expected_status_arg; |
| 9603 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9604 | uint8_t *exported = NULL; |
| 9605 | size_t exported_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9606 | PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9607 | size_t exported_length = SIZE_MAX; |
| 9608 | uint8_t *e_read_buffer = NULL; |
| 9609 | int is_default_public_exponent = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9610 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9611 | size_t e_read_length = SIZE_MAX; |
| 9612 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9613 | if (e_arg->len == 0 || |
| 9614 | (e_arg->len == 3 && |
| 9615 | 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] | 9616 | is_default_public_exponent = 1; |
| 9617 | e_read_size = 0; |
| 9618 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9619 | ASSERT_ALLOC(e_read_buffer, e_read_size); |
| 9620 | ASSERT_ALLOC(exported, exported_size); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9621 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9622 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9623 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9624 | psa_set_key_usage_flags(&attributes, usage); |
| 9625 | psa_set_key_algorithm(&attributes, alg); |
| 9626 | PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type, |
| 9627 | e_arg->x, e_arg->len)); |
| 9628 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9629 | |
| 9630 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9631 | TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status); |
| 9632 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9633 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9634 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9635 | |
| 9636 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9637 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 9638 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 9639 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 9640 | PSA_ASSERT(psa_get_key_domain_parameters(&attributes, |
| 9641 | e_read_buffer, e_read_size, |
| 9642 | &e_read_length)); |
| 9643 | if (is_default_public_exponent) { |
| 9644 | TEST_EQUAL(e_read_length, 0); |
| 9645 | } else { |
| 9646 | ASSERT_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len); |
| 9647 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9648 | |
| 9649 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9650 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9651 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9652 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9653 | |
| 9654 | /* Export the key and check the public exponent. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9655 | PSA_ASSERT(psa_export_public_key(key, |
| 9656 | exported, exported_size, |
| 9657 | &exported_length)); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9658 | { |
| 9659 | uint8_t *p = exported; |
| 9660 | uint8_t *end = exported + exported_length; |
| 9661 | size_t len; |
| 9662 | /* RSAPublicKey ::= SEQUENCE { |
| 9663 | * modulus INTEGER, -- n |
| 9664 | * publicExponent INTEGER } -- e |
| 9665 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9666 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 9667 | MBEDTLS_ASN1_SEQUENCE | |
| 9668 | MBEDTLS_ASN1_CONSTRUCTED)); |
| 9669 | TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1)); |
| 9670 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 9671 | MBEDTLS_ASN1_INTEGER)); |
| 9672 | if (len >= 1 && p[0] == 0) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9673 | ++p; |
| 9674 | --len; |
| 9675 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9676 | if (e_arg->len == 0) { |
| 9677 | TEST_EQUAL(len, 3); |
| 9678 | TEST_EQUAL(p[0], 1); |
| 9679 | TEST_EQUAL(p[1], 0); |
| 9680 | TEST_EQUAL(p[2], 1); |
| 9681 | } else { |
| 9682 | ASSERT_COMPARE(p, len, e_arg->x, e_arg->len); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9683 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9684 | } |
| 9685 | |
| 9686 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9687 | /* |
| 9688 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 9689 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 9690 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9691 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9692 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9693 | psa_destroy_key(key); |
| 9694 | PSA_DONE(); |
| 9695 | mbedtls_free(e_read_buffer); |
| 9696 | mbedtls_free(exported); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9697 | } |
| 9698 | /* END_CASE */ |
| 9699 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9700 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9701 | void persistent_key_load_key_from_storage(data_t *data, |
| 9702 | int type_arg, int bits_arg, |
| 9703 | int usage_flags_arg, int alg_arg, |
| 9704 | int generation_method) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9705 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9706 | 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] | 9707 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9708 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9709 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9710 | psa_key_type_t type = type_arg; |
| 9711 | size_t bits = bits_arg; |
| 9712 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 9713 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9714 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9715 | unsigned char *first_export = NULL; |
| 9716 | unsigned char *second_export = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9717 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9718 | size_t first_exported_length; |
| 9719 | size_t second_exported_length; |
| 9720 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9721 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9722 | ASSERT_ALLOC(first_export, export_size); |
| 9723 | ASSERT_ALLOC(second_export, export_size); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9724 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9725 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9726 | PSA_ASSERT(psa_crypto_init()); |
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 | psa_set_key_id(&attributes, key_id); |
| 9729 | psa_set_key_usage_flags(&attributes, usage_flags); |
| 9730 | psa_set_key_algorithm(&attributes, alg); |
| 9731 | psa_set_key_type(&attributes, type); |
| 9732 | psa_set_key_bits(&attributes, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9733 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9734 | switch (generation_method) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9735 | case IMPORT_KEY: |
| 9736 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9737 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, |
| 9738 | &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9739 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9740 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9741 | case GENERATE_KEY: |
| 9742 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9743 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9744 | break; |
| 9745 | |
| 9746 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 9747 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9748 | { |
| 9749 | /* Create base key */ |
| 9750 | psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256); |
| 9751 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9752 | psa_set_key_usage_flags(&base_attributes, |
| 9753 | PSA_KEY_USAGE_DERIVE); |
| 9754 | psa_set_key_algorithm(&base_attributes, derive_alg); |
| 9755 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9756 | PSA_ASSERT(psa_import_key(&base_attributes, |
| 9757 | data->x, data->len, |
| 9758 | &base_key)); |
| 9759 | /* Derive a key. */ |
| 9760 | PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg)); |
| 9761 | PSA_ASSERT(psa_key_derivation_input_key( |
| 9762 | &operation, |
| 9763 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key)); |
| 9764 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 9765 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
| 9766 | NULL, 0)); |
| 9767 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, |
| 9768 | &operation, |
| 9769 | &key)); |
| 9770 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
| 9771 | PSA_ASSERT(psa_destroy_key(base_key)); |
| 9772 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9773 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9774 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9775 | TEST_ASSUME(!"KDF not supported in this configuration"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9776 | #endif |
| 9777 | break; |
| 9778 | |
| 9779 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9780 | TEST_ASSERT(!"generation_method not implemented in test"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9781 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9782 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9783 | psa_reset_key_attributes(&attributes); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9784 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9785 | /* Export the key if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9786 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9787 | PSA_ASSERT(psa_export_key(key, |
| 9788 | first_export, export_size, |
| 9789 | &first_exported_length)); |
| 9790 | if (generation_method == IMPORT_KEY) { |
| 9791 | ASSERT_COMPARE(data->x, data->len, |
| 9792 | first_export, first_exported_length); |
| 9793 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9794 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9795 | |
| 9796 | /* Shutdown and restart */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9797 | PSA_ASSERT(psa_purge_key(key)); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 9798 | PSA_DONE(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9799 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9800 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9801 | /* Check key slot still contains key data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9802 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 9803 | TEST_ASSERT(mbedtls_svc_key_id_equal( |
| 9804 | psa_get_key_id(&attributes), key_id)); |
| 9805 | TEST_EQUAL(psa_get_key_lifetime(&attributes), |
| 9806 | PSA_KEY_LIFETIME_PERSISTENT); |
| 9807 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 9808 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 9809 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 9810 | mbedtls_test_update_key_usage_flags(usage_flags)); |
| 9811 | TEST_EQUAL(psa_get_key_algorithm(&attributes), alg); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9812 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9813 | /* Export the key again if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9814 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9815 | PSA_ASSERT(psa_export_key(key, |
| 9816 | second_export, export_size, |
| 9817 | &second_exported_length)); |
| 9818 | ASSERT_COMPARE(first_export, first_exported_length, |
| 9819 | second_export, second_exported_length); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9820 | } |
| 9821 | |
| 9822 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9823 | if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9824 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9825 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9826 | |
| 9827 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9828 | /* |
| 9829 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9830 | * thus reset them as required. |
| 9831 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9832 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9833 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9834 | mbedtls_free(first_export); |
| 9835 | mbedtls_free(second_export); |
| 9836 | psa_key_derivation_abort(&operation); |
| 9837 | psa_destroy_key(base_key); |
| 9838 | psa_destroy_key(key); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 9839 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9840 | } |
| 9841 | /* END_CASE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9842 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 9843 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9844 | void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg, |
| 9845 | int primitive_arg, int hash_arg, int role_arg, |
| 9846 | int test_input, data_t *pw_data, |
| 9847 | int inj_err_type_arg, |
| 9848 | int expected_error_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9849 | { |
| 9850 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 9851 | psa_pake_operation_t operation = psa_pake_operation_init(); |
| 9852 | psa_algorithm_t alg = alg_arg; |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9853 | psa_pake_primitive_t primitive = primitive_arg; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 9854 | psa_key_type_t key_type_pw = key_type_pw_arg; |
| 9855 | psa_key_usage_t key_usage_pw = key_usage_pw_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9856 | psa_algorithm_t hash_alg = hash_arg; |
| 9857 | psa_pake_role_t role = role_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9858 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9859 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9860 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
| 9861 | psa_status_t expected_error = expected_error_arg; |
| 9862 | psa_status_t status; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9863 | unsigned char *output_buffer = NULL; |
| 9864 | size_t output_len = 0; |
| 9865 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9866 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9867 | |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9868 | size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9869 | PSA_PAKE_STEP_KEY_SHARE); |
| 9870 | ASSERT_ALLOC(output_buffer, buf_size); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9871 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9872 | if (pw_data->len > 0) { |
| 9873 | psa_set_key_usage_flags(&attributes, key_usage_pw); |
| 9874 | psa_set_key_algorithm(&attributes, alg); |
| 9875 | psa_set_key_type(&attributes, key_type_pw); |
| 9876 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 9877 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9878 | } |
| 9879 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9880 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 9881 | psa_pake_cs_set_primitive(&cipher_suite, primitive); |
| 9882 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9883 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9884 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 9885 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9886 | if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) { |
| 9887 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 9888 | expected_error); |
| 9889 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9890 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 9891 | expected_error); |
| 9892 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9893 | TEST_EQUAL(psa_pake_set_password_key(&operation, key), |
| 9894 | expected_error); |
| 9895 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9896 | TEST_EQUAL(psa_pake_set_role(&operation, role), |
| 9897 | expected_error); |
| 9898 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9899 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 9900 | NULL, 0, NULL), |
| 9901 | expected_error); |
| 9902 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9903 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0), |
| 9904 | expected_error); |
| 9905 | PSA_ASSERT(psa_pake_abort(&operation)); |
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 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9908 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9909 | status = psa_pake_setup(&operation, &cipher_suite); |
| 9910 | if (status != PSA_SUCCESS) { |
| 9911 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9912 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9913 | } |
| 9914 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9915 | if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) { |
| 9916 | TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite), |
| 9917 | expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9918 | goto exit; |
| 9919 | } |
| 9920 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9921 | status = psa_pake_set_role(&operation, role); |
| 9922 | if (status != PSA_SUCCESS) { |
| 9923 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9924 | goto exit; |
| 9925 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9926 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9927 | if (pw_data->len > 0) { |
| 9928 | status = psa_pake_set_password_key(&operation, key); |
| 9929 | if (status != PSA_SUCCESS) { |
| 9930 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9931 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9932 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9933 | } |
| 9934 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9935 | if (inj_err_type == INJECT_ERR_INVALID_USER) { |
| 9936 | TEST_EQUAL(psa_pake_set_user(&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_INVALID_PEER) { |
| 9942 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 9943 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9944 | goto exit; |
| 9945 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9946 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9947 | if (inj_err_type == INJECT_ERR_SET_USER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9948 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9949 | TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4), |
| 9950 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9951 | goto exit; |
| 9952 | } |
| 9953 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9954 | if (inj_err_type == INJECT_ERR_SET_PEER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9955 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9956 | TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4), |
| 9957 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9958 | goto exit; |
| 9959 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9960 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9961 | const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 9962 | PSA_PAKE_STEP_KEY_SHARE); |
| 9963 | const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 9964 | PSA_PAKE_STEP_ZK_PUBLIC); |
| 9965 | const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 9966 | PSA_PAKE_STEP_ZK_PROOF); |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9967 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9968 | if (test_input) { |
| 9969 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 9970 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0), |
| 9971 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9972 | goto exit; |
| 9973 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 9974 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9975 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 9976 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 9977 | output_buffer, size_zk_proof), |
| 9978 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9979 | goto exit; |
| 9980 | } |
| 9981 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9982 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 9983 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 9984 | output_buffer, size_zk_proof), |
| 9985 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9986 | goto exit; |
| 9987 | } |
| 9988 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9989 | status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 9990 | output_buffer, size_key_share); |
| 9991 | if (status != PSA_SUCCESS) { |
| 9992 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9993 | goto exit; |
| 9994 | } |
| 9995 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9996 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 9997 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9998 | output_buffer, size_zk_public + 1), |
| 9999 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10000 | goto exit; |
| 10001 | } |
| 10002 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10003 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10004 | // 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] | 10005 | psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10006 | output_buffer, size_zk_public + 1); |
| 10007 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10008 | output_buffer, size_zk_public), |
| 10009 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10010 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10011 | } |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10012 | } else { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10013 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 10014 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10015 | NULL, 0, NULL), |
| 10016 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10017 | goto exit; |
| 10018 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10019 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10020 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 10021 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 10022 | output_buffer, buf_size, &output_len), |
| 10023 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10024 | goto exit; |
| 10025 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10026 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10027 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 10028 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10029 | output_buffer, buf_size, &output_len), |
| 10030 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10031 | goto exit; |
| 10032 | } |
| 10033 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10034 | status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10035 | output_buffer, buf_size, &output_len); |
| 10036 | if (status != PSA_SUCCESS) { |
| 10037 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10038 | goto exit; |
| 10039 | } |
| 10040 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10041 | TEST_ASSERT(output_len > 0); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10042 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10043 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 10044 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10045 | output_buffer, size_zk_public - 1, &output_len), |
| 10046 | PSA_ERROR_BUFFER_TOO_SMALL); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10047 | goto exit; |
| 10048 | } |
| 10049 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10050 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10051 | // 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] | 10052 | psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10053 | output_buffer, size_zk_public - 1, &output_len); |
| 10054 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10055 | output_buffer, buf_size, &output_len), |
| 10056 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10057 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10058 | } |
| 10059 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10060 | |
| 10061 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10062 | PSA_ASSERT(psa_destroy_key(key)); |
| 10063 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10064 | mbedtls_free(output_buffer); |
| 10065 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10066 | } |
| 10067 | /* END_CASE */ |
| 10068 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 10069 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10070 | void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg, |
| 10071 | int client_input_first, int inject_error, |
| 10072 | data_t *pw_data) |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10073 | { |
| 10074 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10075 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10076 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10077 | psa_algorithm_t alg = alg_arg; |
| 10078 | psa_algorithm_t hash_alg = hash_arg; |
| 10079 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10080 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10081 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10082 | PSA_INIT(); |
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_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10085 | psa_set_key_algorithm(&attributes, alg); |
| 10086 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10087 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10088 | &key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10089 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10090 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10091 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10092 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10093 | |
| 10094 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10095 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10096 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
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 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10099 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
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 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10102 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10103 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10104 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10105 | client_input_first, 1, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10106 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10107 | if (inject_error == 1 || inject_error == 2) { |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10108 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10109 | } |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10110 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10111 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10112 | client_input_first, 2, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10113 | |
| 10114 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10115 | psa_destroy_key(key); |
| 10116 | psa_pake_abort(&server); |
| 10117 | psa_pake_abort(&client); |
| 10118 | PSA_DONE(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10119 | } |
| 10120 | /* END_CASE */ |
| 10121 | |
| 10122 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10123 | void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg, |
| 10124 | int derive_alg_arg, data_t *pw_data, |
| 10125 | int client_input_first, int inj_err_type_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10126 | { |
| 10127 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10128 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10129 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10130 | psa_algorithm_t alg = alg_arg; |
| 10131 | psa_algorithm_t hash_alg = hash_arg; |
| 10132 | psa_algorithm_t derive_alg = derive_alg_arg; |
| 10133 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10134 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10135 | psa_key_derivation_operation_t server_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10136 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10137 | psa_key_derivation_operation_t client_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10138 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10139 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10140 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10141 | PSA_INIT(); |
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_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10144 | psa_set_key_algorithm(&attributes, alg); |
| 10145 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10146 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10147 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10149 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10150 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10151 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10152 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10153 | /* Get shared key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10154 | PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg)); |
| 10155 | PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10156 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10157 | if (PSA_ALG_IS_TLS12_PRF(derive_alg) || |
| 10158 | PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) { |
| 10159 | PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive, |
| 10160 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10161 | (const uint8_t *) "", 0)); |
| 10162 | PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive, |
| 10163 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10164 | (const uint8_t *) "", 0)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10165 | } |
| 10166 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10167 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10168 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
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 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10171 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10172 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10173 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10174 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10175 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10176 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) { |
| 10177 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10178 | PSA_ERROR_BAD_STATE); |
| 10179 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10180 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10181 | goto exit; |
| 10182 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10183 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10184 | /* First round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10185 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10186 | client_input_first, 1, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10187 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10188 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) { |
| 10189 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10190 | PSA_ERROR_BAD_STATE); |
| 10191 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10192 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10193 | goto exit; |
| 10194 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10195 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10196 | /* Second round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10197 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10198 | client_input_first, 2, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10199 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10200 | PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive)); |
| 10201 | PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10202 | |
| 10203 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10204 | psa_key_derivation_abort(&server_derive); |
| 10205 | psa_key_derivation_abort(&client_derive); |
| 10206 | psa_destroy_key(key); |
| 10207 | psa_pake_abort(&server); |
| 10208 | psa_pake_abort(&client); |
| 10209 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10210 | } |
| 10211 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10212 | |
| 10213 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10214 | void ecjpake_size_macros() |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10215 | { |
| 10216 | const psa_algorithm_t alg = PSA_ALG_JPAKE; |
| 10217 | const size_t bits = 256; |
| 10218 | const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10219 | 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] | 10220 | 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] | 10221 | PSA_ECC_FAMILY_SECP_R1); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10222 | |
| 10223 | // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types |
| 10224 | /* 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] | 10225 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10226 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
| 10227 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10228 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10229 | /* The output for ZK_PROOF is the same bitsize as the curve */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10230 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10231 | PSA_BITS_TO_BYTES(bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10232 | |
| 10233 | /* Input sizes are the same as output sizes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10234 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10235 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE)); |
| 10236 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10237 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC)); |
| 10238 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10239 | 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] | 10240 | |
| 10241 | /* These inequalities will always hold even when other PAKEs are added */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10242 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10243 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10244 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10245 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10246 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10247 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10248 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10249 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10250 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10251 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10252 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10253 | PSA_PAKE_INPUT_MAX_SIZE); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10254 | } |
| 10255 | /* END_CASE */ |