Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2 | #include <stdint.h> |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 3 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 4 | #include "mbedtls/asn1.h" |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 5 | #include "mbedtls/asn1write.h" |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 6 | #include "mbedtls/oid.h" |
Gilles Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 7 | #include "common.h" |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 8 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 9 | /* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random() |
| 10 | * uses mbedtls_ctr_drbg internally. */ |
| 11 | #include "mbedtls/ctr_drbg.h" |
| 12 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 13 | #include "psa/crypto.h" |
Ronald Cron | 4184107 | 2020-09-17 15:28:26 +0200 | [diff] [blame] | 14 | #include "psa_crypto_slot_management.h" |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 15 | |
Manuel Pégourié-Gonnard | 7abdf7e | 2023-03-09 11:17:43 +0100 | [diff] [blame] | 16 | /* For psa_can_do_hash() */ |
| 17 | #include "psa_crypto_core.h" |
| 18 | |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 19 | #include "test/asn1_helpers.h" |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 20 | #include "test/psa_crypto_helpers.h" |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 21 | #include "test/psa_exercise_key.h" |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 22 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 23 | #include "test/drivers/test_driver.h" |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 24 | #define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION |
| 25 | #else |
| 26 | #define TEST_DRIVER_LOCATION 0x7fffff |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 27 | #endif |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 28 | |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 29 | /* If this comes up, it's a bug in the test code or in the test data. */ |
| 30 | #define UNUSED 0xdeadbeef |
| 31 | |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 32 | /* Assert that an operation is (not) active. |
| 33 | * This serves as a proxy for checking if the operation is aborted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 34 | #define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0) |
| 35 | #define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 36 | |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 37 | #if defined(PSA_WANT_ALG_JPAKE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 38 | int ecjpake_operation_setup(psa_pake_operation_t *operation, |
| 39 | psa_pake_cipher_suite_t *cipher_suite, |
| 40 | psa_pake_role_t role, |
| 41 | mbedtls_svc_key_id_t key, |
| 42 | size_t key_available) |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 43 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 44 | PSA_ASSERT(psa_pake_abort(operation)); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 45 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 46 | PSA_ASSERT(psa_pake_setup(operation, cipher_suite)); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 47 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 48 | PSA_ASSERT(psa_pake_set_role(operation, role)); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 49 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 50 | if (key_available) { |
| 51 | PSA_ASSERT(psa_pake_set_password_key(operation, key)); |
| 52 | } |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 53 | return 0; |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 54 | exit: |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 55 | return 1; |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 56 | } |
| 57 | #endif |
| 58 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 59 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 60 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 61 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 62 | /** Test if a buffer contains a constant byte value. |
| 63 | * |
| 64 | * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 65 | * |
| 66 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 67 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 68 | * \param size Size of the buffer in bytes. |
| 69 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 70 | * \return 1 if the buffer is all-bits-zero. |
| 71 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 72 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 73 | static int mem_is_char(void *buffer, unsigned char c, size_t size) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 74 | { |
| 75 | size_t i; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 76 | for (i = 0; i < size; i++) { |
| 77 | if (((unsigned char *) buffer)[i] != c) { |
| 78 | return 0; |
| 79 | } |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 80 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 81 | return 1; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 82 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 83 | #if defined(MBEDTLS_ASN1_WRITE_C) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 84 | /* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 85 | static int asn1_write_10x(unsigned char **p, |
| 86 | unsigned char *start, |
| 87 | size_t bits, |
| 88 | unsigned char x) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 89 | { |
| 90 | int ret; |
| 91 | int len = bits / 8 + 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 92 | if (bits == 0) { |
| 93 | return MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 94 | } |
| 95 | if (bits <= 8 && x >= 1 << (bits - 1)) { |
| 96 | return MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 97 | } |
| 98 | if (*p < start || *p - start < (ptrdiff_t) len) { |
| 99 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 100 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 101 | *p -= len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 102 | (*p)[len-1] = x; |
| 103 | if (bits % 8 == 0) { |
| 104 | (*p)[1] |= 1; |
| 105 | } else { |
| 106 | (*p)[0] |= 1 << (bits % 8); |
| 107 | } |
| 108 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 109 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, |
| 110 | MBEDTLS_ASN1_INTEGER)); |
| 111 | return len; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 112 | } |
| 113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 114 | static int construct_fake_rsa_key(unsigned char *buffer, |
| 115 | size_t buffer_size, |
| 116 | unsigned char **p, |
| 117 | size_t bits, |
| 118 | int keypair) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 119 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 120 | size_t half_bits = (bits + 1) / 2; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 121 | int ret; |
| 122 | int len = 0; |
| 123 | /* Construct something that looks like a DER encoding of |
| 124 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 125 | * RSAPrivateKey ::= SEQUENCE { |
| 126 | * version Version, |
| 127 | * modulus INTEGER, -- n |
| 128 | * publicExponent INTEGER, -- e |
| 129 | * privateExponent INTEGER, -- d |
| 130 | * prime1 INTEGER, -- p |
| 131 | * prime2 INTEGER, -- q |
| 132 | * exponent1 INTEGER, -- d mod (p-1) |
| 133 | * exponent2 INTEGER, -- d mod (q-1) |
| 134 | * coefficient INTEGER, -- (inverse of q) mod p |
| 135 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 136 | * } |
| 137 | * Or, for a public key, the same structure with only |
| 138 | * version, modulus and publicExponent. |
| 139 | */ |
| 140 | *p = buffer + buffer_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 141 | if (keypair) { |
| 142 | MBEDTLS_ASN1_CHK_ADD(len, /* pq */ |
| 143 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 144 | MBEDTLS_ASN1_CHK_ADD(len, /* dq */ |
| 145 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 146 | MBEDTLS_ASN1_CHK_ADD(len, /* dp */ |
| 147 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 148 | MBEDTLS_ASN1_CHK_ADD(len, /* q */ |
| 149 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 150 | MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */ |
| 151 | asn1_write_10x(p, buffer, half_bits, 3)); |
| 152 | MBEDTLS_ASN1_CHK_ADD(len, /* d */ |
| 153 | asn1_write_10x(p, buffer, bits, 1)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 154 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 155 | MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */ |
| 156 | asn1_write_10x(p, buffer, 17, 1)); |
| 157 | MBEDTLS_ASN1_CHK_ADD(len, /* n */ |
| 158 | asn1_write_10x(p, buffer, bits, 1)); |
| 159 | if (keypair) { |
| 160 | MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */ |
| 161 | mbedtls_asn1_write_int(p, buffer, 0)); |
| 162 | } |
| 163 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 164 | { |
| 165 | const unsigned char tag = |
| 166 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 167 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 168 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 169 | return len; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 170 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 171 | #endif /* MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 172 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 173 | int exercise_mac_setup(psa_key_type_t key_type, |
| 174 | const unsigned char *key_bytes, |
| 175 | size_t key_length, |
| 176 | psa_algorithm_t alg, |
| 177 | psa_mac_operation_t *operation, |
| 178 | psa_status_t *status) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 179 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 180 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 181 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 183 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 184 | psa_set_key_algorithm(&attributes, alg); |
| 185 | psa_set_key_type(&attributes, key_type); |
| 186 | PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 187 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 188 | *status = psa_mac_sign_setup(operation, key, alg); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 189 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 190 | PSA_ASSERT(psa_mac_abort(operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 191 | /* If setup failed, reproduce the failure, so that the caller can |
| 192 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 193 | if (*status != PSA_SUCCESS) { |
| 194 | TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 195 | } |
| 196 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 197 | psa_destroy_key(key); |
| 198 | return 1; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 199 | |
| 200 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 201 | psa_destroy_key(key); |
| 202 | return 0; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 203 | } |
| 204 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 205 | int exercise_cipher_setup(psa_key_type_t key_type, |
| 206 | const unsigned char *key_bytes, |
| 207 | size_t key_length, |
| 208 | psa_algorithm_t alg, |
| 209 | psa_cipher_operation_t *operation, |
| 210 | psa_status_t *status) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 211 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 212 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 213 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 215 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 216 | psa_set_key_algorithm(&attributes, alg); |
| 217 | psa_set_key_type(&attributes, key_type); |
| 218 | PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 219 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 220 | *status = psa_cipher_encrypt_setup(operation, key, alg); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 221 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 222 | PSA_ASSERT(psa_cipher_abort(operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 223 | /* If setup failed, reproduce the failure, so that the caller can |
| 224 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 225 | if (*status != PSA_SUCCESS) { |
| 226 | TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg), |
| 227 | *status); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 228 | } |
| 229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 230 | psa_destroy_key(key); |
| 231 | return 1; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 232 | |
| 233 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 234 | psa_destroy_key(key); |
| 235 | return 0; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 236 | } |
| 237 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 238 | static int test_operations_on_invalid_key(mbedtls_svc_key_id_t key) |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 239 | { |
| 240 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 241 | mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 0x6964); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 242 | uint8_t buffer[1]; |
| 243 | size_t length; |
| 244 | int ok = 0; |
| 245 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 246 | psa_set_key_id(&attributes, key_id); |
| 247 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 248 | psa_set_key_algorithm(&attributes, PSA_ALG_CTR); |
| 249 | psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); |
| 250 | TEST_EQUAL(psa_get_key_attributes(key, &attributes), |
| 251 | PSA_ERROR_INVALID_HANDLE); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 252 | TEST_EQUAL( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 253 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID(psa_get_key_id(&attributes)), 0); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 254 | TEST_EQUAL( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 255 | MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0); |
| 256 | TEST_EQUAL(psa_get_key_lifetime(&attributes), 0); |
| 257 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0); |
| 258 | TEST_EQUAL(psa_get_key_algorithm(&attributes), 0); |
| 259 | TEST_EQUAL(psa_get_key_type(&attributes), 0); |
| 260 | TEST_EQUAL(psa_get_key_bits(&attributes), 0); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 261 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 262 | TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length), |
| 263 | PSA_ERROR_INVALID_HANDLE); |
| 264 | TEST_EQUAL(psa_export_public_key(key, |
| 265 | buffer, sizeof(buffer), &length), |
| 266 | PSA_ERROR_INVALID_HANDLE); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 267 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 268 | ok = 1; |
| 269 | |
| 270 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 271 | /* |
| 272 | * Key attributes may have been returned by psa_get_key_attributes() |
| 273 | * thus reset them as required. |
| 274 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 275 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 276 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 277 | return ok; |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 278 | } |
| 279 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 280 | /* Assert that a key isn't reported as having a slot number. */ |
| 281 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 282 | #define ASSERT_NO_SLOT_NUMBER(attributes) \ |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 283 | do \ |
| 284 | { \ |
| 285 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 286 | TEST_EQUAL(psa_get_key_slot_number( \ |
| 287 | attributes, \ |
| 288 | &ASSERT_NO_SLOT_NUMBER_slot_number), \ |
| 289 | PSA_ERROR_INVALID_ARGUMENT); \ |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 290 | } \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 291 | while (0) |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 292 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 293 | #define ASSERT_NO_SLOT_NUMBER(attributes) \ |
| 294 | ((void) 0) |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 295 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 296 | |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 297 | #define INPUT_INTEGER 0x10000 /* Out of range of psa_key_type_t */ |
| 298 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 299 | /* An overapproximation of the amount of storage needed for a key of the |
| 300 | * given type and with the given content. The API doesn't make it easy |
| 301 | * to find a good value for the size. The current implementation doesn't |
| 302 | * care about the value anyway. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 303 | #define KEY_BITS_FROM_DATA(type, data) \ |
| 304 | (data)->len |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 305 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 306 | typedef enum { |
| 307 | IMPORT_KEY = 0, |
| 308 | GENERATE_KEY = 1, |
| 309 | DERIVE_KEY = 2 |
| 310 | } generate_method; |
| 311 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 312 | typedef enum { |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 313 | DO_NOT_SET_LENGTHS = 0, |
| 314 | SET_LENGTHS_BEFORE_NONCE = 1, |
| 315 | SET_LENGTHS_AFTER_NONCE = 2 |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 316 | } set_lengths_method_t; |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 317 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 318 | typedef enum { |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 319 | USE_NULL_TAG = 0, |
| 320 | USE_GIVEN_TAG = 1, |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 321 | } tag_usage_method_t; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 322 | |
Kusumit Ghoderao | a14ae5a | 2023-04-19 14:16:26 +0530 | [diff] [blame] | 323 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 324 | /*! |
| 325 | * \brief Internal Function for AEAD multipart tests. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 326 | * \param key_type_arg Type of key passed in |
| 327 | * \param key_data The encryption / decryption key data |
| 328 | * \param alg_arg The type of algorithm used |
| 329 | * \param nonce Nonce data |
| 330 | * \param additional_data Additional data |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 331 | * \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] | 332 | * feed additional data in to be encrypted / |
| 333 | * decrypted. If -1, no chunking. |
| 334 | * \param input_data Data to encrypt / decrypt |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 335 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 336 | * the data in to be encrypted / decrypted. If |
| 337 | * -1, no chunking |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 338 | * \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] | 339 | * expected here, this controls whether or not |
| 340 | * to set lengths, and in what order with |
| 341 | * respect to set nonce. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 342 | * \param expected_output Expected output |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 343 | * \param is_encrypt If non-zero this is an encryption operation. |
Paul Elliott | 41ffae1 | 2021-07-22 21:52:01 +0100 | [diff] [blame] | 344 | * \param do_zero_parts If non-zero, interleave zero length chunks |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 345 | * with normal length chunks. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 346 | * \return int Zero on failure, non-zero on success. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 347 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 348 | static int aead_multipart_internal_func(int key_type_arg, data_t *key_data, |
| 349 | int alg_arg, |
| 350 | data_t *nonce, |
| 351 | data_t *additional_data, |
| 352 | int ad_part_len_arg, |
| 353 | data_t *input_data, |
| 354 | int data_part_len_arg, |
| 355 | set_lengths_method_t set_lengths_method, |
| 356 | data_t *expected_output, |
| 357 | int is_encrypt, |
| 358 | int do_zero_parts) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 359 | { |
| 360 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 361 | psa_key_type_t key_type = key_type_arg; |
| 362 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 363 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 364 | unsigned char *output_data = NULL; |
| 365 | unsigned char *part_data = NULL; |
| 366 | unsigned char *final_data = NULL; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 367 | size_t data_true_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 368 | size_t part_data_size = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 369 | size_t output_size = 0; |
| 370 | size_t final_output_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 371 | size_t output_length = 0; |
| 372 | size_t key_bits = 0; |
| 373 | size_t tag_length = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 374 | size_t part_offset = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 375 | size_t part_length = 0; |
| 376 | size_t output_part_length = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 377 | size_t tag_size = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 378 | size_t ad_part_len = 0; |
| 379 | size_t data_part_len = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 380 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 381 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 382 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 383 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 384 | int test_ok = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 385 | size_t part_count = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 386 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 387 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 389 | if (is_encrypt) { |
| 390 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 391 | } else { |
| 392 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 393 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 394 | |
| 395 | psa_set_key_algorithm(&attributes, alg); |
| 396 | psa_set_key_type(&attributes, key_type); |
| 397 | |
| 398 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 399 | &key)); |
| 400 | |
| 401 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 402 | key_bits = psa_get_key_bits(&attributes); |
| 403 | |
| 404 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); |
| 405 | |
| 406 | if (is_encrypt) { |
| 407 | /* Tag gets written at end of buffer. */ |
| 408 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 409 | (input_data->len + |
| 410 | tag_length)); |
| 411 | data_true_size = input_data->len; |
| 412 | } else { |
| 413 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 414 | (input_data->len - |
| 415 | tag_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 416 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 417 | /* Do not want to attempt to decrypt tag. */ |
| 418 | data_true_size = input_data->len - tag_length; |
| 419 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 420 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 421 | TEST_CALLOC(output_data, output_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 422 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 423 | if (is_encrypt) { |
| 424 | final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
| 425 | TEST_LE_U(final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
| 426 | } else { |
| 427 | final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); |
| 428 | TEST_LE_U(final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 429 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 430 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 431 | TEST_CALLOC(final_data, final_output_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 433 | if (is_encrypt) { |
| 434 | status = psa_aead_encrypt_setup(&operation, key, alg); |
| 435 | } else { |
| 436 | status = psa_aead_decrypt_setup(&operation, key, alg); |
| 437 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 438 | |
| 439 | /* If the operation is not supported, just skip and not fail in case the |
| 440 | * encryption involves a common limitation of cryptography hardwares and |
| 441 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 442 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 443 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 444 | 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] | 445 | } |
| 446 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 447 | PSA_ASSERT(status); |
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 (set_lengths_method == DO_NOT_SET_LENGTHS) { |
| 450 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
| 451 | } else if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { |
| 452 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 453 | data_true_size)); |
| 454 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
| 455 | } else if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) { |
| 456 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 457 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 458 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 459 | data_true_size)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 460 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 461 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 462 | if (ad_part_len_arg != -1) { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 463 | /* Pass additional data in parts */ |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 464 | ad_part_len = (size_t) ad_part_len_arg; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 465 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 466 | for (part_offset = 0, part_count = 0; |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 467 | part_offset < additional_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 468 | part_offset += part_length, part_count++) { |
| 469 | if (do_zero_parts && (part_count & 0x01)) { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 470 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 471 | } else if (additional_data->len - part_offset < ad_part_len) { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 472 | part_length = additional_data->len - part_offset; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 473 | } else { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 474 | part_length = ad_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 475 | } |
| 476 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 477 | PSA_ASSERT(psa_aead_update_ad(&operation, |
| 478 | additional_data->x + part_offset, |
| 479 | part_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 480 | |
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 | } else { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 483 | /* Pass additional data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 484 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 485 | additional_data->len)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 486 | } |
| 487 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 488 | if (data_part_len_arg != -1) { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 489 | /* Pass data in parts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 490 | data_part_len = (size_t) data_part_len_arg; |
| 491 | part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 492 | (size_t) data_part_len); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 493 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 494 | TEST_CALLOC(part_data, part_data_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 495 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 496 | for (part_offset = 0, part_count = 0; |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 497 | part_offset < data_true_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 498 | part_offset += part_length, part_count++) { |
| 499 | if (do_zero_parts && (part_count & 0x01)) { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 500 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 501 | } else if ((data_true_size - part_offset) < data_part_len) { |
| 502 | part_length = (data_true_size - part_offset); |
| 503 | } else { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 504 | part_length = data_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 505 | } |
| 506 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 507 | PSA_ASSERT(psa_aead_update(&operation, |
| 508 | (input_data->x + part_offset), |
| 509 | part_length, part_data, |
| 510 | part_data_size, |
| 511 | &output_part_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 512 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 513 | if (output_data && output_part_length) { |
| 514 | memcpy((output_data + output_length), part_data, |
| 515 | output_part_length); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 516 | } |
| 517 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 518 | output_length += output_part_length; |
| 519 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 520 | } else { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 521 | /* Pass all data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 522 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 523 | data_true_size, output_data, |
| 524 | output_size, &output_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 525 | } |
| 526 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 527 | if (is_encrypt) { |
| 528 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 529 | final_output_size, |
| 530 | &output_part_length, |
| 531 | tag_buffer, tag_length, |
| 532 | &tag_size)); |
| 533 | } else { |
| 534 | PSA_ASSERT(psa_aead_verify(&operation, final_data, |
| 535 | final_output_size, |
| 536 | &output_part_length, |
| 537 | (input_data->x + data_true_size), |
| 538 | tag_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 539 | } |
| 540 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 541 | if (output_data && output_part_length) { |
| 542 | memcpy((output_data + output_length), final_data, |
| 543 | output_part_length); |
| 544 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 545 | |
| 546 | output_length += output_part_length; |
| 547 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 548 | |
| 549 | /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE |
| 550 | * should be exact.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 551 | if (is_encrypt) { |
| 552 | TEST_EQUAL(tag_length, tag_size); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 553 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 554 | if (output_data && tag_length) { |
| 555 | memcpy((output_data + output_length), tag_buffer, |
| 556 | tag_length); |
| 557 | } |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 558 | |
| 559 | output_length += tag_length; |
| 560 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 561 | TEST_EQUAL(output_length, |
| 562 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 563 | input_data->len)); |
| 564 | TEST_LE_U(output_length, |
| 565 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
| 566 | } else { |
| 567 | TEST_EQUAL(output_length, |
| 568 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, |
| 569 | input_data->len)); |
| 570 | TEST_LE_U(output_length, |
| 571 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 572 | } |
| 573 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 574 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 575 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 576 | output_data, output_length); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 577 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 578 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 579 | test_ok = 1; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 580 | |
| 581 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 582 | psa_destroy_key(key); |
| 583 | psa_aead_abort(&operation); |
| 584 | mbedtls_free(output_data); |
| 585 | mbedtls_free(part_data); |
| 586 | mbedtls_free(final_data); |
| 587 | PSA_DONE(); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 588 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 589 | return test_ok; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 590 | } |
| 591 | |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 592 | /*! |
| 593 | * \brief Internal Function for MAC multipart tests. |
| 594 | * \param key_type_arg Type of key passed in |
| 595 | * \param key_data The encryption / decryption key data |
| 596 | * \param alg_arg The type of algorithm used |
| 597 | * \param input_data Data to encrypt / decrypt |
| 598 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 599 | * the data in to be encrypted / decrypted. If |
| 600 | * -1, no chunking |
| 601 | * \param expected_output Expected output |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 602 | * \param is_verify If non-zero this is a verify operation. |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 603 | * \param do_zero_parts If non-zero, interleave zero length chunks |
| 604 | * with normal length chunks. |
| 605 | * \return int Zero on failure, non-zero on success. |
| 606 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 607 | static int mac_multipart_internal_func(int key_type_arg, data_t *key_data, |
| 608 | int alg_arg, |
| 609 | data_t *input_data, |
| 610 | int data_part_len_arg, |
| 611 | data_t *expected_output, |
| 612 | int is_verify, |
| 613 | int do_zero_parts) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 614 | { |
| 615 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 616 | psa_key_type_t key_type = key_type_arg; |
| 617 | psa_algorithm_t alg = alg_arg; |
| 618 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 619 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
| 620 | size_t part_offset = 0; |
| 621 | size_t part_length = 0; |
| 622 | size_t data_part_len = 0; |
| 623 | size_t mac_len = 0; |
| 624 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 625 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 626 | |
| 627 | int test_ok = 0; |
| 628 | size_t part_count = 0; |
| 629 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 630 | PSA_INIT(); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 631 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 632 | if (is_verify) { |
| 633 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 634 | } else { |
| 635 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 636 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 637 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 638 | psa_set_key_algorithm(&attributes, alg); |
| 639 | psa_set_key_type(&attributes, key_type); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 640 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 641 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 642 | &key)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 643 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 644 | if (is_verify) { |
| 645 | status = psa_mac_verify_setup(&operation, key, alg); |
| 646 | } else { |
| 647 | status = psa_mac_sign_setup(&operation, key, alg); |
| 648 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 649 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 650 | PSA_ASSERT(status); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 651 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 652 | if (data_part_len_arg != -1) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 653 | /* Pass data in parts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 654 | data_part_len = (size_t) data_part_len_arg; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 655 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 656 | for (part_offset = 0, part_count = 0; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 657 | part_offset < input_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 658 | part_offset += part_length, part_count++) { |
| 659 | if (do_zero_parts && (part_count & 0x01)) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 660 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 661 | } else if ((input_data->len - part_offset) < data_part_len) { |
| 662 | part_length = (input_data->len - part_offset); |
| 663 | } else { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 664 | part_length = data_part_len; |
| 665 | } |
| 666 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 667 | PSA_ASSERT(psa_mac_update(&operation, |
| 668 | (input_data->x + part_offset), |
| 669 | part_length)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 670 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 671 | } else { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 672 | /* Pass all data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 673 | PSA_ASSERT(psa_mac_update(&operation, input_data->x, |
| 674 | input_data->len)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 675 | } |
| 676 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 677 | if (is_verify) { |
| 678 | PSA_ASSERT(psa_mac_verify_finish(&operation, expected_output->x, |
| 679 | expected_output->len)); |
| 680 | } else { |
| 681 | PSA_ASSERT(psa_mac_sign_finish(&operation, mac, |
| 682 | PSA_MAC_MAX_SIZE, &mac_len)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 683 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 684 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 685 | mac, mac_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | test_ok = 1; |
| 689 | |
| 690 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 691 | psa_destroy_key(key); |
| 692 | psa_mac_abort(&operation); |
| 693 | PSA_DONE(); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 694 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 695 | return test_ok; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 696 | } |
| 697 | |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 698 | #if defined(PSA_WANT_ALG_JPAKE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 699 | static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive, |
| 700 | psa_pake_operation_t *server, |
| 701 | psa_pake_operation_t *client, |
| 702 | int client_input_first, |
| 703 | int round, int inject_error) |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 704 | { |
| 705 | unsigned char *buffer0 = NULL, *buffer1 = NULL; |
| 706 | size_t buffer_length = ( |
| 707 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) + |
| 708 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) + |
| 709 | 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] | 710 | /* The output should be exactly this size according to the spec */ |
| 711 | const size_t expected_size_key_share = |
| 712 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE); |
| 713 | /* The output should be exactly this size according to the spec */ |
| 714 | const size_t expected_size_zk_public = |
| 715 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC); |
| 716 | /* The output can be smaller: the spec allows stripping leading zeroes */ |
| 717 | const size_t max_expected_size_zk_proof = |
| 718 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 719 | size_t buffer0_off = 0; |
| 720 | size_t buffer1_off = 0; |
| 721 | size_t s_g1_len, s_g2_len, s_a_len; |
| 722 | size_t s_g1_off, s_g2_off, s_a_off; |
| 723 | size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len; |
| 724 | size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off; |
| 725 | size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len; |
| 726 | size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off; |
| 727 | size_t c_g1_len, c_g2_len, c_a_len; |
| 728 | size_t c_g1_off, c_g2_off, c_a_off; |
| 729 | size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len; |
| 730 | size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off; |
| 731 | size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len; |
| 732 | size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off; |
| 733 | psa_status_t expected_status = PSA_SUCCESS; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 734 | psa_status_t status; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 735 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 736 | TEST_CALLOC(buffer0, buffer_length); |
| 737 | TEST_CALLOC(buffer1, buffer_length); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 738 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 739 | switch (round) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 740 | case 1: |
| 741 | /* Server first round Output */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 742 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 743 | buffer0 + buffer0_off, |
| 744 | 512 - buffer0_off, &s_g1_len)); |
| 745 | TEST_EQUAL(s_g1_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 746 | s_g1_off = buffer0_off; |
| 747 | buffer0_off += s_g1_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 748 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 749 | buffer0 + buffer0_off, |
| 750 | 512 - buffer0_off, &s_x1_pk_len)); |
| 751 | TEST_EQUAL(s_x1_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 752 | s_x1_pk_off = buffer0_off; |
| 753 | buffer0_off += s_x1_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 754 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 755 | buffer0 + buffer0_off, |
| 756 | 512 - buffer0_off, &s_x1_pr_len)); |
| 757 | TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 758 | s_x1_pr_off = buffer0_off; |
| 759 | buffer0_off += s_x1_pr_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 760 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 761 | buffer0 + buffer0_off, |
| 762 | 512 - buffer0_off, &s_g2_len)); |
| 763 | TEST_EQUAL(s_g2_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 764 | s_g2_off = buffer0_off; |
| 765 | buffer0_off += s_g2_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 766 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 767 | buffer0 + buffer0_off, |
| 768 | 512 - buffer0_off, &s_x2_pk_len)); |
| 769 | TEST_EQUAL(s_x2_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 770 | s_x2_pk_off = buffer0_off; |
| 771 | buffer0_off += s_x2_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 772 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 773 | buffer0 + buffer0_off, |
| 774 | 512 - buffer0_off, &s_x2_pr_len)); |
| 775 | TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 776 | s_x2_pr_off = buffer0_off; |
| 777 | buffer0_off += s_x2_pr_len; |
| 778 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 779 | if (inject_error == 1) { |
Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 780 | buffer0[s_x1_pr_off + 8] ^= 1; |
| 781 | buffer0[s_x2_pr_off + 7] ^= 1; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 782 | expected_status = PSA_ERROR_DATA_INVALID; |
| 783 | } |
| 784 | |
Neil Armstrong | 51009d7 | 2022-09-05 17:59:54 +0200 | [diff] [blame] | 785 | /* |
| 786 | * When injecting errors in inputs, the implementation is |
| 787 | * free to detect it right away of with a delay. |
| 788 | * This permits delaying the error until the end of the input |
| 789 | * sequence, if no error appears then, this will be treated |
| 790 | * as an error. |
| 791 | */ |
| 792 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 793 | if (client_input_first == 1) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 794 | /* Client first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 795 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 796 | buffer0 + s_g1_off, s_g1_len); |
| 797 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 798 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 799 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 800 | } else { |
| 801 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 802 | } |
| 803 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 804 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 805 | buffer0 + s_x1_pk_off, |
| 806 | s_x1_pk_len); |
| 807 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 808 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 809 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 810 | } else { |
| 811 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 812 | } |
| 813 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 814 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 815 | buffer0 + s_x1_pr_off, |
| 816 | s_x1_pr_len); |
| 817 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 818 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 819 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 820 | } else { |
| 821 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 822 | } |
| 823 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 824 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 825 | buffer0 + s_g2_off, |
| 826 | s_g2_len); |
| 827 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 828 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 829 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 830 | } else { |
| 831 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 832 | } |
| 833 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 834 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 835 | buffer0 + s_x2_pk_off, |
| 836 | s_x2_pk_len); |
| 837 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 838 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 839 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 840 | } else { |
| 841 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 842 | } |
| 843 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 844 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 845 | buffer0 + s_x2_pr_off, |
| 846 | s_x2_pr_len); |
| 847 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 848 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 849 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 850 | } else { |
| 851 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 852 | } |
| 853 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 854 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 855 | if (inject_error == 1) { |
| 856 | TEST_ASSERT( |
| 857 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 858 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | /* Client first round Output */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 862 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 863 | buffer1 + buffer1_off, |
| 864 | 512 - buffer1_off, &c_g1_len)); |
| 865 | TEST_EQUAL(c_g1_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 866 | c_g1_off = buffer1_off; |
| 867 | buffer1_off += c_g1_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 868 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 869 | buffer1 + buffer1_off, |
| 870 | 512 - buffer1_off, &c_x1_pk_len)); |
| 871 | TEST_EQUAL(c_x1_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 872 | c_x1_pk_off = buffer1_off; |
| 873 | buffer1_off += c_x1_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 874 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 875 | buffer1 + buffer1_off, |
| 876 | 512 - buffer1_off, &c_x1_pr_len)); |
| 877 | TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 878 | c_x1_pr_off = buffer1_off; |
| 879 | buffer1_off += c_x1_pr_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 880 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 881 | buffer1 + buffer1_off, |
| 882 | 512 - buffer1_off, &c_g2_len)); |
| 883 | TEST_EQUAL(c_g2_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 884 | c_g2_off = buffer1_off; |
| 885 | buffer1_off += c_g2_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 886 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 887 | buffer1 + buffer1_off, |
| 888 | 512 - buffer1_off, &c_x2_pk_len)); |
| 889 | TEST_EQUAL(c_x2_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 890 | c_x2_pk_off = buffer1_off; |
| 891 | buffer1_off += c_x2_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 892 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 893 | buffer1 + buffer1_off, |
| 894 | 512 - buffer1_off, &c_x2_pr_len)); |
| 895 | TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 896 | c_x2_pr_off = buffer1_off; |
| 897 | buffer1_off += c_x2_pr_len; |
| 898 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 899 | if (client_input_first == 0) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 900 | /* Client first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 901 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 902 | buffer0 + s_g1_off, s_g1_len); |
| 903 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 904 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 905 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 906 | } else { |
| 907 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 908 | } |
| 909 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 910 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 911 | buffer0 + s_x1_pk_off, |
| 912 | s_x1_pk_len); |
| 913 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 914 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 915 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 916 | } else { |
| 917 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 918 | } |
| 919 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 920 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 921 | buffer0 + s_x1_pr_off, |
| 922 | s_x1_pr_len); |
| 923 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 924 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 925 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 926 | } else { |
| 927 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 928 | } |
| 929 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 930 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 931 | buffer0 + s_g2_off, |
| 932 | s_g2_len); |
| 933 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 934 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 935 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 936 | } else { |
| 937 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 938 | } |
| 939 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 940 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 941 | buffer0 + s_x2_pk_off, |
| 942 | s_x2_pk_len); |
| 943 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 944 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 945 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 946 | } else { |
| 947 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 948 | } |
| 949 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 950 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 951 | buffer0 + s_x2_pr_off, |
| 952 | s_x2_pr_len); |
| 953 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 954 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 955 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 956 | } else { |
| 957 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 958 | } |
| 959 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 960 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 961 | if (inject_error == 1) { |
| 962 | TEST_ASSERT( |
| 963 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 964 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 965 | } |
| 966 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 967 | if (inject_error == 2) { |
Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 968 | buffer1[c_x1_pr_off + 12] ^= 1; |
| 969 | buffer1[c_x2_pr_off + 7] ^= 1; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 970 | expected_status = PSA_ERROR_DATA_INVALID; |
| 971 | } |
| 972 | |
| 973 | /* Server first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 974 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 975 | buffer1 + c_g1_off, c_g1_len); |
| 976 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 977 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 978 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 979 | } else { |
| 980 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 981 | } |
| 982 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 983 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 984 | buffer1 + c_x1_pk_off, c_x1_pk_len); |
| 985 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 986 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 987 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 988 | } else { |
| 989 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 990 | } |
| 991 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 992 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 993 | buffer1 + c_x1_pr_off, c_x1_pr_len); |
| 994 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 995 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 996 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 997 | } else { |
| 998 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 999 | } |
| 1000 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1001 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 1002 | buffer1 + c_g2_off, c_g2_len); |
| 1003 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1004 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1005 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1006 | } else { |
| 1007 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1008 | } |
| 1009 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1010 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1011 | buffer1 + c_x2_pk_off, c_x2_pk_len); |
| 1012 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1013 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1014 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1015 | } else { |
| 1016 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1017 | } |
| 1018 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1019 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1020 | buffer1 + c_x2_pr_off, c_x2_pr_len); |
| 1021 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1022 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1023 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1024 | } else { |
| 1025 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1026 | } |
| 1027 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1028 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1029 | if (inject_error == 2) { |
| 1030 | TEST_ASSERT( |
| 1031 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1032 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1033 | |
| 1034 | break; |
| 1035 | |
| 1036 | case 2: |
| 1037 | /* Server second round Output */ |
| 1038 | buffer0_off = 0; |
| 1039 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1040 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 1041 | buffer0 + buffer0_off, |
| 1042 | 512 - buffer0_off, &s_a_len)); |
| 1043 | TEST_EQUAL(s_a_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1044 | s_a_off = buffer0_off; |
| 1045 | buffer0_off += s_a_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1046 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1047 | buffer0 + buffer0_off, |
| 1048 | 512 - buffer0_off, &s_x2s_pk_len)); |
| 1049 | TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1050 | s_x2s_pk_off = buffer0_off; |
| 1051 | buffer0_off += s_x2s_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1052 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1053 | buffer0 + buffer0_off, |
| 1054 | 512 - buffer0_off, &s_x2s_pr_len)); |
| 1055 | TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1056 | s_x2s_pr_off = buffer0_off; |
| 1057 | buffer0_off += s_x2s_pr_len; |
| 1058 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1059 | if (inject_error == 3) { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1060 | buffer0[s_x2s_pk_off + 12] += 0x33; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1061 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1062 | } |
| 1063 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1064 | if (client_input_first == 1) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1065 | /* Client second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1066 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1067 | buffer0 + s_a_off, s_a_len); |
| 1068 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1069 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1070 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1071 | } else { |
| 1072 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1073 | } |
| 1074 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1075 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1076 | buffer0 + s_x2s_pk_off, |
| 1077 | s_x2s_pk_len); |
| 1078 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1079 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1080 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1081 | } else { |
| 1082 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1083 | } |
| 1084 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1085 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1086 | buffer0 + s_x2s_pr_off, |
| 1087 | s_x2s_pr_len); |
| 1088 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1089 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1090 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1091 | } else { |
| 1092 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1093 | } |
| 1094 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1095 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1096 | if (inject_error == 3) { |
| 1097 | TEST_ASSERT( |
| 1098 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1099 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | /* Client second round Output */ |
| 1103 | buffer1_off = 0; |
| 1104 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1105 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1106 | buffer1 + buffer1_off, |
| 1107 | 512 - buffer1_off, &c_a_len)); |
| 1108 | TEST_EQUAL(c_a_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1109 | c_a_off = buffer1_off; |
| 1110 | buffer1_off += c_a_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1111 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1112 | buffer1 + buffer1_off, |
| 1113 | 512 - buffer1_off, &c_x2s_pk_len)); |
| 1114 | TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1115 | c_x2s_pk_off = buffer1_off; |
| 1116 | buffer1_off += c_x2s_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1117 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1118 | buffer1 + buffer1_off, |
| 1119 | 512 - buffer1_off, &c_x2s_pr_len)); |
| 1120 | TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1121 | c_x2s_pr_off = buffer1_off; |
| 1122 | buffer1_off += c_x2s_pr_len; |
| 1123 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1124 | if (client_input_first == 0) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1125 | /* Client second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1126 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1127 | buffer0 + s_a_off, s_a_len); |
| 1128 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1129 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1130 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1131 | } else { |
| 1132 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1133 | } |
| 1134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1135 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1136 | buffer0 + s_x2s_pk_off, |
| 1137 | s_x2s_pk_len); |
| 1138 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1139 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1140 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1141 | } else { |
| 1142 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1143 | } |
| 1144 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1145 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1146 | buffer0 + s_x2s_pr_off, |
| 1147 | s_x2s_pr_len); |
| 1148 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1149 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1150 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1151 | } else { |
| 1152 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1153 | } |
| 1154 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1155 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1156 | if (inject_error == 3) { |
| 1157 | TEST_ASSERT( |
| 1158 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1159 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1160 | } |
| 1161 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1162 | if (inject_error == 4) { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1163 | buffer1[c_x2s_pk_off + 7] += 0x28; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1164 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1165 | } |
| 1166 | |
| 1167 | /* Server second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1168 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 1169 | buffer1 + c_a_off, c_a_len); |
| 1170 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1171 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1172 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1173 | } else { |
| 1174 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1175 | } |
| 1176 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1177 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1178 | buffer1 + c_x2s_pk_off, c_x2s_pk_len); |
| 1179 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1180 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1181 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1182 | } else { |
| 1183 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1184 | } |
| 1185 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1186 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1187 | buffer1 + c_x2s_pr_off, c_x2s_pr_len); |
| 1188 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1189 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1190 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1191 | } else { |
| 1192 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1193 | } |
| 1194 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1195 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1196 | if (inject_error == 4) { |
| 1197 | TEST_ASSERT( |
| 1198 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1199 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1200 | |
| 1201 | break; |
| 1202 | |
| 1203 | } |
| 1204 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1205 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1206 | mbedtls_free(buffer0); |
| 1207 | mbedtls_free(buffer1); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1208 | } |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 1209 | #endif /* PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1210 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1211 | typedef enum { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 1212 | INJECT_ERR_NONE = 0, |
| 1213 | INJECT_ERR_UNINITIALIZED_ACCESS, |
| 1214 | INJECT_ERR_DUPLICATE_SETUP, |
| 1215 | INJECT_ERR_INVALID_USER, |
| 1216 | INJECT_ERR_INVALID_PEER, |
| 1217 | INJECT_ERR_SET_USER, |
| 1218 | INJECT_ERR_SET_PEER, |
| 1219 | INJECT_EMPTY_IO_BUFFER, |
| 1220 | INJECT_UNKNOWN_STEP, |
| 1221 | INJECT_INVALID_FIRST_STEP, |
| 1222 | INJECT_WRONG_BUFFER_SIZE, |
| 1223 | INJECT_VALID_OPERATION_AFTER_FAILURE, |
| 1224 | INJECT_ANTICIPATE_KEY_DERIVATION_1, |
| 1225 | INJECT_ANTICIPATE_KEY_DERIVATION_2, |
| 1226 | } ecjpake_injected_failure_t; |
| 1227 | |
Paul Elliott | 01885fa | 2023-02-09 12:07:30 +0000 | [diff] [blame] | 1228 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Paul Elliott | 1243f93 | 2023-02-07 11:21:10 +0000 | [diff] [blame] | 1229 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1230 | static void interruptible_signverify_get_minmax_completes(uint32_t max_ops, |
| 1231 | psa_status_t expected_status, |
| 1232 | size_t *min_completes, |
| 1233 | size_t *max_completes) |
| 1234 | { |
| 1235 | |
| 1236 | /* This is slightly contrived, but we only really know that with a minimum |
| 1237 | value of max_ops that a successful operation should take more than one op |
| 1238 | to complete, and likewise that with a max_ops of |
| 1239 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */ |
| 1240 | if (max_ops == 0 || max_ops == 1) { |
Paul Elliott | c86d45e | 2023-02-15 17:38:05 +0000 | [diff] [blame] | 1241 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1242 | if (expected_status == PSA_SUCCESS) { |
| 1243 | *min_completes = 2; |
| 1244 | } else { |
| 1245 | *min_completes = 1; |
| 1246 | } |
| 1247 | |
| 1248 | *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED; |
| 1249 | } else { |
| 1250 | *min_completes = 1; |
| 1251 | *max_completes = 1; |
| 1252 | } |
| 1253 | } |
Paul Elliott | 01885fa | 2023-02-09 12:07:30 +0000 | [diff] [blame] | 1254 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1255 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1256 | /* END_HEADER */ |
| 1257 | |
| 1258 | /* BEGIN_DEPENDENCIES |
| 1259 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1260 | * END_DEPENDENCIES |
| 1261 | */ |
| 1262 | |
| 1263 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 7abdf7e | 2023-03-09 11:17:43 +0100 | [diff] [blame] | 1264 | void psa_can_do_hash() |
| 1265 | { |
| 1266 | /* We can't test that this is specific to drivers until partial init has |
| 1267 | * been implemented, but we can at least test before/after full init. */ |
| 1268 | TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE)); |
| 1269 | PSA_INIT(); |
| 1270 | TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE)); |
| 1271 | PSA_DONE(); |
| 1272 | } |
| 1273 | /* END_CASE */ |
| 1274 | |
| 1275 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1276 | void static_checks() |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1277 | { |
| 1278 | size_t max_truncated_mac_size = |
| 1279 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1280 | |
| 1281 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1282 | * encoding. The shifted mask is the maximum truncated value. The |
| 1283 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1284 | TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size); |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1285 | } |
| 1286 | /* END_CASE */ |
| 1287 | |
| 1288 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1289 | void import_with_policy(int type_arg, |
| 1290 | int usage_arg, int alg_arg, |
| 1291 | int expected_status_arg) |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1292 | { |
| 1293 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1294 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1295 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1296 | psa_key_type_t type = type_arg; |
| 1297 | psa_key_usage_t usage = usage_arg; |
| 1298 | psa_algorithm_t alg = alg_arg; |
| 1299 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1300 | const uint8_t key_material[16] = { 0 }; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1301 | psa_status_t status; |
| 1302 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1303 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1304 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1305 | psa_set_key_type(&attributes, type); |
| 1306 | psa_set_key_usage_flags(&attributes, usage); |
| 1307 | psa_set_key_algorithm(&attributes, alg); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1308 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1309 | status = psa_import_key(&attributes, |
| 1310 | key_material, sizeof(key_material), |
| 1311 | &key); |
| 1312 | TEST_EQUAL(status, expected_status); |
| 1313 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1314 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1315 | } |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1316 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1317 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1318 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1319 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), |
| 1320 | mbedtls_test_update_key_usage_flags(usage)); |
| 1321 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 1322 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1323 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1324 | PSA_ASSERT(psa_destroy_key(key)); |
| 1325 | test_operations_on_invalid_key(key); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1326 | |
| 1327 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1328 | /* |
| 1329 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1330 | * thus reset them as required. |
| 1331 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1332 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1333 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1334 | psa_destroy_key(key); |
| 1335 | PSA_DONE(); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1336 | } |
| 1337 | /* END_CASE */ |
| 1338 | |
| 1339 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1340 | void import_with_data(data_t *data, int type_arg, |
| 1341 | int attr_bits_arg, |
| 1342 | int expected_status_arg) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1343 | { |
| 1344 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1345 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1346 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1347 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1348 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1349 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1350 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1352 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1354 | psa_set_key_type(&attributes, type); |
| 1355 | psa_set_key_bits(&attributes, attr_bits); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1356 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1357 | status = psa_import_key(&attributes, data->x, data->len, &key); |
Manuel Pégourié-Gonnard | 0509b58 | 2023-08-08 12:47:56 +0200 | [diff] [blame] | 1358 | /* When expecting INVALID_ARGUMENT, also accept NOT_SUPPORTED. |
| 1359 | * |
| 1360 | * This can happen with a type supported only by a driver: |
| 1361 | * - the driver sees the invalid data (for example wrong size) and thinks |
| 1362 | * "well perhaps this is a key size I don't support" so it returns |
| 1363 | * NOT_SUPPORTED which is correct at this point; |
| 1364 | * - we fallback to built-ins, which don't support this type, so return |
| 1365 | * NOT_SUPPORTED which again is correct at this point. |
| 1366 | */ |
| 1367 | if (expected_status == PSA_ERROR_INVALID_ARGUMENT && |
| 1368 | status == PSA_ERROR_NOT_SUPPORTED) { |
| 1369 | ; // OK |
| 1370 | } else { |
| 1371 | TEST_EQUAL(status, expected_status); |
| 1372 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1373 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1374 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1375 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1377 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1378 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1379 | if (attr_bits != 0) { |
| 1380 | TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes)); |
| 1381 | } |
| 1382 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1383 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1384 | PSA_ASSERT(psa_destroy_key(key)); |
| 1385 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1386 | |
| 1387 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1388 | /* |
| 1389 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1390 | * thus reset them as required. |
| 1391 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1392 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1393 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1394 | psa_destroy_key(key); |
| 1395 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1396 | } |
| 1397 | /* END_CASE */ |
| 1398 | |
| 1399 | /* BEGIN_CASE */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1400 | /* Construct and attempt to import a large unstructured key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1401 | void import_large_key(int type_arg, int byte_size_arg, |
| 1402 | int expected_status_arg) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1403 | { |
| 1404 | psa_key_type_t type = type_arg; |
| 1405 | size_t byte_size = byte_size_arg; |
| 1406 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1407 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1408 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1409 | psa_status_t status; |
| 1410 | uint8_t *buffer = NULL; |
| 1411 | size_t buffer_size = byte_size + 1; |
| 1412 | size_t n; |
| 1413 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1414 | /* Skip the test case if the target running the test cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1415 | * accommodate large keys due to heap size constraints */ |
Tom Cosgrove | 412a813 | 2023-07-20 16:55:14 +0100 | [diff] [blame] | 1416 | TEST_CALLOC_OR_SKIP(buffer, buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1417 | memset(buffer, 'K', byte_size); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1418 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1419 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1420 | |
| 1421 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1422 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1423 | psa_set_key_type(&attributes, type); |
| 1424 | status = psa_import_key(&attributes, buffer, byte_size, &key); |
| 1425 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 1426 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1427 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1428 | if (status == PSA_SUCCESS) { |
| 1429 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1430 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 1431 | TEST_EQUAL(psa_get_key_bits(&attributes), |
| 1432 | PSA_BYTES_TO_BITS(byte_size)); |
| 1433 | ASSERT_NO_SLOT_NUMBER(&attributes); |
| 1434 | memset(buffer, 0, byte_size + 1); |
| 1435 | PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n)); |
| 1436 | for (n = 0; n < byte_size; n++) { |
| 1437 | TEST_EQUAL(buffer[n], 'K'); |
| 1438 | } |
| 1439 | for (n = byte_size; n < buffer_size; n++) { |
| 1440 | TEST_EQUAL(buffer[n], 0); |
| 1441 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1445 | /* |
| 1446 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1447 | * thus reset them as required. |
| 1448 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1449 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1450 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1451 | psa_destroy_key(key); |
| 1452 | PSA_DONE(); |
| 1453 | mbedtls_free(buffer); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1454 | } |
| 1455 | /* END_CASE */ |
| 1456 | |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 1457 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1458 | /* Import an RSA key with a valid structure (but not valid numbers |
| 1459 | * inside, beyond having sensible size and parity). This is expected to |
| 1460 | * fail for large keys. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1461 | 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] | 1462 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1463 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1464 | size_t bits = bits_arg; |
| 1465 | psa_status_t expected_status = expected_status_arg; |
| 1466 | psa_status_t status; |
| 1467 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1468 | 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] | 1469 | size_t buffer_size = /* Slight overapproximations */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1470 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1471 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1472 | unsigned char *p; |
| 1473 | int ret; |
| 1474 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1475 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1476 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1477 | PSA_ASSERT(psa_crypto_init()); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1478 | TEST_CALLOC(buffer, buffer_size); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1479 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1480 | TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p, |
| 1481 | bits, keypair)) >= 0); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1482 | length = ret; |
| 1483 | |
| 1484 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1485 | psa_set_key_type(&attributes, type); |
| 1486 | status = psa_import_key(&attributes, p, length, &key); |
| 1487 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1488 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1489 | if (status == PSA_SUCCESS) { |
| 1490 | PSA_ASSERT(psa_destroy_key(key)); |
| 1491 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1492 | |
| 1493 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1494 | mbedtls_free(buffer); |
| 1495 | PSA_DONE(); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1496 | } |
| 1497 | /* END_CASE */ |
| 1498 | |
| 1499 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1500 | void import_export(data_t *data, |
| 1501 | int type_arg, |
| 1502 | int usage_arg, int alg_arg, |
| 1503 | int lifetime_arg, |
| 1504 | int expected_bits, |
| 1505 | int export_size_delta, |
| 1506 | int expected_export_status_arg, |
| 1507 | /*whether reexport must give the original input exactly*/ |
| 1508 | int canonical_input) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1509 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1510 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1511 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1512 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1513 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1514 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1515 | psa_key_lifetime_t lifetime = lifetime_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1516 | unsigned char *exported = NULL; |
| 1517 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1518 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1519 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1520 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1521 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1522 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1523 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1524 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1525 | TEST_CALLOC(exported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1526 | if (!canonical_input) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1527 | TEST_CALLOC(reexported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1528 | } |
| 1529 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1530 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1531 | psa_set_key_lifetime(&attributes, lifetime); |
| 1532 | psa_set_key_usage_flags(&attributes, usage_arg); |
| 1533 | psa_set_key_algorithm(&attributes, alg); |
| 1534 | psa_set_key_type(&attributes, type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1535 | |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1536 | if (PSA_KEY_TYPE_IS_DH(type) && |
| 1537 | expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) { |
Przemek Stekiel | 654bef0 | 2022-12-15 13:28:02 +0100 | [diff] [blame] | 1538 | /* Simulate that buffer is too small, by decreasing its size by 1 byte. */ |
| 1539 | export_size -= 1; |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1540 | } |
| 1541 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1542 | /* Import the key */ |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1543 | TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key), |
Przemek Stekiel | 2e7c33d | 2023-04-27 12:29:45 +0200 | [diff] [blame] | 1544 | PSA_SUCCESS); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1545 | |
| 1546 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1547 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1548 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1549 | TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits); |
| 1550 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1551 | |
| 1552 | /* Export the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1553 | status = psa_export_key(key, exported, export_size, &exported_length); |
| 1554 | TEST_EQUAL(status, expected_export_status); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1555 | |
| 1556 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1557 | * and export_size. On errors, the exported length must be 0. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1558 | TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH); |
| 1559 | TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0); |
| 1560 | TEST_LE_U(exported_length, export_size); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1562 | TEST_ASSERT(mem_is_char(exported + exported_length, 0, |
| 1563 | export_size - exported_length)); |
| 1564 | if (status != PSA_SUCCESS) { |
| 1565 | TEST_EQUAL(exported_length, 0); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1566 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1567 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1568 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1569 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1570 | * this validates the canonical representations. For canonical inputs, |
| 1571 | * this doesn't directly validate the implementation, but it still helps |
| 1572 | * by cross-validating the test data with the sanity check code. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1573 | if (!psa_key_lifetime_is_external(lifetime)) { |
| 1574 | if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) { |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1575 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1576 | } |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1577 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1578 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1579 | if (canonical_input) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1580 | TEST_MEMORY_COMPARE(data->x, data->len, exported, exported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1581 | } else { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1582 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1583 | PSA_ASSERT(psa_import_key(&attributes, exported, exported_length, |
| 1584 | &key2)); |
| 1585 | PSA_ASSERT(psa_export_key(key2, |
| 1586 | reexported, |
| 1587 | export_size, |
| 1588 | &reexported_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1589 | TEST_MEMORY_COMPARE(exported, exported_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1590 | reexported, reexported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1591 | PSA_ASSERT(psa_destroy_key(key2)); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1592 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1593 | TEST_LE_U(exported_length, |
| 1594 | PSA_EXPORT_KEY_OUTPUT_SIZE(type, |
| 1595 | psa_get_key_bits(&got_attributes))); |
Valerio Setti | 1eacae8 | 2023-07-28 16:07:03 +0200 | [diff] [blame] | 1596 | if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 1597 | TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
| 1598 | } else if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) { |
| 1599 | TEST_LE_U(exported_length, PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
| 1600 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1601 | |
| 1602 | destroy: |
| 1603 | /* Destroy the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1604 | PSA_ASSERT(psa_destroy_key(key)); |
| 1605 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1606 | |
| 1607 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1608 | /* |
| 1609 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1610 | * thus reset them as required. |
| 1611 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1612 | psa_reset_key_attributes(&got_attributes); |
| 1613 | psa_destroy_key(key); |
| 1614 | mbedtls_free(exported); |
| 1615 | mbedtls_free(reexported); |
| 1616 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1617 | } |
| 1618 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1619 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1620 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1621 | void import_export_public_key(data_t *data, |
| 1622 | int type_arg, // key pair or public key |
| 1623 | int alg_arg, |
| 1624 | int lifetime_arg, |
| 1625 | int export_size_delta, |
| 1626 | int expected_export_status_arg, |
| 1627 | data_t *expected_public_key) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1628 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1629 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1630 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1631 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1632 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1633 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1634 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1635 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1636 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1637 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1638 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1639 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1640 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1642 | psa_set_key_lifetime(&attributes, lifetime); |
| 1643 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1644 | psa_set_key_algorithm(&attributes, alg); |
| 1645 | psa_set_key_type(&attributes, type); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1646 | |
| 1647 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1648 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1649 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1650 | /* Export the public key */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1651 | TEST_CALLOC(exported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1652 | status = psa_export_public_key(key, |
| 1653 | exported, export_size, |
| 1654 | &exported_length); |
| 1655 | TEST_EQUAL(status, expected_export_status); |
| 1656 | if (status == PSA_SUCCESS) { |
| 1657 | 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] | 1658 | size_t bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1659 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1660 | bits = psa_get_key_bits(&attributes); |
| 1661 | TEST_LE_U(expected_public_key->len, |
| 1662 | PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1663 | TEST_LE_U(expected_public_key->len, |
| 1664 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1665 | TEST_LE_U(expected_public_key->len, |
| 1666 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1667 | TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1668 | exported, exported_length); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1669 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1670 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1671 | /* |
| 1672 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1673 | * thus reset them as required. |
| 1674 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1675 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1676 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1677 | mbedtls_free(exported); |
| 1678 | psa_destroy_key(key); |
| 1679 | PSA_DONE(); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1680 | } |
| 1681 | /* END_CASE */ |
| 1682 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1683 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1684 | void import_and_exercise_key(data_t *data, |
| 1685 | int type_arg, |
| 1686 | int bits_arg, |
| 1687 | int alg_arg) |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1688 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1689 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1690 | psa_key_type_t type = type_arg; |
| 1691 | size_t bits = bits_arg; |
| 1692 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1693 | 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] | 1694 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1695 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1696 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1697 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1698 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1699 | psa_set_key_usage_flags(&attributes, usage); |
| 1700 | psa_set_key_algorithm(&attributes, alg); |
| 1701 | psa_set_key_type(&attributes, type); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1702 | |
| 1703 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1704 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1705 | |
| 1706 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1707 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1708 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1709 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1710 | |
| 1711 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1712 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1713 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1714 | } |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1715 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1716 | PSA_ASSERT(psa_destroy_key(key)); |
| 1717 | test_operations_on_invalid_key(key); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1718 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1719 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1720 | /* |
| 1721 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1722 | * thus reset them as required. |
| 1723 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1724 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1725 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1726 | psa_reset_key_attributes(&attributes); |
| 1727 | psa_destroy_key(key); |
| 1728 | PSA_DONE(); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1729 | } |
| 1730 | /* END_CASE */ |
| 1731 | |
| 1732 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1733 | void effective_key_attributes(int type_arg, int expected_type_arg, |
| 1734 | int bits_arg, int expected_bits_arg, |
| 1735 | int usage_arg, int expected_usage_arg, |
| 1736 | int alg_arg, int expected_alg_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1737 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1738 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1739 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1740 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1741 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1742 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1743 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1744 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1745 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1746 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1747 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1748 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1749 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1750 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1751 | psa_set_key_usage_flags(&attributes, usage); |
| 1752 | psa_set_key_algorithm(&attributes, alg); |
| 1753 | psa_set_key_type(&attributes, key_type); |
| 1754 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1755 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1756 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
| 1757 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1758 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1759 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1760 | TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type); |
| 1761 | TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits); |
| 1762 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
| 1763 | TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1764 | |
| 1765 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1766 | /* |
| 1767 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1768 | * thus reset them as required. |
| 1769 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1770 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1771 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1772 | psa_destroy_key(key); |
| 1773 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1774 | } |
| 1775 | /* END_CASE */ |
| 1776 | |
| 1777 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1778 | void check_key_policy(int type_arg, int bits_arg, |
| 1779 | int usage_arg, int alg_arg) |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1780 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1781 | test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg, |
| 1782 | usage_arg, |
| 1783 | mbedtls_test_update_key_usage_flags(usage_arg), |
| 1784 | alg_arg, alg_arg); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1785 | goto exit; |
| 1786 | } |
| 1787 | /* END_CASE */ |
| 1788 | |
| 1789 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1790 | void key_attributes_init() |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1791 | { |
| 1792 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1793 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1794 | * 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] | 1795 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1796 | psa_key_attributes_t func = psa_key_attributes_init(); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1797 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1798 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1799 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1800 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1801 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1802 | TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE); |
| 1803 | TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE); |
| 1804 | TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1805 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1806 | TEST_EQUAL(psa_get_key_type(&func), 0); |
| 1807 | TEST_EQUAL(psa_get_key_type(&init), 0); |
| 1808 | TEST_EQUAL(psa_get_key_type(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1809 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1810 | TEST_EQUAL(psa_get_key_bits(&func), 0); |
| 1811 | TEST_EQUAL(psa_get_key_bits(&init), 0); |
| 1812 | TEST_EQUAL(psa_get_key_bits(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1813 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1814 | TEST_EQUAL(psa_get_key_usage_flags(&func), 0); |
| 1815 | TEST_EQUAL(psa_get_key_usage_flags(&init), 0); |
| 1816 | TEST_EQUAL(psa_get_key_usage_flags(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1817 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1818 | TEST_EQUAL(psa_get_key_algorithm(&func), 0); |
| 1819 | TEST_EQUAL(psa_get_key_algorithm(&init), 0); |
| 1820 | TEST_EQUAL(psa_get_key_algorithm(&zero), 0); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1821 | } |
| 1822 | /* END_CASE */ |
| 1823 | |
| 1824 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1825 | void mac_key_policy(int policy_usage_arg, |
| 1826 | int policy_alg_arg, |
| 1827 | int key_type_arg, |
| 1828 | data_t *key_data, |
| 1829 | int exercise_alg_arg, |
| 1830 | int expected_status_sign_arg, |
| 1831 | int expected_status_verify_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1832 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1833 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1834 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1835 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1836 | psa_key_type_t key_type = key_type_arg; |
| 1837 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 1838 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 1839 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1840 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1841 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 1842 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1843 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
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 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1846 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1847 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1848 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1849 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1850 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1851 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1852 | &key)); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1853 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1854 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 1855 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1856 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1857 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 1858 | TEST_EQUAL(status, expected_status_sign); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1859 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1860 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1861 | uint8_t input[128] = { 0 }; |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1862 | size_t mac_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1863 | TEST_EQUAL(psa_mac_compute(key, exercise_alg, |
| 1864 | input, 128, |
| 1865 | mac, PSA_MAC_MAX_SIZE, &mac_len), |
| 1866 | expected_status_sign); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1867 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1868 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1869 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 1870 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 1871 | if (status == PSA_SUCCESS) { |
| 1872 | status = psa_mac_update(&operation, input, 128); |
| 1873 | if (status == PSA_SUCCESS) { |
| 1874 | TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE, |
| 1875 | &mac_len), |
| 1876 | expected_status_sign); |
| 1877 | } else { |
| 1878 | TEST_EQUAL(status, expected_status_sign); |
| 1879 | } |
| 1880 | } else { |
| 1881 | TEST_EQUAL(status, expected_status_sign); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1882 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1883 | PSA_ASSERT(psa_mac_abort(&operation)); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1884 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1885 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1886 | status = psa_mac_verify(key, exercise_alg, input, 128, |
| 1887 | mac, mac_len); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1888 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1889 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 1890 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1891 | } else { |
| 1892 | TEST_EQUAL(status, expected_status_verify); |
| 1893 | } |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1894 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1895 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1896 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 1897 | if (status == PSA_SUCCESS) { |
| 1898 | status = psa_mac_update(&operation, input, 128); |
| 1899 | if (status == PSA_SUCCESS) { |
| 1900 | status = psa_mac_verify_finish(&operation, mac, mac_len); |
| 1901 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 1902 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1903 | } else { |
| 1904 | TEST_EQUAL(status, expected_status_verify); |
| 1905 | } |
| 1906 | } else { |
| 1907 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1908 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1909 | } else { |
| 1910 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1911 | } |
| 1912 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1913 | psa_mac_abort(&operation); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1914 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1915 | memset(mac, 0, sizeof(mac)); |
| 1916 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 1917 | TEST_EQUAL(status, expected_status_verify); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1918 | |
| 1919 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1920 | psa_mac_abort(&operation); |
| 1921 | psa_destroy_key(key); |
| 1922 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1923 | } |
| 1924 | /* END_CASE */ |
| 1925 | |
| 1926 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1927 | void cipher_key_policy(int policy_usage_arg, |
| 1928 | int policy_alg, |
| 1929 | int key_type, |
| 1930 | data_t *key_data, |
| 1931 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1932 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1933 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1934 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1935 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1936 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1937 | size_t output_buffer_size = 0; |
| 1938 | size_t input_buffer_size = 0; |
| 1939 | size_t output_length = 0; |
| 1940 | uint8_t *output = NULL; |
| 1941 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1942 | psa_status_t status; |
| 1943 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1944 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg); |
| 1945 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg, |
| 1946 | input_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1947 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1948 | TEST_CALLOC(input, input_buffer_size); |
| 1949 | TEST_CALLOC(output, output_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1950 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1951 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1952 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1953 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1954 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1955 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1956 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1957 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1958 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1959 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1960 | /* Check if no key usage flag implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1961 | TEST_EQUAL(policy_usage, |
| 1962 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1963 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1964 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1965 | status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size, |
| 1966 | output, output_buffer_size, |
| 1967 | &output_length); |
| 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 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1974 | |
| 1975 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1976 | status = psa_cipher_encrypt_setup(&operation, key, exercise_alg); |
| 1977 | if (policy_alg == exercise_alg && |
| 1978 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1979 | PSA_ASSERT(status); |
| 1980 | } else { |
| 1981 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1982 | } |
| 1983 | psa_cipher_abort(&operation); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1984 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1985 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1986 | status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size, |
| 1987 | input, input_buffer_size, |
| 1988 | &output_length); |
| 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 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1995 | |
| 1996 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1997 | status = psa_cipher_decrypt_setup(&operation, key, exercise_alg); |
| 1998 | if (policy_alg == exercise_alg && |
| 1999 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2000 | PSA_ASSERT(status); |
| 2001 | } else { |
| 2002 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2003 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2004 | |
| 2005 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2006 | psa_cipher_abort(&operation); |
| 2007 | mbedtls_free(input); |
| 2008 | mbedtls_free(output); |
| 2009 | psa_destroy_key(key); |
| 2010 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2011 | } |
| 2012 | /* END_CASE */ |
| 2013 | |
| 2014 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2015 | void aead_key_policy(int policy_usage_arg, |
| 2016 | int policy_alg, |
| 2017 | int key_type, |
| 2018 | data_t *key_data, |
| 2019 | int nonce_length_arg, |
| 2020 | int tag_length_arg, |
| 2021 | int exercise_alg, |
| 2022 | int expected_status_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2023 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2024 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2025 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2026 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2027 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2028 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2029 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2030 | unsigned char nonce[16] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2031 | size_t nonce_length = nonce_length_arg; |
| 2032 | unsigned char tag[16]; |
| 2033 | size_t tag_length = tag_length_arg; |
| 2034 | size_t output_length; |
| 2035 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2036 | TEST_LE_U(nonce_length, sizeof(nonce)); |
| 2037 | TEST_LE_U(tag_length, sizeof(tag)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2038 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2039 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2040 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2041 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2042 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2043 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2044 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2045 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2046 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2047 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2048 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2049 | TEST_EQUAL(policy_usage, |
| 2050 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2051 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2052 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2053 | status = psa_aead_encrypt(key, exercise_alg, |
| 2054 | nonce, nonce_length, |
| 2055 | NULL, 0, |
| 2056 | NULL, 0, |
| 2057 | tag, tag_length, |
| 2058 | &output_length); |
| 2059 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2060 | TEST_EQUAL(status, expected_status); |
| 2061 | } else { |
| 2062 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2063 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2064 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2065 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2066 | status = psa_aead_encrypt_setup(&operation, key, exercise_alg); |
| 2067 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2068 | TEST_EQUAL(status, expected_status); |
| 2069 | } else { |
| 2070 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2071 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2072 | |
| 2073 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2074 | memset(tag, 0, sizeof(tag)); |
| 2075 | status = psa_aead_decrypt(key, exercise_alg, |
| 2076 | nonce, nonce_length, |
| 2077 | NULL, 0, |
| 2078 | tag, tag_length, |
| 2079 | NULL, 0, |
| 2080 | &output_length); |
| 2081 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2082 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2083 | } else if (expected_status == PSA_SUCCESS) { |
| 2084 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2085 | } else { |
| 2086 | TEST_EQUAL(status, expected_status); |
| 2087 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2088 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2089 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2090 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2091 | status = psa_aead_decrypt_setup(&operation, key, exercise_alg); |
| 2092 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2093 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2094 | } else { |
| 2095 | TEST_EQUAL(status, expected_status); |
| 2096 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2097 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2098 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2099 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2100 | psa_destroy_key(key); |
| 2101 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2102 | } |
| 2103 | /* END_CASE */ |
| 2104 | |
| 2105 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2106 | void asymmetric_encryption_key_policy(int policy_usage_arg, |
| 2107 | int policy_alg, |
| 2108 | int key_type, |
| 2109 | data_t *key_data, |
| 2110 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2111 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2112 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2113 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2114 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2115 | psa_status_t status; |
| 2116 | size_t key_bits; |
| 2117 | size_t buffer_length; |
| 2118 | unsigned char *buffer = NULL; |
| 2119 | size_t output_length; |
| 2120 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2121 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2122 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2123 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2124 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2125 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2126 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2127 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2128 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2129 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2130 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2131 | TEST_EQUAL(policy_usage, |
| 2132 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2133 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2134 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 2135 | key_bits = psa_get_key_bits(&attributes); |
| 2136 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, |
| 2137 | exercise_alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2138 | TEST_CALLOC(buffer, buffer_length); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2139 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2140 | status = psa_asymmetric_encrypt(key, exercise_alg, |
| 2141 | NULL, 0, |
| 2142 | NULL, 0, |
| 2143 | buffer, buffer_length, |
| 2144 | &output_length); |
| 2145 | if (policy_alg == exercise_alg && |
| 2146 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2147 | PSA_ASSERT(status); |
| 2148 | } else { |
| 2149 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2150 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2151 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2152 | if (buffer_length != 0) { |
| 2153 | memset(buffer, 0, buffer_length); |
| 2154 | } |
| 2155 | status = psa_asymmetric_decrypt(key, exercise_alg, |
| 2156 | buffer, buffer_length, |
| 2157 | NULL, 0, |
| 2158 | buffer, buffer_length, |
| 2159 | &output_length); |
| 2160 | if (policy_alg == exercise_alg && |
| 2161 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2162 | TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING); |
| 2163 | } else { |
| 2164 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2165 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2166 | |
| 2167 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2168 | /* |
| 2169 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2170 | * thus reset them as required. |
| 2171 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2172 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2173 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2174 | psa_destroy_key(key); |
| 2175 | PSA_DONE(); |
| 2176 | mbedtls_free(buffer); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2177 | } |
| 2178 | /* END_CASE */ |
| 2179 | |
| 2180 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2181 | void asymmetric_signature_key_policy(int policy_usage_arg, |
| 2182 | int policy_alg, |
| 2183 | int key_type, |
| 2184 | data_t *key_data, |
| 2185 | int exercise_alg, |
| 2186 | int payload_length_arg, |
| 2187 | int expected_usage_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2188 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2189 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2190 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2191 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 2192 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2193 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2194 | unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 }; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2195 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2196 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2197 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2198 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2199 | int compatible_alg = payload_length_arg > 0; |
| 2200 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2201 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2202 | size_t signature_length; |
| 2203 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2204 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2205 | in the expected usage flags. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2206 | TEST_EQUAL(expected_usage, |
| 2207 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2208 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2209 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2210 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2211 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2212 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2213 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2215 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2216 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2217 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2218 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2219 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2220 | status = psa_sign_hash(key, exercise_alg, |
| 2221 | payload, payload_length, |
| 2222 | signature, sizeof(signature), |
| 2223 | &signature_length); |
| 2224 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) { |
| 2225 | PSA_ASSERT(status); |
| 2226 | } else { |
| 2227 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2228 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2230 | memset(signature, 0, sizeof(signature)); |
| 2231 | status = psa_verify_hash(key, exercise_alg, |
| 2232 | payload, payload_length, |
| 2233 | signature, sizeof(signature)); |
| 2234 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) { |
| 2235 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2236 | } else { |
| 2237 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2238 | } |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2239 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2240 | if (PSA_ALG_IS_SIGN_HASH(exercise_alg) && |
| 2241 | PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) { |
| 2242 | status = psa_sign_message(key, exercise_alg, |
| 2243 | payload, payload_length, |
| 2244 | signature, sizeof(signature), |
| 2245 | &signature_length); |
| 2246 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) { |
| 2247 | PSA_ASSERT(status); |
| 2248 | } else { |
| 2249 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2250 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2252 | memset(signature, 0, sizeof(signature)); |
| 2253 | status = psa_verify_message(key, exercise_alg, |
| 2254 | payload, payload_length, |
| 2255 | signature, sizeof(signature)); |
| 2256 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) { |
| 2257 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2258 | } else { |
| 2259 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2260 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2261 | } |
| 2262 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2263 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2264 | psa_destroy_key(key); |
| 2265 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2266 | } |
| 2267 | /* END_CASE */ |
| 2268 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2269 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2270 | void derive_key_policy(int policy_usage, |
| 2271 | int policy_alg, |
| 2272 | int key_type, |
| 2273 | data_t *key_data, |
| 2274 | int exercise_alg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2275 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2276 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2277 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2278 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2279 | psa_status_t status; |
| 2280 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2281 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2282 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2283 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2284 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2285 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2286 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2287 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2288 | &key)); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2289 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2290 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2291 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2292 | if (PSA_ALG_IS_TLS12_PRF(exercise_alg) || |
| 2293 | PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) { |
| 2294 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 2295 | &operation, |
| 2296 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2297 | (const uint8_t *) "", 0)); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2298 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2299 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2300 | status = psa_key_derivation_input_key(&operation, |
| 2301 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 2302 | key); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2303 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2304 | if (policy_alg == exercise_alg && |
| 2305 | (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) { |
| 2306 | PSA_ASSERT(status); |
| 2307 | } else { |
| 2308 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2309 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2310 | |
| 2311 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2312 | psa_key_derivation_abort(&operation); |
| 2313 | psa_destroy_key(key); |
| 2314 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2315 | } |
| 2316 | /* END_CASE */ |
| 2317 | |
| 2318 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2319 | void agreement_key_policy(int policy_usage, |
| 2320 | int policy_alg, |
| 2321 | int key_type_arg, |
| 2322 | data_t *key_data, |
| 2323 | int exercise_alg, |
| 2324 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2325 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2326 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2327 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2328 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2329 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2330 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2331 | psa_status_t expected_status = expected_status_arg; |
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_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2334 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2335 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2336 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2337 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2338 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2339 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2340 | &key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2341 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2342 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
| 2343 | status = mbedtls_test_psa_key_agreement_with_self(&operation, key); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2344 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2345 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2346 | |
| 2347 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2348 | psa_key_derivation_abort(&operation); |
| 2349 | psa_destroy_key(key); |
| 2350 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2351 | } |
| 2352 | /* END_CASE */ |
| 2353 | |
| 2354 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2355 | void key_policy_alg2(int key_type_arg, data_t *key_data, |
| 2356 | int usage_arg, int alg_arg, int alg2_arg) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2357 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2358 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2359 | psa_key_type_t key_type = key_type_arg; |
| 2360 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2361 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2362 | psa_key_usage_t usage = usage_arg; |
| 2363 | psa_algorithm_t alg = alg_arg; |
| 2364 | psa_algorithm_t alg2 = alg2_arg; |
| 2365 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2366 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2367 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2368 | psa_set_key_usage_flags(&attributes, usage); |
| 2369 | psa_set_key_algorithm(&attributes, alg); |
| 2370 | psa_set_key_enrollment_algorithm(&attributes, alg2); |
| 2371 | psa_set_key_type(&attributes, key_type); |
| 2372 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2373 | &key)); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2374 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2375 | /* Update the usage flags to obtain implicit usage flags */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2376 | usage = mbedtls_test_update_key_usage_flags(usage); |
| 2377 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 2378 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage); |
| 2379 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 2380 | TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2381 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2382 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2383 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2384 | } |
| 2385 | if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2386 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2387 | } |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2388 | |
| 2389 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2390 | /* |
| 2391 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2392 | * thus reset them as required. |
| 2393 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2394 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2395 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2396 | psa_destroy_key(key); |
| 2397 | PSA_DONE(); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2398 | } |
| 2399 | /* END_CASE */ |
| 2400 | |
| 2401 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2402 | void raw_agreement_key_policy(int policy_usage, |
| 2403 | int policy_alg, |
| 2404 | int key_type_arg, |
| 2405 | data_t *key_data, |
| 2406 | int exercise_alg, |
| 2407 | int expected_status_arg) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2408 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2409 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2410 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2411 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2412 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2413 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2414 | psa_status_t expected_status = expected_status_arg; |
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 | PSA_ASSERT(psa_crypto_init()); |
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 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2419 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2420 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2421 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2422 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2423 | &key)); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2424 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2425 | status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2426 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2427 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2428 | |
| 2429 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2430 | psa_key_derivation_abort(&operation); |
| 2431 | psa_destroy_key(key); |
| 2432 | PSA_DONE(); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2433 | } |
| 2434 | /* END_CASE */ |
| 2435 | |
| 2436 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2437 | void copy_success(int source_usage_arg, |
| 2438 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4ea4ad0 | 2022-12-04 15:11:00 +0100 | [diff] [blame] | 2439 | int source_lifetime_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2440 | int type_arg, data_t *material, |
| 2441 | int copy_attributes, |
| 2442 | int target_usage_arg, |
| 2443 | int target_alg_arg, int target_alg2_arg, |
Gilles Peskine | 4ea4ad0 | 2022-12-04 15:11:00 +0100 | [diff] [blame] | 2444 | int target_lifetime_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2445 | int expected_usage_arg, |
| 2446 | int expected_alg_arg, int expected_alg2_arg) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2447 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2448 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2449 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2450 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2451 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2452 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2453 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 2454 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2455 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2456 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2457 | uint8_t *export_buffer = NULL; |
| 2458 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2459 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2460 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2461 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2462 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2463 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2464 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2465 | psa_set_key_type(&source_attributes, type_arg); |
| 2466 | psa_set_key_lifetime(&source_attributes, source_lifetime); |
| 2467 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2468 | material->x, material->len, |
| 2469 | &source_key)); |
| 2470 | PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2471 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2472 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2473 | if (copy_attributes) { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2474 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2475 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2476 | psa_set_key_lifetime(&target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2477 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2478 | if (target_usage_arg != -1) { |
| 2479 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2480 | } |
| 2481 | if (target_alg_arg != -1) { |
| 2482 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2483 | } |
| 2484 | if (target_alg2_arg != -1) { |
| 2485 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
| 2486 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2487 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2488 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2489 | /* Copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2490 | PSA_ASSERT(psa_copy_key(source_key, |
| 2491 | &target_attributes, &target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2492 | |
| 2493 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2494 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2495 | |
| 2496 | /* Test that the target slot has the expected content and policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2497 | PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes)); |
| 2498 | TEST_EQUAL(psa_get_key_type(&source_attributes), |
| 2499 | psa_get_key_type(&target_attributes)); |
| 2500 | TEST_EQUAL(psa_get_key_bits(&source_attributes), |
| 2501 | psa_get_key_bits(&target_attributes)); |
| 2502 | TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes)); |
| 2503 | TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes)); |
| 2504 | TEST_EQUAL(expected_alg2, |
| 2505 | psa_get_key_enrollment_algorithm(&target_attributes)); |
| 2506 | if (expected_usage & PSA_KEY_USAGE_EXPORT) { |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2507 | size_t length; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2508 | TEST_CALLOC(export_buffer, material->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2509 | PSA_ASSERT(psa_export_key(target_key, export_buffer, |
| 2510 | material->len, &length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2511 | TEST_MEMORY_COMPARE(material->x, material->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2512 | export_buffer, length); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2513 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2514 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2515 | if (!psa_key_lifetime_is_external(target_lifetime)) { |
| 2516 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2517 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2518 | } |
| 2519 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2520 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2521 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2522 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2523 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2524 | PSA_ASSERT(psa_destroy_key(target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2525 | |
| 2526 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2527 | /* |
| 2528 | * Source and target key attributes may have been returned by |
| 2529 | * psa_get_key_attributes() thus reset them as required. |
| 2530 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2531 | psa_reset_key_attributes(&source_attributes); |
| 2532 | psa_reset_key_attributes(&target_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2533 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2534 | PSA_DONE(); |
| 2535 | mbedtls_free(export_buffer); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2536 | } |
| 2537 | /* END_CASE */ |
| 2538 | |
| 2539 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2540 | void copy_fail(int source_usage_arg, |
| 2541 | int source_alg_arg, int source_alg2_arg, |
| 2542 | int source_lifetime_arg, |
| 2543 | int type_arg, data_t *material, |
| 2544 | int target_type_arg, int target_bits_arg, |
| 2545 | int target_usage_arg, |
| 2546 | int target_alg_arg, int target_alg2_arg, |
| 2547 | int target_id_arg, int target_lifetime_arg, |
| 2548 | int expected_status_arg) |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2549 | { |
| 2550 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2551 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2552 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2553 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2554 | 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] | 2555 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2556 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2557 | |
| 2558 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2559 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2560 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2561 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2562 | psa_set_key_type(&source_attributes, type_arg); |
| 2563 | psa_set_key_lifetime(&source_attributes, source_lifetime_arg); |
| 2564 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2565 | material->x, material->len, |
| 2566 | &source_key)); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2567 | |
| 2568 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2569 | psa_set_key_id(&target_attributes, key_id); |
| 2570 | psa_set_key_lifetime(&target_attributes, target_lifetime_arg); |
| 2571 | psa_set_key_type(&target_attributes, target_type_arg); |
| 2572 | psa_set_key_bits(&target_attributes, target_bits_arg); |
| 2573 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2574 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2575 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2576 | |
| 2577 | /* Try to copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2578 | TEST_EQUAL(psa_copy_key(source_key, |
| 2579 | &target_attributes, &target_key), |
| 2580 | expected_status_arg); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2581 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2582 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2583 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2584 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2585 | psa_reset_key_attributes(&source_attributes); |
| 2586 | psa_reset_key_attributes(&target_attributes); |
| 2587 | PSA_DONE(); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2588 | } |
| 2589 | /* END_CASE */ |
| 2590 | |
| 2591 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2592 | void hash_operation_init() |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2593 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2594 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2595 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2596 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2597 | * 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] | 2598 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2599 | psa_hash_operation_t func = psa_hash_operation_init(); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2600 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2601 | psa_hash_operation_t zero; |
| 2602 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2603 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2604 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2605 | /* A freshly-initialized hash operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2606 | TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)), |
| 2607 | PSA_ERROR_BAD_STATE); |
| 2608 | TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)), |
| 2609 | PSA_ERROR_BAD_STATE); |
| 2610 | TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)), |
| 2611 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2612 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2613 | /* A default hash operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2614 | PSA_ASSERT(psa_hash_abort(&func)); |
| 2615 | PSA_ASSERT(psa_hash_abort(&init)); |
| 2616 | PSA_ASSERT(psa_hash_abort(&zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2617 | } |
| 2618 | /* END_CASE */ |
| 2619 | |
| 2620 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2621 | void hash_setup(int alg_arg, |
| 2622 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2623 | { |
| 2624 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2625 | uint8_t *output = NULL; |
| 2626 | size_t output_size = 0; |
| 2627 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2628 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2629 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2630 | psa_status_t status; |
| 2631 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2632 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2633 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2634 | /* Hash Setup, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2635 | output_size = PSA_HASH_LENGTH(alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2636 | TEST_CALLOC(output, output_size); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2637 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2638 | status = psa_hash_compute(alg, NULL, 0, |
| 2639 | output, output_size, &output_length); |
| 2640 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2641 | |
| 2642 | /* Hash Setup, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2643 | status = psa_hash_setup(&operation, alg); |
| 2644 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2645 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2646 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2647 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2648 | |
| 2649 | /* If setup failed, reproduce the failure, so as to |
| 2650 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2651 | if (status != PSA_SUCCESS) { |
| 2652 | TEST_EQUAL(psa_hash_setup(&operation, alg), status); |
| 2653 | } |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2654 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2655 | /* Now the operation object should be reusable. */ |
| 2656 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2657 | PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG)); |
| 2658 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2659 | #endif |
| 2660 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2661 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2662 | mbedtls_free(output); |
| 2663 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2664 | } |
| 2665 | /* END_CASE */ |
| 2666 | |
| 2667 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2668 | void hash_compute_fail(int alg_arg, data_t *input, |
| 2669 | int output_size_arg, int expected_status_arg) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2670 | { |
| 2671 | psa_algorithm_t alg = alg_arg; |
| 2672 | uint8_t *output = NULL; |
| 2673 | size_t output_size = output_size_arg; |
| 2674 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2675 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2676 | psa_status_t expected_status = expected_status_arg; |
| 2677 | psa_status_t status; |
| 2678 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2679 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2680 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2681 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2682 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2683 | /* Hash Compute, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2684 | status = psa_hash_compute(alg, input->x, input->len, |
| 2685 | output, output_size, &output_length); |
| 2686 | TEST_EQUAL(status, expected_status); |
| 2687 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2688 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2689 | /* Hash Compute, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2690 | status = psa_hash_setup(&operation, alg); |
| 2691 | if (status == PSA_SUCCESS) { |
| 2692 | status = psa_hash_update(&operation, input->x, input->len); |
| 2693 | if (status == PSA_SUCCESS) { |
| 2694 | status = psa_hash_finish(&operation, output, output_size, |
| 2695 | &output_length); |
| 2696 | if (status == PSA_SUCCESS) { |
| 2697 | TEST_LE_U(output_length, output_size); |
| 2698 | } else { |
| 2699 | TEST_EQUAL(status, expected_status); |
| 2700 | } |
| 2701 | } else { |
| 2702 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2703 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2704 | } else { |
| 2705 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2706 | } |
| 2707 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2708 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2709 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2710 | mbedtls_free(output); |
| 2711 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2712 | } |
| 2713 | /* END_CASE */ |
| 2714 | |
| 2715 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2716 | void hash_compare_fail(int alg_arg, data_t *input, |
| 2717 | data_t *reference_hash, |
| 2718 | int expected_status_arg) |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2719 | { |
| 2720 | psa_algorithm_t alg = alg_arg; |
| 2721 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2722 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2723 | psa_status_t status; |
| 2724 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2725 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2726 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2727 | /* Hash Compare, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2728 | status = psa_hash_compare(alg, input->x, input->len, |
| 2729 | reference_hash->x, reference_hash->len); |
| 2730 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2731 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2732 | /* Hash Compare, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2733 | status = psa_hash_setup(&operation, alg); |
| 2734 | if (status == PSA_SUCCESS) { |
| 2735 | status = psa_hash_update(&operation, input->x, input->len); |
| 2736 | if (status == PSA_SUCCESS) { |
| 2737 | status = psa_hash_verify(&operation, reference_hash->x, |
| 2738 | reference_hash->len); |
| 2739 | TEST_EQUAL(status, expected_status); |
| 2740 | } else { |
| 2741 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2742 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2743 | } else { |
| 2744 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2745 | } |
| 2746 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2747 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2748 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2749 | PSA_DONE(); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2750 | } |
| 2751 | /* END_CASE */ |
| 2752 | |
| 2753 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2754 | void hash_compute_compare(int alg_arg, data_t *input, |
| 2755 | data_t *expected_output) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2756 | { |
| 2757 | psa_algorithm_t alg = alg_arg; |
| 2758 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2759 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2760 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2761 | size_t i; |
| 2762 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2763 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2764 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2765 | /* Compute with tight buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2766 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 2767 | output, PSA_HASH_LENGTH(alg), |
| 2768 | &output_length)); |
| 2769 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2770 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2771 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2772 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2773 | /* Compute with tight buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2774 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2775 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2776 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 2777 | PSA_HASH_LENGTH(alg), |
| 2778 | &output_length)); |
| 2779 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2780 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2781 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2782 | |
| 2783 | /* Compute with larger buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2784 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 2785 | output, sizeof(output), |
| 2786 | &output_length)); |
| 2787 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2788 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2789 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2790 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2791 | /* Compute with larger buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2792 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2793 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2794 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 2795 | sizeof(output), &output_length)); |
| 2796 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2797 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2798 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2799 | |
| 2800 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2801 | PSA_ASSERT(psa_hash_compare(alg, input->x, input->len, |
| 2802 | output, output_length)); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2803 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2804 | /* Compare with correct hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2805 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2806 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2807 | PSA_ASSERT(psa_hash_verify(&operation, output, |
| 2808 | output_length)); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2809 | |
| 2810 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2811 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2812 | output, output_length + 1), |
| 2813 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2814 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2815 | /* Compare with trailing garbage, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2816 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2817 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2818 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1), |
| 2819 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2820 | |
| 2821 | /* Compare with truncated hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2822 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2823 | output, output_length - 1), |
| 2824 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2825 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2826 | /* Compare with truncated hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2827 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2828 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2829 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1), |
| 2830 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2831 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2832 | /* Compare with corrupted value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2833 | for (i = 0; i < output_length; i++) { |
| 2834 | mbedtls_test_set_step(i); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2835 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2836 | |
| 2837 | /* One-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2838 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2839 | output, output_length), |
| 2840 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2841 | |
| 2842 | /* Multi-Part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2843 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2844 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2845 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length), |
| 2846 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2847 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2848 | output[i] ^= 1; |
| 2849 | } |
| 2850 | |
| 2851 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2852 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2853 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2854 | } |
| 2855 | /* END_CASE */ |
| 2856 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2857 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2858 | void hash_bad_order() |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2859 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2860 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2861 | unsigned char input[] = ""; |
| 2862 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2863 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2864 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2865 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2866 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 |
| 2867 | }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2868 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2869 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2870 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2871 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2872 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2873 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2874 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2875 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2876 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2877 | TEST_EQUAL(psa_hash_setup(&operation, alg), |
| 2878 | PSA_ERROR_BAD_STATE); |
| 2879 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2880 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2881 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2882 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2883 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2884 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2885 | PSA_ERROR_BAD_STATE); |
| 2886 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2887 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2888 | /* Check that update calls abort on error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2889 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 2890 | operation.id = UINT_MAX; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2891 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2892 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2893 | PSA_ERROR_BAD_STATE); |
| 2894 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2895 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2896 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2897 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2898 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2899 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2900 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2901 | hash, sizeof(hash), &hash_len)); |
| 2902 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2903 | PSA_ERROR_BAD_STATE); |
| 2904 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2905 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2906 | /* Call verify without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 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 after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2913 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2914 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2915 | hash, sizeof(hash), &hash_len)); |
| 2916 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2917 | valid_hash, sizeof(valid_hash)), |
| 2918 | PSA_ERROR_BAD_STATE); |
| 2919 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2920 | |
| 2921 | /* Call verify twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2922 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2923 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2924 | PSA_ASSERT(psa_hash_verify(&operation, |
| 2925 | valid_hash, sizeof(valid_hash))); |
| 2926 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2927 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2928 | valid_hash, sizeof(valid_hash)), |
| 2929 | PSA_ERROR_BAD_STATE); |
| 2930 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2931 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2932 | |
| 2933 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 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 twice in a row. */ |
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_finish(&operation, |
| 2942 | hash, sizeof(hash), &hash_len)); |
| 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)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2947 | |
| 2948 | /* Call finish after calling verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2949 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2950 | PSA_ASSERT(psa_hash_verify(&operation, |
| 2951 | valid_hash, sizeof(valid_hash))); |
| 2952 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2953 | hash, sizeof(hash), &hash_len), |
| 2954 | PSA_ERROR_BAD_STATE); |
| 2955 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2956 | |
| 2957 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2958 | PSA_DONE(); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2959 | } |
| 2960 | /* END_CASE */ |
| 2961 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2962 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2963 | void hash_verify_bad_args() |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2964 | { |
| 2965 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2966 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2967 | * appended to it */ |
| 2968 | unsigned char hash[] = { |
| 2969 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2970 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2971 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb |
| 2972 | }; |
| 2973 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2974 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2975 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2976 | PSA_ASSERT(psa_crypto_init()); |
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 smaller hash than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2979 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2980 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2981 | TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1), |
| 2982 | PSA_ERROR_INVALID_SIGNATURE); |
| 2983 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2984 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2985 | ASSERT_OPERATION_IS_INACTIVE(operation); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2986 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2987 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2988 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2989 | TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size), |
| 2990 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2991 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2992 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2993 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2994 | TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)), |
| 2995 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2996 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2997 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2998 | PSA_DONE(); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2999 | } |
| 3000 | /* END_CASE */ |
| 3001 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3002 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3003 | void hash_finish_bad_args() |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3004 | { |
| 3005 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 3006 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3007 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 3008 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3009 | size_t hash_len; |
| 3010 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3011 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3012 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3013 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3014 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3015 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3016 | hash, expected_size - 1, &hash_len), |
| 3017 | PSA_ERROR_BUFFER_TOO_SMALL); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3018 | |
| 3019 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3020 | PSA_DONE(); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3021 | } |
| 3022 | /* END_CASE */ |
| 3023 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3024 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3025 | void hash_clone_source_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3026 | { |
| 3027 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3028 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3029 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 3030 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3031 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3032 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3033 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3034 | size_t hash_len; |
| 3035 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3036 | PSA_ASSERT(psa_crypto_init()); |
| 3037 | PSA_ASSERT(psa_hash_setup(&op_source, alg)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3038 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3039 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3040 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3041 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3042 | hash, sizeof(hash), &hash_len)); |
| 3043 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3044 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3045 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3046 | TEST_EQUAL(psa_hash_clone(&op_source, &op_setup), |
| 3047 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3048 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3049 | PSA_ASSERT(psa_hash_clone(&op_source, &op_init)); |
| 3050 | PSA_ASSERT(psa_hash_finish(&op_init, |
| 3051 | hash, sizeof(hash), &hash_len)); |
| 3052 | PSA_ASSERT(psa_hash_clone(&op_source, &op_finished)); |
| 3053 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3054 | hash, sizeof(hash), &hash_len)); |
| 3055 | PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted)); |
| 3056 | PSA_ASSERT(psa_hash_finish(&op_aborted, |
| 3057 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3058 | |
| 3059 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3060 | psa_hash_abort(&op_source); |
| 3061 | psa_hash_abort(&op_init); |
| 3062 | psa_hash_abort(&op_setup); |
| 3063 | psa_hash_abort(&op_finished); |
| 3064 | psa_hash_abort(&op_aborted); |
| 3065 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3066 | } |
| 3067 | /* END_CASE */ |
| 3068 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3069 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3070 | void hash_clone_target_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3071 | { |
| 3072 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3073 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3074 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3075 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3076 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3077 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3078 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 3079 | size_t hash_len; |
| 3080 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3081 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3082 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3083 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3084 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3085 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3086 | hash, sizeof(hash), &hash_len)); |
| 3087 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3088 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3089 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3090 | PSA_ASSERT(psa_hash_clone(&op_setup, &op_target)); |
| 3091 | PSA_ASSERT(psa_hash_finish(&op_target, |
| 3092 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3093 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3094 | TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE); |
| 3095 | TEST_EQUAL(psa_hash_clone(&op_finished, &op_target), |
| 3096 | PSA_ERROR_BAD_STATE); |
| 3097 | TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target), |
| 3098 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3099 | |
| 3100 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3101 | psa_hash_abort(&op_target); |
| 3102 | psa_hash_abort(&op_init); |
| 3103 | psa_hash_abort(&op_setup); |
| 3104 | psa_hash_abort(&op_finished); |
| 3105 | psa_hash_abort(&op_aborted); |
| 3106 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3107 | } |
| 3108 | /* END_CASE */ |
| 3109 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3110 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3111 | void mac_operation_init() |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3112 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3113 | const uint8_t input[1] = { 0 }; |
| 3114 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3115 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3116 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3117 | * 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] | 3118 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3119 | psa_mac_operation_t func = psa_mac_operation_init(); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3120 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 3121 | psa_mac_operation_t zero; |
| 3122 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3123 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3124 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3125 | /* A freshly-initialized MAC operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3126 | TEST_EQUAL(psa_mac_update(&func, |
| 3127 | input, sizeof(input)), |
| 3128 | PSA_ERROR_BAD_STATE); |
| 3129 | TEST_EQUAL(psa_mac_update(&init, |
| 3130 | input, sizeof(input)), |
| 3131 | PSA_ERROR_BAD_STATE); |
| 3132 | TEST_EQUAL(psa_mac_update(&zero, |
| 3133 | input, sizeof(input)), |
| 3134 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3135 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3136 | /* A default MAC operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3137 | PSA_ASSERT(psa_mac_abort(&func)); |
| 3138 | PSA_ASSERT(psa_mac_abort(&init)); |
| 3139 | PSA_ASSERT(psa_mac_abort(&zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3140 | } |
| 3141 | /* END_CASE */ |
| 3142 | |
| 3143 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3144 | void mac_setup(int key_type_arg, |
| 3145 | data_t *key, |
| 3146 | int alg_arg, |
| 3147 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3148 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3149 | psa_key_type_t key_type = key_type_arg; |
| 3150 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3151 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3152 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3153 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3154 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3155 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3156 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3157 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3158 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3159 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3160 | if (!exercise_mac_setup(key_type, key->x, key->len, alg, |
| 3161 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3162 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3163 | } |
| 3164 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3165 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3166 | /* The operation object should be reusable. */ |
| 3167 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3168 | if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 3169 | smoke_test_key_data, |
| 3170 | sizeof(smoke_test_key_data), |
| 3171 | KNOWN_SUPPORTED_MAC_ALG, |
| 3172 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3173 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3174 | } |
| 3175 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3176 | #endif |
| 3177 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3178 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3179 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3180 | } |
| 3181 | /* END_CASE */ |
| 3182 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3183 | /* 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] | 3184 | void mac_bad_order() |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3185 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3186 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3187 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 3188 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3189 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3190 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3191 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3192 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa |
| 3193 | }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3194 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3195 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3196 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 3197 | size_t sign_mac_length = 0; |
| 3198 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3199 | const uint8_t verify_mac[] = { |
| 3200 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 3201 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3202 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 |
| 3203 | }; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3204 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3205 | PSA_ASSERT(psa_crypto_init()); |
| 3206 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 3207 | psa_set_key_algorithm(&attributes, alg); |
| 3208 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3209 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3210 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3211 | &key)); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3212 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3213 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3214 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3215 | PSA_ERROR_BAD_STATE); |
| 3216 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3217 | |
| 3218 | /* Call sign finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3219 | TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac), |
| 3220 | &sign_mac_length), |
| 3221 | PSA_ERROR_BAD_STATE); |
| 3222 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3223 | |
| 3224 | /* Call verify finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3225 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3226 | verify_mac, sizeof(verify_mac)), |
| 3227 | PSA_ERROR_BAD_STATE); |
| 3228 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3229 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3230 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3231 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3232 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3233 | TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg), |
| 3234 | PSA_ERROR_BAD_STATE); |
| 3235 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3236 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3237 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3238 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3239 | /* Call update after sign finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3240 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3241 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3242 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3243 | sign_mac, sizeof(sign_mac), |
| 3244 | &sign_mac_length)); |
| 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 update after verify finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3250 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3251 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3252 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3253 | verify_mac, sizeof(verify_mac))); |
| 3254 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3255 | PSA_ERROR_BAD_STATE); |
| 3256 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3257 | |
| 3258 | /* Call sign finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3259 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3260 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3261 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3262 | sign_mac, sizeof(sign_mac), |
| 3263 | &sign_mac_length)); |
| 3264 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3265 | sign_mac, sizeof(sign_mac), |
| 3266 | &sign_mac_length), |
| 3267 | PSA_ERROR_BAD_STATE); |
| 3268 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3269 | |
| 3270 | /* Call verify finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3271 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3272 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3273 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3274 | verify_mac, sizeof(verify_mac))); |
| 3275 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3276 | verify_mac, sizeof(verify_mac)), |
| 3277 | PSA_ERROR_BAD_STATE); |
| 3278 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3279 | |
| 3280 | /* Setup sign but try verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3281 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3282 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3283 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3284 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3285 | verify_mac, sizeof(verify_mac)), |
| 3286 | PSA_ERROR_BAD_STATE); |
| 3287 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3288 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3289 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3290 | |
| 3291 | /* Setup verify but try sign. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3292 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3293 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3294 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3295 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3296 | sign_mac, sizeof(sign_mac), |
| 3297 | &sign_mac_length), |
| 3298 | PSA_ERROR_BAD_STATE); |
| 3299 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3300 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3301 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3302 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3303 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3304 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3305 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3306 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3307 | } |
| 3308 | /* END_CASE */ |
| 3309 | |
| 3310 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3311 | void mac_sign_verify_multi(int key_type_arg, |
| 3312 | data_t *key_data, |
| 3313 | int alg_arg, |
| 3314 | data_t *input, |
| 3315 | int is_verify, |
| 3316 | data_t *expected_mac) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3317 | { |
| 3318 | size_t data_part_len = 0; |
| 3319 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3320 | 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] | 3321 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3322 | mbedtls_test_set_step(2000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3323 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3324 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3325 | alg_arg, |
| 3326 | input, data_part_len, |
| 3327 | expected_mac, |
| 3328 | is_verify, 0) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3329 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3330 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3331 | |
| 3332 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3333 | mbedtls_test_set_step(3000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3334 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3335 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3336 | alg_arg, |
| 3337 | input, data_part_len, |
| 3338 | expected_mac, |
| 3339 | is_verify, 1) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3340 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3341 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3342 | } |
| 3343 | |
| 3344 | /* Goto is required to silence warnings about unused labels, as we |
| 3345 | * don't actually do any test assertions in this function. */ |
| 3346 | goto exit; |
| 3347 | } |
| 3348 | /* END_CASE */ |
| 3349 | |
| 3350 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3351 | void mac_sign(int key_type_arg, |
| 3352 | data_t *key_data, |
| 3353 | int alg_arg, |
| 3354 | data_t *input, |
| 3355 | data_t *expected_mac) |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3356 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3357 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3358 | psa_key_type_t key_type = key_type_arg; |
| 3359 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3360 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3361 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3362 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3363 | size_t mac_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3364 | 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] | 3365 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3366 | const size_t output_sizes_to_test[] = { |
| 3367 | 0, |
| 3368 | 1, |
| 3369 | expected_mac->len - 1, |
| 3370 | expected_mac->len, |
| 3371 | expected_mac->len + 1, |
| 3372 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3374 | TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3375 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3376 | TEST_ASSERT(expected_mac->len == mac_buffer_size); |
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 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3379 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3380 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 3381 | psa_set_key_algorithm(&attributes, alg); |
| 3382 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3383 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3384 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3385 | &key)); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3386 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3387 | 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] | 3388 | const size_t output_size = output_sizes_to_test[i]; |
| 3389 | psa_status_t expected_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3390 | (output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3391 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3392 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3393 | mbedtls_test_set_step(output_size); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3394 | TEST_CALLOC(actual_mac, output_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3395 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3396 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3397 | TEST_EQUAL(psa_mac_compute(key, alg, |
| 3398 | input->x, input->len, |
| 3399 | actual_mac, output_size, &mac_length), |
| 3400 | expected_status); |
| 3401 | if (expected_status == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3402 | TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3403 | actual_mac, mac_length); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3404 | } |
| 3405 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3406 | if (output_size > 0) { |
| 3407 | memset(actual_mac, 0, output_size); |
| 3408 | } |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3409 | |
| 3410 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3411 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3412 | PSA_ASSERT(psa_mac_update(&operation, |
| 3413 | input->x, input->len)); |
| 3414 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3415 | actual_mac, output_size, |
| 3416 | &mac_length), |
| 3417 | expected_status); |
| 3418 | PSA_ASSERT(psa_mac_abort(&operation)); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3419 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3420 | if (expected_status == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3421 | TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3422 | actual_mac, mac_length); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3423 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3424 | mbedtls_free(actual_mac); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3425 | actual_mac = NULL; |
| 3426 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3427 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3428 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3429 | psa_mac_abort(&operation); |
| 3430 | psa_destroy_key(key); |
| 3431 | PSA_DONE(); |
| 3432 | mbedtls_free(actual_mac); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3433 | } |
| 3434 | /* END_CASE */ |
| 3435 | |
| 3436 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3437 | void mac_verify(int key_type_arg, |
| 3438 | data_t *key_data, |
| 3439 | int alg_arg, |
| 3440 | data_t *input, |
| 3441 | data_t *expected_mac) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3442 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3443 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3444 | psa_key_type_t key_type = key_type_arg; |
| 3445 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3446 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3447 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3448 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3449 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3450 | TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3451 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3452 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3453 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3454 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 3455 | psa_set_key_algorithm(&attributes, alg); |
| 3456 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3457 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3458 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3459 | &key)); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3460 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3461 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3462 | PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len, |
| 3463 | expected_mac->x, expected_mac->len)); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3464 | |
| 3465 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3466 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3467 | PSA_ASSERT(psa_mac_update(&operation, |
| 3468 | input->x, input->len)); |
| 3469 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3470 | expected_mac->x, |
| 3471 | expected_mac->len)); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3472 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3473 | /* Test a MAC that's too short, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3474 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3475 | input->x, input->len, |
| 3476 | expected_mac->x, |
| 3477 | expected_mac->len - 1), |
| 3478 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3479 | |
| 3480 | /* Test a MAC that's too short, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3481 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3482 | PSA_ASSERT(psa_mac_update(&operation, |
| 3483 | input->x, input->len)); |
| 3484 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3485 | expected_mac->x, |
| 3486 | expected_mac->len - 1), |
| 3487 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3488 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3489 | /* Test a MAC that's too long, one-shot case. */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3490 | TEST_CALLOC(perturbed_mac, expected_mac->len + 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3491 | memcpy(perturbed_mac, expected_mac->x, expected_mac->len); |
| 3492 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3493 | input->x, input->len, |
| 3494 | perturbed_mac, expected_mac->len + 1), |
| 3495 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3496 | |
| 3497 | /* Test a MAC that's too long, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3498 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3499 | PSA_ASSERT(psa_mac_update(&operation, |
| 3500 | input->x, input->len)); |
| 3501 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3502 | perturbed_mac, |
| 3503 | expected_mac->len + 1), |
| 3504 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3505 | |
| 3506 | /* Test changing one byte. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3507 | for (size_t i = 0; i < expected_mac->len; i++) { |
| 3508 | mbedtls_test_set_step(i); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3509 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3510 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3511 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3512 | input->x, input->len, |
| 3513 | perturbed_mac, expected_mac->len), |
| 3514 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3515 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3516 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3517 | PSA_ASSERT(psa_mac_update(&operation, |
| 3518 | input->x, input->len)); |
| 3519 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3520 | perturbed_mac, |
| 3521 | expected_mac->len), |
| 3522 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3523 | perturbed_mac[i] ^= 1; |
| 3524 | } |
| 3525 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3526 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3527 | psa_mac_abort(&operation); |
| 3528 | psa_destroy_key(key); |
| 3529 | PSA_DONE(); |
| 3530 | mbedtls_free(perturbed_mac); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3531 | } |
| 3532 | /* END_CASE */ |
| 3533 | |
| 3534 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3535 | void cipher_operation_init() |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3536 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3537 | const uint8_t input[1] = { 0 }; |
| 3538 | unsigned char output[1] = { 0 }; |
| 3539 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3540 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3541 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3542 | * 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] | 3543 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3544 | psa_cipher_operation_t func = psa_cipher_operation_init(); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3545 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3546 | psa_cipher_operation_t zero; |
| 3547 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3548 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3549 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3550 | /* A freshly-initialized cipher operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3551 | TEST_EQUAL(psa_cipher_update(&func, |
| 3552 | input, sizeof(input), |
| 3553 | output, sizeof(output), |
| 3554 | &output_length), |
| 3555 | PSA_ERROR_BAD_STATE); |
| 3556 | TEST_EQUAL(psa_cipher_update(&init, |
| 3557 | input, sizeof(input), |
| 3558 | output, sizeof(output), |
| 3559 | &output_length), |
| 3560 | PSA_ERROR_BAD_STATE); |
| 3561 | TEST_EQUAL(psa_cipher_update(&zero, |
| 3562 | input, sizeof(input), |
| 3563 | output, sizeof(output), |
| 3564 | &output_length), |
| 3565 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3566 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3567 | /* A default cipher operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3568 | PSA_ASSERT(psa_cipher_abort(&func)); |
| 3569 | PSA_ASSERT(psa_cipher_abort(&init)); |
| 3570 | PSA_ASSERT(psa_cipher_abort(&zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3571 | } |
| 3572 | /* END_CASE */ |
| 3573 | |
| 3574 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3575 | void cipher_setup(int key_type_arg, |
| 3576 | data_t *key, |
| 3577 | int alg_arg, |
| 3578 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3579 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3580 | psa_key_type_t key_type = key_type_arg; |
| 3581 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3582 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3583 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3584 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3585 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3586 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3587 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3588 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3589 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3590 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3591 | if (!exercise_cipher_setup(key_type, key->x, key->len, alg, |
| 3592 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3593 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3594 | } |
| 3595 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3596 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3597 | /* The operation object should be reusable. */ |
| 3598 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3599 | if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3600 | smoke_test_key_data, |
| 3601 | sizeof(smoke_test_key_data), |
| 3602 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3603 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3604 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3605 | } |
| 3606 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3607 | #endif |
| 3608 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3609 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3610 | psa_cipher_abort(&operation); |
| 3611 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3612 | } |
| 3613 | /* END_CASE */ |
| 3614 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3615 | /* 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] | 3616 | void cipher_bad_order() |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3617 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3618 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3619 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3620 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3621 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3622 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3623 | 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] | 3624 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3625 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3626 | 0xaa, 0xaa, 0xaa, 0xaa |
| 3627 | }; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3628 | const uint8_t text[] = { |
| 3629 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3630 | 0xbb, 0xbb, 0xbb, 0xbb |
| 3631 | }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3632 | 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] | 3633 | size_t length = 0; |
| 3634 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3635 | PSA_ASSERT(psa_crypto_init()); |
| 3636 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3637 | psa_set_key_algorithm(&attributes, alg); |
| 3638 | psa_set_key_type(&attributes, key_type); |
| 3639 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3640 | &key)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3641 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3642 | /* Call encrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3643 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3644 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3645 | TEST_EQUAL(psa_cipher_encrypt_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 | |
| 3651 | /* Call decrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3652 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3653 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3654 | TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg), |
| 3655 | PSA_ERROR_BAD_STATE); |
| 3656 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3657 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3658 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3659 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3660 | /* Generate an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3661 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3662 | buffer, sizeof(buffer), |
| 3663 | &length), |
| 3664 | PSA_ERROR_BAD_STATE); |
| 3665 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3666 | |
| 3667 | /* Generate an IV twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3668 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3669 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3670 | buffer, sizeof(buffer), |
| 3671 | &length)); |
| 3672 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3673 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3674 | buffer, sizeof(buffer), |
| 3675 | &length), |
| 3676 | PSA_ERROR_BAD_STATE); |
| 3677 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3678 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3679 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3680 | |
| 3681 | /* Generate an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3682 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3683 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3684 | iv, sizeof(iv))); |
| 3685 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3686 | buffer, sizeof(buffer), |
| 3687 | &length), |
| 3688 | PSA_ERROR_BAD_STATE); |
| 3689 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3690 | |
| 3691 | /* Set an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3692 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3693 | iv, sizeof(iv)), |
| 3694 | PSA_ERROR_BAD_STATE); |
| 3695 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3696 | |
| 3697 | /* Set an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3698 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3699 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3700 | iv, sizeof(iv))); |
| 3701 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3702 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3703 | iv, sizeof(iv)), |
| 3704 | PSA_ERROR_BAD_STATE); |
| 3705 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3706 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3707 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3708 | |
| 3709 | /* Set an IV after it's already generated. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3710 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3711 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3712 | buffer, sizeof(buffer), |
| 3713 | &length)); |
| 3714 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3715 | iv, sizeof(iv)), |
| 3716 | PSA_ERROR_BAD_STATE); |
| 3717 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3718 | |
| 3719 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3720 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3721 | text, sizeof(text), |
| 3722 | buffer, sizeof(buffer), |
| 3723 | &length), |
| 3724 | PSA_ERROR_BAD_STATE); |
| 3725 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3726 | |
| 3727 | /* Call update without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3728 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3729 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3730 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3731 | text, sizeof(text), |
| 3732 | buffer, sizeof(buffer), |
| 3733 | &length), |
| 3734 | PSA_ERROR_BAD_STATE); |
| 3735 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3736 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3737 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3738 | |
| 3739 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3740 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3741 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3742 | iv, sizeof(iv))); |
| 3743 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3744 | buffer, sizeof(buffer), &length)); |
| 3745 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3746 | text, sizeof(text), |
| 3747 | buffer, sizeof(buffer), |
| 3748 | &length), |
| 3749 | PSA_ERROR_BAD_STATE); |
| 3750 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3751 | |
| 3752 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3753 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3754 | buffer, sizeof(buffer), &length), |
| 3755 | PSA_ERROR_BAD_STATE); |
| 3756 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3757 | |
| 3758 | /* Call finish without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3759 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3760 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3761 | * for cipher modes with padding. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3762 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3763 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3764 | buffer, sizeof(buffer), &length), |
| 3765 | PSA_ERROR_BAD_STATE); |
| 3766 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3767 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3768 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3769 | |
| 3770 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3771 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3772 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3773 | iv, sizeof(iv))); |
| 3774 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3775 | buffer, sizeof(buffer), &length)); |
| 3776 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3777 | buffer, sizeof(buffer), &length), |
| 3778 | PSA_ERROR_BAD_STATE); |
| 3779 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3780 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3781 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3782 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3783 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3784 | psa_cipher_abort(&operation); |
| 3785 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3786 | } |
| 3787 | /* END_CASE */ |
| 3788 | |
| 3789 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3790 | void cipher_encrypt_fail(int alg_arg, |
| 3791 | int key_type_arg, |
| 3792 | data_t *key_data, |
| 3793 | data_t *input, |
| 3794 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3795 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3796 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3797 | psa_status_t status; |
| 3798 | psa_key_type_t key_type = key_type_arg; |
| 3799 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3800 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3801 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 }; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3802 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; |
| 3803 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3804 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3805 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3806 | size_t output_length = 0; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3807 | size_t function_output_length; |
| 3808 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3809 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3810 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3811 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 3812 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3813 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3814 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3815 | psa_set_key_algorithm(&attributes, alg); |
| 3816 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3817 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3818 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 3819 | input->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3820 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3821 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3822 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3823 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3824 | } |
| 3825 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3826 | /* Encrypt, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3827 | status = psa_cipher_encrypt(key, alg, input->x, input->len, output, |
| 3828 | output_buffer_size, &output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3829 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3830 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3831 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3832 | /* Encrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3833 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
| 3834 | if (status == PSA_SUCCESS) { |
| 3835 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 3836 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3837 | iv, iv_size, |
| 3838 | &iv_length)); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3839 | } |
| 3840 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3841 | status = psa_cipher_update(&operation, input->x, input->len, |
| 3842 | output, output_buffer_size, |
| 3843 | &function_output_length); |
| 3844 | if (status == PSA_SUCCESS) { |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3845 | output_length += function_output_length; |
| 3846 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3847 | status = psa_cipher_finish(&operation, output + output_length, |
| 3848 | output_buffer_size - output_length, |
| 3849 | &function_output_length); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3850 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3851 | TEST_EQUAL(status, expected_status); |
| 3852 | } else { |
| 3853 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3854 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3855 | } else { |
| 3856 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3857 | } |
| 3858 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3859 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3860 | psa_cipher_abort(&operation); |
| 3861 | mbedtls_free(output); |
| 3862 | psa_destroy_key(key); |
| 3863 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3864 | } |
| 3865 | /* END_CASE */ |
| 3866 | |
| 3867 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3868 | void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data, |
| 3869 | data_t *input, int iv_length, |
| 3870 | int expected_result) |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3871 | { |
| 3872 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3873 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3874 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3875 | size_t output_buffer_size = 0; |
| 3876 | unsigned char *output = NULL; |
| 3877 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3878 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3879 | TEST_CALLOC(output, output_buffer_size); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3880 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3881 | PSA_ASSERT(psa_crypto_init()); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3882 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3883 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3884 | psa_set_key_algorithm(&attributes, alg); |
| 3885 | psa_set_key_type(&attributes, key_type); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3886 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3887 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3888 | &key)); |
| 3889 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3890 | TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output, |
| 3891 | iv_length)); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3892 | |
| 3893 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3894 | psa_cipher_abort(&operation); |
| 3895 | mbedtls_free(output); |
| 3896 | psa_destroy_key(key); |
| 3897 | PSA_DONE(); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3898 | } |
| 3899 | /* END_CASE */ |
| 3900 | |
| 3901 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3902 | void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data, |
| 3903 | data_t *plaintext, data_t *ciphertext) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3904 | { |
| 3905 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3906 | psa_key_type_t key_type = key_type_arg; |
| 3907 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3908 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3909 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3910 | unsigned char *output = NULL; |
| 3911 | size_t output_buffer_size = 0; |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3912 | size_t output_length, length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3913 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3914 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3915 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3916 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3917 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3918 | TEST_LE_U(ciphertext->len, |
| 3919 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len)); |
| 3920 | TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len), |
| 3921 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len)); |
| 3922 | TEST_LE_U(plaintext->len, |
| 3923 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len)); |
| 3924 | TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len), |
| 3925 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len)); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3926 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3927 | |
| 3928 | /* Set up key and output buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3929 | psa_set_key_usage_flags(&attributes, |
| 3930 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3931 | psa_set_key_algorithm(&attributes, alg); |
| 3932 | psa_set_key_type(&attributes, key_type); |
| 3933 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3934 | &key)); |
| 3935 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 3936 | plaintext->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3937 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3938 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3939 | /* set_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3940 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3941 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 3942 | PSA_ERROR_BAD_STATE); |
| 3943 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3944 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 3945 | PSA_ERROR_BAD_STATE); |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3946 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3947 | /* generate_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3948 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3949 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 3950 | &length), |
| 3951 | PSA_ERROR_BAD_STATE); |
| 3952 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3953 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 3954 | &length), |
| 3955 | PSA_ERROR_BAD_STATE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3956 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3957 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3958 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3959 | output_length = 0; |
| 3960 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3961 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3962 | plaintext->x, plaintext->len, |
| 3963 | output, output_buffer_size, |
| 3964 | &length)); |
| 3965 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3966 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3967 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3968 | mbedtls_buffer_offset(output, output_length), |
| 3969 | output_buffer_size - output_length, |
| 3970 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3971 | output_length += length; |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3972 | TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3973 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3974 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3975 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3976 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3977 | output_length = 0; |
| 3978 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3979 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3980 | ciphertext->x, ciphertext->len, |
| 3981 | output, output_buffer_size, |
| 3982 | &length)); |
| 3983 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3984 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3985 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3986 | mbedtls_buffer_offset(output, output_length), |
| 3987 | output_buffer_size - output_length, |
| 3988 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3989 | output_length += length; |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3990 | TEST_MEMORY_COMPARE(plaintext->x, plaintext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3991 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3992 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3993 | /* One-shot encryption */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3994 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3995 | PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len, |
| 3996 | output, output_buffer_size, |
| 3997 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3998 | TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3999 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4000 | |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4001 | /* One-shot decryption */ |
| 4002 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4003 | PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len, |
| 4004 | output, output_buffer_size, |
| 4005 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4006 | TEST_MEMORY_COMPARE(plaintext->x, plaintext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4007 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4008 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4009 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4010 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4011 | mbedtls_free(output); |
| 4012 | psa_cipher_abort(&operation); |
| 4013 | psa_destroy_key(key); |
| 4014 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4015 | } |
| 4016 | /* END_CASE */ |
| 4017 | |
| 4018 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4019 | 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] | 4020 | { |
| 4021 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4022 | psa_algorithm_t alg = alg_arg; |
| 4023 | psa_key_type_t key_type = key_type_arg; |
| 4024 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4025 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4026 | psa_status_t status; |
| 4027 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4028 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4029 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4030 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4031 | psa_set_key_algorithm(&attributes, alg); |
| 4032 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4033 | |
| 4034 | /* Usage of either of these two size macros would cause divide by zero |
| 4035 | * with incorrect key types previously. Input length should be irrelevant |
| 4036 | * here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4037 | TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16), |
| 4038 | 0); |
| 4039 | TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4040 | |
| 4041 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4042 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4043 | &key)); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4044 | |
| 4045 | /* Should fail due to invalid alg type (to support invalid key type). |
| 4046 | * Encrypt or decrypt will end up in the same place. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4047 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4048 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4049 | TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4050 | |
| 4051 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4052 | psa_cipher_abort(&operation); |
| 4053 | psa_destroy_key(key); |
| 4054 | PSA_DONE(); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4055 | } |
| 4056 | /* END_CASE */ |
| 4057 | |
| 4058 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4059 | void cipher_encrypt_validation(int alg_arg, |
| 4060 | int key_type_arg, |
| 4061 | data_t *key_data, |
| 4062 | data_t *input) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4063 | { |
| 4064 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4065 | psa_key_type_t key_type = key_type_arg; |
| 4066 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4067 | size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4068 | unsigned char *output1 = NULL; |
| 4069 | size_t output1_buffer_size = 0; |
| 4070 | size_t output1_length = 0; |
| 4071 | unsigned char *output2 = NULL; |
| 4072 | size_t output2_buffer_size = 0; |
| 4073 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4074 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4075 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4076 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4077 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4078 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4079 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4080 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4081 | psa_set_key_algorithm(&attributes, alg); |
| 4082 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4083 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4084 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4085 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4086 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4087 | TEST_CALLOC(output1, output1_buffer_size); |
| 4088 | TEST_CALLOC(output2, output2_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4089 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4090 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4091 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4092 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 4093 | /* The one-shot cipher encryption uses generated iv so validating |
| 4094 | the output is not possible. Validating with multipart encryption. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4095 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1, |
| 4096 | output1_buffer_size, &output1_length)); |
| 4097 | TEST_LE_U(output1_length, |
| 4098 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4099 | TEST_LE_U(output1_length, |
| 4100 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4101 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4102 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4103 | PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4104 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4105 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4106 | input->x, input->len, |
| 4107 | output2, output2_buffer_size, |
| 4108 | &function_output_length)); |
| 4109 | TEST_LE_U(function_output_length, |
| 4110 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4111 | TEST_LE_U(function_output_length, |
| 4112 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4113 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4114 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4115 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4116 | output2 + output2_length, |
| 4117 | output2_buffer_size - output2_length, |
| 4118 | &function_output_length)); |
| 4119 | TEST_LE_U(function_output_length, |
| 4120 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4121 | TEST_LE_U(function_output_length, |
| 4122 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4123 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4124 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4125 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4126 | TEST_MEMORY_COMPARE(output1 + iv_size, output1_length - iv_size, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4127 | output2, output2_length); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4128 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4129 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4130 | psa_cipher_abort(&operation); |
| 4131 | mbedtls_free(output1); |
| 4132 | mbedtls_free(output2); |
| 4133 | psa_destroy_key(key); |
| 4134 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4135 | } |
| 4136 | /* END_CASE */ |
| 4137 | |
| 4138 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4139 | void cipher_encrypt_multipart(int alg_arg, int key_type_arg, |
| 4140 | data_t *key_data, data_t *iv, |
| 4141 | data_t *input, |
| 4142 | int first_part_size_arg, |
| 4143 | int output1_length_arg, int output2_length_arg, |
| 4144 | data_t *expected_output, |
| 4145 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4146 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4147 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4148 | psa_key_type_t key_type = key_type_arg; |
| 4149 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4150 | psa_status_t status; |
| 4151 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4152 | size_t first_part_size = first_part_size_arg; |
| 4153 | size_t output1_length = output1_length_arg; |
| 4154 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4155 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4156 | size_t output_buffer_size = 0; |
| 4157 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4158 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4159 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4160 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
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_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4164 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4165 | psa_set_key_algorithm(&attributes, alg); |
| 4166 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4167 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4168 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4169 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4170 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4171 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4172 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4173 | if (iv->len > 0) { |
| 4174 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4175 | } |
| 4176 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4177 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4178 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4179 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4181 | TEST_LE_U(first_part_size, input->len); |
| 4182 | PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size, |
| 4183 | output, output_buffer_size, |
| 4184 | &function_output_length)); |
| 4185 | TEST_ASSERT(function_output_length == output1_length); |
| 4186 | TEST_LE_U(function_output_length, |
| 4187 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4188 | TEST_LE_U(function_output_length, |
| 4189 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4190 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4191 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4192 | if (first_part_size < input->len) { |
| 4193 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4194 | input->x + first_part_size, |
| 4195 | input->len - first_part_size, |
| 4196 | (output_buffer_size == 0 ? NULL : |
| 4197 | output + total_output_length), |
| 4198 | output_buffer_size - total_output_length, |
| 4199 | &function_output_length)); |
| 4200 | TEST_ASSERT(function_output_length == output2_length); |
| 4201 | TEST_LE_U(function_output_length, |
| 4202 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4203 | alg, |
| 4204 | input->len - first_part_size)); |
| 4205 | TEST_LE_U(function_output_length, |
| 4206 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4207 | total_output_length += function_output_length; |
| 4208 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4209 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4210 | status = psa_cipher_finish(&operation, |
| 4211 | (output_buffer_size == 0 ? NULL : |
| 4212 | output + total_output_length), |
| 4213 | output_buffer_size - total_output_length, |
| 4214 | &function_output_length); |
| 4215 | TEST_LE_U(function_output_length, |
| 4216 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4217 | TEST_LE_U(function_output_length, |
| 4218 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4219 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4220 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4221 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4222 | if (expected_status == PSA_SUCCESS) { |
| 4223 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4224 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4225 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4226 | output, total_output_length); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4227 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4228 | |
| 4229 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4230 | psa_cipher_abort(&operation); |
| 4231 | mbedtls_free(output); |
| 4232 | psa_destroy_key(key); |
| 4233 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4234 | } |
| 4235 | /* END_CASE */ |
| 4236 | |
| 4237 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4238 | void cipher_decrypt_multipart(int alg_arg, int key_type_arg, |
| 4239 | data_t *key_data, data_t *iv, |
| 4240 | data_t *input, |
| 4241 | int first_part_size_arg, |
| 4242 | int output1_length_arg, int output2_length_arg, |
| 4243 | data_t *expected_output, |
| 4244 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4245 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4246 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4247 | psa_key_type_t key_type = key_type_arg; |
| 4248 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4249 | psa_status_t status; |
| 4250 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4251 | size_t first_part_size = first_part_size_arg; |
| 4252 | size_t output1_length = output1_length_arg; |
| 4253 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4254 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4255 | size_t output_buffer_size = 0; |
| 4256 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4257 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4258 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4259 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
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_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4263 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4264 | psa_set_key_algorithm(&attributes, alg); |
| 4265 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4266 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4267 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4268 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4269 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4270 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4271 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4272 | if (iv->len > 0) { |
| 4273 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4274 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4275 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4276 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4277 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4278 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4279 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4280 | TEST_LE_U(first_part_size, input->len); |
| 4281 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4282 | input->x, first_part_size, |
| 4283 | output, output_buffer_size, |
| 4284 | &function_output_length)); |
| 4285 | TEST_ASSERT(function_output_length == output1_length); |
| 4286 | TEST_LE_U(function_output_length, |
| 4287 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4288 | TEST_LE_U(function_output_length, |
| 4289 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4290 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4291 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4292 | if (first_part_size < input->len) { |
| 4293 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4294 | input->x + first_part_size, |
| 4295 | input->len - first_part_size, |
| 4296 | (output_buffer_size == 0 ? NULL : |
| 4297 | output + total_output_length), |
| 4298 | output_buffer_size - total_output_length, |
| 4299 | &function_output_length)); |
| 4300 | TEST_ASSERT(function_output_length == output2_length); |
| 4301 | TEST_LE_U(function_output_length, |
| 4302 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4303 | alg, |
| 4304 | input->len - first_part_size)); |
| 4305 | TEST_LE_U(function_output_length, |
| 4306 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4307 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4308 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4309 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4310 | status = psa_cipher_finish(&operation, |
| 4311 | (output_buffer_size == 0 ? NULL : |
| 4312 | output + total_output_length), |
| 4313 | output_buffer_size - total_output_length, |
| 4314 | &function_output_length); |
| 4315 | TEST_LE_U(function_output_length, |
| 4316 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4317 | TEST_LE_U(function_output_length, |
| 4318 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4319 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4320 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4321 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4322 | if (expected_status == PSA_SUCCESS) { |
| 4323 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4324 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4325 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4326 | output, total_output_length); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4327 | } |
| 4328 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4329 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4330 | psa_cipher_abort(&operation); |
| 4331 | mbedtls_free(output); |
| 4332 | psa_destroy_key(key); |
| 4333 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4334 | } |
| 4335 | /* END_CASE */ |
| 4336 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4337 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4338 | void cipher_decrypt_fail(int alg_arg, |
| 4339 | int key_type_arg, |
| 4340 | data_t *key_data, |
| 4341 | data_t *iv, |
| 4342 | data_t *input_arg, |
| 4343 | int expected_status_arg) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4344 | { |
| 4345 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4346 | psa_status_t status; |
| 4347 | psa_key_type_t key_type = key_type_arg; |
| 4348 | psa_algorithm_t alg = alg_arg; |
| 4349 | psa_status_t expected_status = expected_status_arg; |
| 4350 | unsigned char *input = NULL; |
| 4351 | size_t input_buffer_size = 0; |
| 4352 | unsigned char *output = NULL; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4353 | unsigned char *output_multi = NULL; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4354 | size_t output_buffer_size = 0; |
| 4355 | size_t output_length = 0; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4356 | size_t function_output_length; |
| 4357 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4358 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4359 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4360 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 4361 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4362 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4363 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4364 | psa_set_key_algorithm(&attributes, alg); |
| 4365 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4367 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4368 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4369 | } |
| 4370 | |
| 4371 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4372 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4373 | if (input_buffer_size > 0) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4374 | TEST_CALLOC(input, input_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4375 | memcpy(input, iv->x, iv->len); |
| 4376 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4377 | } |
| 4378 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4379 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4380 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4381 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4382 | /* Decrypt, one-short */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4383 | status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4384 | output_buffer_size, &output_length); |
| 4385 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4386 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4387 | /* Decrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4388 | status = psa_cipher_decrypt_setup(&operation, key, alg); |
| 4389 | if (status == PSA_SUCCESS) { |
| 4390 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 4391 | input_arg->len) + |
| 4392 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4393 | TEST_CALLOC(output_multi, output_buffer_size); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4394 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4395 | if (iv->len > 0) { |
| 4396 | status = psa_cipher_set_iv(&operation, iv->x, iv->len); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4397 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4398 | if (status != PSA_SUCCESS) { |
| 4399 | TEST_EQUAL(status, expected_status); |
| 4400 | } |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4401 | } |
| 4402 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4403 | if (status == PSA_SUCCESS) { |
| 4404 | status = psa_cipher_update(&operation, |
| 4405 | input_arg->x, input_arg->len, |
| 4406 | output_multi, output_buffer_size, |
| 4407 | &function_output_length); |
| 4408 | if (status == PSA_SUCCESS) { |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4409 | output_length = function_output_length; |
| 4410 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4411 | status = psa_cipher_finish(&operation, |
| 4412 | output_multi + output_length, |
| 4413 | output_buffer_size - output_length, |
| 4414 | &function_output_length); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4415 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4416 | TEST_EQUAL(status, expected_status); |
| 4417 | } else { |
| 4418 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4419 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4420 | } else { |
| 4421 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4422 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4423 | } else { |
| 4424 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4425 | } |
| 4426 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4427 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4428 | psa_cipher_abort(&operation); |
| 4429 | mbedtls_free(input); |
| 4430 | mbedtls_free(output); |
| 4431 | mbedtls_free(output_multi); |
| 4432 | psa_destroy_key(key); |
| 4433 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4434 | } |
| 4435 | /* END_CASE */ |
| 4436 | |
| 4437 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4438 | void cipher_decrypt(int alg_arg, |
| 4439 | int key_type_arg, |
| 4440 | data_t *key_data, |
| 4441 | data_t *iv, |
| 4442 | data_t *input_arg, |
| 4443 | data_t *expected_output) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4444 | { |
| 4445 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4446 | psa_key_type_t key_type = key_type_arg; |
| 4447 | psa_algorithm_t alg = alg_arg; |
| 4448 | unsigned char *input = NULL; |
| 4449 | size_t input_buffer_size = 0; |
| 4450 | unsigned char *output = NULL; |
| 4451 | size_t output_buffer_size = 0; |
| 4452 | size_t output_length = 0; |
| 4453 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4454 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4455 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4456 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4457 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4458 | psa_set_key_algorithm(&attributes, alg); |
| 4459 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4460 | |
| 4461 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4462 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4463 | if (input_buffer_size > 0) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4464 | TEST_CALLOC(input, input_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4465 | memcpy(input, iv->x, iv->len); |
| 4466 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4467 | } |
| 4468 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4469 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4470 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4471 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4472 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4473 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4474 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4475 | PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4476 | output_buffer_size, &output_length)); |
| 4477 | TEST_LE_U(output_length, |
| 4478 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size)); |
| 4479 | TEST_LE_U(output_length, |
| 4480 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4481 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4482 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4483 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4484 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4485 | mbedtls_free(input); |
| 4486 | mbedtls_free(output); |
| 4487 | psa_destroy_key(key); |
| 4488 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4489 | } |
| 4490 | /* END_CASE */ |
| 4491 | |
| 4492 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4493 | void cipher_verify_output(int alg_arg, |
| 4494 | int key_type_arg, |
| 4495 | data_t *key_data, |
| 4496 | data_t *input) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4497 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4498 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4499 | psa_key_type_t key_type = key_type_arg; |
| 4500 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4501 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4502 | size_t output1_size = 0; |
| 4503 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4504 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4505 | size_t output2_size = 0; |
| 4506 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4507 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4508 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4509 | PSA_ASSERT(psa_crypto_init()); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4510 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4511 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4512 | psa_set_key_algorithm(&attributes, alg); |
| 4513 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4514 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4515 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4516 | &key)); |
| 4517 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4518 | TEST_CALLOC(output1, output1_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4519 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4520 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, |
| 4521 | output1, output1_size, |
| 4522 | &output1_length)); |
| 4523 | TEST_LE_U(output1_length, |
| 4524 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4525 | TEST_LE_U(output1_length, |
| 4526 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4527 | |
| 4528 | output2_size = output1_length; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4529 | TEST_CALLOC(output2, output2_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4530 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4531 | PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length, |
| 4532 | output2, output2_size, |
| 4533 | &output2_length)); |
| 4534 | TEST_LE_U(output2_length, |
| 4535 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4536 | TEST_LE_U(output2_length, |
| 4537 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4538 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4539 | TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4540 | |
| 4541 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4542 | mbedtls_free(output1); |
| 4543 | mbedtls_free(output2); |
| 4544 | psa_destroy_key(key); |
| 4545 | PSA_DONE(); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4546 | } |
| 4547 | /* END_CASE */ |
| 4548 | |
| 4549 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4550 | void cipher_verify_output_multipart(int alg_arg, |
| 4551 | int key_type_arg, |
| 4552 | data_t *key_data, |
| 4553 | data_t *input, |
| 4554 | int first_part_size_arg) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4555 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4556 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4557 | psa_key_type_t key_type = key_type_arg; |
| 4558 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4559 | size_t first_part_size = first_part_size_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4560 | unsigned char iv[16] = { 0 }; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4561 | size_t iv_size = 16; |
| 4562 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4563 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4564 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4565 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4566 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4567 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4568 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4569 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4570 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 4571 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4572 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
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_crypto_init()); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4575 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4576 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4577 | psa_set_key_algorithm(&attributes, alg); |
| 4578 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4579 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4580 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4581 | &key)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4582 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4583 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg)); |
| 4584 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4585 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4586 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 4587 | PSA_ASSERT(psa_cipher_generate_iv(&operation1, |
| 4588 | iv, iv_size, |
| 4589 | &iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4590 | } |
| 4591 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4592 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4593 | TEST_LE_U(output1_buffer_size, |
| 4594 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4595 | TEST_CALLOC(output1, output1_buffer_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4596 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4597 | TEST_LE_U(first_part_size, input->len); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4598 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4599 | PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size, |
| 4600 | output1, output1_buffer_size, |
| 4601 | &function_output_length)); |
| 4602 | TEST_LE_U(function_output_length, |
| 4603 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4604 | TEST_LE_U(function_output_length, |
| 4605 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4606 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4607 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4608 | PSA_ASSERT(psa_cipher_update(&operation1, |
| 4609 | input->x + first_part_size, |
| 4610 | input->len - first_part_size, |
| 4611 | output1, output1_buffer_size, |
| 4612 | &function_output_length)); |
| 4613 | TEST_LE_U(function_output_length, |
| 4614 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4615 | alg, |
| 4616 | input->len - first_part_size)); |
| 4617 | TEST_LE_U(function_output_length, |
| 4618 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4619 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4620 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4621 | PSA_ASSERT(psa_cipher_finish(&operation1, |
| 4622 | output1 + output1_length, |
| 4623 | output1_buffer_size - output1_length, |
| 4624 | &function_output_length)); |
| 4625 | TEST_LE_U(function_output_length, |
| 4626 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4627 | TEST_LE_U(function_output_length, |
| 4628 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4629 | output1_length += function_output_length; |
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 | PSA_ASSERT(psa_cipher_abort(&operation1)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4632 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4633 | output2_buffer_size = output1_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4634 | TEST_LE_U(output2_buffer_size, |
| 4635 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4636 | TEST_LE_U(output2_buffer_size, |
| 4637 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4638 | TEST_CALLOC(output2, output2_buffer_size); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4639 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4640 | if (iv_length > 0) { |
| 4641 | PSA_ASSERT(psa_cipher_set_iv(&operation2, |
| 4642 | iv, iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4643 | } |
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, output1, first_part_size, |
| 4646 | output2, output2_buffer_size, |
| 4647 | &function_output_length)); |
| 4648 | TEST_LE_U(function_output_length, |
| 4649 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4650 | TEST_LE_U(function_output_length, |
| 4651 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4652 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4653 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4654 | PSA_ASSERT(psa_cipher_update(&operation2, |
| 4655 | output1 + first_part_size, |
| 4656 | output1_length - first_part_size, |
| 4657 | output2, output2_buffer_size, |
| 4658 | &function_output_length)); |
| 4659 | TEST_LE_U(function_output_length, |
| 4660 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4661 | alg, |
| 4662 | output1_length - first_part_size)); |
| 4663 | TEST_LE_U(function_output_length, |
| 4664 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4665 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4666 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4667 | PSA_ASSERT(psa_cipher_finish(&operation2, |
| 4668 | output2 + output2_length, |
| 4669 | output2_buffer_size - output2_length, |
| 4670 | &function_output_length)); |
| 4671 | TEST_LE_U(function_output_length, |
| 4672 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4673 | TEST_LE_U(function_output_length, |
| 4674 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4675 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4676 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4677 | PSA_ASSERT(psa_cipher_abort(&operation2)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4678 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4679 | TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4680 | |
| 4681 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4682 | psa_cipher_abort(&operation1); |
| 4683 | psa_cipher_abort(&operation2); |
| 4684 | mbedtls_free(output1); |
| 4685 | mbedtls_free(output2); |
| 4686 | psa_destroy_key(key); |
| 4687 | PSA_DONE(); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4688 | } |
| 4689 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 4690 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4691 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4692 | void aead_encrypt_decrypt(int key_type_arg, data_t *key_data, |
| 4693 | int alg_arg, |
| 4694 | data_t *nonce, |
| 4695 | data_t *additional_data, |
| 4696 | data_t *input_data, |
| 4697 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4698 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4699 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4700 | psa_key_type_t key_type = key_type_arg; |
| 4701 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4702 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4703 | unsigned char *output_data = NULL; |
| 4704 | size_t output_size = 0; |
| 4705 | size_t output_length = 0; |
| 4706 | unsigned char *output_data2 = NULL; |
| 4707 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4708 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4709 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4710 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4711 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4712 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4713 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4714 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4715 | psa_set_key_algorithm(&attributes, alg); |
| 4716 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4717 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4718 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4719 | &key)); |
| 4720 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4721 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4722 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4723 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4724 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4725 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4726 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4727 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4728 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
| 4729 | TEST_EQUAL(output_size, |
| 4730 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4731 | TEST_LE_U(output_size, |
| 4732 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4733 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4734 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4735 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4736 | status = psa_aead_encrypt(key, alg, |
| 4737 | nonce->x, nonce->len, |
| 4738 | additional_data->x, |
| 4739 | additional_data->len, |
| 4740 | input_data->x, input_data->len, |
| 4741 | output_data, output_size, |
| 4742 | &output_length); |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4743 | |
| 4744 | /* If the operation is not supported, just skip and not fail in case the |
| 4745 | * encryption involves a common limitation of cryptography hardwares and |
| 4746 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4747 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4748 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4749 | 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] | 4750 | } |
| 4751 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4752 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4753 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4754 | if (PSA_SUCCESS == expected_result) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4755 | TEST_CALLOC(output_data2, output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4756 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4757 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4758 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4759 | TEST_EQUAL(input_data->len, |
| 4760 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length)); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4761 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4762 | TEST_LE_U(input_data->len, |
| 4763 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length)); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4764 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4765 | TEST_EQUAL(psa_aead_decrypt(key, alg, |
| 4766 | nonce->x, nonce->len, |
| 4767 | additional_data->x, |
| 4768 | additional_data->len, |
| 4769 | output_data, output_length, |
| 4770 | output_data2, output_length, |
| 4771 | &output_length2), |
| 4772 | expected_result); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4773 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4774 | TEST_MEMORY_COMPARE(input_data->x, input_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4775 | output_data2, output_length2); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4776 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4777 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4778 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4779 | psa_destroy_key(key); |
| 4780 | mbedtls_free(output_data); |
| 4781 | mbedtls_free(output_data2); |
| 4782 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4783 | } |
| 4784 | /* END_CASE */ |
| 4785 | |
| 4786 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4787 | void aead_encrypt(int key_type_arg, data_t *key_data, |
| 4788 | int alg_arg, |
| 4789 | data_t *nonce, |
| 4790 | data_t *additional_data, |
| 4791 | data_t *input_data, |
| 4792 | data_t *expected_result) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4793 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4794 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4795 | psa_key_type_t key_type = key_type_arg; |
| 4796 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4797 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4798 | unsigned char *output_data = NULL; |
| 4799 | size_t output_size = 0; |
| 4800 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4801 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4802 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4803 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4804 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4805 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4806 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4807 | psa_set_key_algorithm(&attributes, alg); |
| 4808 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4809 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4810 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4811 | &key)); |
| 4812 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4813 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4814 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4815 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4816 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4817 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4818 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4819 | TEST_EQUAL(output_size, |
| 4820 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4821 | TEST_LE_U(output_size, |
| 4822 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4823 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4824 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4825 | status = psa_aead_encrypt(key, alg, |
| 4826 | nonce->x, nonce->len, |
| 4827 | additional_data->x, additional_data->len, |
| 4828 | input_data->x, input_data->len, |
| 4829 | output_data, output_size, |
| 4830 | &output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4831 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4832 | /* If the operation is not supported, just skip and not fail in case the |
| 4833 | * encryption involves a common limitation of cryptography hardwares and |
| 4834 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4835 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4836 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4837 | 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] | 4838 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4839 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4840 | PSA_ASSERT(status); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4841 | TEST_MEMORY_COMPARE(expected_result->x, expected_result->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4842 | output_data, output_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4843 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4844 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4845 | psa_destroy_key(key); |
| 4846 | mbedtls_free(output_data); |
| 4847 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4848 | } |
| 4849 | /* END_CASE */ |
| 4850 | |
| 4851 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4852 | void aead_decrypt(int key_type_arg, data_t *key_data, |
| 4853 | int alg_arg, |
| 4854 | data_t *nonce, |
| 4855 | data_t *additional_data, |
| 4856 | data_t *input_data, |
| 4857 | data_t *expected_data, |
| 4858 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4859 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4860 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4861 | psa_key_type_t key_type = key_type_arg; |
| 4862 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4863 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4864 | unsigned char *output_data = NULL; |
| 4865 | size_t output_size = 0; |
| 4866 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4867 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4868 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4869 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4870 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4871 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4872 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4873 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4874 | psa_set_key_algorithm(&attributes, alg); |
| 4875 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4876 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4877 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4878 | &key)); |
| 4879 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4880 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4881 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4882 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4883 | alg); |
| 4884 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4885 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4886 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4887 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4888 | TEST_EQUAL(output_size, |
| 4889 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4890 | TEST_LE_U(output_size, |
| 4891 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4892 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4893 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4894 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4895 | status = psa_aead_decrypt(key, alg, |
| 4896 | nonce->x, nonce->len, |
| 4897 | additional_data->x, |
| 4898 | additional_data->len, |
| 4899 | input_data->x, input_data->len, |
| 4900 | output_data, output_size, |
| 4901 | &output_length); |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4902 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4903 | /* If the operation is not supported, just skip and not fail in case the |
| 4904 | * decryption involves a common limitation of cryptography hardwares and |
| 4905 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4906 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4907 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4908 | 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] | 4909 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4910 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4911 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4912 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4913 | if (expected_result == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4914 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4915 | output_data, output_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4916 | } |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4917 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4918 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4919 | psa_destroy_key(key); |
| 4920 | mbedtls_free(output_data); |
| 4921 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4922 | } |
| 4923 | /* END_CASE */ |
| 4924 | |
| 4925 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4926 | void aead_multipart_encrypt(int key_type_arg, data_t *key_data, |
| 4927 | int alg_arg, |
| 4928 | data_t *nonce, |
| 4929 | data_t *additional_data, |
| 4930 | data_t *input_data, |
| 4931 | int do_set_lengths, |
| 4932 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4933 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4934 | size_t ad_part_len = 0; |
| 4935 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4936 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4937 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4938 | for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) { |
| 4939 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4940 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4941 | if (do_set_lengths) { |
| 4942 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4943 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4944 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4945 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4946 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4947 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4948 | |
| 4949 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4950 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4951 | alg_arg, nonce, |
| 4952 | additional_data, |
| 4953 | ad_part_len, |
| 4954 | input_data, -1, |
| 4955 | set_lengths_method, |
| 4956 | expected_output, |
| 4957 | 1, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4958 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4959 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4960 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4961 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 4962 | mbedtls_test_set_step(1000 + ad_part_len); |
| 4963 | |
| 4964 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4965 | alg_arg, nonce, |
| 4966 | additional_data, |
| 4967 | ad_part_len, |
| 4968 | input_data, -1, |
| 4969 | set_lengths_method, |
| 4970 | expected_output, |
| 4971 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4972 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4973 | } |
| 4974 | } |
| 4975 | |
| 4976 | for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) { |
| 4977 | /* Split data into length(data_part_len) parts. */ |
| 4978 | mbedtls_test_set_step(2000 + data_part_len); |
| 4979 | |
| 4980 | if (do_set_lengths) { |
| 4981 | if (data_part_len & 0x01) { |
| 4982 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4983 | } else { |
| 4984 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
| 4985 | } |
| 4986 | } |
| 4987 | |
| 4988 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4989 | alg_arg, nonce, |
| 4990 | additional_data, -1, |
| 4991 | input_data, data_part_len, |
| 4992 | set_lengths_method, |
| 4993 | expected_output, |
| 4994 | 1, 0)) { |
| 4995 | break; |
| 4996 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4997 | |
| 4998 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4999 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5000 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5001 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5002 | alg_arg, nonce, |
| 5003 | additional_data, -1, |
| 5004 | input_data, data_part_len, |
| 5005 | set_lengths_method, |
| 5006 | expected_output, |
| 5007 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5008 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5009 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5010 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5011 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5012 | /* Goto is required to silence warnings about unused labels, as we |
| 5013 | * don't actually do any test assertions in this function. */ |
| 5014 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5015 | } |
| 5016 | /* END_CASE */ |
| 5017 | |
| 5018 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5019 | void aead_multipart_decrypt(int key_type_arg, data_t *key_data, |
| 5020 | int alg_arg, |
| 5021 | data_t *nonce, |
| 5022 | data_t *additional_data, |
| 5023 | data_t *input_data, |
| 5024 | int do_set_lengths, |
| 5025 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5026 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5027 | size_t ad_part_len = 0; |
| 5028 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5029 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5030 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5031 | 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] | 5032 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5033 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5035 | if (do_set_lengths) { |
| 5036 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5037 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5038 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5039 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5040 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5041 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5042 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5043 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5044 | alg_arg, nonce, |
| 5045 | additional_data, |
| 5046 | ad_part_len, |
| 5047 | input_data, -1, |
| 5048 | set_lengths_method, |
| 5049 | expected_output, |
| 5050 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5051 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5052 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5053 | |
| 5054 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5055 | mbedtls_test_set_step(1000 + ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5056 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5057 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5058 | alg_arg, nonce, |
| 5059 | additional_data, |
| 5060 | ad_part_len, |
| 5061 | input_data, -1, |
| 5062 | set_lengths_method, |
| 5063 | expected_output, |
| 5064 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5065 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5066 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5067 | } |
| 5068 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5069 | 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] | 5070 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5071 | mbedtls_test_set_step(2000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5072 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5073 | if (do_set_lengths) { |
| 5074 | if (data_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5075 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5076 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5077 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5078 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5079 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5080 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5081 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5082 | alg_arg, nonce, |
| 5083 | additional_data, -1, |
| 5084 | input_data, data_part_len, |
| 5085 | set_lengths_method, |
| 5086 | expected_output, |
| 5087 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5088 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5089 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5090 | |
| 5091 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5092 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5093 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5094 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5095 | alg_arg, nonce, |
| 5096 | additional_data, -1, |
| 5097 | input_data, data_part_len, |
| 5098 | set_lengths_method, |
| 5099 | expected_output, |
| 5100 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5101 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5102 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5103 | } |
| 5104 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5105 | /* Goto is required to silence warnings about unused labels, as we |
| 5106 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5107 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5108 | } |
| 5109 | /* END_CASE */ |
| 5110 | |
| 5111 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5112 | void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data, |
| 5113 | int alg_arg, |
| 5114 | int nonce_length, |
| 5115 | int expected_nonce_length_arg, |
| 5116 | data_t *additional_data, |
| 5117 | data_t *input_data, |
| 5118 | int expected_status_arg) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5119 | { |
| 5120 | |
| 5121 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5122 | psa_key_type_t key_type = key_type_arg; |
| 5123 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5124 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5125 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5126 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5127 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5128 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5129 | size_t actual_nonce_length = 0; |
| 5130 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 5131 | unsigned char *output = NULL; |
| 5132 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5133 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5134 | size_t ciphertext_size = 0; |
| 5135 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5136 | size_t tag_length = 0; |
| 5137 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
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_crypto_init()); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5140 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5141 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5142 | psa_set_key_algorithm(&attributes, alg); |
| 5143 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5144 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5145 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5146 | &key)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5147 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5148 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5150 | 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] | 5151 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5152 | TEST_CALLOC(output, output_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5153 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5154 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5155 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5156 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5157 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5158 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5159 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5160 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5161 | |
| 5162 | /* If the operation is not supported, just skip and not fail in case the |
| 5163 | * encryption involves a common limitation of cryptography hardwares and |
| 5164 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5165 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5166 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5167 | 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] | 5168 | } |
| 5169 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5170 | PSA_ASSERT(status); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5172 | status = psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5173 | nonce_length, |
| 5174 | &actual_nonce_length); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5175 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5176 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5177 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5178 | TEST_EQUAL(actual_nonce_length, expected_nonce_length); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 5179 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5180 | if (expected_status == PSA_SUCCESS) { |
| 5181 | TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type, |
| 5182 | alg)); |
| 5183 | } |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 5184 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5185 | TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 5186 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5187 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5188 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5189 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5190 | input_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5191 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5192 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5193 | additional_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5194 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5195 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5196 | output, output_size, |
| 5197 | &ciphertext_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5198 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5199 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5200 | &ciphertext_length, tag_buffer, |
| 5201 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5202 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5203 | |
| 5204 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5205 | psa_destroy_key(key); |
| 5206 | mbedtls_free(output); |
| 5207 | mbedtls_free(ciphertext); |
| 5208 | psa_aead_abort(&operation); |
| 5209 | PSA_DONE(); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5210 | } |
| 5211 | /* END_CASE */ |
| 5212 | |
| 5213 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5214 | void aead_multipart_set_nonce(int key_type_arg, data_t *key_data, |
| 5215 | int alg_arg, |
| 5216 | int nonce_length_arg, |
| 5217 | int set_lengths_method_arg, |
| 5218 | data_t *additional_data, |
| 5219 | data_t *input_data, |
| 5220 | int expected_status_arg) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5221 | { |
| 5222 | |
| 5223 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5224 | psa_key_type_t key_type = key_type_arg; |
| 5225 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5226 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5227 | uint8_t *nonce_buffer = NULL; |
| 5228 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5229 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5230 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5231 | unsigned char *output = NULL; |
| 5232 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5233 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5234 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5235 | size_t ciphertext_size = 0; |
| 5236 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5237 | size_t tag_length = 0; |
| 5238 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5239 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5240 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
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_crypto_init()); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5243 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5244 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5245 | psa_set_key_algorithm(&attributes, alg); |
| 5246 | psa_set_key_type(&attributes, key_type); |
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 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5249 | &key)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5250 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5251 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5252 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5253 | 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] | 5254 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5255 | TEST_CALLOC(output, output_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5256 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5257 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5258 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5259 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5260 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5261 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5263 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5264 | |
| 5265 | /* If the operation is not supported, just skip and not fail in case the |
| 5266 | * encryption involves a common limitation of cryptography hardwares and |
| 5267 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5268 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5269 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5270 | 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] | 5271 | } |
| 5272 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5273 | PSA_ASSERT(status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5274 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5275 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5276 | if (nonce_length_arg == -1) { |
| 5277 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5278 | TEST_CALLOC(nonce_buffer, 4); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5279 | nonce_length = 0; |
| 5280 | } else { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5281 | /* If length is zero, then this will return NULL. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5282 | nonce_length = (size_t) nonce_length_arg; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5283 | TEST_CALLOC(nonce_buffer, nonce_length); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5284 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5285 | if (nonce_buffer) { |
| 5286 | for (index = 0; index < nonce_length - 1; ++index) { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5287 | nonce_buffer[index] = 'a' + index; |
| 5288 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5289 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5290 | } |
| 5291 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5292 | if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { |
| 5293 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5294 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5295 | } |
| 5296 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5297 | status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5298 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5299 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5300 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5301 | if (expected_status == PSA_SUCCESS) { |
| 5302 | if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) { |
| 5303 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5304 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5305 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5306 | 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] | 5307 | expected_status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5308 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5309 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5310 | /* 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] | 5311 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5312 | additional_data->len), |
| 5313 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5314 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5315 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5316 | output, output_size, |
| 5317 | &ciphertext_length), |
| 5318 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5319 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5320 | TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5321 | &ciphertext_length, tag_buffer, |
| 5322 | PSA_AEAD_TAG_MAX_SIZE, &tag_length), |
| 5323 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5324 | } |
| 5325 | |
| 5326 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5327 | psa_destroy_key(key); |
| 5328 | mbedtls_free(output); |
| 5329 | mbedtls_free(ciphertext); |
| 5330 | mbedtls_free(nonce_buffer); |
| 5331 | psa_aead_abort(&operation); |
| 5332 | PSA_DONE(); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5333 | } |
| 5334 | /* END_CASE */ |
| 5335 | |
| 5336 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5337 | 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] | 5338 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5339 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5340 | data_t *nonce, |
| 5341 | data_t *additional_data, |
| 5342 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5343 | int expected_status_arg) |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5344 | { |
| 5345 | |
| 5346 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5347 | psa_key_type_t key_type = key_type_arg; |
| 5348 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5349 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5350 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5351 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5352 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5353 | unsigned char *output = NULL; |
| 5354 | unsigned char *ciphertext = NULL; |
| 5355 | size_t output_size = output_size_arg; |
| 5356 | size_t ciphertext_size = 0; |
| 5357 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5358 | size_t tag_length = 0; |
| 5359 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5360 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5361 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5362 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5363 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5364 | psa_set_key_algorithm(&attributes, alg); |
| 5365 | psa_set_key_type(&attributes, key_type); |
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 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5368 | &key)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5370 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5371 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5372 | TEST_CALLOC(output, output_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5374 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5375 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5376 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5377 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5378 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5379 | |
| 5380 | /* If the operation is not supported, just skip and not fail in case the |
| 5381 | * encryption involves a common limitation of cryptography hardwares and |
| 5382 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5383 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5384 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5385 | 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] | 5386 | } |
| 5387 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5388 | PSA_ASSERT(status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5389 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5390 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5391 | input_data->len)); |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5392 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5393 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5394 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5395 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5396 | additional_data->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5397 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5398 | status = psa_aead_update(&operation, input_data->x, input_data->len, |
| 5399 | output, output_size, &ciphertext_length); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5400 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5401 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5402 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5403 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5404 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5405 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5406 | &ciphertext_length, tag_buffer, |
| 5407 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5408 | } |
| 5409 | |
| 5410 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5411 | psa_destroy_key(key); |
| 5412 | mbedtls_free(output); |
| 5413 | mbedtls_free(ciphertext); |
| 5414 | psa_aead_abort(&operation); |
| 5415 | PSA_DONE(); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5416 | } |
| 5417 | /* END_CASE */ |
| 5418 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5419 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5420 | void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data, |
| 5421 | int alg_arg, |
| 5422 | int finish_ciphertext_size_arg, |
| 5423 | int tag_size_arg, |
| 5424 | data_t *nonce, |
| 5425 | data_t *additional_data, |
| 5426 | data_t *input_data, |
| 5427 | int expected_status_arg) |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5428 | { |
| 5429 | |
| 5430 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5431 | psa_key_type_t key_type = key_type_arg; |
| 5432 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5433 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5434 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5435 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5436 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5437 | unsigned char *ciphertext = NULL; |
| 5438 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5439 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5440 | size_t ciphertext_size = 0; |
| 5441 | size_t ciphertext_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5442 | size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg; |
| 5443 | size_t tag_size = (size_t) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5444 | size_t tag_length = 0; |
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_crypto_init()); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5447 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5448 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5449 | psa_set_key_algorithm(&attributes, alg); |
| 5450 | psa_set_key_type(&attributes, key_type); |
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 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5453 | &key)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5454 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5455 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5456 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5457 | 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] | 5458 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5459 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5460 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5461 | TEST_CALLOC(finish_ciphertext, finish_ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5462 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5463 | TEST_CALLOC(tag_buffer, tag_size); |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5464 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5465 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5466 | |
| 5467 | /* If the operation is not supported, just skip and not fail in case the |
| 5468 | * encryption involves a common limitation of cryptography hardwares and |
| 5469 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5470 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5471 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5472 | 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] | 5473 | } |
| 5474 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5475 | PSA_ASSERT(status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5476 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5477 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5478 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5479 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5480 | input_data->len)); |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5481 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5482 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5483 | additional_data->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5484 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5485 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5486 | ciphertext, ciphertext_size, &ciphertext_length)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5487 | |
| 5488 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5489 | status = psa_aead_finish(&operation, finish_ciphertext, |
| 5490 | finish_ciphertext_size, |
| 5491 | &ciphertext_length, tag_buffer, |
| 5492 | tag_size, &tag_length); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5493 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5494 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5495 | |
| 5496 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5497 | psa_destroy_key(key); |
| 5498 | mbedtls_free(ciphertext); |
| 5499 | mbedtls_free(finish_ciphertext); |
| 5500 | mbedtls_free(tag_buffer); |
| 5501 | psa_aead_abort(&operation); |
| 5502 | PSA_DONE(); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5503 | } |
| 5504 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5505 | |
| 5506 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5507 | void aead_multipart_verify(int key_type_arg, data_t *key_data, |
| 5508 | int alg_arg, |
| 5509 | data_t *nonce, |
| 5510 | data_t *additional_data, |
| 5511 | data_t *input_data, |
| 5512 | data_t *tag, |
| 5513 | int tag_usage_arg, |
| 5514 | int expected_setup_status_arg, |
| 5515 | int expected_status_arg) |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5516 | { |
| 5517 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5518 | psa_key_type_t key_type = key_type_arg; |
| 5519 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5520 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5521 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5522 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5523 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5524 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5525 | unsigned char *plaintext = NULL; |
| 5526 | unsigned char *finish_plaintext = NULL; |
| 5527 | size_t plaintext_size = 0; |
| 5528 | size_t plaintext_length = 0; |
| 5529 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5530 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5531 | unsigned char *tag_buffer = NULL; |
| 5532 | size_t tag_size = 0; |
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_crypto_init()); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5535 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5536 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 5537 | psa_set_key_algorithm(&attributes, alg); |
| 5538 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5539 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5540 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5541 | &key)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5542 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5543 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
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 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 5546 | input_data->len); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5547 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5548 | TEST_CALLOC(plaintext, plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5549 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5550 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5551 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5552 | TEST_CALLOC(finish_plaintext, verify_plaintext_size); |
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 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5555 | |
| 5556 | /* If the operation is not supported, just skip and not fail in case the |
| 5557 | * encryption involves a common limitation of cryptography hardwares and |
| 5558 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5559 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5560 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5561 | 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] | 5562 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5563 | TEST_EQUAL(status, expected_setup_status); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5564 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5565 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5566 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5567 | } |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5568 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5569 | PSA_ASSERT(status); |
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_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5572 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5573 | status = psa_aead_set_lengths(&operation, additional_data->len, |
| 5574 | input_data->len); |
| 5575 | PSA_ASSERT(status); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5576 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5577 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5578 | additional_data->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5579 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5580 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 5581 | input_data->len, |
| 5582 | plaintext, plaintext_size, |
| 5583 | &plaintext_length)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5584 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5585 | if (tag_usage == USE_GIVEN_TAG) { |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5586 | tag_buffer = tag->x; |
| 5587 | tag_size = tag->len; |
| 5588 | } |
| 5589 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5590 | status = psa_aead_verify(&operation, finish_plaintext, |
| 5591 | verify_plaintext_size, |
| 5592 | &plaintext_length, |
| 5593 | tag_buffer, tag_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5594 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5595 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5596 | |
| 5597 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5598 | psa_destroy_key(key); |
| 5599 | mbedtls_free(plaintext); |
| 5600 | mbedtls_free(finish_plaintext); |
| 5601 | psa_aead_abort(&operation); |
| 5602 | PSA_DONE(); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5603 | } |
| 5604 | /* END_CASE */ |
| 5605 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5606 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5607 | void aead_multipart_setup(int key_type_arg, data_t *key_data, |
| 5608 | int alg_arg, int expected_status_arg) |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5609 | { |
| 5610 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5611 | psa_key_type_t key_type = key_type_arg; |
| 5612 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5613 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5614 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5615 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5616 | psa_status_t expected_status = expected_status_arg; |
| 5617 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5618 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5619 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5620 | psa_set_key_usage_flags(&attributes, |
| 5621 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5622 | psa_set_key_algorithm(&attributes, alg); |
| 5623 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5624 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5625 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5626 | &key)); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5627 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5628 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5629 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5630 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5631 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5632 | psa_aead_abort(&operation); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5633 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5634 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5635 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5636 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5637 | |
| 5638 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5639 | psa_destroy_key(key); |
| 5640 | psa_aead_abort(&operation); |
| 5641 | PSA_DONE(); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5642 | } |
| 5643 | /* END_CASE */ |
| 5644 | |
| 5645 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5646 | void aead_multipart_state_test(int key_type_arg, data_t *key_data, |
| 5647 | int alg_arg, |
| 5648 | data_t *nonce, |
| 5649 | data_t *additional_data, |
| 5650 | data_t *input_data) |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5651 | { |
| 5652 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5653 | psa_key_type_t key_type = key_type_arg; |
| 5654 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5655 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5656 | unsigned char *output_data = NULL; |
| 5657 | unsigned char *final_data = NULL; |
| 5658 | size_t output_size = 0; |
| 5659 | size_t finish_output_size = 0; |
| 5660 | size_t output_length = 0; |
| 5661 | size_t key_bits = 0; |
| 5662 | size_t tag_length = 0; |
| 5663 | size_t tag_size = 0; |
| 5664 | size_t nonce_length = 0; |
| 5665 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5666 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5667 | size_t output_part_length = 0; |
| 5668 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5669 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5670 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5671 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5672 | psa_set_key_usage_flags(&attributes, |
| 5673 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5674 | psa_set_key_algorithm(&attributes, alg); |
| 5675 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5676 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5677 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5678 | &key)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5679 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5680 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5681 | key_bits = psa_get_key_bits(&attributes); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5682 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5683 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5684 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5685 | TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5686 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5687 | 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] | 5688 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5689 | TEST_CALLOC(output_data, output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5690 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5691 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5692 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5693 | TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5694 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5695 | TEST_CALLOC(final_data, finish_output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5696 | |
| 5697 | /* Test all operations error without calling setup first. */ |
| 5698 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5699 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5700 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5701 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5702 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5703 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5704 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5705 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5706 | &nonce_length), |
| 5707 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5708 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5709 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5710 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5711 | /* ------------------------------------------------------- */ |
| 5712 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5713 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 5714 | input_data->len), |
| 5715 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5716 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5717 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5718 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5719 | /* ------------------------------------------------------- */ |
| 5720 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5721 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5722 | additional_data->len), |
| 5723 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5724 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5725 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5726 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5727 | /* ------------------------------------------------------- */ |
| 5728 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5729 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 5730 | input_data->len, output_data, |
| 5731 | output_size, &output_length), |
| 5732 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5733 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5734 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5735 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5736 | /* ------------------------------------------------------- */ |
| 5737 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5738 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 5739 | finish_output_size, |
| 5740 | &output_part_length, |
| 5741 | tag_buffer, tag_length, |
| 5742 | &tag_size), |
| 5743 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5744 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5745 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5746 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5747 | /* ------------------------------------------------------- */ |
| 5748 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5749 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 5750 | finish_output_size, |
| 5751 | &output_part_length, |
| 5752 | tag_buffer, |
| 5753 | tag_length), |
| 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 | |
| 5758 | /* Test for double setups. */ |
| 5759 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5760 | PSA_ASSERT(psa_aead_encrypt_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_encrypt_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 | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5767 | /* ------------------------------------------------------- */ |
| 5768 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5769 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5771 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 5772 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5773 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5774 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5775 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5776 | /* ------------------------------------------------------- */ |
| 5777 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5778 | PSA_ASSERT(psa_aead_encrypt_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_decrypt_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 | |
| 5785 | /* ------------------------------------------------------- */ |
| 5786 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5787 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5788 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5789 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 5790 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5791 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5792 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5793 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5794 | /* Test for not setting a nonce. */ |
| 5795 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5796 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5797 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5798 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5799 | additional_data->len), |
| 5800 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5801 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5802 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5803 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5804 | /* ------------------------------------------------------- */ |
| 5805 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5806 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5807 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5808 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 5809 | input_data->len, output_data, |
| 5810 | output_size, &output_length), |
| 5811 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5812 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5813 | psa_aead_abort(&operation); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5814 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5815 | /* ------------------------------------------------------- */ |
| 5816 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5817 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5818 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5819 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 5820 | finish_output_size, |
| 5821 | &output_part_length, |
| 5822 | tag_buffer, tag_length, |
| 5823 | &tag_size), |
| 5824 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5825 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5826 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5827 | |
| 5828 | /* ------------------------------------------------------- */ |
| 5829 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5830 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5831 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5832 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 5833 | finish_output_size, |
| 5834 | &output_part_length, |
| 5835 | tag_buffer, |
| 5836 | tag_length), |
| 5837 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5838 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5839 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5840 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5841 | /* Test for double setting nonce. */ |
| 5842 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5843 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5844 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5845 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5846 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5847 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5848 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5849 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5850 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5851 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5852 | /* Test for double generating nonce. */ |
| 5853 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5854 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5855 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5856 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5857 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5858 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5859 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5860 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5861 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5862 | &nonce_length), |
| 5863 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5864 | |
| 5865 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5866 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5867 | |
| 5868 | /* Test for generate nonce then set and vice versa */ |
| 5869 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5870 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5871 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5872 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5873 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5874 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5875 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5876 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5877 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5878 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5879 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5880 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5881 | /* Test for generating nonce after calling set lengths */ |
| 5882 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5883 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5884 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5885 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5886 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5887 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5888 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5889 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5890 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5891 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5892 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5893 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5894 | /* 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] | 5895 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5896 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5897 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5898 | if (operation.alg == PSA_ALG_CCM) { |
| 5899 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5900 | input_data->len), |
| 5901 | PSA_ERROR_INVALID_ARGUMENT); |
| 5902 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5903 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5904 | &nonce_length), |
| 5905 | PSA_ERROR_BAD_STATE); |
| 5906 | } else { |
| 5907 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5908 | input_data->len)); |
| 5909 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5910 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5911 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5912 | } |
| 5913 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5914 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5915 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5916 | /* 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] | 5917 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5918 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5919 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5920 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 5921 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5922 | input_data->len), |
| 5923 | PSA_ERROR_INVALID_ARGUMENT); |
| 5924 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5925 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5926 | &nonce_length), |
| 5927 | PSA_ERROR_BAD_STATE); |
| 5928 | } else { |
| 5929 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5930 | input_data->len)); |
| 5931 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5932 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5933 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5934 | } |
| 5935 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5936 | psa_aead_abort(&operation); |
Dave Rodgman | d26d744 | 2023-02-11 17:14:54 +0000 | [diff] [blame] | 5937 | #endif |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5938 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5939 | /* 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] | 5940 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5941 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5942 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5943 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5944 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5945 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5946 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5947 | if (operation.alg == PSA_ALG_CCM) { |
| 5948 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5949 | input_data->len), |
| 5950 | PSA_ERROR_INVALID_ARGUMENT); |
| 5951 | } else { |
| 5952 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5953 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5954 | } |
| 5955 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5956 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5957 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5958 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5959 | /* Test for setting nonce after calling set lengths */ |
| 5960 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5961 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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_set_lengths(&operation, additional_data->len, |
| 5964 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5965 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5966 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5967 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5968 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5969 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5970 | /* 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] | 5971 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5972 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5973 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5974 | if (operation.alg == PSA_ALG_CCM) { |
| 5975 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5976 | input_data->len), |
| 5977 | PSA_ERROR_INVALID_ARGUMENT); |
| 5978 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5979 | PSA_ERROR_BAD_STATE); |
| 5980 | } else { |
| 5981 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5982 | input_data->len)); |
| 5983 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5984 | } |
| 5985 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5986 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5987 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5988 | /* 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] | 5989 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5990 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5991 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5992 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 5993 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5994 | input_data->len), |
| 5995 | PSA_ERROR_INVALID_ARGUMENT); |
| 5996 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5997 | PSA_ERROR_BAD_STATE); |
| 5998 | } else { |
| 5999 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 6000 | input_data->len)); |
| 6001 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6002 | } |
| 6003 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6004 | psa_aead_abort(&operation); |
Dave Rodgman | a476363 | 2023-02-11 18:36:23 +0000 | [diff] [blame] | 6005 | #endif |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6006 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6007 | /* 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] | 6008 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6009 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6010 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6011 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6012 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6013 | if (operation.alg == PSA_ALG_CCM) { |
| 6014 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6015 | input_data->len), |
| 6016 | PSA_ERROR_INVALID_ARGUMENT); |
| 6017 | } else { |
| 6018 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6019 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6020 | } |
| 6021 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6022 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6023 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6024 | /* 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] | 6025 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6026 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6027 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6028 | if (operation.alg == PSA_ALG_GCM) { |
| 6029 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6030 | SIZE_MAX), |
| 6031 | PSA_ERROR_INVALID_ARGUMENT); |
| 6032 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6033 | PSA_ERROR_BAD_STATE); |
| 6034 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6035 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6036 | SIZE_MAX)); |
| 6037 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6038 | } |
| 6039 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6040 | psa_aead_abort(&operation); |
Dave Rodgman | 91e8321 | 2023-02-11 20:07:43 +0000 | [diff] [blame] | 6041 | #endif |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6042 | |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 6043 | /* 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] | 6044 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6045 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6046 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6047 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6048 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6049 | if (operation.alg == PSA_ALG_GCM) { |
| 6050 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6051 | SIZE_MAX), |
| 6052 | PSA_ERROR_INVALID_ARGUMENT); |
| 6053 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6054 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6055 | SIZE_MAX)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6056 | } |
| 6057 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6058 | psa_aead_abort(&operation); |
Dave Rodgman | 641288b | 2023-02-11 22:02:04 +0000 | [diff] [blame] | 6059 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6060 | |
| 6061 | /* ------------------------------------------------------- */ |
| 6062 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6063 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6064 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6065 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6066 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6067 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6068 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6069 | &nonce_length), |
| 6070 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6071 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6072 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6073 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6074 | /* Test for generating nonce in decrypt setup. */ |
| 6075 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6076 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6077 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6078 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6079 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6080 | &nonce_length), |
| 6081 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6082 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6083 | psa_aead_abort(&operation); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6084 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6085 | /* Test for setting lengths twice. */ |
| 6086 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6087 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6090 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6091 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6092 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6093 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6094 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6095 | input_data->len), |
| 6096 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6097 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6098 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6099 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6100 | /* Test for setting lengths after setting nonce + already starting data. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6101 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6102 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6103 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6104 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6105 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6106 | if (operation.alg == PSA_ALG_CCM) { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6107 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6108 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6109 | additional_data->len), |
| 6110 | PSA_ERROR_BAD_STATE); |
| 6111 | } else { |
| 6112 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6113 | additional_data->len)); |
| 6114 | |
| 6115 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6116 | input_data->len), |
| 6117 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6118 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6119 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6120 | |
| 6121 | /* ------------------------------------------------------- */ |
| 6122 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6123 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6124 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6125 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6126 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6127 | if (operation.alg == PSA_ALG_CCM) { |
| 6128 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6129 | input_data->len, output_data, |
| 6130 | output_size, &output_length), |
| 6131 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6132 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6133 | } else { |
| 6134 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6135 | input_data->len, output_data, |
| 6136 | output_size, &output_length)); |
| 6137 | |
| 6138 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6139 | input_data->len), |
| 6140 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6141 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6142 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6143 | |
| 6144 | /* ------------------------------------------------------- */ |
| 6145 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6146 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6147 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6148 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6150 | if (operation.alg == PSA_ALG_CCM) { |
| 6151 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6152 | finish_output_size, |
| 6153 | &output_part_length, |
| 6154 | tag_buffer, tag_length, |
| 6155 | &tag_size)); |
| 6156 | } else { |
| 6157 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6158 | finish_output_size, |
| 6159 | &output_part_length, |
| 6160 | tag_buffer, tag_length, |
| 6161 | &tag_size)); |
| 6162 | |
| 6163 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6164 | input_data->len), |
| 6165 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6166 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6167 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6168 | |
| 6169 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 6170 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6171 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6172 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6173 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6174 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6175 | &nonce_length)); |
| 6176 | if (operation.alg == PSA_ALG_CCM) { |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6177 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6178 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6179 | additional_data->len), |
| 6180 | PSA_ERROR_BAD_STATE); |
| 6181 | } else { |
| 6182 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6183 | additional_data->len)); |
| 6184 | |
| 6185 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6186 | input_data->len), |
| 6187 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6188 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6189 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6190 | |
| 6191 | /* ------------------------------------------------------- */ |
| 6192 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6193 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6196 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6197 | &nonce_length)); |
| 6198 | if (operation.alg == PSA_ALG_CCM) { |
| 6199 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6200 | input_data->len, output_data, |
| 6201 | output_size, &output_length), |
| 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 | } else { |
| 6205 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6206 | input_data->len, output_data, |
| 6207 | output_size, &output_length)); |
| 6208 | |
| 6209 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6210 | input_data->len), |
| 6211 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6212 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6213 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6214 | |
| 6215 | /* ------------------------------------------------------- */ |
| 6216 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6217 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6218 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6219 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6220 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6221 | &nonce_length)); |
| 6222 | if (operation.alg == PSA_ALG_CCM) { |
| 6223 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6224 | finish_output_size, |
| 6225 | &output_part_length, |
| 6226 | tag_buffer, tag_length, |
| 6227 | &tag_size)); |
| 6228 | } else { |
| 6229 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6230 | finish_output_size, |
| 6231 | &output_part_length, |
| 6232 | tag_buffer, tag_length, |
| 6233 | &tag_size)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6234 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6235 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6236 | input_data->len), |
| 6237 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6238 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6239 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6240 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6241 | /* Test for not sending any additional data or data after setting non zero |
| 6242 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6243 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6244 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6245 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6246 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6247 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6248 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6249 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6250 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6251 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6252 | finish_output_size, |
| 6253 | &output_part_length, |
| 6254 | tag_buffer, tag_length, |
| 6255 | &tag_size), |
| 6256 | PSA_ERROR_INVALID_ARGUMENT); |
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_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6259 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6260 | /* Test for not sending any additional data or data after setting non-zero |
| 6261 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6263 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6264 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6265 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6266 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6267 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6268 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6269 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6270 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6271 | finish_output_size, |
| 6272 | &output_part_length, |
| 6273 | tag_buffer, |
| 6274 | tag_length), |
| 6275 | PSA_ERROR_INVALID_ARGUMENT); |
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_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6278 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6279 | /* Test for not sending any additional data after setting a non-zero length |
| 6280 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6282 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6283 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6284 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6285 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6286 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6287 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6288 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6289 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6290 | input_data->len, output_data, |
| 6291 | output_size, &output_length), |
| 6292 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6293 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6294 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6295 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6296 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 6297 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6298 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6299 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6300 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6301 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6302 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6303 | input_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6304 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6305 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6306 | additional_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6307 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6308 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6309 | finish_output_size, |
| 6310 | &output_part_length, |
| 6311 | tag_buffer, tag_length, |
| 6312 | &tag_size), |
| 6313 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6314 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6315 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6316 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6317 | /* Test for sending too much additional data after setting lengths. */ |
| 6318 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6319 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6322 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6323 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6324 | |
| 6325 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6326 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6327 | additional_data->len), |
| 6328 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6329 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6330 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6331 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6332 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6333 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6334 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6335 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6336 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6337 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6338 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6339 | input_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6340 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6341 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6342 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6343 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6344 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6345 | 1), |
| 6346 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6347 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6348 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6349 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6350 | /* Test for sending too much data after setting lengths. */ |
| 6351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6352 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6356 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6358 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6359 | input_data->len, output_data, |
| 6360 | output_size, &output_length), |
| 6361 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6362 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6363 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6364 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6365 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6367 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6368 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6369 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6371 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6372 | input_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6374 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6375 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6377 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6378 | input_data->len, output_data, |
| 6379 | output_size, &output_length)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6380 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6381 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6382 | 1, output_data, |
| 6383 | output_size, &output_length), |
| 6384 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6386 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6387 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6388 | /* Test sending additional data after data. */ |
| 6389 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6390 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6392 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6393 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6394 | if (operation.alg != PSA_ALG_CCM) { |
| 6395 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6396 | input_data->len, output_data, |
| 6397 | output_size, &output_length)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6398 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6399 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6400 | additional_data->len), |
| 6401 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6402 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6403 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6404 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6405 | /* Test calling finish on decryption. */ |
| 6406 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6407 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
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_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6410 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6411 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6412 | finish_output_size, |
| 6413 | &output_part_length, |
| 6414 | tag_buffer, tag_length, |
| 6415 | &tag_size), |
| 6416 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6417 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6418 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6419 | |
| 6420 | /* Test calling verify on encryption. */ |
| 6421 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6422 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6425 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6426 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6427 | finish_output_size, |
| 6428 | &output_part_length, |
| 6429 | tag_buffer, |
| 6430 | tag_length), |
| 6431 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6433 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6434 | |
| 6435 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6436 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6437 | psa_destroy_key(key); |
| 6438 | psa_aead_abort(&operation); |
| 6439 | mbedtls_free(output_data); |
| 6440 | mbedtls_free(final_data); |
| 6441 | PSA_DONE(); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6442 | } |
| 6443 | /* END_CASE */ |
| 6444 | |
| 6445 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6446 | void signature_size(int type_arg, |
| 6447 | int bits, |
| 6448 | int alg_arg, |
| 6449 | int expected_size_arg) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6450 | { |
| 6451 | psa_key_type_t type = type_arg; |
| 6452 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6453 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6454 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6455 | TEST_EQUAL(actual_size, (size_t) expected_size_arg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6456 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6457 | exit: |
| 6458 | ; |
| 6459 | } |
| 6460 | /* END_CASE */ |
| 6461 | |
| 6462 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6463 | void sign_hash_deterministic(int key_type_arg, data_t *key_data, |
| 6464 | int alg_arg, data_t *input_data, |
| 6465 | data_t *output_data) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6466 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6467 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6468 | psa_key_type_t key_type = key_type_arg; |
| 6469 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6470 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6471 | unsigned char *signature = NULL; |
| 6472 | size_t signature_size; |
| 6473 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6474 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6475 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6476 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6477 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6478 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6479 | psa_set_key_algorithm(&attributes, alg); |
| 6480 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6481 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6482 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6483 | &key)); |
| 6484 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6485 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6486 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6487 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6488 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6489 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6490 | key_bits, alg); |
| 6491 | TEST_ASSERT(signature_size != 0); |
| 6492 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6493 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6494 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6495 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6496 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6497 | input_data->x, input_data->len, |
| 6498 | signature, signature_size, |
| 6499 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6500 | /* Verify that the signature is what is expected. */ |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 6501 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 6502 | signature, signature_length); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6503 | |
| 6504 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6505 | /* |
| 6506 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6507 | * thus reset them as required. |
| 6508 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6509 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6510 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6511 | psa_destroy_key(key); |
| 6512 | mbedtls_free(signature); |
| 6513 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6514 | } |
| 6515 | /* END_CASE */ |
| 6516 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6517 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6518 | /** |
| 6519 | * sign_hash_interruptible() test intentions: |
| 6520 | * |
| 6521 | * Note: This test can currently only handle ECDSA. |
| 6522 | * |
| 6523 | * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 6524 | * and private keys / keypairs only). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6525 | * |
| 6526 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 6527 | * expected for different max_ops values. |
| 6528 | * |
| 6529 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 6530 | * and that each successful stage completes some ops (this is not mandated by |
| 6531 | * the PSA specification, but is currently the case). |
| 6532 | * |
| 6533 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 6534 | * complete() calls does not alter the number of ops returned. |
| 6535 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6536 | void sign_hash_interruptible(int key_type_arg, data_t *key_data, |
| 6537 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6538 | data_t *output_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6539 | { |
| 6540 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6541 | psa_key_type_t key_type = key_type_arg; |
| 6542 | psa_algorithm_t alg = alg_arg; |
| 6543 | size_t key_bits; |
| 6544 | unsigned char *signature = NULL; |
| 6545 | size_t signature_size; |
| 6546 | size_t signature_length = 0xdeadbeef; |
| 6547 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6548 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6549 | uint32_t num_ops = 0; |
| 6550 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6551 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6552 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6553 | size_t min_completes = 0; |
| 6554 | size_t max_completes = 0; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6555 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6556 | psa_sign_hash_interruptible_operation_t operation = |
| 6557 | psa_sign_hash_interruptible_operation_init(); |
| 6558 | |
| 6559 | PSA_ASSERT(psa_crypto_init()); |
| 6560 | |
| 6561 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6562 | psa_set_key_algorithm(&attributes, alg); |
| 6563 | psa_set_key_type(&attributes, key_type); |
| 6564 | |
| 6565 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6566 | &key)); |
| 6567 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6568 | key_bits = psa_get_key_bits(&attributes); |
| 6569 | |
| 6570 | /* Allocate a buffer which has the size advertised by the |
| 6571 | * library. */ |
| 6572 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6573 | key_bits, alg); |
| 6574 | TEST_ASSERT(signature_size != 0); |
| 6575 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6576 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6577 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6578 | psa_interruptible_set_max_ops(max_ops); |
| 6579 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6580 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6581 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6582 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6583 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6584 | TEST_ASSERT(num_ops_prior == 0); |
| 6585 | |
| 6586 | /* Start performing the signature. */ |
| 6587 | PSA_ASSERT(psa_sign_hash_start(&operation, key, alg, |
| 6588 | input_data->x, input_data->len)); |
| 6589 | |
| 6590 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6591 | TEST_ASSERT(num_ops_prior == 0); |
| 6592 | |
| 6593 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6594 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6595 | status = psa_sign_hash_complete(&operation, signature, signature_size, |
| 6596 | &signature_length); |
| 6597 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6598 | num_completes++; |
| 6599 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6600 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 6601 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6602 | /* We are asserting here that every complete makes progress |
| 6603 | * (completes some ops), which is true of the internal |
| 6604 | * implementation and probably any implementation, however this is |
| 6605 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6606 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6607 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6608 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 6609 | |
| 6610 | /* Ensure calling get_num_ops() twice still returns the same |
| 6611 | * number of ops as previously reported. */ |
| 6612 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6613 | |
| 6614 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6615 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6616 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6617 | |
| 6618 | TEST_ASSERT(status == PSA_SUCCESS); |
| 6619 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6620 | TEST_LE_U(min_completes, num_completes); |
| 6621 | TEST_LE_U(num_completes, max_completes); |
| 6622 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6623 | /* Verify that the signature is what is expected. */ |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 6624 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 6625 | signature, signature_length); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6626 | |
| 6627 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6628 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6629 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6630 | TEST_ASSERT(num_ops == 0); |
| 6631 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6632 | exit: |
| 6633 | |
| 6634 | /* |
| 6635 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6636 | * thus reset them as required. |
| 6637 | */ |
| 6638 | psa_reset_key_attributes(&attributes); |
| 6639 | |
| 6640 | psa_destroy_key(key); |
| 6641 | mbedtls_free(signature); |
| 6642 | PSA_DONE(); |
| 6643 | } |
| 6644 | /* END_CASE */ |
| 6645 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6646 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6647 | void sign_hash_fail(int key_type_arg, data_t *key_data, |
| 6648 | int alg_arg, data_t *input_data, |
| 6649 | int signature_size_arg, int expected_status_arg) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6650 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6651 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6652 | psa_key_type_t key_type = key_type_arg; |
| 6653 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6654 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6655 | psa_status_t actual_status; |
| 6656 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 6657 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 6658 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6659 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6660 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6661 | TEST_CALLOC(signature, signature_size); |
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 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6664 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6665 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6666 | psa_set_key_algorithm(&attributes, alg); |
| 6667 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6668 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6669 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6670 | &key)); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6671 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6672 | actual_status = psa_sign_hash(key, alg, |
| 6673 | input_data->x, input_data->len, |
| 6674 | signature, signature_size, |
| 6675 | &signature_length); |
| 6676 | TEST_EQUAL(actual_status, expected_status); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6677 | /* The value of *signature_length is unspecified on error, but |
| 6678 | * whatever it is, it should be less than signature_size, so that |
| 6679 | * if the caller tries to read *signature_length bytes without |
| 6680 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6681 | TEST_LE_U(signature_length, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6682 | |
| 6683 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6684 | psa_reset_key_attributes(&attributes); |
| 6685 | psa_destroy_key(key); |
| 6686 | mbedtls_free(signature); |
| 6687 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6688 | } |
| 6689 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6690 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6691 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6692 | /** |
| 6693 | * sign_hash_fail_interruptible() test intentions: |
| 6694 | * |
| 6695 | * Note: This test can currently only handle ECDSA. |
| 6696 | * |
| 6697 | * 1. Test that various failure cases for interruptible sign hash fail with the |
| 6698 | * correct error codes, and at the correct point (at start or during |
| 6699 | * complete). |
| 6700 | * |
| 6701 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 6702 | * expected for different max_ops values. |
| 6703 | * |
| 6704 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 6705 | * and that each successful stage completes some ops (this is not mandated by |
| 6706 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 6707 | * |
| 6708 | * 4. Check that calling complete() when start() fails and complete() |
| 6709 | * after completion results in a BAD_STATE error. |
| 6710 | * |
| 6711 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 6712 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6713 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6714 | void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 6715 | int alg_arg, data_t *input_data, |
| 6716 | int signature_size_arg, |
| 6717 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6718 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6719 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6720 | { |
| 6721 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6722 | psa_key_type_t key_type = key_type_arg; |
| 6723 | psa_algorithm_t alg = alg_arg; |
| 6724 | size_t signature_size = signature_size_arg; |
| 6725 | psa_status_t actual_status; |
| 6726 | psa_status_t expected_start_status = expected_start_status_arg; |
| 6727 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 6728 | unsigned char *signature = NULL; |
| 6729 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6730 | uint32_t num_ops = 0; |
| 6731 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6732 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6733 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6734 | size_t min_completes = 0; |
| 6735 | size_t max_completes = 0; |
| 6736 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6737 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6738 | psa_sign_hash_interruptible_operation_t operation = |
| 6739 | psa_sign_hash_interruptible_operation_init(); |
| 6740 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6741 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6742 | |
| 6743 | PSA_ASSERT(psa_crypto_init()); |
| 6744 | |
| 6745 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6746 | psa_set_key_algorithm(&attributes, alg); |
| 6747 | psa_set_key_type(&attributes, key_type); |
| 6748 | |
| 6749 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6750 | &key)); |
| 6751 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6752 | psa_interruptible_set_max_ops(max_ops); |
| 6753 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6754 | interruptible_signverify_get_minmax_completes(max_ops, |
| 6755 | expected_complete_status, |
| 6756 | &min_completes, |
| 6757 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6758 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6759 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6760 | TEST_ASSERT(num_ops_prior == 0); |
| 6761 | |
| 6762 | /* Start performing the signature. */ |
| 6763 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 6764 | input_data->x, input_data->len); |
| 6765 | |
| 6766 | TEST_EQUAL(actual_status, expected_start_status); |
| 6767 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6768 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 6769 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 6770 | * start failed. */ |
| 6771 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6772 | signature_size, |
| 6773 | &signature_length); |
| 6774 | |
| 6775 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 6776 | |
| 6777 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6778 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 6779 | input_data->x, input_data->len); |
| 6780 | |
| 6781 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 6782 | } |
| 6783 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6784 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6785 | TEST_ASSERT(num_ops_prior == 0); |
| 6786 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6787 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6788 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6789 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6790 | signature_size, |
| 6791 | &signature_length); |
| 6792 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6793 | num_completes++; |
| 6794 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6795 | if (actual_status == PSA_SUCCESS || |
| 6796 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6797 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6798 | /* We are asserting here that every complete makes progress |
| 6799 | * (completes some ops), which is true of the internal |
| 6800 | * implementation and probably any implementation, however this is |
| 6801 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6802 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6803 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6804 | num_ops_prior = num_ops; |
| 6805 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6806 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6807 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6808 | TEST_EQUAL(actual_status, expected_complete_status); |
| 6809 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 6810 | /* Check that another complete returns BAD_STATE. */ |
| 6811 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6812 | signature_size, |
| 6813 | &signature_length); |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6814 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 6815 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6816 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6817 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6818 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6819 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6820 | TEST_ASSERT(num_ops == 0); |
| 6821 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6822 | /* The value of *signature_length is unspecified on error, but |
| 6823 | * whatever it is, it should be less than signature_size, so that |
| 6824 | * if the caller tries to read *signature_length bytes without |
| 6825 | * checking the error code then they don't overflow a buffer. */ |
| 6826 | TEST_LE_U(signature_length, signature_size); |
| 6827 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6828 | TEST_LE_U(min_completes, num_completes); |
| 6829 | TEST_LE_U(num_completes, max_completes); |
| 6830 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6831 | exit: |
| 6832 | psa_reset_key_attributes(&attributes); |
| 6833 | psa_destroy_key(key); |
| 6834 | mbedtls_free(signature); |
| 6835 | PSA_DONE(); |
| 6836 | } |
| 6837 | /* END_CASE */ |
| 6838 | |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6839 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6840 | void sign_verify_hash(int key_type_arg, data_t *key_data, |
| 6841 | int alg_arg, data_t *input_data) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6842 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6843 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6844 | psa_key_type_t key_type = key_type_arg; |
| 6845 | psa_algorithm_t alg = alg_arg; |
| 6846 | size_t key_bits; |
| 6847 | unsigned char *signature = NULL; |
| 6848 | size_t signature_size; |
| 6849 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6850 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6851 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6852 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6853 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6854 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 6855 | psa_set_key_algorithm(&attributes, alg); |
| 6856 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6857 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6858 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6859 | &key)); |
| 6860 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6861 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6862 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6863 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6864 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6865 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6866 | key_bits, alg); |
| 6867 | TEST_ASSERT(signature_size != 0); |
| 6868 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6869 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6870 | |
| 6871 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6872 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6873 | input_data->x, input_data->len, |
| 6874 | signature, signature_size, |
| 6875 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6876 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6877 | TEST_LE_U(signature_length, signature_size); |
| 6878 | TEST_ASSERT(signature_length > 0); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6879 | |
| 6880 | /* Use the library to verify that the signature is correct. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6881 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 6882 | input_data->x, input_data->len, |
| 6883 | signature, signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6884 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6885 | if (input_data->len != 0) { |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6886 | /* Flip a bit in the input and verify that the signature is now |
| 6887 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6888 | * because ECDSA may ignore the last few bits of the input. */ |
| 6889 | input_data->x[0] ^= 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6890 | TEST_EQUAL(psa_verify_hash(key, alg, |
| 6891 | input_data->x, input_data->len, |
| 6892 | signature, signature_length), |
| 6893 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6894 | } |
| 6895 | |
| 6896 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6897 | /* |
| 6898 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6899 | * thus reset them as required. |
| 6900 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6901 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6902 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6903 | psa_destroy_key(key); |
| 6904 | mbedtls_free(signature); |
| 6905 | PSA_DONE(); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6906 | } |
| 6907 | /* END_CASE */ |
| 6908 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6909 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6910 | /** |
| 6911 | * sign_verify_hash_interruptible() test intentions: |
| 6912 | * |
| 6913 | * Note: This test can currently only handle ECDSA. |
| 6914 | * |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 6915 | * 1. Test that we can sign an input hash with the given keypair and then |
| 6916 | * afterwards verify that signature. This is currently the only way to test |
| 6917 | * non deterministic ECDSA, but this test can also handle deterministic. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6918 | * |
| 6919 | * 2. Test that after corrupting the hash, the verification detects an invalid |
| 6920 | * signature. |
| 6921 | * |
| 6922 | * 3. Test the number of calls to psa_sign_hash_complete() required are as |
| 6923 | * expected for different max_ops values. |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6924 | * |
| 6925 | * 4. Test that the number of ops done prior to starting signing and after abort |
| 6926 | * is zero and that each successful signing stage completes some ops (this is |
| 6927 | * not mandated by the PSA specification, but is currently the case). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6928 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6929 | 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] | 6930 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6931 | int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6932 | { |
| 6933 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6934 | psa_key_type_t key_type = key_type_arg; |
| 6935 | psa_algorithm_t alg = alg_arg; |
| 6936 | size_t key_bits; |
| 6937 | unsigned char *signature = NULL; |
| 6938 | size_t signature_size; |
| 6939 | size_t signature_length = 0xdeadbeef; |
| 6940 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6941 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6942 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6943 | uint32_t num_ops = 0; |
| 6944 | uint32_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6945 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6946 | size_t min_completes = 0; |
| 6947 | size_t max_completes = 0; |
| 6948 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6949 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 6950 | psa_sign_hash_interruptible_operation_init(); |
| 6951 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 6952 | psa_verify_hash_interruptible_operation_init(); |
| 6953 | |
| 6954 | PSA_ASSERT(psa_crypto_init()); |
| 6955 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6956 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 6957 | PSA_KEY_USAGE_VERIFY_HASH); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6958 | psa_set_key_algorithm(&attributes, alg); |
| 6959 | psa_set_key_type(&attributes, key_type); |
| 6960 | |
| 6961 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6962 | &key)); |
| 6963 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6964 | key_bits = psa_get_key_bits(&attributes); |
| 6965 | |
| 6966 | /* Allocate a buffer which has the size advertised by the |
| 6967 | * library. */ |
| 6968 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6969 | key_bits, alg); |
| 6970 | TEST_ASSERT(signature_size != 0); |
| 6971 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6972 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6973 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6974 | psa_interruptible_set_max_ops(max_ops); |
| 6975 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6976 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6977 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6978 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6979 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); |
| 6980 | TEST_ASSERT(num_ops_prior == 0); |
| 6981 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6982 | /* Start performing the signature. */ |
| 6983 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 6984 | input_data->x, input_data->len)); |
| 6985 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6986 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); |
| 6987 | TEST_ASSERT(num_ops_prior == 0); |
| 6988 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6989 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6990 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6991 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6992 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 6993 | signature_size, |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6994 | &signature_length); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6995 | |
| 6996 | num_completes++; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6997 | |
| 6998 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 6999 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7000 | /* We are asserting here that every complete makes progress |
| 7001 | * (completes some ops), which is true of the internal |
| 7002 | * implementation and probably any implementation, however this is |
| 7003 | * not mandated by the PSA specification. */ |
| 7004 | TEST_ASSERT(num_ops > num_ops_prior); |
| 7005 | |
| 7006 | num_ops_prior = num_ops; |
| 7007 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7008 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7009 | |
| 7010 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7011 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7012 | TEST_LE_U(min_completes, num_completes); |
| 7013 | TEST_LE_U(num_completes, max_completes); |
| 7014 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7015 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7016 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7017 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7018 | TEST_ASSERT(num_ops == 0); |
| 7019 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7020 | /* Check that the signature length looks sensible. */ |
| 7021 | TEST_LE_U(signature_length, signature_size); |
| 7022 | TEST_ASSERT(signature_length > 0); |
| 7023 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7024 | num_completes = 0; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7025 | |
| 7026 | /* Start verification. */ |
| 7027 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7028 | input_data->x, input_data->len, |
| 7029 | signature, signature_length)); |
| 7030 | |
| 7031 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7032 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7033 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7034 | |
| 7035 | num_completes++; |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7036 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7037 | |
| 7038 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7039 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7040 | TEST_LE_U(min_completes, num_completes); |
| 7041 | TEST_LE_U(num_completes, max_completes); |
| 7042 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7043 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7044 | |
| 7045 | verify_operation = psa_verify_hash_interruptible_operation_init(); |
| 7046 | |
| 7047 | if (input_data->len != 0) { |
| 7048 | /* Flip a bit in the input and verify that the signature is now |
| 7049 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 7050 | * because ECDSA may ignore the last few bits of the input. */ |
| 7051 | input_data->x[0] ^= 1; |
| 7052 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7053 | /* Start verification. */ |
| 7054 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7055 | input_data->x, input_data->len, |
| 7056 | signature, signature_length)); |
| 7057 | |
| 7058 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7059 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7060 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7061 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7062 | |
| 7063 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7064 | } |
| 7065 | |
| 7066 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7067 | |
| 7068 | exit: |
| 7069 | /* |
| 7070 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7071 | * thus reset them as required. |
| 7072 | */ |
| 7073 | psa_reset_key_attributes(&attributes); |
| 7074 | |
| 7075 | psa_destroy_key(key); |
| 7076 | mbedtls_free(signature); |
| 7077 | PSA_DONE(); |
| 7078 | } |
| 7079 | /* END_CASE */ |
| 7080 | |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7081 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7082 | void verify_hash(int key_type_arg, data_t *key_data, |
| 7083 | int alg_arg, data_t *hash_data, |
| 7084 | data_t *signature_data) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7085 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7086 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7087 | psa_key_type_t key_type = key_type_arg; |
| 7088 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7089 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7090 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7091 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 7092 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7093 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7094 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7095 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7096 | psa_set_key_algorithm(&attributes, alg); |
| 7097 | psa_set_key_type(&attributes, key_type); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7098 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7099 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7100 | &key)); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7101 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7102 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 7103 | hash_data->x, hash_data->len, |
| 7104 | signature_data->x, signature_data->len)); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 7105 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7106 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7107 | psa_reset_key_attributes(&attributes); |
| 7108 | psa_destroy_key(key); |
| 7109 | PSA_DONE(); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7110 | } |
| 7111 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7112 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7113 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7114 | /** |
| 7115 | * verify_hash_interruptible() test intentions: |
| 7116 | * |
| 7117 | * Note: This test can currently only handle ECDSA. |
| 7118 | * |
| 7119 | * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 7120 | * only). Given this test only does verification it can accept public keys as |
| 7121 | * well as private keys / keypairs. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7122 | * |
| 7123 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7124 | * expected for different max_ops values. |
| 7125 | * |
| 7126 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7127 | * and that each successful stage completes some ops (this is not mandated by |
| 7128 | * the PSA specification, but is currently the case). |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7129 | * |
| 7130 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 7131 | * complete() calls does not alter the number of ops returned. |
| 7132 | * |
| 7133 | * 5. Test that after corrupting the hash, the verification detects an invalid |
| 7134 | * signature. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7135 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7136 | void verify_hash_interruptible(int key_type_arg, data_t *key_data, |
| 7137 | int alg_arg, data_t *hash_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7138 | data_t *signature_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7139 | { |
| 7140 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7141 | psa_key_type_t key_type = key_type_arg; |
| 7142 | psa_algorithm_t alg = alg_arg; |
| 7143 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7144 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7145 | uint32_t num_ops = 0; |
| 7146 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7147 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7148 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7149 | size_t min_completes = 0; |
| 7150 | size_t max_completes = 0; |
| 7151 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7152 | psa_verify_hash_interruptible_operation_t operation = |
| 7153 | psa_verify_hash_interruptible_operation_init(); |
| 7154 | |
| 7155 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 7156 | |
| 7157 | PSA_ASSERT(psa_crypto_init()); |
| 7158 | |
| 7159 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7160 | psa_set_key_algorithm(&attributes, alg); |
| 7161 | psa_set_key_type(&attributes, key_type); |
| 7162 | |
| 7163 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7164 | &key)); |
| 7165 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7166 | psa_interruptible_set_max_ops(max_ops); |
| 7167 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7168 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 7169 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7170 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7171 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7172 | |
| 7173 | TEST_ASSERT(num_ops_prior == 0); |
| 7174 | |
| 7175 | /* Start verification. */ |
| 7176 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7177 | hash_data->x, hash_data->len, |
| 7178 | signature_data->x, signature_data->len) |
| 7179 | ); |
| 7180 | |
| 7181 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7182 | |
| 7183 | TEST_ASSERT(num_ops_prior == 0); |
| 7184 | |
| 7185 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7186 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7187 | status = psa_verify_hash_complete(&operation); |
| 7188 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7189 | num_completes++; |
| 7190 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7191 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 7192 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7193 | /* We are asserting here that every complete makes progress |
| 7194 | * (completes some ops), which is true of the internal |
| 7195 | * implementation and probably any implementation, however this is |
| 7196 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7197 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7198 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7199 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 7200 | |
| 7201 | /* Ensure calling get_num_ops() twice still returns the same |
| 7202 | * number of ops as previously reported. */ |
| 7203 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7204 | |
| 7205 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7206 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7207 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7208 | |
| 7209 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7210 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7211 | TEST_LE_U(min_completes, num_completes); |
| 7212 | TEST_LE_U(num_completes, max_completes); |
| 7213 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7214 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7215 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7216 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7217 | TEST_ASSERT(num_ops == 0); |
| 7218 | |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7219 | if (hash_data->len != 0) { |
| 7220 | /* Flip a bit in the hash and verify that the signature is now detected |
| 7221 | * as invalid. Flip a bit at the beginning, not at the end, because |
| 7222 | * ECDSA may ignore the last few bits of the input. */ |
| 7223 | hash_data->x[0] ^= 1; |
| 7224 | |
| 7225 | /* Start verification. */ |
| 7226 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7227 | hash_data->x, hash_data->len, |
| 7228 | signature_data->x, signature_data->len)); |
| 7229 | |
| 7230 | /* Continue performing the signature until complete. */ |
| 7231 | do { |
| 7232 | status = psa_verify_hash_complete(&operation); |
| 7233 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7234 | |
| 7235 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7236 | } |
| 7237 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7238 | exit: |
| 7239 | psa_reset_key_attributes(&attributes); |
| 7240 | psa_destroy_key(key); |
| 7241 | PSA_DONE(); |
| 7242 | } |
| 7243 | /* END_CASE */ |
| 7244 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7245 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7246 | void verify_hash_fail(int key_type_arg, data_t *key_data, |
| 7247 | int alg_arg, data_t *hash_data, |
| 7248 | data_t *signature_data, |
| 7249 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7250 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7251 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7252 | psa_key_type_t key_type = key_type_arg; |
| 7253 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7254 | psa_status_t actual_status; |
| 7255 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7256 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
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 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7259 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7260 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7261 | psa_set_key_algorithm(&attributes, alg); |
| 7262 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7263 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7264 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7265 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7266 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7267 | actual_status = psa_verify_hash(key, alg, |
| 7268 | hash_data->x, hash_data->len, |
| 7269 | signature_data->x, signature_data->len); |
| 7270 | TEST_EQUAL(actual_status, expected_status); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7271 | |
| 7272 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7273 | psa_reset_key_attributes(&attributes); |
| 7274 | psa_destroy_key(key); |
| 7275 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7276 | } |
| 7277 | /* END_CASE */ |
| 7278 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7279 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7280 | /** |
| 7281 | * verify_hash_fail_interruptible() test intentions: |
| 7282 | * |
| 7283 | * Note: This test can currently only handle ECDSA. |
| 7284 | * |
| 7285 | * 1. Test that various failure cases for interruptible verify hash fail with |
| 7286 | * the correct error codes, and at the correct point (at start or during |
| 7287 | * complete). |
| 7288 | * |
| 7289 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7290 | * expected for different max_ops values. |
| 7291 | * |
| 7292 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7293 | * and that each successful stage completes some ops (this is not mandated by |
| 7294 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7295 | * |
| 7296 | * 4. Check that calling complete() when start() fails and complete() |
| 7297 | * after completion results in a BAD_STATE error. |
| 7298 | * |
| 7299 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 7300 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7301 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7302 | void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 7303 | int alg_arg, data_t *hash_data, |
| 7304 | data_t *signature_data, |
| 7305 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7306 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7307 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7308 | { |
| 7309 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7310 | psa_key_type_t key_type = key_type_arg; |
| 7311 | psa_algorithm_t alg = alg_arg; |
| 7312 | psa_status_t actual_status; |
| 7313 | psa_status_t expected_start_status = expected_start_status_arg; |
| 7314 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 7315 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7316 | uint32_t num_ops = 0; |
| 7317 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7318 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7319 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7320 | size_t min_completes = 0; |
| 7321 | size_t max_completes = 0; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7322 | psa_verify_hash_interruptible_operation_t operation = |
| 7323 | psa_verify_hash_interruptible_operation_init(); |
| 7324 | |
| 7325 | PSA_ASSERT(psa_crypto_init()); |
| 7326 | |
| 7327 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7328 | psa_set_key_algorithm(&attributes, alg); |
| 7329 | psa_set_key_type(&attributes, key_type); |
| 7330 | |
| 7331 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7332 | &key)); |
| 7333 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7334 | psa_interruptible_set_max_ops(max_ops); |
| 7335 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7336 | interruptible_signverify_get_minmax_completes(max_ops, |
| 7337 | expected_complete_status, |
| 7338 | &min_completes, |
| 7339 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7340 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7341 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7342 | TEST_ASSERT(num_ops_prior == 0); |
| 7343 | |
| 7344 | /* Start verification. */ |
| 7345 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7346 | hash_data->x, hash_data->len, |
| 7347 | signature_data->x, |
| 7348 | signature_data->len); |
| 7349 | |
| 7350 | TEST_EQUAL(actual_status, expected_start_status); |
| 7351 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7352 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 7353 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7354 | * start failed. */ |
| 7355 | actual_status = psa_verify_hash_complete(&operation); |
| 7356 | |
| 7357 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7358 | |
| 7359 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7360 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7361 | hash_data->x, hash_data->len, |
| 7362 | signature_data->x, |
| 7363 | signature_data->len); |
| 7364 | |
| 7365 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7366 | } |
| 7367 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7368 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7369 | TEST_ASSERT(num_ops_prior == 0); |
| 7370 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7371 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7372 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7373 | actual_status = psa_verify_hash_complete(&operation); |
| 7374 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7375 | num_completes++; |
| 7376 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7377 | if (actual_status == PSA_SUCCESS || |
| 7378 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7379 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7380 | /* We are asserting here that every complete makes progress |
| 7381 | * (completes some ops), which is true of the internal |
| 7382 | * implementation and probably any implementation, however this is |
| 7383 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7384 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7385 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7386 | num_ops_prior = num_ops; |
| 7387 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7388 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7389 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7390 | TEST_EQUAL(actual_status, expected_complete_status); |
| 7391 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 7392 | /* Check that another complete returns BAD_STATE. */ |
| 7393 | actual_status = psa_verify_hash_complete(&operation); |
| 7394 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7395 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7396 | TEST_LE_U(min_completes, num_completes); |
| 7397 | TEST_LE_U(num_completes, max_completes); |
| 7398 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7399 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7400 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7401 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7402 | TEST_ASSERT(num_ops == 0); |
| 7403 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7404 | exit: |
| 7405 | psa_reset_key_attributes(&attributes); |
| 7406 | psa_destroy_key(key); |
| 7407 | PSA_DONE(); |
| 7408 | } |
| 7409 | /* END_CASE */ |
| 7410 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7411 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7412 | /** |
| 7413 | * interruptible_signverify_hash_state_test() test intentions: |
| 7414 | * |
| 7415 | * Note: This test can currently only handle ECDSA. |
| 7416 | * |
| 7417 | * 1. Test that calling the various interruptible sign and verify hash functions |
| 7418 | * in incorrect orders returns BAD_STATE errors. |
| 7419 | */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7420 | void interruptible_signverify_hash_state_test(int key_type_arg, |
| 7421 | data_t *key_data, int alg_arg, data_t *input_data) |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7422 | { |
| 7423 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7424 | psa_key_type_t key_type = key_type_arg; |
| 7425 | psa_algorithm_t alg = alg_arg; |
| 7426 | size_t key_bits; |
| 7427 | unsigned char *signature = NULL; |
| 7428 | size_t signature_size; |
| 7429 | size_t signature_length = 0xdeadbeef; |
| 7430 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7431 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7432 | psa_sign_hash_interruptible_operation_init(); |
| 7433 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7434 | psa_verify_hash_interruptible_operation_init(); |
| 7435 | |
| 7436 | PSA_ASSERT(psa_crypto_init()); |
| 7437 | |
| 7438 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7439 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7440 | psa_set_key_algorithm(&attributes, alg); |
| 7441 | psa_set_key_type(&attributes, key_type); |
| 7442 | |
| 7443 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7444 | &key)); |
| 7445 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7446 | key_bits = psa_get_key_bits(&attributes); |
| 7447 | |
| 7448 | /* Allocate a buffer which has the size advertised by the |
| 7449 | * library. */ |
| 7450 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7451 | key_bits, alg); |
| 7452 | TEST_ASSERT(signature_size != 0); |
| 7453 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7454 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7455 | |
| 7456 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7457 | |
| 7458 | /* --- Attempt completes prior to starts --- */ |
| 7459 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7460 | signature_size, |
| 7461 | &signature_length), |
| 7462 | PSA_ERROR_BAD_STATE); |
| 7463 | |
| 7464 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7465 | |
| 7466 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7467 | PSA_ERROR_BAD_STATE); |
| 7468 | |
| 7469 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7470 | |
| 7471 | /* --- Aborts in all other places. --- */ |
| 7472 | psa_sign_hash_abort(&sign_operation); |
| 7473 | |
| 7474 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7475 | input_data->x, input_data->len)); |
| 7476 | |
| 7477 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7478 | |
| 7479 | psa_interruptible_set_max_ops(1); |
| 7480 | |
| 7481 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7482 | input_data->x, input_data->len)); |
| 7483 | |
| 7484 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7485 | signature_size, |
| 7486 | &signature_length), |
| 7487 | PSA_OPERATION_INCOMPLETE); |
| 7488 | |
| 7489 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7490 | |
| 7491 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7492 | |
| 7493 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7494 | input_data->x, input_data->len)); |
| 7495 | |
| 7496 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7497 | signature_size, |
| 7498 | &signature_length)); |
| 7499 | |
| 7500 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7501 | |
| 7502 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7503 | |
| 7504 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7505 | input_data->x, input_data->len, |
| 7506 | signature, signature_length)); |
| 7507 | |
| 7508 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7509 | |
| 7510 | psa_interruptible_set_max_ops(1); |
| 7511 | |
| 7512 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7513 | input_data->x, input_data->len, |
| 7514 | signature, signature_length)); |
| 7515 | |
| 7516 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7517 | PSA_OPERATION_INCOMPLETE); |
| 7518 | |
| 7519 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7520 | |
| 7521 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7522 | |
| 7523 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7524 | input_data->x, input_data->len, |
| 7525 | signature, signature_length)); |
| 7526 | |
| 7527 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7528 | |
| 7529 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7530 | |
| 7531 | /* --- Attempt double starts. --- */ |
| 7532 | |
| 7533 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7534 | input_data->x, input_data->len)); |
| 7535 | |
| 7536 | TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg, |
| 7537 | input_data->x, input_data->len), |
| 7538 | PSA_ERROR_BAD_STATE); |
| 7539 | |
| 7540 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7541 | |
| 7542 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7543 | input_data->x, input_data->len, |
| 7544 | signature, signature_length)); |
| 7545 | |
| 7546 | TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg, |
| 7547 | input_data->x, input_data->len, |
| 7548 | signature, signature_length), |
| 7549 | PSA_ERROR_BAD_STATE); |
| 7550 | |
| 7551 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7552 | |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7553 | exit: |
| 7554 | /* |
| 7555 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7556 | * thus reset them as required. |
| 7557 | */ |
| 7558 | psa_reset_key_attributes(&attributes); |
| 7559 | |
| 7560 | psa_destroy_key(key); |
| 7561 | mbedtls_free(signature); |
| 7562 | PSA_DONE(); |
| 7563 | } |
| 7564 | /* END_CASE */ |
| 7565 | |
| 7566 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7567 | /** |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7568 | * interruptible_signverify_hash_edgecase_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7569 | * |
| 7570 | * Note: This test can currently only handle ECDSA. |
| 7571 | * |
| 7572 | * 1. Test various edge cases in the interruptible sign and verify hash |
| 7573 | * interfaces. |
| 7574 | */ |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7575 | void interruptible_signverify_hash_edgecase_tests(int key_type_arg, |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7576 | data_t *key_data, int alg_arg, data_t *input_data) |
| 7577 | { |
| 7578 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7579 | psa_key_type_t key_type = key_type_arg; |
| 7580 | psa_algorithm_t alg = alg_arg; |
| 7581 | size_t key_bits; |
| 7582 | unsigned char *signature = NULL; |
| 7583 | size_t signature_size; |
| 7584 | size_t signature_length = 0xdeadbeef; |
| 7585 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7586 | uint8_t *input_buffer = NULL; |
| 7587 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7588 | psa_sign_hash_interruptible_operation_init(); |
| 7589 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7590 | psa_verify_hash_interruptible_operation_init(); |
| 7591 | |
| 7592 | PSA_ASSERT(psa_crypto_init()); |
| 7593 | |
| 7594 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7595 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7596 | psa_set_key_algorithm(&attributes, alg); |
| 7597 | psa_set_key_type(&attributes, key_type); |
| 7598 | |
| 7599 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7600 | &key)); |
| 7601 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7602 | key_bits = psa_get_key_bits(&attributes); |
| 7603 | |
| 7604 | /* Allocate a buffer which has the size advertised by the |
| 7605 | * library. */ |
| 7606 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7607 | key_bits, alg); |
| 7608 | TEST_ASSERT(signature_size != 0); |
| 7609 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7610 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7611 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7612 | /* --- Change function inputs mid run, to cause an error (sign only, |
| 7613 | * verify passes all inputs to start. --- */ |
| 7614 | |
| 7615 | psa_interruptible_set_max_ops(1); |
| 7616 | |
| 7617 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7618 | input_data->x, input_data->len)); |
| 7619 | |
| 7620 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7621 | signature_size, |
| 7622 | &signature_length), |
| 7623 | PSA_OPERATION_INCOMPLETE); |
| 7624 | |
| 7625 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7626 | 0, |
| 7627 | &signature_length), |
| 7628 | PSA_ERROR_BUFFER_TOO_SMALL); |
| 7629 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7630 | /* And test that this invalidates the operation. */ |
| 7631 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7632 | 0, |
| 7633 | &signature_length), |
| 7634 | PSA_ERROR_BAD_STATE); |
| 7635 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7636 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7637 | |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7638 | /* Trash the hash buffer in between start and complete, to ensure |
| 7639 | * no reliance on external buffers. */ |
| 7640 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7641 | |
| 7642 | input_buffer = mbedtls_calloc(1, input_data->len); |
| 7643 | TEST_ASSERT(input_buffer != NULL); |
| 7644 | |
| 7645 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7646 | |
| 7647 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7648 | input_buffer, input_data->len)); |
| 7649 | |
| 7650 | memset(input_buffer, '!', input_data->len); |
| 7651 | mbedtls_free(input_buffer); |
| 7652 | input_buffer = NULL; |
| 7653 | |
| 7654 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7655 | signature_size, |
| 7656 | &signature_length)); |
| 7657 | |
| 7658 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7659 | |
| 7660 | input_buffer = mbedtls_calloc(1, input_data->len); |
| 7661 | TEST_ASSERT(input_buffer != NULL); |
| 7662 | |
| 7663 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7664 | |
| 7665 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7666 | input_buffer, input_data->len, |
| 7667 | signature, signature_length)); |
| 7668 | |
| 7669 | memset(input_buffer, '!', input_data->len); |
| 7670 | mbedtls_free(input_buffer); |
| 7671 | input_buffer = NULL; |
| 7672 | |
| 7673 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7674 | |
| 7675 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7676 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7677 | exit: |
| 7678 | /* |
| 7679 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7680 | * thus reset them as required. |
| 7681 | */ |
| 7682 | psa_reset_key_attributes(&attributes); |
| 7683 | |
| 7684 | psa_destroy_key(key); |
| 7685 | mbedtls_free(signature); |
| 7686 | PSA_DONE(); |
| 7687 | } |
| 7688 | /* END_CASE */ |
| 7689 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7690 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7691 | /** |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 7692 | * interruptible_signverify_hash_ops_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7693 | * |
| 7694 | * Note: This test can currently only handle ECDSA. |
| 7695 | * |
| 7696 | * 1. Test that setting max ops is reflected in both interruptible sign and |
| 7697 | * verify hash |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7698 | * 2. Test that changing the value of max_ops to unlimited during an operation |
| 7699 | * causes that operation to complete in the next call. |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7700 | * |
| 7701 | * 3. Test that calling get_num_ops() between complete calls gives the same |
| 7702 | * 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] | 7703 | */ |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 7704 | void interruptible_signverify_hash_ops_tests(int key_type_arg, |
| 7705 | data_t *key_data, int alg_arg, |
| 7706 | data_t *input_data) |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7707 | { |
| 7708 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7709 | psa_key_type_t key_type = key_type_arg; |
| 7710 | psa_algorithm_t alg = alg_arg; |
| 7711 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7712 | size_t key_bits; |
| 7713 | unsigned char *signature = NULL; |
| 7714 | size_t signature_size; |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7715 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7716 | uint32_t num_ops = 0; |
| 7717 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 7718 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7719 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7720 | psa_sign_hash_interruptible_operation_init(); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7721 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7722 | psa_verify_hash_interruptible_operation_init(); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7723 | |
| 7724 | PSA_ASSERT(psa_crypto_init()); |
| 7725 | |
| 7726 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7727 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7728 | psa_set_key_algorithm(&attributes, alg); |
| 7729 | psa_set_key_type(&attributes, key_type); |
| 7730 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7731 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key)); |
| 7732 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7733 | key_bits = psa_get_key_bits(&attributes); |
| 7734 | |
| 7735 | /* Allocate a buffer which has the size advertised by the |
| 7736 | * library. */ |
| 7737 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7738 | |
| 7739 | TEST_ASSERT(signature_size != 0); |
| 7740 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7741 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7742 | |
| 7743 | /* Check that default max ops gets set if we don't set it. */ |
| 7744 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7745 | input_data->x, input_data->len)); |
| 7746 | |
| 7747 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 7748 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7749 | |
| 7750 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7751 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7752 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7753 | input_data->x, input_data->len, |
| 7754 | signature, signature_size)); |
| 7755 | |
| 7756 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 7757 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7758 | |
| 7759 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7760 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7761 | /* Check that max ops gets set properly. */ |
| 7762 | |
| 7763 | psa_interruptible_set_max_ops(0xbeef); |
| 7764 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7765 | TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7766 | |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7767 | /* --- Ensure changing the max ops mid operation works (operation should |
| 7768 | * complete successfully after setting max ops to unlimited --- */ |
| 7769 | psa_interruptible_set_max_ops(1); |
| 7770 | |
| 7771 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7772 | input_data->x, input_data->len)); |
| 7773 | |
| 7774 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7775 | signature_size, |
| 7776 | &signature_length), |
| 7777 | PSA_OPERATION_INCOMPLETE); |
| 7778 | |
| 7779 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7780 | |
| 7781 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7782 | signature_size, |
| 7783 | &signature_length)); |
| 7784 | |
| 7785 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7786 | |
| 7787 | psa_interruptible_set_max_ops(1); |
| 7788 | |
| 7789 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7790 | input_data->x, input_data->len, |
| 7791 | signature, signature_length)); |
| 7792 | |
| 7793 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7794 | PSA_OPERATION_INCOMPLETE); |
| 7795 | |
| 7796 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7797 | |
| 7798 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7799 | |
| 7800 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7801 | |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7802 | /* --- Test that not calling get_num_ops inbetween complete calls does not |
| 7803 | * result in lost ops. ---*/ |
| 7804 | |
| 7805 | psa_interruptible_set_max_ops(1); |
| 7806 | |
| 7807 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7808 | input_data->x, input_data->len)); |
| 7809 | |
| 7810 | /* Continue performing the signature until complete. */ |
| 7811 | do { |
| 7812 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7813 | signature_size, |
| 7814 | &signature_length); |
| 7815 | |
| 7816 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7817 | |
| 7818 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7819 | |
| 7820 | PSA_ASSERT(status); |
| 7821 | |
| 7822 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7823 | |
| 7824 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7825 | input_data->x, input_data->len)); |
| 7826 | |
| 7827 | /* Continue performing the signature until complete. */ |
| 7828 | do { |
| 7829 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7830 | signature_size, |
| 7831 | &signature_length); |
| 7832 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7833 | |
| 7834 | PSA_ASSERT(status); |
| 7835 | |
| 7836 | TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation)); |
| 7837 | |
| 7838 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7839 | |
| 7840 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7841 | input_data->x, input_data->len, |
| 7842 | signature, signature_length)); |
| 7843 | |
| 7844 | /* Continue performing the verification until complete. */ |
| 7845 | do { |
| 7846 | status = psa_verify_hash_complete(&verify_operation); |
| 7847 | |
| 7848 | num_ops = psa_verify_hash_get_num_ops(&verify_operation); |
| 7849 | |
| 7850 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7851 | |
| 7852 | PSA_ASSERT(status); |
| 7853 | |
| 7854 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7855 | |
| 7856 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7857 | input_data->x, input_data->len, |
| 7858 | signature, signature_length)); |
| 7859 | |
| 7860 | /* Continue performing the verification until complete. */ |
| 7861 | do { |
| 7862 | status = psa_verify_hash_complete(&verify_operation); |
| 7863 | |
| 7864 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7865 | |
| 7866 | PSA_ASSERT(status); |
| 7867 | |
| 7868 | TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation)); |
| 7869 | |
| 7870 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7871 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7872 | exit: |
| 7873 | /* |
| 7874 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7875 | * thus reset them as required. |
| 7876 | */ |
| 7877 | psa_reset_key_attributes(&attributes); |
| 7878 | |
| 7879 | psa_destroy_key(key); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7880 | mbedtls_free(signature); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7881 | PSA_DONE(); |
| 7882 | } |
| 7883 | /* END_CASE */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7884 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7885 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7886 | void sign_message_deterministic(int key_type_arg, |
| 7887 | data_t *key_data, |
| 7888 | int alg_arg, |
| 7889 | data_t *input_data, |
| 7890 | data_t *output_data) |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7891 | { |
| 7892 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7893 | psa_key_type_t key_type = key_type_arg; |
| 7894 | psa_algorithm_t alg = alg_arg; |
| 7895 | size_t key_bits; |
| 7896 | unsigned char *signature = NULL; |
| 7897 | size_t signature_size; |
| 7898 | size_t signature_length = 0xdeadbeef; |
| 7899 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7900 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7901 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7902 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7903 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 7904 | psa_set_key_algorithm(&attributes, alg); |
| 7905 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7906 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7907 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7908 | &key)); |
| 7909 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7910 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7911 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7912 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7913 | TEST_ASSERT(signature_size != 0); |
| 7914 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7915 | TEST_CALLOC(signature, signature_size); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7916 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7917 | PSA_ASSERT(psa_sign_message(key, alg, |
| 7918 | input_data->x, input_data->len, |
| 7919 | signature, signature_size, |
| 7920 | &signature_length)); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7921 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 7922 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 7923 | signature, signature_length); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7924 | |
| 7925 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7926 | psa_reset_key_attributes(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7927 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7928 | psa_destroy_key(key); |
| 7929 | mbedtls_free(signature); |
| 7930 | PSA_DONE(); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7931 | |
| 7932 | } |
| 7933 | /* END_CASE */ |
| 7934 | |
| 7935 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7936 | void sign_message_fail(int key_type_arg, |
| 7937 | data_t *key_data, |
| 7938 | int alg_arg, |
| 7939 | data_t *input_data, |
| 7940 | int signature_size_arg, |
| 7941 | int expected_status_arg) |
| 7942 | { |
| 7943 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7944 | psa_key_type_t key_type = key_type_arg; |
| 7945 | psa_algorithm_t alg = alg_arg; |
| 7946 | size_t signature_size = signature_size_arg; |
| 7947 | psa_status_t actual_status; |
| 7948 | psa_status_t expected_status = expected_status_arg; |
| 7949 | unsigned char *signature = NULL; |
| 7950 | size_t signature_length = 0xdeadbeef; |
| 7951 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7952 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7953 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7954 | |
| 7955 | PSA_ASSERT(psa_crypto_init()); |
| 7956 | |
| 7957 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 7958 | psa_set_key_algorithm(&attributes, alg); |
| 7959 | psa_set_key_type(&attributes, key_type); |
| 7960 | |
| 7961 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7962 | &key)); |
| 7963 | |
| 7964 | actual_status = psa_sign_message(key, alg, |
| 7965 | input_data->x, input_data->len, |
| 7966 | signature, signature_size, |
| 7967 | &signature_length); |
| 7968 | TEST_EQUAL(actual_status, expected_status); |
| 7969 | /* The value of *signature_length is unspecified on error, but |
| 7970 | * whatever it is, it should be less than signature_size, so that |
| 7971 | * if the caller tries to read *signature_length bytes without |
| 7972 | * checking the error code then they don't overflow a buffer. */ |
| 7973 | TEST_LE_U(signature_length, signature_size); |
| 7974 | |
| 7975 | exit: |
| 7976 | psa_reset_key_attributes(&attributes); |
| 7977 | psa_destroy_key(key); |
| 7978 | mbedtls_free(signature); |
| 7979 | PSA_DONE(); |
| 7980 | } |
| 7981 | /* END_CASE */ |
| 7982 | |
| 7983 | /* BEGIN_CASE */ |
| 7984 | void sign_verify_message(int key_type_arg, |
| 7985 | data_t *key_data, |
| 7986 | int alg_arg, |
| 7987 | data_t *input_data) |
| 7988 | { |
| 7989 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7990 | psa_key_type_t key_type = key_type_arg; |
| 7991 | psa_algorithm_t alg = alg_arg; |
| 7992 | size_t key_bits; |
| 7993 | unsigned char *signature = NULL; |
| 7994 | size_t signature_size; |
| 7995 | size_t signature_length = 0xdeadbeef; |
| 7996 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7997 | |
| 7998 | PSA_ASSERT(psa_crypto_init()); |
| 7999 | |
| 8000 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 8001 | PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8002 | psa_set_key_algorithm(&attributes, alg); |
| 8003 | psa_set_key_type(&attributes, key_type); |
| 8004 | |
| 8005 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8006 | &key)); |
| 8007 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8008 | key_bits = psa_get_key_bits(&attributes); |
| 8009 | |
| 8010 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8011 | TEST_ASSERT(signature_size != 0); |
| 8012 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8013 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8014 | |
| 8015 | PSA_ASSERT(psa_sign_message(key, alg, |
| 8016 | input_data->x, input_data->len, |
| 8017 | signature, signature_size, |
| 8018 | &signature_length)); |
| 8019 | TEST_LE_U(signature_length, signature_size); |
| 8020 | TEST_ASSERT(signature_length > 0); |
| 8021 | |
| 8022 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8023 | input_data->x, input_data->len, |
| 8024 | signature, signature_length)); |
| 8025 | |
| 8026 | if (input_data->len != 0) { |
| 8027 | /* Flip a bit in the input and verify that the signature is now |
| 8028 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 8029 | * because ECDSA may ignore the last few bits of the input. */ |
| 8030 | input_data->x[0] ^= 1; |
| 8031 | TEST_EQUAL(psa_verify_message(key, alg, |
| 8032 | input_data->x, input_data->len, |
| 8033 | signature, signature_length), |
| 8034 | PSA_ERROR_INVALID_SIGNATURE); |
| 8035 | } |
| 8036 | |
| 8037 | exit: |
| 8038 | psa_reset_key_attributes(&attributes); |
| 8039 | |
| 8040 | psa_destroy_key(key); |
| 8041 | mbedtls_free(signature); |
| 8042 | PSA_DONE(); |
| 8043 | } |
| 8044 | /* END_CASE */ |
| 8045 | |
| 8046 | /* BEGIN_CASE */ |
| 8047 | void verify_message(int key_type_arg, |
| 8048 | data_t *key_data, |
| 8049 | int alg_arg, |
| 8050 | data_t *input_data, |
| 8051 | data_t *signature_data) |
| 8052 | { |
| 8053 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8054 | psa_key_type_t key_type = key_type_arg; |
| 8055 | psa_algorithm_t alg = alg_arg; |
| 8056 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8057 | |
| 8058 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 8059 | |
| 8060 | PSA_ASSERT(psa_crypto_init()); |
| 8061 | |
| 8062 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8063 | psa_set_key_algorithm(&attributes, alg); |
| 8064 | psa_set_key_type(&attributes, key_type); |
| 8065 | |
| 8066 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8067 | &key)); |
| 8068 | |
| 8069 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8070 | input_data->x, input_data->len, |
| 8071 | signature_data->x, signature_data->len)); |
| 8072 | |
| 8073 | exit: |
| 8074 | psa_reset_key_attributes(&attributes); |
| 8075 | psa_destroy_key(key); |
| 8076 | PSA_DONE(); |
| 8077 | } |
| 8078 | /* END_CASE */ |
| 8079 | |
| 8080 | /* BEGIN_CASE */ |
| 8081 | void verify_message_fail(int key_type_arg, |
| 8082 | data_t *key_data, |
| 8083 | int alg_arg, |
| 8084 | data_t *hash_data, |
| 8085 | data_t *signature_data, |
| 8086 | int expected_status_arg) |
| 8087 | { |
| 8088 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8089 | psa_key_type_t key_type = key_type_arg; |
| 8090 | psa_algorithm_t alg = alg_arg; |
| 8091 | psa_status_t actual_status; |
| 8092 | psa_status_t expected_status = expected_status_arg; |
| 8093 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8094 | |
| 8095 | PSA_ASSERT(psa_crypto_init()); |
| 8096 | |
| 8097 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8098 | psa_set_key_algorithm(&attributes, alg); |
| 8099 | psa_set_key_type(&attributes, key_type); |
| 8100 | |
| 8101 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8102 | &key)); |
| 8103 | |
| 8104 | actual_status = psa_verify_message(key, alg, |
| 8105 | hash_data->x, hash_data->len, |
| 8106 | signature_data->x, |
| 8107 | signature_data->len); |
| 8108 | TEST_EQUAL(actual_status, expected_status); |
| 8109 | |
| 8110 | exit: |
| 8111 | psa_reset_key_attributes(&attributes); |
| 8112 | psa_destroy_key(key); |
| 8113 | PSA_DONE(); |
| 8114 | } |
| 8115 | /* END_CASE */ |
| 8116 | |
| 8117 | /* BEGIN_CASE */ |
| 8118 | void asymmetric_encrypt(int key_type_arg, |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8119 | data_t *key_data, |
| 8120 | int alg_arg, |
| 8121 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8122 | data_t *label, |
| 8123 | int expected_output_length_arg, |
| 8124 | int expected_status_arg) |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8125 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8126 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8127 | psa_key_type_t key_type = key_type_arg; |
| 8128 | psa_algorithm_t alg = alg_arg; |
| 8129 | size_t expected_output_length = expected_output_length_arg; |
| 8130 | size_t key_bits; |
| 8131 | unsigned char *output = NULL; |
| 8132 | size_t output_size; |
| 8133 | size_t output_length = ~0; |
| 8134 | psa_status_t actual_status; |
| 8135 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8136 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8137 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8138 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 8139 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8140 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8141 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 8142 | psa_set_key_algorithm(&attributes, alg); |
| 8143 | psa_set_key_type(&attributes, key_type); |
| 8144 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8145 | &key)); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8146 | |
| 8147 | /* Determine the maximum output length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8148 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8149 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8151 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8152 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8153 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8154 | |
| 8155 | /* Encrypt the input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8156 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8157 | input_data->x, input_data->len, |
| 8158 | label->x, label->len, |
| 8159 | output, output_size, |
| 8160 | &output_length); |
| 8161 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8162 | if (actual_status == PSA_SUCCESS) { |
| 8163 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8164 | } else { |
| 8165 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8166 | } |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8167 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8168 | /* If the label is empty, the test framework puts a non-null pointer |
| 8169 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8170 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8171 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8172 | if (output_size != 0) { |
| 8173 | memset(output, 0, output_size); |
| 8174 | } |
| 8175 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8176 | input_data->x, input_data->len, |
| 8177 | NULL, label->len, |
| 8178 | output, output_size, |
| 8179 | &output_length); |
| 8180 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8181 | if (actual_status == PSA_SUCCESS) { |
| 8182 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8183 | } else { |
| 8184 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8185 | } |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8186 | } |
| 8187 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8188 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8189 | /* |
| 8190 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8191 | * thus reset them as required. |
| 8192 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8193 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8194 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8195 | psa_destroy_key(key); |
| 8196 | mbedtls_free(output); |
| 8197 | PSA_DONE(); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8198 | } |
| 8199 | /* END_CASE */ |
| 8200 | |
| 8201 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8202 | void asymmetric_encrypt_decrypt(int key_type_arg, |
| 8203 | data_t *key_data, |
| 8204 | int alg_arg, |
| 8205 | data_t *input_data, |
| 8206 | data_t *label) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8207 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8208 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8209 | psa_key_type_t key_type = key_type_arg; |
| 8210 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8211 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8212 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8213 | size_t output_size; |
| 8214 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8215 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8216 | size_t output2_size; |
| 8217 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8218 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8219 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8220 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8221 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8222 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 8223 | psa_set_key_algorithm(&attributes, alg); |
| 8224 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8225 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8226 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8227 | &key)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8228 | |
| 8229 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8230 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8231 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8232 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8233 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8234 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8235 | TEST_CALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8236 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8237 | output2_size = input_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8238 | TEST_LE_U(output2_size, |
| 8239 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)); |
| 8240 | TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8241 | TEST_CALLOC(output2, output2_size); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8242 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 8243 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 8244 | * the original plaintext because of the non-optional random |
| 8245 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8246 | PSA_ASSERT(psa_asymmetric_encrypt(key, alg, |
| 8247 | input_data->x, input_data->len, |
| 8248 | label->x, label->len, |
| 8249 | output, output_size, |
| 8250 | &output_length)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8251 | /* We don't know what ciphertext length to expect, but check that |
| 8252 | * it looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8253 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8254 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8255 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8256 | output, output_length, |
| 8257 | label->x, label->len, |
| 8258 | output2, output2_size, |
| 8259 | &output2_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8260 | TEST_MEMORY_COMPARE(input_data->x, input_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8261 | output2, output2_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8262 | |
| 8263 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8264 | /* |
| 8265 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8266 | * thus reset them as required. |
| 8267 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8268 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8269 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8270 | psa_destroy_key(key); |
| 8271 | mbedtls_free(output); |
| 8272 | mbedtls_free(output2); |
| 8273 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8274 | } |
| 8275 | /* END_CASE */ |
| 8276 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8277 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8278 | void asymmetric_decrypt(int key_type_arg, |
| 8279 | data_t *key_data, |
| 8280 | int alg_arg, |
| 8281 | data_t *input_data, |
| 8282 | data_t *label, |
| 8283 | data_t *expected_data) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8284 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8285 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8286 | psa_key_type_t key_type = key_type_arg; |
| 8287 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8288 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8289 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 8290 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8291 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8292 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
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_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8295 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8296 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8297 | psa_set_key_algorithm(&attributes, alg); |
| 8298 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8299 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8300 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8301 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8302 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8303 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8304 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8305 | |
| 8306 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8307 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8308 | TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8309 | TEST_CALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8310 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8311 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8312 | input_data->x, input_data->len, |
| 8313 | label->x, label->len, |
| 8314 | output, |
| 8315 | output_size, |
| 8316 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8317 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8318 | output, output_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8319 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8320 | /* If the label is empty, the test framework puts a non-null pointer |
| 8321 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8322 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8323 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8324 | if (output_size != 0) { |
| 8325 | memset(output, 0, output_size); |
| 8326 | } |
| 8327 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8328 | input_data->x, input_data->len, |
| 8329 | NULL, label->len, |
| 8330 | output, |
| 8331 | output_size, |
| 8332 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8333 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8334 | output, output_length); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8335 | } |
| 8336 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8337 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8338 | psa_reset_key_attributes(&attributes); |
| 8339 | psa_destroy_key(key); |
| 8340 | mbedtls_free(output); |
| 8341 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8342 | } |
| 8343 | /* END_CASE */ |
| 8344 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8345 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8346 | void asymmetric_decrypt_fail(int key_type_arg, |
| 8347 | data_t *key_data, |
| 8348 | int alg_arg, |
| 8349 | data_t *input_data, |
| 8350 | data_t *label, |
| 8351 | int output_size_arg, |
| 8352 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8353 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8354 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8355 | psa_key_type_t key_type = key_type_arg; |
| 8356 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8357 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 8358 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8359 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8360 | psa_status_t actual_status; |
| 8361 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8362 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8363 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8364 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8365 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8366 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8367 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8368 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8369 | psa_set_key_algorithm(&attributes, alg); |
| 8370 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8371 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8372 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8373 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8374 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8375 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8376 | input_data->x, input_data->len, |
| 8377 | label->x, label->len, |
| 8378 | output, output_size, |
| 8379 | &output_length); |
| 8380 | TEST_EQUAL(actual_status, expected_status); |
| 8381 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8382 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8383 | /* If the label is empty, the test framework puts a non-null pointer |
| 8384 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8385 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8386 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8387 | if (output_size != 0) { |
| 8388 | memset(output, 0, output_size); |
| 8389 | } |
| 8390 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8391 | input_data->x, input_data->len, |
| 8392 | NULL, label->len, |
| 8393 | output, output_size, |
| 8394 | &output_length); |
| 8395 | TEST_EQUAL(actual_status, expected_status); |
| 8396 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8397 | } |
| 8398 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8399 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8400 | psa_reset_key_attributes(&attributes); |
| 8401 | psa_destroy_key(key); |
| 8402 | mbedtls_free(output); |
| 8403 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8404 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8405 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8406 | |
| 8407 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8408 | void key_derivation_init() |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8409 | { |
| 8410 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 8411 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 8412 | * 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] | 8413 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8414 | size_t capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8415 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init(); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 8416 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8417 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8418 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8419 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8420 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8421 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8422 | TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity), |
| 8423 | PSA_ERROR_BAD_STATE); |
| 8424 | TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity), |
| 8425 | PSA_ERROR_BAD_STATE); |
| 8426 | TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity), |
| 8427 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8428 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8429 | /* A default operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8430 | PSA_ASSERT(psa_key_derivation_abort(&func)); |
| 8431 | PSA_ASSERT(psa_key_derivation_abort(&init)); |
| 8432 | PSA_ASSERT(psa_key_derivation_abort(&zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8433 | } |
| 8434 | /* END_CASE */ |
| 8435 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 8436 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8437 | void derive_setup(int alg_arg, int expected_status_arg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8438 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8439 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8440 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8441 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8442 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8443 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8444 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8445 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8446 | expected_status); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8447 | |
| 8448 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8449 | psa_key_derivation_abort(&operation); |
| 8450 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8451 | } |
| 8452 | /* END_CASE */ |
| 8453 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8454 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8455 | void derive_set_capacity(int alg_arg, int capacity_arg, |
| 8456 | int expected_status_arg) |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8457 | { |
| 8458 | psa_algorithm_t alg = alg_arg; |
| 8459 | size_t capacity = capacity_arg; |
| 8460 | psa_status_t expected_status = expected_status_arg; |
| 8461 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8462 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8463 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8464 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8465 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8466 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8467 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 8468 | expected_status); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8469 | |
| 8470 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8471 | psa_key_derivation_abort(&operation); |
| 8472 | PSA_DONE(); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8473 | } |
| 8474 | /* END_CASE */ |
| 8475 | |
| 8476 | /* BEGIN_CASE */ |
Kusumit Ghoderao | d60dfc0 | 2023-05-01 17:39:27 +0530 | [diff] [blame] | 8477 | void parse_binary_string_test(data_t *input, int output) |
| 8478 | { |
| 8479 | uint64_t value; |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 8480 | value = mbedtls_test_parse_binary_string(input); |
Kusumit Ghoderao | d60dfc0 | 2023-05-01 17:39:27 +0530 | [diff] [blame] | 8481 | TEST_EQUAL(value, output); |
| 8482 | } |
| 8483 | /* END_CASE */ |
| 8484 | |
| 8485 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8486 | void derive_input(int alg_arg, |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8487 | int step_arg1, int key_type_arg1, data_t *input1, |
| 8488 | int expected_status_arg1, |
| 8489 | int step_arg2, int key_type_arg2, data_t *input2, |
| 8490 | int expected_status_arg2, |
| 8491 | int step_arg3, int key_type_arg3, data_t *input3, |
| 8492 | int expected_status_arg3, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8493 | int output_key_type_arg, int expected_output_status_arg) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8494 | { |
| 8495 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8496 | psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 }; |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8497 | uint32_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 }; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8498 | psa_status_t expected_statuses[] = { expected_status_arg1, |
| 8499 | expected_status_arg2, |
| 8500 | expected_status_arg3 }; |
| 8501 | data_t *inputs[] = { input1, input2, input3 }; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8502 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8503 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8504 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8505 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8506 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8507 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8508 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8509 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8510 | psa_status_t expected_output_status = expected_output_status_arg; |
| 8511 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8512 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8513 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8514 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8515 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8516 | psa_set_key_algorithm(&attributes, alg); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8517 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8518 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8519 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8520 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8521 | mbedtls_test_set_step(i); |
| 8522 | if (steps[i] == 0) { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 8523 | /* Skip this step */ |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8524 | } else if (((psa_key_type_t) key_types[i]) != PSA_KEY_TYPE_NONE && |
| 8525 | key_types[i] != INPUT_INTEGER) { |
| 8526 | psa_set_key_type(&attributes, ((psa_key_type_t) key_types[i])); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8527 | PSA_ASSERT(psa_import_key(&attributes, |
| 8528 | inputs[i]->x, inputs[i]->len, |
| 8529 | &keys[i])); |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8530 | if (PSA_KEY_TYPE_IS_KEY_PAIR((psa_key_type_t) key_types[i]) && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8531 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8532 | // When taking a private key as secret input, use key agreement |
| 8533 | // to add the shared secret to the derivation |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8534 | TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self( |
| 8535 | &operation, keys[i]), |
| 8536 | expected_statuses[i]); |
| 8537 | } else { |
| 8538 | TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i], |
| 8539 | keys[i]), |
| 8540 | expected_statuses[i]); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8541 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8542 | } else { |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8543 | if (key_types[i] == INPUT_INTEGER) { |
| 8544 | TEST_EQUAL(psa_key_derivation_input_integer( |
| 8545 | &operation, steps[i], |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 8546 | mbedtls_test_parse_binary_string(inputs[i])), |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8547 | expected_statuses[i]); |
| 8548 | } else { |
Kusumit Ghoderao | 02326d5 | 2023-04-06 17:47:59 +0530 | [diff] [blame] | 8549 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8550 | &operation, steps[i], |
| 8551 | inputs[i]->x, inputs[i]->len), |
| 8552 | expected_statuses[i]); |
Kusumit Ghoderao | 02326d5 | 2023-04-06 17:47:59 +0530 | [diff] [blame] | 8553 | } |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8554 | } |
| 8555 | } |
| 8556 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8557 | if (output_key_type != PSA_KEY_TYPE_NONE) { |
| 8558 | psa_reset_key_attributes(&attributes); |
| 8559 | psa_set_key_type(&attributes, output_key_type); |
| 8560 | psa_set_key_bits(&attributes, 8); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8561 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8562 | psa_key_derivation_output_key(&attributes, &operation, |
| 8563 | &output_key); |
| 8564 | } else { |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8565 | uint8_t buffer[1]; |
| 8566 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8567 | psa_key_derivation_output_bytes(&operation, |
| 8568 | buffer, sizeof(buffer)); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8569 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8570 | TEST_EQUAL(actual_output_status, expected_output_status); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8571 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8572 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8573 | psa_key_derivation_abort(&operation); |
| 8574 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8575 | psa_destroy_key(keys[i]); |
| 8576 | } |
| 8577 | psa_destroy_key(output_key); |
| 8578 | PSA_DONE(); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8579 | } |
| 8580 | /* END_CASE */ |
| 8581 | |
Kusumit Ghoderao | 42b02b9 | 2023-06-06 16:48:46 +0530 | [diff] [blame] | 8582 | /* BEGIN_CASE*/ |
| 8583 | void derive_input_invalid_cost(int alg_arg, int64_t cost) |
| 8584 | { |
| 8585 | psa_algorithm_t alg = alg_arg; |
| 8586 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8587 | |
| 8588 | PSA_ASSERT(psa_crypto_init()); |
| 8589 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8590 | |
| 8591 | TEST_EQUAL(psa_key_derivation_input_integer(&operation, |
| 8592 | PSA_KEY_DERIVATION_INPUT_COST, |
| 8593 | cost), |
| 8594 | PSA_ERROR_NOT_SUPPORTED); |
| 8595 | |
| 8596 | exit: |
| 8597 | psa_key_derivation_abort(&operation); |
| 8598 | PSA_DONE(); |
| 8599 | } |
| 8600 | /* END_CASE*/ |
| 8601 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8602 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8603 | void derive_over_capacity(int alg_arg) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8604 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8605 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8606 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 8607 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8608 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8609 | unsigned char input1[] = "Input 1"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8610 | size_t input1_length = sizeof(input1); |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8611 | unsigned char input2[] = "Input 2"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8612 | size_t input2_length = sizeof(input2); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8613 | uint8_t buffer[42]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8614 | size_t capacity = sizeof(buffer); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 8615 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 8616 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8617 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8618 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8619 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8620 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8621 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8622 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8623 | psa_set_key_algorithm(&attributes, alg); |
| 8624 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8625 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8626 | PSA_ASSERT(psa_import_key(&attributes, |
| 8627 | key_data, sizeof(key_data), |
| 8628 | &key)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8629 | |
| 8630 | /* valid key derivation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8631 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8632 | input1, input1_length, |
| 8633 | input2, input2_length, |
| 8634 | capacity)) { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8635 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8636 | } |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8637 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8638 | /* state of operation shouldn't allow additional generation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8639 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8640 | PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8642 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8643 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8644 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity), |
| 8645 | PSA_ERROR_INSUFFICIENT_DATA); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8646 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8647 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8648 | psa_key_derivation_abort(&operation); |
| 8649 | psa_destroy_key(key); |
| 8650 | PSA_DONE(); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8651 | } |
| 8652 | /* END_CASE */ |
| 8653 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8654 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8655 | void derive_actions_without_setup() |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8656 | { |
| 8657 | uint8_t output_buffer[16]; |
| 8658 | size_t buffer_size = 16; |
| 8659 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8660 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8661 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8662 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8663 | output_buffer, buffer_size) |
| 8664 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8665 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8666 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8667 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8668 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8669 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8670 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8671 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8672 | output_buffer, buffer_size) |
| 8673 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8674 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8675 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8676 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8677 | |
| 8678 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8679 | psa_key_derivation_abort(&operation); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8680 | } |
| 8681 | /* END_CASE */ |
| 8682 | |
| 8683 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8684 | void derive_output(int alg_arg, |
| 8685 | int step1_arg, data_t *input1, int expected_status_arg1, |
| 8686 | int step2_arg, data_t *input2, int expected_status_arg2, |
| 8687 | int step3_arg, data_t *input3, int expected_status_arg3, |
| 8688 | int step4_arg, data_t *input4, int expected_status_arg4, |
| 8689 | data_t *key_agreement_peer_key, |
| 8690 | int requested_capacity_arg, |
| 8691 | data_t *expected_output1, |
| 8692 | data_t *expected_output2, |
| 8693 | int other_key_input_type, |
| 8694 | int key_input_type, |
| 8695 | int derive_type) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8696 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8697 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8698 | psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg }; |
| 8699 | data_t *inputs[] = { input1, input2, input3, input4 }; |
| 8700 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8701 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8702 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8703 | MBEDTLS_SVC_KEY_ID_INIT }; |
| 8704 | psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2, |
| 8705 | expected_status_arg3, expected_status_arg4 }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8706 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8707 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8708 | uint8_t *expected_outputs[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8709 | { expected_output1->x, expected_output2->x }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8710 | size_t output_sizes[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8711 | { expected_output1->len, expected_output2->len }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8712 | size_t output_buffer_size = 0; |
| 8713 | uint8_t *output_buffer = NULL; |
| 8714 | size_t expected_capacity; |
| 8715 | size_t current_capacity; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8716 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; |
| 8717 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; |
| 8718 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; |
| 8719 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8720 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8721 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8722 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8723 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8724 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
| 8725 | if (output_sizes[i] > output_buffer_size) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8726 | output_buffer_size = output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8727 | } |
| 8728 | if (output_sizes[i] == 0) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8729 | expected_outputs[i] = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8730 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8731 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8732 | TEST_CALLOC(output_buffer, output_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8733 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8734 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8735 | /* Extraction phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8736 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8737 | PSA_ASSERT(psa_key_derivation_set_capacity(&operation, |
| 8738 | requested_capacity)); |
| 8739 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8740 | switch (steps[i]) { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8741 | case 0: |
| 8742 | break; |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 8743 | case PSA_KEY_DERIVATION_INPUT_COST: |
| 8744 | TEST_EQUAL(psa_key_derivation_input_integer( |
Kusumit Ghoderao | f28e0f5 | 2023-06-06 15:03:22 +0530 | [diff] [blame] | 8745 | &operation, steps[i], |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 8746 | mbedtls_test_parse_binary_string(inputs[i])), |
Kusumit Ghoderao | f28e0f5 | 2023-06-06 15:03:22 +0530 | [diff] [blame] | 8747 | statuses[i]); |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 8748 | if (statuses[i] != PSA_SUCCESS) { |
| 8749 | goto exit; |
| 8750 | } |
| 8751 | break; |
| 8752 | case PSA_KEY_DERIVATION_INPUT_PASSWORD: |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8753 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8754 | switch (key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8755 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8756 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8757 | &operation, steps[i], |
| 8758 | inputs[i]->x, inputs[i]->len), |
| 8759 | statuses[i]); |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8760 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8761 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8762 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8763 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8764 | break; |
| 8765 | case 1: // input key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8766 | psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE); |
| 8767 | psa_set_key_algorithm(&attributes1, alg); |
| 8768 | psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8769 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8770 | PSA_ASSERT(psa_import_key(&attributes1, |
| 8771 | inputs[i]->x, inputs[i]->len, |
| 8772 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8773 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8774 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 8775 | PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1)); |
| 8776 | TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)), |
| 8777 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8778 | } |
| 8779 | |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 8780 | TEST_EQUAL(psa_key_derivation_input_key(&operation, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8781 | steps[i], |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 8782 | keys[i]), |
| 8783 | statuses[i]); |
| 8784 | |
| 8785 | if (statuses[i] != PSA_SUCCESS) { |
| 8786 | goto exit; |
| 8787 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8788 | break; |
| 8789 | default: |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 8790 | TEST_FAIL("default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8791 | break; |
| 8792 | } |
| 8793 | break; |
| 8794 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8795 | switch (other_key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8796 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8797 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 8798 | steps[i], |
| 8799 | inputs[i]->x, |
| 8800 | inputs[i]->len), |
| 8801 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8802 | break; |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 8803 | case 1: // input key, type DERIVE |
| 8804 | case 11: // input key, type RAW |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8805 | psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE); |
| 8806 | psa_set_key_algorithm(&attributes2, alg); |
| 8807 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8808 | |
| 8809 | // other secret of type RAW_DATA passed with input_key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8810 | if (other_key_input_type == 11) { |
| 8811 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA); |
| 8812 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8813 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8814 | PSA_ASSERT(psa_import_key(&attributes2, |
| 8815 | inputs[i]->x, inputs[i]->len, |
| 8816 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8817 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8818 | TEST_EQUAL(psa_key_derivation_input_key(&operation, |
| 8819 | steps[i], |
| 8820 | keys[i]), |
| 8821 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8822 | break; |
| 8823 | case 2: // key agreement |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8824 | psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE); |
| 8825 | psa_set_key_algorithm(&attributes3, alg); |
| 8826 | psa_set_key_type(&attributes3, |
| 8827 | PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8828 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8829 | PSA_ASSERT(psa_import_key(&attributes3, |
| 8830 | inputs[i]->x, inputs[i]->len, |
| 8831 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8832 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8833 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 8834 | &operation, |
| 8835 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 8836 | keys[i], key_agreement_peer_key->x, |
| 8837 | key_agreement_peer_key->len), statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8838 | break; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8839 | default: |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 8840 | TEST_FAIL("default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8841 | break; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8842 | } |
| 8843 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8844 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8845 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8846 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8847 | break; |
| 8848 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8849 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8850 | &operation, steps[i], |
| 8851 | inputs[i]->x, inputs[i]->len), statuses[i]); |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 8852 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8853 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 8854 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8855 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8856 | break; |
| 8857 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 8858 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8859 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8860 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8861 | ¤t_capacity)); |
| 8862 | TEST_EQUAL(current_capacity, requested_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8863 | expected_capacity = requested_capacity; |
| 8864 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8865 | if (derive_type == 1) { // output key |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8866 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; |
| 8867 | |
| 8868 | /* For output key derivation secret must be provided using |
| 8869 | input key, otherwise operation is not permitted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8870 | if (key_input_type == 1) { |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8871 | expected_status = PSA_SUCCESS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8872 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8873 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8874 | psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT); |
| 8875 | psa_set_key_algorithm(&attributes4, alg); |
| 8876 | psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE); |
| 8877 | psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8878 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8879 | TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation, |
| 8880 | &derived_key), expected_status); |
| 8881 | } else { // output bytes |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8882 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8883 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8884 | /* Read some bytes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8885 | status = psa_key_derivation_output_bytes(&operation, |
| 8886 | output_buffer, output_sizes[i]); |
| 8887 | if (expected_capacity == 0 && output_sizes[i] == 0) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8888 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8889 | TEST_ASSERT(status == PSA_SUCCESS || |
| 8890 | status == PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8891 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8892 | } else if (expected_capacity == 0 || |
| 8893 | output_sizes[i] > expected_capacity) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8894 | /* Capacity exceeded. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8895 | TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8896 | expected_capacity = 0; |
| 8897 | continue; |
| 8898 | } |
| 8899 | /* Success. Check the read data. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8900 | PSA_ASSERT(status); |
| 8901 | if (output_sizes[i] != 0) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8902 | TEST_MEMORY_COMPARE(output_buffer, output_sizes[i], |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8903 | expected_outputs[i], output_sizes[i]); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8904 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8905 | /* Check the operation status. */ |
| 8906 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8907 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8908 | ¤t_capacity)); |
| 8909 | TEST_EQUAL(expected_capacity, current_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8910 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8911 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8912 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8913 | |
| 8914 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8915 | mbedtls_free(output_buffer); |
| 8916 | psa_key_derivation_abort(&operation); |
| 8917 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8918 | psa_destroy_key(keys[i]); |
| 8919 | } |
| 8920 | psa_destroy_key(derived_key); |
| 8921 | PSA_DONE(); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8922 | } |
| 8923 | /* END_CASE */ |
| 8924 | |
| 8925 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8926 | void derive_full(int alg_arg, |
| 8927 | data_t *key_data, |
| 8928 | data_t *input1, |
| 8929 | data_t *input2, |
| 8930 | int requested_capacity_arg) |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8931 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8932 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8933 | psa_algorithm_t alg = alg_arg; |
| 8934 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8935 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8936 | unsigned char output_buffer[16]; |
| 8937 | size_t expected_capacity = requested_capacity; |
| 8938 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8939 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8940 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8941 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8942 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8943 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8944 | psa_set_key_algorithm(&attributes, alg); |
| 8945 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8946 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8947 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8948 | &key)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8949 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8950 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8951 | input1->x, input1->len, |
| 8952 | input2->x, input2->len, |
| 8953 | requested_capacity)) { |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 8954 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8955 | } |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 8956 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8957 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8958 | ¤t_capacity)); |
| 8959 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8960 | |
| 8961 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8962 | while (current_capacity > 0) { |
| 8963 | size_t read_size = sizeof(output_buffer); |
| 8964 | if (read_size > current_capacity) { |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8965 | read_size = current_capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8966 | } |
| 8967 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8968 | output_buffer, |
| 8969 | read_size)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8970 | expected_capacity -= read_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8971 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8972 | ¤t_capacity)); |
| 8973 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8974 | } |
| 8975 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8976 | /* Check that the operation refuses to go over capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8977 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1), |
| 8978 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8979 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8980 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8981 | |
| 8982 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8983 | psa_key_derivation_abort(&operation); |
| 8984 | psa_destroy_key(key); |
| 8985 | PSA_DONE(); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8986 | } |
| 8987 | /* END_CASE */ |
| 8988 | |
Stephan Koch | 78109f5 | 2023-04-12 14:19:36 +0200 | [diff] [blame] | 8989 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8990 | void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg, |
| 8991 | int derivation_step, |
| 8992 | int capacity, int expected_capacity_status_arg, |
| 8993 | data_t *expected_output, |
| 8994 | int expected_output_status_arg) |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8995 | { |
| 8996 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 8997 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | d378504 | 2022-09-16 06:45:44 -0400 | [diff] [blame] | 8998 | 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] | 8999 | uint8_t *output_buffer = NULL; |
| 9000 | psa_status_t status; |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 9001 | psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg; |
| 9002 | psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg; |
| 9003 | 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] | 9004 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9005 | TEST_CALLOC(output_buffer, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9006 | PSA_ASSERT(psa_crypto_init()); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9007 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9008 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9009 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 9010 | expected_capacity_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9011 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9012 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 9013 | step, input->x, input->len), |
| 9014 | expected_input_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9015 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9016 | if (((psa_status_t) expected_input_status) != PSA_SUCCESS) { |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9017 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9018 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9019 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9020 | status = psa_key_derivation_output_bytes(&operation, output_buffer, |
| 9021 | expected_output->len); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9022 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9023 | TEST_EQUAL(status, expected_output_status); |
| 9024 | if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9025 | TEST_MEMORY_COMPARE(output_buffer, expected_output->len, expected_output->x, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9026 | expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9027 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9028 | |
| 9029 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9030 | mbedtls_free(output_buffer); |
| 9031 | psa_key_derivation_abort(&operation); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9032 | PSA_DONE(); |
| 9033 | } |
| 9034 | /* END_CASE */ |
| 9035 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9036 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9037 | void derive_key_exercise(int alg_arg, |
| 9038 | data_t *key_data, |
| 9039 | data_t *input1, |
| 9040 | data_t *input2, |
| 9041 | int derived_type_arg, |
| 9042 | int derived_bits_arg, |
| 9043 | int derived_usage_arg, |
| 9044 | int derived_alg_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9045 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9046 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9047 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9048 | psa_algorithm_t alg = alg_arg; |
| 9049 | psa_key_type_t derived_type = derived_type_arg; |
| 9050 | size_t derived_bits = derived_bits_arg; |
| 9051 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 9052 | psa_algorithm_t derived_alg = derived_alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9053 | size_t capacity = PSA_BITS_TO_BYTES(derived_bits); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9054 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9055 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 9056 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9057 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9058 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9059 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9060 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9061 | psa_set_key_algorithm(&attributes, alg); |
| 9062 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
| 9063 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 9064 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9065 | |
| 9066 | /* Derive a key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9067 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9068 | input1->x, input1->len, |
| 9069 | input2->x, input2->len, |
| 9070 | capacity)) { |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9071 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9072 | } |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9073 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9074 | psa_set_key_usage_flags(&attributes, derived_usage); |
| 9075 | psa_set_key_algorithm(&attributes, derived_alg); |
| 9076 | psa_set_key_type(&attributes, derived_type); |
| 9077 | psa_set_key_bits(&attributes, derived_bits); |
| 9078 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation, |
| 9079 | &derived_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9080 | |
| 9081 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9082 | PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes)); |
| 9083 | TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type); |
| 9084 | TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9085 | |
| 9086 | /* Exercise the derived key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9087 | if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) { |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9088 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9089 | } |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9090 | |
| 9091 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9092 | /* |
| 9093 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9094 | * thus reset them as required. |
| 9095 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9096 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9097 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9098 | psa_key_derivation_abort(&operation); |
| 9099 | psa_destroy_key(base_key); |
| 9100 | psa_destroy_key(derived_key); |
| 9101 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9102 | } |
| 9103 | /* END_CASE */ |
| 9104 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9105 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9106 | void derive_key_export(int alg_arg, |
| 9107 | data_t *key_data, |
| 9108 | data_t *input1, |
| 9109 | data_t *input2, |
| 9110 | int bytes1_arg, |
| 9111 | int bytes2_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9112 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9113 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9114 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9115 | psa_algorithm_t alg = alg_arg; |
| 9116 | size_t bytes1 = bytes1_arg; |
| 9117 | size_t bytes2 = bytes2_arg; |
| 9118 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9119 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9120 | uint8_t *output_buffer = NULL; |
| 9121 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9122 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9123 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9124 | size_t length; |
| 9125 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9126 | TEST_CALLOC(output_buffer, capacity); |
| 9127 | TEST_CALLOC(export_buffer, capacity); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9128 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9129 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9130 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9131 | psa_set_key_algorithm(&base_attributes, alg); |
| 9132 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9133 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9134 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9135 | |
| 9136 | /* Derive some material and output it. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9137 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9138 | input1->x, input1->len, |
| 9139 | input2->x, input2->len, |
| 9140 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9141 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9142 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9143 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9144 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9145 | output_buffer, |
| 9146 | capacity)); |
| 9147 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9148 | |
| 9149 | /* 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] | 9150 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9151 | input1->x, input1->len, |
| 9152 | input2->x, input2->len, |
| 9153 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9154 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9155 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9156 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9157 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9158 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9159 | psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA); |
| 9160 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1)); |
| 9161 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9162 | &derived_key)); |
| 9163 | PSA_ASSERT(psa_export_key(derived_key, |
| 9164 | export_buffer, bytes1, |
| 9165 | &length)); |
| 9166 | TEST_EQUAL(length, bytes1); |
| 9167 | PSA_ASSERT(psa_destroy_key(derived_key)); |
| 9168 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2)); |
| 9169 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9170 | &derived_key)); |
| 9171 | PSA_ASSERT(psa_export_key(derived_key, |
| 9172 | export_buffer + bytes1, bytes2, |
| 9173 | &length)); |
| 9174 | TEST_EQUAL(length, bytes2); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9175 | |
| 9176 | /* Compare the outputs from the two runs. */ |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9177 | TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9178 | export_buffer, capacity); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9179 | |
| 9180 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9181 | mbedtls_free(output_buffer); |
| 9182 | mbedtls_free(export_buffer); |
| 9183 | psa_key_derivation_abort(&operation); |
| 9184 | psa_destroy_key(base_key); |
| 9185 | psa_destroy_key(derived_key); |
| 9186 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9187 | } |
| 9188 | /* END_CASE */ |
| 9189 | |
| 9190 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9191 | void derive_key_type(int alg_arg, |
| 9192 | data_t *key_data, |
| 9193 | data_t *input1, |
| 9194 | data_t *input2, |
| 9195 | int key_type_arg, int bits_arg, |
| 9196 | data_t *expected_export) |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9197 | { |
| 9198 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9199 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9200 | const psa_algorithm_t alg = alg_arg; |
| 9201 | const psa_key_type_t key_type = key_type_arg; |
| 9202 | const size_t bits = bits_arg; |
| 9203 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9204 | const size_t export_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9205 | PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9206 | uint8_t *export_buffer = NULL; |
| 9207 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9208 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9209 | size_t export_length; |
| 9210 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9211 | TEST_CALLOC(export_buffer, export_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9212 | PSA_ASSERT(psa_crypto_init()); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9213 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9214 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9215 | psa_set_key_algorithm(&base_attributes, alg); |
| 9216 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9217 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9218 | &base_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9219 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9220 | if (mbedtls_test_psa_setup_key_derivation_wrap( |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9221 | &operation, base_key, alg, |
| 9222 | input1->x, input1->len, |
| 9223 | input2->x, input2->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9224 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) { |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9225 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9226 | } |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9227 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9228 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9229 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9230 | psa_set_key_type(&derived_attributes, key_type); |
| 9231 | psa_set_key_bits(&derived_attributes, bits); |
| 9232 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9233 | &derived_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9234 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9235 | PSA_ASSERT(psa_export_key(derived_key, |
| 9236 | export_buffer, export_buffer_size, |
| 9237 | &export_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9238 | TEST_MEMORY_COMPARE(export_buffer, export_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9239 | expected_export->x, expected_export->len); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9240 | |
| 9241 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9242 | mbedtls_free(export_buffer); |
| 9243 | psa_key_derivation_abort(&operation); |
| 9244 | psa_destroy_key(base_key); |
| 9245 | psa_destroy_key(derived_key); |
| 9246 | PSA_DONE(); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9247 | } |
| 9248 | /* END_CASE */ |
| 9249 | |
| 9250 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9251 | void derive_key(int alg_arg, |
| 9252 | data_t *key_data, data_t *input1, data_t *input2, |
| 9253 | int type_arg, int bits_arg, |
| 9254 | int expected_status_arg, |
| 9255 | int is_large_output) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9256 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9257 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9258 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9259 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 9260 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9261 | size_t bits = bits_arg; |
| 9262 | psa_status_t expected_status = expected_status_arg; |
| 9263 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9264 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9265 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9266 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9267 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9268 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9269 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9270 | psa_set_key_algorithm(&base_attributes, alg); |
| 9271 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9272 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9273 | &base_key)); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9274 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9275 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9276 | input1->x, input1->len, |
| 9277 | input2->x, input2->len, |
| 9278 | SIZE_MAX)) { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9279 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9280 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9282 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9283 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9284 | psa_set_key_type(&derived_attributes, type); |
| 9285 | psa_set_key_bits(&derived_attributes, bits); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9286 | |
| 9287 | psa_status_t status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9288 | psa_key_derivation_output_key(&derived_attributes, |
| 9289 | &operation, |
| 9290 | &derived_key); |
| 9291 | if (is_large_output > 0) { |
| 9292 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9293 | } |
| 9294 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9295 | |
| 9296 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9297 | psa_key_derivation_abort(&operation); |
| 9298 | psa_destroy_key(base_key); |
| 9299 | psa_destroy_key(derived_key); |
| 9300 | PSA_DONE(); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9301 | } |
| 9302 | /* END_CASE */ |
| 9303 | |
| 9304 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9305 | void key_agreement_setup(int alg_arg, |
| 9306 | int our_key_type_arg, int our_key_alg_arg, |
| 9307 | data_t *our_key_data, data_t *peer_key_data, |
| 9308 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9309 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9310 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9311 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 9312 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9313 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9314 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9315 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9316 | psa_status_t expected_status = expected_status_arg; |
| 9317 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9318 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9319 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9320 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9321 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9322 | psa_set_key_algorithm(&attributes, our_key_alg); |
| 9323 | psa_set_key_type(&attributes, our_key_type); |
| 9324 | PSA_ASSERT(psa_import_key(&attributes, |
| 9325 | our_key_data->x, our_key_data->len, |
| 9326 | &our_key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9327 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9328 | /* The tests currently include inputs that should fail at either step. |
| 9329 | * Test cases that fail at the setup step should be changed to call |
| 9330 | * key_derivation_setup instead, and this function should be renamed |
| 9331 | * to key_agreement_fail. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9332 | status = psa_key_derivation_setup(&operation, alg); |
| 9333 | if (status == PSA_SUCCESS) { |
| 9334 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 9335 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 9336 | our_key, |
| 9337 | peer_key_data->x, peer_key_data->len), |
| 9338 | expected_status); |
| 9339 | } else { |
| 9340 | TEST_ASSERT(status == expected_status); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9341 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9342 | |
| 9343 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9344 | psa_key_derivation_abort(&operation); |
| 9345 | psa_destroy_key(our_key); |
| 9346 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9347 | } |
| 9348 | /* END_CASE */ |
| 9349 | |
| 9350 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9351 | void raw_key_agreement(int alg_arg, |
| 9352 | int our_key_type_arg, data_t *our_key_data, |
| 9353 | data_t *peer_key_data, |
| 9354 | data_t *expected_output) |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9355 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9356 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9357 | psa_algorithm_t alg = alg_arg; |
| 9358 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9359 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9360 | unsigned char *output = NULL; |
| 9361 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9362 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9363 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9364 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9365 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9366 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9367 | psa_set_key_algorithm(&attributes, alg); |
| 9368 | psa_set_key_type(&attributes, our_key_type); |
| 9369 | PSA_ASSERT(psa_import_key(&attributes, |
| 9370 | our_key_data->x, our_key_data->len, |
| 9371 | &our_key)); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9372 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9373 | PSA_ASSERT(psa_get_key_attributes(our_key, &attributes)); |
| 9374 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9375 | |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9376 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9377 | TEST_LE_U(expected_output->len, |
| 9378 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits)); |
| 9379 | TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits), |
| 9380 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9381 | |
| 9382 | /* Good case with exact output size */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9383 | TEST_CALLOC(output, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9384 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9385 | peer_key_data->x, peer_key_data->len, |
| 9386 | output, expected_output->len, |
| 9387 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9388 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9389 | expected_output->x, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9390 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9391 | output = NULL; |
| 9392 | output_length = ~0; |
| 9393 | |
| 9394 | /* Larger buffer */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9395 | TEST_CALLOC(output, expected_output->len + 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9396 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9397 | peer_key_data->x, peer_key_data->len, |
| 9398 | output, expected_output->len + 1, |
| 9399 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9400 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9401 | expected_output->x, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9402 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9403 | output = NULL; |
| 9404 | output_length = ~0; |
| 9405 | |
| 9406 | /* Buffer too small */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9407 | TEST_CALLOC(output, expected_output->len - 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9408 | TEST_EQUAL(psa_raw_key_agreement(alg, our_key, |
| 9409 | peer_key_data->x, peer_key_data->len, |
| 9410 | output, expected_output->len - 1, |
| 9411 | &output_length), |
| 9412 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9413 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9414 | TEST_LE_U(output_length, expected_output->len - 1); |
| 9415 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9416 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9417 | |
| 9418 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9419 | mbedtls_free(output); |
| 9420 | psa_destroy_key(our_key); |
| 9421 | PSA_DONE(); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9422 | } |
| 9423 | /* END_CASE */ |
| 9424 | |
| 9425 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9426 | void key_agreement_capacity(int alg_arg, |
| 9427 | int our_key_type_arg, data_t *our_key_data, |
| 9428 | data_t *peer_key_data, |
| 9429 | int expected_capacity_arg) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9430 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9431 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9432 | psa_algorithm_t alg = alg_arg; |
| 9433 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9434 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9435 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9436 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9437 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9438 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9439 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9440 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9441 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9442 | psa_set_key_algorithm(&attributes, alg); |
| 9443 | psa_set_key_type(&attributes, our_key_type); |
| 9444 | PSA_ASSERT(psa_import_key(&attributes, |
| 9445 | our_key_data->x, our_key_data->len, |
| 9446 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9447 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9448 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9449 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9450 | &operation, |
| 9451 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9452 | peer_key_data->x, peer_key_data->len)); |
| 9453 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9454 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9455 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9456 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9457 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9458 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9459 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 9460 | /* Test the advertised capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9461 | PSA_ASSERT(psa_key_derivation_get_capacity( |
| 9462 | &operation, &actual_capacity)); |
| 9463 | TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9464 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9465 | /* Test the actual capacity by reading the output. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9466 | while (actual_capacity > sizeof(output)) { |
| 9467 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9468 | output, sizeof(output))); |
| 9469 | actual_capacity -= sizeof(output); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9470 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9471 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9472 | output, actual_capacity)); |
| 9473 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1), |
| 9474 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9475 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9476 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9477 | psa_key_derivation_abort(&operation); |
| 9478 | psa_destroy_key(our_key); |
| 9479 | PSA_DONE(); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9480 | } |
| 9481 | /* END_CASE */ |
| 9482 | |
| 9483 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9484 | void key_agreement_output(int alg_arg, |
| 9485 | int our_key_type_arg, data_t *our_key_data, |
| 9486 | data_t *peer_key_data, |
| 9487 | data_t *expected_output1, data_t *expected_output2) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9488 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9489 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9490 | psa_algorithm_t alg = alg_arg; |
| 9491 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9492 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9493 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9494 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9495 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9496 | TEST_CALLOC(actual_output, MAX(expected_output1->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9497 | expected_output2->len)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9498 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9499 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9500 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9501 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9502 | psa_set_key_algorithm(&attributes, alg); |
| 9503 | psa_set_key_type(&attributes, our_key_type); |
| 9504 | PSA_ASSERT(psa_import_key(&attributes, |
| 9505 | our_key_data->x, our_key_data->len, |
| 9506 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9507 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9508 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9509 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9510 | &operation, |
| 9511 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9512 | peer_key_data->x, peer_key_data->len)); |
| 9513 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9514 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9515 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9516 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9517 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9518 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9519 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9520 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9521 | actual_output, |
| 9522 | expected_output1->len)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9523 | TEST_MEMORY_COMPARE(actual_output, expected_output1->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9524 | expected_output1->x, expected_output1->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9525 | if (expected_output2->len != 0) { |
| 9526 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9527 | actual_output, |
| 9528 | expected_output2->len)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9529 | TEST_MEMORY_COMPARE(actual_output, expected_output2->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9530 | expected_output2->x, expected_output2->len); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9531 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9532 | |
| 9533 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9534 | psa_key_derivation_abort(&operation); |
| 9535 | psa_destroy_key(our_key); |
| 9536 | PSA_DONE(); |
| 9537 | mbedtls_free(actual_output); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9538 | } |
| 9539 | /* END_CASE */ |
| 9540 | |
| 9541 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9542 | void generate_random(int bytes_arg) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9543 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9544 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9545 | unsigned char *output = NULL; |
| 9546 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9547 | size_t i; |
| 9548 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9549 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9550 | TEST_ASSERT(bytes_arg >= 0); |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 9551 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9552 | TEST_CALLOC(output, bytes); |
| 9553 | TEST_CALLOC(changed, bytes); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9554 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9555 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9556 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9557 | /* Run several times, to ensure that every output byte will be |
| 9558 | * nonzero at least once with overwhelming probability |
| 9559 | * (2^(-8*number_of_runs)). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9560 | for (run = 0; run < 10; run++) { |
| 9561 | if (bytes != 0) { |
| 9562 | memset(output, 0, bytes); |
| 9563 | } |
| 9564 | PSA_ASSERT(psa_generate_random(output, bytes)); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9565 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9566 | for (i = 0; i < bytes; i++) { |
| 9567 | if (output[i] != 0) { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9568 | ++changed[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9569 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9570 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9571 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9572 | |
| 9573 | /* Check that every byte was changed to nonzero at least once. This |
| 9574 | * validates that psa_generate_random is overwriting every byte of |
| 9575 | * the output buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9576 | for (i = 0; i < bytes; i++) { |
| 9577 | TEST_ASSERT(changed[i] != 0); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9578 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9579 | |
| 9580 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9581 | PSA_DONE(); |
| 9582 | mbedtls_free(output); |
| 9583 | mbedtls_free(changed); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9584 | } |
| 9585 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9586 | |
| 9587 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9588 | void generate_key(int type_arg, |
| 9589 | int bits_arg, |
| 9590 | int usage_arg, |
| 9591 | int alg_arg, |
| 9592 | int expected_status_arg, |
| 9593 | int is_large_key) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9594 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9595 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9596 | psa_key_type_t type = type_arg; |
| 9597 | psa_key_usage_t usage = usage_arg; |
| 9598 | size_t bits = bits_arg; |
| 9599 | psa_algorithm_t alg = alg_arg; |
| 9600 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9601 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 9602 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9603 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9604 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9605 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9606 | psa_set_key_usage_flags(&attributes, usage); |
| 9607 | psa_set_key_algorithm(&attributes, alg); |
| 9608 | psa_set_key_type(&attributes, type); |
| 9609 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9610 | |
| 9611 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9612 | psa_status_t status = psa_generate_key(&attributes, &key); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9613 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9614 | if (is_large_key > 0) { |
| 9615 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9616 | } |
| 9617 | TEST_EQUAL(status, expected_status); |
| 9618 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9619 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9620 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9621 | |
| 9622 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9623 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 9624 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 9625 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9626 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 9627 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9628 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 9629 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9630 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9631 | |
| 9632 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9633 | /* |
| 9634 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9635 | * thus reset them as required. |
| 9636 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9637 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9638 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9639 | psa_destroy_key(key); |
| 9640 | PSA_DONE(); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9641 | } |
| 9642 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 9643 | |
Valerio Setti | 19fec54 | 2023-07-25 12:31:50 +0200 | [diff] [blame] | 9644 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE:PSA_WANT_ALG_RSA_PKCS1V15_CRYPT:PSA_WANT_ALG_RSA_PKCS1V15_SIGN */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9645 | void generate_key_rsa(int bits_arg, |
| 9646 | data_t *e_arg, |
| 9647 | int expected_status_arg) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9648 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9649 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 9650 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9651 | size_t bits = bits_arg; |
| 9652 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 9653 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 9654 | psa_status_t expected_status = expected_status_arg; |
| 9655 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9656 | uint8_t *exported = NULL; |
| 9657 | size_t exported_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9658 | PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9659 | size_t exported_length = SIZE_MAX; |
| 9660 | uint8_t *e_read_buffer = NULL; |
| 9661 | int is_default_public_exponent = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9662 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9663 | size_t e_read_length = SIZE_MAX; |
| 9664 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9665 | if (e_arg->len == 0 || |
| 9666 | (e_arg->len == 3 && |
| 9667 | 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] | 9668 | is_default_public_exponent = 1; |
| 9669 | e_read_size = 0; |
| 9670 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9671 | TEST_CALLOC(e_read_buffer, e_read_size); |
| 9672 | TEST_CALLOC(exported, exported_size); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9673 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9674 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9676 | psa_set_key_usage_flags(&attributes, usage); |
| 9677 | psa_set_key_algorithm(&attributes, alg); |
| 9678 | PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type, |
| 9679 | e_arg->x, e_arg->len)); |
| 9680 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9681 | |
| 9682 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9683 | TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status); |
| 9684 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9685 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9686 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9687 | |
| 9688 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9689 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 9690 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 9691 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 9692 | PSA_ASSERT(psa_get_key_domain_parameters(&attributes, |
| 9693 | e_read_buffer, e_read_size, |
| 9694 | &e_read_length)); |
| 9695 | if (is_default_public_exponent) { |
| 9696 | TEST_EQUAL(e_read_length, 0); |
| 9697 | } else { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9698 | TEST_MEMORY_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9699 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9700 | |
| 9701 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9702 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9703 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9704 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9705 | |
| 9706 | /* Export the key and check the public exponent. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9707 | PSA_ASSERT(psa_export_public_key(key, |
| 9708 | exported, exported_size, |
| 9709 | &exported_length)); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9710 | { |
| 9711 | uint8_t *p = exported; |
| 9712 | uint8_t *end = exported + exported_length; |
| 9713 | size_t len; |
| 9714 | /* RSAPublicKey ::= SEQUENCE { |
| 9715 | * modulus INTEGER, -- n |
| 9716 | * publicExponent INTEGER } -- e |
| 9717 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9718 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 9719 | MBEDTLS_ASN1_SEQUENCE | |
| 9720 | MBEDTLS_ASN1_CONSTRUCTED)); |
| 9721 | TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1)); |
| 9722 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 9723 | MBEDTLS_ASN1_INTEGER)); |
| 9724 | if (len >= 1 && p[0] == 0) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9725 | ++p; |
| 9726 | --len; |
| 9727 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9728 | if (e_arg->len == 0) { |
| 9729 | TEST_EQUAL(len, 3); |
| 9730 | TEST_EQUAL(p[0], 1); |
| 9731 | TEST_EQUAL(p[1], 0); |
| 9732 | TEST_EQUAL(p[2], 1); |
| 9733 | } else { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9734 | TEST_MEMORY_COMPARE(p, len, e_arg->x, e_arg->len); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9735 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9736 | } |
| 9737 | |
| 9738 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9739 | /* |
| 9740 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 9741 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 9742 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9743 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9744 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9745 | psa_destroy_key(key); |
| 9746 | PSA_DONE(); |
| 9747 | mbedtls_free(e_read_buffer); |
| 9748 | mbedtls_free(exported); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9749 | } |
| 9750 | /* END_CASE */ |
| 9751 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9752 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9753 | void persistent_key_load_key_from_storage(data_t *data, |
| 9754 | int type_arg, int bits_arg, |
| 9755 | int usage_flags_arg, int alg_arg, |
| 9756 | int generation_method) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9757 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9758 | 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] | 9759 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9760 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9761 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9762 | psa_key_type_t type = type_arg; |
| 9763 | size_t bits = bits_arg; |
| 9764 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 9765 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9766 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9767 | unsigned char *first_export = NULL; |
| 9768 | unsigned char *second_export = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9769 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits); |
Sergey | bef1f63 | 2023-03-06 15:25:06 -0700 | [diff] [blame] | 9770 | size_t first_exported_length = 0; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9771 | size_t second_exported_length; |
| 9772 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9773 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9774 | TEST_CALLOC(first_export, export_size); |
| 9775 | TEST_CALLOC(second_export, export_size); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9776 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9777 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9778 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9779 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9780 | psa_set_key_id(&attributes, key_id); |
| 9781 | psa_set_key_usage_flags(&attributes, usage_flags); |
| 9782 | psa_set_key_algorithm(&attributes, alg); |
| 9783 | psa_set_key_type(&attributes, type); |
| 9784 | psa_set_key_bits(&attributes, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9785 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9786 | switch (generation_method) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9787 | case IMPORT_KEY: |
| 9788 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9789 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, |
| 9790 | &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9791 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9792 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9793 | case GENERATE_KEY: |
| 9794 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9795 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9796 | break; |
| 9797 | |
| 9798 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 9799 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9800 | { |
| 9801 | /* Create base key */ |
| 9802 | psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256); |
| 9803 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9804 | psa_set_key_usage_flags(&base_attributes, |
| 9805 | PSA_KEY_USAGE_DERIVE); |
| 9806 | psa_set_key_algorithm(&base_attributes, derive_alg); |
| 9807 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9808 | PSA_ASSERT(psa_import_key(&base_attributes, |
| 9809 | data->x, data->len, |
| 9810 | &base_key)); |
| 9811 | /* Derive a key. */ |
| 9812 | PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg)); |
| 9813 | PSA_ASSERT(psa_key_derivation_input_key( |
| 9814 | &operation, |
| 9815 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key)); |
| 9816 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 9817 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
| 9818 | NULL, 0)); |
| 9819 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, |
| 9820 | &operation, |
| 9821 | &key)); |
| 9822 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
| 9823 | PSA_ASSERT(psa_destroy_key(base_key)); |
| 9824 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9825 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9826 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9827 | TEST_ASSUME(!"KDF not supported in this configuration"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9828 | #endif |
| 9829 | break; |
| 9830 | |
| 9831 | default: |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 9832 | TEST_FAIL("generation_method not implemented in test"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9833 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9834 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9835 | psa_reset_key_attributes(&attributes); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9836 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9837 | /* Export the key if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9838 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9839 | PSA_ASSERT(psa_export_key(key, |
| 9840 | first_export, export_size, |
| 9841 | &first_exported_length)); |
| 9842 | if (generation_method == IMPORT_KEY) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9843 | TEST_MEMORY_COMPARE(data->x, data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9844 | first_export, first_exported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9845 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9846 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9847 | |
| 9848 | /* Shutdown and restart */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9849 | PSA_ASSERT(psa_purge_key(key)); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 9850 | PSA_DONE(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9851 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9852 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9853 | /* Check key slot still contains key data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9854 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 9855 | TEST_ASSERT(mbedtls_svc_key_id_equal( |
| 9856 | psa_get_key_id(&attributes), key_id)); |
| 9857 | TEST_EQUAL(psa_get_key_lifetime(&attributes), |
| 9858 | PSA_KEY_LIFETIME_PERSISTENT); |
| 9859 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 9860 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 9861 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 9862 | mbedtls_test_update_key_usage_flags(usage_flags)); |
| 9863 | TEST_EQUAL(psa_get_key_algorithm(&attributes), alg); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9864 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9865 | /* Export the key again if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9866 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9867 | PSA_ASSERT(psa_export_key(key, |
| 9868 | second_export, export_size, |
| 9869 | &second_exported_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9870 | TEST_MEMORY_COMPARE(first_export, first_exported_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9871 | second_export, second_exported_length); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9872 | } |
| 9873 | |
| 9874 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9875 | if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9876 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9877 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9878 | |
| 9879 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9880 | /* |
| 9881 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9882 | * thus reset them as required. |
| 9883 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9884 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9885 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9886 | mbedtls_free(first_export); |
| 9887 | mbedtls_free(second_export); |
| 9888 | psa_key_derivation_abort(&operation); |
| 9889 | psa_destroy_key(base_key); |
| 9890 | psa_destroy_key(key); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 9891 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9892 | } |
| 9893 | /* END_CASE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9894 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 9895 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9896 | void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg, |
| 9897 | int primitive_arg, int hash_arg, int role_arg, |
| 9898 | int test_input, data_t *pw_data, |
| 9899 | int inj_err_type_arg, |
| 9900 | int expected_error_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9901 | { |
| 9902 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 9903 | psa_pake_operation_t operation = psa_pake_operation_init(); |
| 9904 | psa_algorithm_t alg = alg_arg; |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9905 | psa_pake_primitive_t primitive = primitive_arg; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 9906 | psa_key_type_t key_type_pw = key_type_pw_arg; |
| 9907 | psa_key_usage_t key_usage_pw = key_usage_pw_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9908 | psa_algorithm_t hash_alg = hash_arg; |
| 9909 | psa_pake_role_t role = role_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9910 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9911 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9912 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
| 9913 | psa_status_t expected_error = expected_error_arg; |
| 9914 | psa_status_t status; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9915 | unsigned char *output_buffer = NULL; |
| 9916 | size_t output_len = 0; |
| 9917 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9918 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9919 | |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9920 | size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9921 | PSA_PAKE_STEP_KEY_SHARE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9922 | TEST_CALLOC(output_buffer, buf_size); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9923 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9924 | if (pw_data->len > 0) { |
| 9925 | psa_set_key_usage_flags(&attributes, key_usage_pw); |
| 9926 | psa_set_key_algorithm(&attributes, alg); |
| 9927 | psa_set_key_type(&attributes, key_type_pw); |
| 9928 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 9929 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9930 | } |
| 9931 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9932 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 9933 | psa_pake_cs_set_primitive(&cipher_suite, primitive); |
| 9934 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9935 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9936 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 9937 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9938 | if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) { |
| 9939 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 9940 | expected_error); |
| 9941 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9942 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 9943 | expected_error); |
| 9944 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9945 | TEST_EQUAL(psa_pake_set_password_key(&operation, key), |
| 9946 | expected_error); |
| 9947 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9948 | TEST_EQUAL(psa_pake_set_role(&operation, role), |
| 9949 | expected_error); |
| 9950 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9951 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 9952 | NULL, 0, NULL), |
| 9953 | expected_error); |
| 9954 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9955 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0), |
| 9956 | expected_error); |
| 9957 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9958 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9959 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9960 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9961 | status = psa_pake_setup(&operation, &cipher_suite); |
| 9962 | if (status != PSA_SUCCESS) { |
| 9963 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9964 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9965 | } |
| 9966 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9967 | if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) { |
| 9968 | TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite), |
| 9969 | expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9970 | goto exit; |
| 9971 | } |
| 9972 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9973 | status = psa_pake_set_role(&operation, role); |
| 9974 | if (status != PSA_SUCCESS) { |
| 9975 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9976 | goto exit; |
| 9977 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9978 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9979 | if (pw_data->len > 0) { |
| 9980 | status = psa_pake_set_password_key(&operation, key); |
| 9981 | if (status != PSA_SUCCESS) { |
| 9982 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9983 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9984 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9985 | } |
| 9986 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9987 | if (inj_err_type == INJECT_ERR_INVALID_USER) { |
| 9988 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 9989 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9990 | goto exit; |
| 9991 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9992 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9993 | if (inj_err_type == INJECT_ERR_INVALID_PEER) { |
| 9994 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 9995 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9996 | goto exit; |
| 9997 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9998 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9999 | if (inj_err_type == INJECT_ERR_SET_USER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10000 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10001 | TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4), |
| 10002 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10003 | goto exit; |
| 10004 | } |
| 10005 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10006 | if (inj_err_type == INJECT_ERR_SET_PEER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10007 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10008 | TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4), |
| 10009 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10010 | goto exit; |
| 10011 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 10012 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10013 | const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10014 | PSA_PAKE_STEP_KEY_SHARE); |
| 10015 | const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10016 | PSA_PAKE_STEP_ZK_PUBLIC); |
| 10017 | const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10018 | PSA_PAKE_STEP_ZK_PROOF); |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 10019 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10020 | if (test_input) { |
| 10021 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 10022 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0), |
| 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_UNKNOWN_STEP) { |
| 10028 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 10029 | output_buffer, size_zk_proof), |
| 10030 | PSA_ERROR_INVALID_ARGUMENT); |
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 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 10035 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10036 | output_buffer, size_zk_proof), |
| 10037 | PSA_ERROR_BAD_STATE); |
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 | status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10042 | output_buffer, size_key_share); |
| 10043 | if (status != PSA_SUCCESS) { |
| 10044 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10045 | goto exit; |
| 10046 | } |
| 10047 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10048 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 10049 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10050 | output_buffer, size_zk_public + 1), |
| 10051 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10052 | goto exit; |
| 10053 | } |
| 10054 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10055 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10056 | // 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] | 10057 | psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10058 | output_buffer, size_zk_public + 1); |
| 10059 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10060 | output_buffer, size_zk_public), |
| 10061 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10062 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10063 | } |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10064 | } else { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10065 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 10066 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10067 | NULL, 0, NULL), |
| 10068 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10069 | goto exit; |
| 10070 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10071 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10072 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 10073 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 10074 | output_buffer, buf_size, &output_len), |
| 10075 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10076 | goto exit; |
| 10077 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10078 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10079 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 10080 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10081 | output_buffer, buf_size, &output_len), |
| 10082 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10083 | goto exit; |
| 10084 | } |
| 10085 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10086 | status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10087 | output_buffer, buf_size, &output_len); |
| 10088 | if (status != PSA_SUCCESS) { |
| 10089 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10090 | goto exit; |
| 10091 | } |
| 10092 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10093 | TEST_ASSERT(output_len > 0); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10094 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10095 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 10096 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10097 | output_buffer, size_zk_public - 1, &output_len), |
| 10098 | PSA_ERROR_BUFFER_TOO_SMALL); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10099 | goto exit; |
| 10100 | } |
| 10101 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10102 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10103 | // 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] | 10104 | psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10105 | output_buffer, size_zk_public - 1, &output_len); |
| 10106 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10107 | output_buffer, buf_size, &output_len), |
| 10108 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10109 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10110 | } |
| 10111 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10112 | |
| 10113 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10114 | PSA_ASSERT(psa_destroy_key(key)); |
| 10115 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10116 | mbedtls_free(output_buffer); |
| 10117 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10118 | } |
| 10119 | /* END_CASE */ |
| 10120 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 10121 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10122 | void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg, |
| 10123 | int client_input_first, int inject_error, |
| 10124 | data_t *pw_data) |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10125 | { |
| 10126 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10127 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10128 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10129 | psa_algorithm_t alg = alg_arg; |
| 10130 | psa_algorithm_t hash_alg = hash_arg; |
| 10131 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10132 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10133 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10134 | PSA_INIT(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10135 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10136 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10137 | psa_set_key_algorithm(&attributes, alg); |
| 10138 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10139 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10140 | &key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10141 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10142 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10143 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10144 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10145 | |
| 10146 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10147 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10148 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10150 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10151 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10153 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10154 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10155 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10156 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10157 | client_input_first, 1, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10159 | if (inject_error == 1 || inject_error == 2) { |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10160 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10161 | } |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10162 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10163 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10164 | client_input_first, 2, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10165 | |
| 10166 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10167 | psa_destroy_key(key); |
| 10168 | psa_pake_abort(&server); |
| 10169 | psa_pake_abort(&client); |
| 10170 | PSA_DONE(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10171 | } |
| 10172 | /* END_CASE */ |
| 10173 | |
| 10174 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10175 | void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg, |
| 10176 | int derive_alg_arg, data_t *pw_data, |
| 10177 | int client_input_first, int inj_err_type_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10178 | { |
| 10179 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10180 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10181 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10182 | psa_algorithm_t alg = alg_arg; |
| 10183 | psa_algorithm_t hash_alg = hash_arg; |
| 10184 | psa_algorithm_t derive_alg = derive_alg_arg; |
| 10185 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10186 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10187 | psa_key_derivation_operation_t server_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10188 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10189 | psa_key_derivation_operation_t client_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10190 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10191 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10192 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10193 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10194 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10195 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10196 | psa_set_key_algorithm(&attributes, alg); |
| 10197 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10198 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10199 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10200 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10201 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10202 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10203 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10204 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10205 | /* Get shared key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10206 | PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg)); |
| 10207 | PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10208 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10209 | if (PSA_ALG_IS_TLS12_PRF(derive_alg) || |
| 10210 | PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) { |
| 10211 | PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive, |
| 10212 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10213 | (const uint8_t *) "", 0)); |
| 10214 | PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive, |
| 10215 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10216 | (const uint8_t *) "", 0)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10217 | } |
| 10218 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10219 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10220 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10221 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10222 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10223 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10224 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10225 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10226 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10227 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10228 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) { |
| 10229 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10230 | PSA_ERROR_BAD_STATE); |
| 10231 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10232 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10233 | goto exit; |
| 10234 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10235 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10236 | /* First round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10237 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10238 | client_input_first, 1, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10239 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10240 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) { |
| 10241 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10242 | PSA_ERROR_BAD_STATE); |
| 10243 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10244 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10245 | goto exit; |
| 10246 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10247 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10248 | /* Second round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10249 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10250 | client_input_first, 2, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10252 | PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive)); |
| 10253 | PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10254 | |
| 10255 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10256 | psa_key_derivation_abort(&server_derive); |
| 10257 | psa_key_derivation_abort(&client_derive); |
| 10258 | psa_destroy_key(key); |
| 10259 | psa_pake_abort(&server); |
| 10260 | psa_pake_abort(&client); |
| 10261 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10262 | } |
| 10263 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10264 | |
| 10265 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10266 | void ecjpake_size_macros() |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10267 | { |
| 10268 | const psa_algorithm_t alg = PSA_ALG_JPAKE; |
| 10269 | const size_t bits = 256; |
| 10270 | const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10271 | 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] | 10272 | 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] | 10273 | PSA_ECC_FAMILY_SECP_R1); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10274 | |
| 10275 | // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types |
| 10276 | /* 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] | 10277 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10278 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
| 10279 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10280 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10281 | /* The output for ZK_PROOF is the same bitsize as the curve */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10282 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10283 | PSA_BITS_TO_BYTES(bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10284 | |
| 10285 | /* Input sizes are the same as output sizes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10286 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10287 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE)); |
| 10288 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10289 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC)); |
| 10290 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10291 | 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] | 10292 | |
| 10293 | /* These inequalities will always hold even when other PAKEs are added */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10294 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10295 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10296 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10297 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10298 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10299 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10300 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10301 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10302 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10303 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10304 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10305 | PSA_PAKE_INPUT_MAX_SIZE); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10306 | } |
| 10307 | /* END_CASE */ |