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 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 297 | /* An overapproximation of the amount of storage needed for a key of the |
| 298 | * given type and with the given content. The API doesn't make it easy |
| 299 | * to find a good value for the size. The current implementation doesn't |
| 300 | * care about the value anyway. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 301 | #define KEY_BITS_FROM_DATA(type, data) \ |
| 302 | (data)->len |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 303 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 304 | typedef enum { |
| 305 | IMPORT_KEY = 0, |
| 306 | GENERATE_KEY = 1, |
| 307 | DERIVE_KEY = 2 |
| 308 | } generate_method; |
| 309 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 310 | typedef enum { |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 311 | DO_NOT_SET_LENGTHS = 0, |
| 312 | SET_LENGTHS_BEFORE_NONCE = 1, |
| 313 | SET_LENGTHS_AFTER_NONCE = 2 |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 314 | } set_lengths_method_t; |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 315 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 316 | typedef enum { |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 317 | USE_NULL_TAG = 0, |
| 318 | USE_GIVEN_TAG = 1, |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 319 | } tag_usage_method_t; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 320 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 321 | /*! |
| 322 | * \brief Internal Function for AEAD multipart tests. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 323 | * \param key_type_arg Type of key passed in |
| 324 | * \param key_data The encryption / decryption key data |
| 325 | * \param alg_arg The type of algorithm used |
| 326 | * \param nonce Nonce data |
| 327 | * \param additional_data Additional data |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 328 | * \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] | 329 | * feed additional data in to be encrypted / |
| 330 | * decrypted. If -1, no chunking. |
| 331 | * \param input_data Data to encrypt / decrypt |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 332 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 333 | * the data in to be encrypted / decrypted. If |
| 334 | * -1, no chunking |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 335 | * \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] | 336 | * expected here, this controls whether or not |
| 337 | * to set lengths, and in what order with |
| 338 | * respect to set nonce. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 339 | * \param expected_output Expected output |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 340 | * \param is_encrypt If non-zero this is an encryption operation. |
Paul Elliott | 41ffae1 | 2021-07-22 21:52:01 +0100 | [diff] [blame] | 341 | * \param do_zero_parts If non-zero, interleave zero length chunks |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 342 | * with normal length chunks. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 343 | * \return int Zero on failure, non-zero on success. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 344 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 345 | static int aead_multipart_internal_func(int key_type_arg, data_t *key_data, |
| 346 | int alg_arg, |
| 347 | data_t *nonce, |
| 348 | data_t *additional_data, |
| 349 | int ad_part_len_arg, |
| 350 | data_t *input_data, |
| 351 | int data_part_len_arg, |
| 352 | set_lengths_method_t set_lengths_method, |
| 353 | data_t *expected_output, |
| 354 | int is_encrypt, |
| 355 | int do_zero_parts) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 356 | { |
| 357 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 358 | psa_key_type_t key_type = key_type_arg; |
| 359 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 360 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 361 | unsigned char *output_data = NULL; |
| 362 | unsigned char *part_data = NULL; |
| 363 | unsigned char *final_data = NULL; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 364 | size_t data_true_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 365 | size_t part_data_size = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 366 | size_t output_size = 0; |
| 367 | size_t final_output_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 368 | size_t output_length = 0; |
| 369 | size_t key_bits = 0; |
| 370 | size_t tag_length = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 371 | size_t part_offset = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 372 | size_t part_length = 0; |
| 373 | size_t output_part_length = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 374 | size_t tag_size = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 375 | size_t ad_part_len = 0; |
| 376 | size_t data_part_len = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 377 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 378 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 379 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 380 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 381 | int test_ok = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 382 | size_t part_count = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 383 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 384 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 386 | if (is_encrypt) { |
| 387 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 388 | } else { |
| 389 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 390 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 391 | |
| 392 | psa_set_key_algorithm(&attributes, alg); |
| 393 | psa_set_key_type(&attributes, key_type); |
| 394 | |
| 395 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 396 | &key)); |
| 397 | |
| 398 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 399 | key_bits = psa_get_key_bits(&attributes); |
| 400 | |
| 401 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); |
| 402 | |
| 403 | if (is_encrypt) { |
| 404 | /* Tag gets written at end of buffer. */ |
| 405 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 406 | (input_data->len + |
| 407 | tag_length)); |
| 408 | data_true_size = input_data->len; |
| 409 | } else { |
| 410 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 411 | (input_data->len - |
| 412 | tag_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 413 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 414 | /* Do not want to attempt to decrypt tag. */ |
| 415 | data_true_size = input_data->len - tag_length; |
| 416 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 417 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 418 | ASSERT_ALLOC(output_data, output_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 419 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 420 | if (is_encrypt) { |
| 421 | final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
| 422 | TEST_LE_U(final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
| 423 | } else { |
| 424 | final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); |
| 425 | TEST_LE_U(final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 426 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 427 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 428 | ASSERT_ALLOC(final_data, final_output_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 429 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 430 | if (is_encrypt) { |
| 431 | status = psa_aead_encrypt_setup(&operation, key, alg); |
| 432 | } else { |
| 433 | status = psa_aead_decrypt_setup(&operation, key, alg); |
| 434 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 435 | |
| 436 | /* If the operation is not supported, just skip and not fail in case the |
| 437 | * encryption involves a common limitation of cryptography hardwares and |
| 438 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 439 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 440 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 441 | 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] | 442 | } |
| 443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 444 | PSA_ASSERT(status); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 445 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 446 | if (set_lengths_method == DO_NOT_SET_LENGTHS) { |
| 447 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
| 448 | } else if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { |
| 449 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 450 | data_true_size)); |
| 451 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
| 452 | } else if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) { |
| 453 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 454 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 455 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 456 | data_true_size)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 457 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 458 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 459 | if (ad_part_len_arg != -1) { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 460 | /* Pass additional data in parts */ |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 461 | ad_part_len = (size_t) ad_part_len_arg; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 462 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 463 | for (part_offset = 0, part_count = 0; |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 464 | part_offset < additional_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 465 | part_offset += part_length, part_count++) { |
| 466 | if (do_zero_parts && (part_count & 0x01)) { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 467 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 468 | } else if (additional_data->len - part_offset < ad_part_len) { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 469 | part_length = additional_data->len - part_offset; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 470 | } else { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 471 | part_length = ad_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 472 | } |
| 473 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 474 | PSA_ASSERT(psa_aead_update_ad(&operation, |
| 475 | additional_data->x + part_offset, |
| 476 | part_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 477 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 478 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 479 | } else { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 480 | /* Pass additional data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 481 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 482 | additional_data->len)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 483 | } |
| 484 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 485 | if (data_part_len_arg != -1) { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 486 | /* Pass data in parts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 487 | data_part_len = (size_t) data_part_len_arg; |
| 488 | part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 489 | (size_t) data_part_len); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 490 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 491 | ASSERT_ALLOC(part_data, part_data_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 492 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 493 | for (part_offset = 0, part_count = 0; |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 494 | part_offset < data_true_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 495 | part_offset += part_length, part_count++) { |
| 496 | if (do_zero_parts && (part_count & 0x01)) { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 497 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 498 | } else if ((data_true_size - part_offset) < data_part_len) { |
| 499 | part_length = (data_true_size - part_offset); |
| 500 | } else { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 501 | part_length = data_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 502 | } |
| 503 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 504 | PSA_ASSERT(psa_aead_update(&operation, |
| 505 | (input_data->x + part_offset), |
| 506 | part_length, part_data, |
| 507 | part_data_size, |
| 508 | &output_part_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 509 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 510 | if (output_data && output_part_length) { |
| 511 | memcpy((output_data + output_length), part_data, |
| 512 | output_part_length); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 513 | } |
| 514 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 515 | output_length += output_part_length; |
| 516 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 517 | } else { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 518 | /* Pass all data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 519 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 520 | data_true_size, output_data, |
| 521 | output_size, &output_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 522 | } |
| 523 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 524 | if (is_encrypt) { |
| 525 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 526 | final_output_size, |
| 527 | &output_part_length, |
| 528 | tag_buffer, tag_length, |
| 529 | &tag_size)); |
| 530 | } else { |
| 531 | PSA_ASSERT(psa_aead_verify(&operation, final_data, |
| 532 | final_output_size, |
| 533 | &output_part_length, |
| 534 | (input_data->x + data_true_size), |
| 535 | tag_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 536 | } |
| 537 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 538 | if (output_data && output_part_length) { |
| 539 | memcpy((output_data + output_length), final_data, |
| 540 | output_part_length); |
| 541 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 542 | |
| 543 | output_length += output_part_length; |
| 544 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 545 | |
| 546 | /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE |
| 547 | * should be exact.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 548 | if (is_encrypt) { |
| 549 | TEST_EQUAL(tag_length, tag_size); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 550 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 551 | if (output_data && tag_length) { |
| 552 | memcpy((output_data + output_length), tag_buffer, |
| 553 | tag_length); |
| 554 | } |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 555 | |
| 556 | output_length += tag_length; |
| 557 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 558 | TEST_EQUAL(output_length, |
| 559 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 560 | input_data->len)); |
| 561 | TEST_LE_U(output_length, |
| 562 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
| 563 | } else { |
| 564 | TEST_EQUAL(output_length, |
| 565 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, |
| 566 | input_data->len)); |
| 567 | TEST_LE_U(output_length, |
| 568 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 569 | } |
| 570 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 571 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 572 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 573 | output_data, output_length); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 574 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 575 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 576 | test_ok = 1; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 577 | |
| 578 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 579 | psa_destroy_key(key); |
| 580 | psa_aead_abort(&operation); |
| 581 | mbedtls_free(output_data); |
| 582 | mbedtls_free(part_data); |
| 583 | mbedtls_free(final_data); |
| 584 | PSA_DONE(); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 585 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 586 | return test_ok; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 587 | } |
| 588 | |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 589 | /*! |
| 590 | * \brief Internal Function for MAC multipart tests. |
| 591 | * \param key_type_arg Type of key passed in |
| 592 | * \param key_data The encryption / decryption key data |
| 593 | * \param alg_arg The type of algorithm used |
| 594 | * \param input_data Data to encrypt / decrypt |
| 595 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 596 | * the data in to be encrypted / decrypted. If |
| 597 | * -1, no chunking |
| 598 | * \param expected_output Expected output |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 599 | * \param is_verify If non-zero this is a verify operation. |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 600 | * \param do_zero_parts If non-zero, interleave zero length chunks |
| 601 | * with normal length chunks. |
| 602 | * \return int Zero on failure, non-zero on success. |
| 603 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 604 | static int mac_multipart_internal_func(int key_type_arg, data_t *key_data, |
| 605 | int alg_arg, |
| 606 | data_t *input_data, |
| 607 | int data_part_len_arg, |
| 608 | data_t *expected_output, |
| 609 | int is_verify, |
| 610 | int do_zero_parts) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 611 | { |
| 612 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 613 | psa_key_type_t key_type = key_type_arg; |
| 614 | psa_algorithm_t alg = alg_arg; |
| 615 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 616 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
| 617 | size_t part_offset = 0; |
| 618 | size_t part_length = 0; |
| 619 | size_t data_part_len = 0; |
| 620 | size_t mac_len = 0; |
| 621 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 622 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 623 | |
| 624 | int test_ok = 0; |
| 625 | size_t part_count = 0; |
| 626 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 627 | PSA_INIT(); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 628 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 629 | if (is_verify) { |
| 630 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 631 | } else { |
| 632 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 633 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 634 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 635 | psa_set_key_algorithm(&attributes, alg); |
| 636 | psa_set_key_type(&attributes, key_type); |
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_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 639 | &key)); |
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 | if (is_verify) { |
| 642 | status = psa_mac_verify_setup(&operation, key, alg); |
| 643 | } else { |
| 644 | status = psa_mac_sign_setup(&operation, key, alg); |
| 645 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 646 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 647 | PSA_ASSERT(status); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 648 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 649 | if (data_part_len_arg != -1) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 650 | /* Pass data in parts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 651 | data_part_len = (size_t) data_part_len_arg; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 652 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 653 | for (part_offset = 0, part_count = 0; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 654 | part_offset < input_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 655 | part_offset += part_length, part_count++) { |
| 656 | if (do_zero_parts && (part_count & 0x01)) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 657 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 658 | } else if ((input_data->len - part_offset) < data_part_len) { |
| 659 | part_length = (input_data->len - part_offset); |
| 660 | } else { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 661 | part_length = data_part_len; |
| 662 | } |
| 663 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 664 | PSA_ASSERT(psa_mac_update(&operation, |
| 665 | (input_data->x + part_offset), |
| 666 | part_length)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 667 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 668 | } else { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 669 | /* Pass all data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 670 | PSA_ASSERT(psa_mac_update(&operation, input_data->x, |
| 671 | input_data->len)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 672 | } |
| 673 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 674 | if (is_verify) { |
| 675 | PSA_ASSERT(psa_mac_verify_finish(&operation, expected_output->x, |
| 676 | expected_output->len)); |
| 677 | } else { |
| 678 | PSA_ASSERT(psa_mac_sign_finish(&operation, mac, |
| 679 | PSA_MAC_MAX_SIZE, &mac_len)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 680 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 681 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 682 | mac, mac_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | test_ok = 1; |
| 686 | |
| 687 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 688 | psa_destroy_key(key); |
| 689 | psa_mac_abort(&operation); |
| 690 | PSA_DONE(); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 691 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 692 | return test_ok; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 693 | } |
| 694 | |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 695 | #if defined(PSA_WANT_ALG_JPAKE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 696 | static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive, |
| 697 | psa_pake_operation_t *server, |
| 698 | psa_pake_operation_t *client, |
| 699 | int client_input_first, |
| 700 | int round, int inject_error) |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 701 | { |
| 702 | unsigned char *buffer0 = NULL, *buffer1 = NULL; |
| 703 | size_t buffer_length = ( |
| 704 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) + |
| 705 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) + |
| 706 | 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] | 707 | /* The output should be exactly this size according to the spec */ |
| 708 | const size_t expected_size_key_share = |
| 709 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE); |
| 710 | /* The output should be exactly this size according to the spec */ |
| 711 | const size_t expected_size_zk_public = |
| 712 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC); |
| 713 | /* The output can be smaller: the spec allows stripping leading zeroes */ |
| 714 | const size_t max_expected_size_zk_proof = |
| 715 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 716 | size_t buffer0_off = 0; |
| 717 | size_t buffer1_off = 0; |
| 718 | size_t s_g1_len, s_g2_len, s_a_len; |
| 719 | size_t s_g1_off, s_g2_off, s_a_off; |
| 720 | size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len; |
| 721 | size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off; |
| 722 | size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len; |
| 723 | size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off; |
| 724 | size_t c_g1_len, c_g2_len, c_a_len; |
| 725 | size_t c_g1_off, c_g2_off, c_a_off; |
| 726 | size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len; |
| 727 | size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off; |
| 728 | size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len; |
| 729 | size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off; |
| 730 | psa_status_t expected_status = PSA_SUCCESS; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 731 | psa_status_t status; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 732 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 733 | ASSERT_ALLOC(buffer0, buffer_length); |
| 734 | ASSERT_ALLOC(buffer1, buffer_length); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 735 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 736 | switch (round) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 737 | case 1: |
| 738 | /* Server first round Output */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 739 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 740 | buffer0 + buffer0_off, |
| 741 | 512 - buffer0_off, &s_g1_len)); |
| 742 | TEST_EQUAL(s_g1_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 743 | s_g1_off = buffer0_off; |
| 744 | buffer0_off += s_g1_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 745 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 746 | buffer0 + buffer0_off, |
| 747 | 512 - buffer0_off, &s_x1_pk_len)); |
| 748 | TEST_EQUAL(s_x1_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 749 | s_x1_pk_off = buffer0_off; |
| 750 | buffer0_off += s_x1_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 751 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 752 | buffer0 + buffer0_off, |
| 753 | 512 - buffer0_off, &s_x1_pr_len)); |
| 754 | TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 755 | s_x1_pr_off = buffer0_off; |
| 756 | buffer0_off += s_x1_pr_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 757 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 758 | buffer0 + buffer0_off, |
| 759 | 512 - buffer0_off, &s_g2_len)); |
| 760 | TEST_EQUAL(s_g2_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 761 | s_g2_off = buffer0_off; |
| 762 | buffer0_off += s_g2_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 763 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 764 | buffer0 + buffer0_off, |
| 765 | 512 - buffer0_off, &s_x2_pk_len)); |
| 766 | TEST_EQUAL(s_x2_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 767 | s_x2_pk_off = buffer0_off; |
| 768 | buffer0_off += s_x2_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 769 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 770 | buffer0 + buffer0_off, |
| 771 | 512 - buffer0_off, &s_x2_pr_len)); |
| 772 | TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 773 | s_x2_pr_off = buffer0_off; |
| 774 | buffer0_off += s_x2_pr_len; |
| 775 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 776 | if (inject_error == 1) { |
Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 777 | buffer0[s_x1_pr_off + 8] ^= 1; |
| 778 | buffer0[s_x2_pr_off + 7] ^= 1; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 779 | expected_status = PSA_ERROR_DATA_INVALID; |
| 780 | } |
| 781 | |
Neil Armstrong | 51009d7 | 2022-09-05 17:59:54 +0200 | [diff] [blame] | 782 | /* |
| 783 | * When injecting errors in inputs, the implementation is |
| 784 | * free to detect it right away of with a delay. |
| 785 | * This permits delaying the error until the end of the input |
| 786 | * sequence, if no error appears then, this will be treated |
| 787 | * as an error. |
| 788 | */ |
| 789 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 790 | if (client_input_first == 1) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 791 | /* Client first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 792 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 793 | buffer0 + s_g1_off, s_g1_len); |
| 794 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 795 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 796 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 797 | } else { |
| 798 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 799 | } |
| 800 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 801 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 802 | buffer0 + s_x1_pk_off, |
| 803 | s_x1_pk_len); |
| 804 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 805 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 806 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 807 | } else { |
| 808 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 809 | } |
| 810 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 811 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 812 | buffer0 + s_x1_pr_off, |
| 813 | s_x1_pr_len); |
| 814 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 815 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 816 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 817 | } else { |
| 818 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 819 | } |
| 820 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 821 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 822 | buffer0 + s_g2_off, |
| 823 | s_g2_len); |
| 824 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 825 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 826 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 827 | } else { |
| 828 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 829 | } |
| 830 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 831 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 832 | buffer0 + s_x2_pk_off, |
| 833 | s_x2_pk_len); |
| 834 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 835 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 836 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 837 | } else { |
| 838 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 839 | } |
| 840 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 841 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 842 | buffer0 + s_x2_pr_off, |
| 843 | s_x2_pr_len); |
| 844 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 845 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 846 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 847 | } else { |
| 848 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 849 | } |
| 850 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 851 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 852 | if (inject_error == 1) { |
| 853 | TEST_ASSERT( |
| 854 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 855 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | /* Client first round Output */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 859 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 860 | buffer1 + buffer1_off, |
| 861 | 512 - buffer1_off, &c_g1_len)); |
| 862 | TEST_EQUAL(c_g1_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 863 | c_g1_off = buffer1_off; |
| 864 | buffer1_off += c_g1_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 865 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 866 | buffer1 + buffer1_off, |
| 867 | 512 - buffer1_off, &c_x1_pk_len)); |
| 868 | TEST_EQUAL(c_x1_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 869 | c_x1_pk_off = buffer1_off; |
| 870 | buffer1_off += c_x1_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 871 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 872 | buffer1 + buffer1_off, |
| 873 | 512 - buffer1_off, &c_x1_pr_len)); |
| 874 | TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 875 | c_x1_pr_off = buffer1_off; |
| 876 | buffer1_off += c_x1_pr_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 877 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 878 | buffer1 + buffer1_off, |
| 879 | 512 - buffer1_off, &c_g2_len)); |
| 880 | TEST_EQUAL(c_g2_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 881 | c_g2_off = buffer1_off; |
| 882 | buffer1_off += c_g2_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 883 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 884 | buffer1 + buffer1_off, |
| 885 | 512 - buffer1_off, &c_x2_pk_len)); |
| 886 | TEST_EQUAL(c_x2_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 887 | c_x2_pk_off = buffer1_off; |
| 888 | buffer1_off += c_x2_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 889 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 890 | buffer1 + buffer1_off, |
| 891 | 512 - buffer1_off, &c_x2_pr_len)); |
| 892 | TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 893 | c_x2_pr_off = buffer1_off; |
| 894 | buffer1_off += c_x2_pr_len; |
| 895 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 896 | if (client_input_first == 0) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 897 | /* Client first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 898 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 899 | buffer0 + s_g1_off, s_g1_len); |
| 900 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 901 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 902 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 903 | } else { |
| 904 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 905 | } |
| 906 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 907 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 908 | buffer0 + s_x1_pk_off, |
| 909 | s_x1_pk_len); |
| 910 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 911 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 912 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 913 | } else { |
| 914 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 915 | } |
| 916 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 917 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 918 | buffer0 + s_x1_pr_off, |
| 919 | s_x1_pr_len); |
| 920 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 921 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 922 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 923 | } else { |
| 924 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 925 | } |
| 926 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 927 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 928 | buffer0 + s_g2_off, |
| 929 | s_g2_len); |
| 930 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 931 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 932 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 933 | } else { |
| 934 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 935 | } |
| 936 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 937 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 938 | buffer0 + s_x2_pk_off, |
| 939 | s_x2_pk_len); |
| 940 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 941 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 942 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 943 | } else { |
| 944 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 945 | } |
| 946 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 947 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 948 | buffer0 + s_x2_pr_off, |
| 949 | s_x2_pr_len); |
| 950 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 951 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 952 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 953 | } else { |
| 954 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 955 | } |
| 956 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 957 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 958 | if (inject_error == 1) { |
| 959 | TEST_ASSERT( |
| 960 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 961 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 962 | } |
| 963 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 964 | if (inject_error == 2) { |
Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 965 | buffer1[c_x1_pr_off + 12] ^= 1; |
| 966 | buffer1[c_x2_pr_off + 7] ^= 1; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 967 | expected_status = PSA_ERROR_DATA_INVALID; |
| 968 | } |
| 969 | |
| 970 | /* Server first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 971 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 972 | buffer1 + c_g1_off, c_g1_len); |
| 973 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 974 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 975 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 976 | } else { |
| 977 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 978 | } |
| 979 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 980 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 981 | buffer1 + c_x1_pk_off, c_x1_pk_len); |
| 982 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 983 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 984 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 985 | } else { |
| 986 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 987 | } |
| 988 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 989 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 990 | buffer1 + c_x1_pr_off, c_x1_pr_len); |
| 991 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 992 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 993 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 994 | } else { |
| 995 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 996 | } |
| 997 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 998 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 999 | buffer1 + c_g2_off, c_g2_len); |
| 1000 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1001 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1002 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1003 | } else { |
| 1004 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1005 | } |
| 1006 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1007 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1008 | buffer1 + c_x2_pk_off, c_x2_pk_len); |
| 1009 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1010 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1011 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1012 | } else { |
| 1013 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1014 | } |
| 1015 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1016 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1017 | buffer1 + c_x2_pr_off, c_x2_pr_len); |
| 1018 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1019 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1020 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1021 | } else { |
| 1022 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1023 | } |
| 1024 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1025 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1026 | if (inject_error == 2) { |
| 1027 | TEST_ASSERT( |
| 1028 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1029 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1030 | |
| 1031 | break; |
| 1032 | |
| 1033 | case 2: |
| 1034 | /* Server second round Output */ |
| 1035 | buffer0_off = 0; |
| 1036 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1037 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 1038 | buffer0 + buffer0_off, |
| 1039 | 512 - buffer0_off, &s_a_len)); |
| 1040 | TEST_EQUAL(s_a_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1041 | s_a_off = buffer0_off; |
| 1042 | buffer0_off += s_a_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1043 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1044 | buffer0 + buffer0_off, |
| 1045 | 512 - buffer0_off, &s_x2s_pk_len)); |
| 1046 | TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1047 | s_x2s_pk_off = buffer0_off; |
| 1048 | buffer0_off += s_x2s_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1049 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1050 | buffer0 + buffer0_off, |
| 1051 | 512 - buffer0_off, &s_x2s_pr_len)); |
| 1052 | TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1053 | s_x2s_pr_off = buffer0_off; |
| 1054 | buffer0_off += s_x2s_pr_len; |
| 1055 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1056 | if (inject_error == 3) { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1057 | buffer0[s_x2s_pk_off + 12] += 0x33; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1058 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1059 | } |
| 1060 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1061 | if (client_input_first == 1) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1062 | /* Client second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1063 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1064 | buffer0 + s_a_off, s_a_len); |
| 1065 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1066 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1067 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1068 | } else { |
| 1069 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1070 | } |
| 1071 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1072 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1073 | buffer0 + s_x2s_pk_off, |
| 1074 | s_x2s_pk_len); |
| 1075 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1076 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1077 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1078 | } else { |
| 1079 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1080 | } |
| 1081 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1082 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1083 | buffer0 + s_x2s_pr_off, |
| 1084 | s_x2s_pr_len); |
| 1085 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1086 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1087 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1088 | } else { |
| 1089 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1090 | } |
| 1091 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1092 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1093 | if (inject_error == 3) { |
| 1094 | TEST_ASSERT( |
| 1095 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1096 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | /* Client second round Output */ |
| 1100 | buffer1_off = 0; |
| 1101 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1102 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1103 | buffer1 + buffer1_off, |
| 1104 | 512 - buffer1_off, &c_a_len)); |
| 1105 | TEST_EQUAL(c_a_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1106 | c_a_off = buffer1_off; |
| 1107 | buffer1_off += c_a_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1108 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1109 | buffer1 + buffer1_off, |
| 1110 | 512 - buffer1_off, &c_x2s_pk_len)); |
| 1111 | TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1112 | c_x2s_pk_off = buffer1_off; |
| 1113 | buffer1_off += c_x2s_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1114 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1115 | buffer1 + buffer1_off, |
| 1116 | 512 - buffer1_off, &c_x2s_pr_len)); |
| 1117 | TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1118 | c_x2s_pr_off = buffer1_off; |
| 1119 | buffer1_off += c_x2s_pr_len; |
| 1120 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1121 | if (client_input_first == 0) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1122 | /* Client second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1123 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1124 | buffer0 + s_a_off, s_a_len); |
| 1125 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1126 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1127 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1128 | } else { |
| 1129 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1130 | } |
| 1131 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1132 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1133 | buffer0 + s_x2s_pk_off, |
| 1134 | s_x2s_pk_len); |
| 1135 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1136 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1137 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1138 | } else { |
| 1139 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1140 | } |
| 1141 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1142 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1143 | buffer0 + s_x2s_pr_off, |
| 1144 | s_x2s_pr_len); |
| 1145 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1146 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1147 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1148 | } else { |
| 1149 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1150 | } |
| 1151 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1152 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1153 | if (inject_error == 3) { |
| 1154 | TEST_ASSERT( |
| 1155 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1156 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1157 | } |
| 1158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1159 | if (inject_error == 4) { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1160 | buffer1[c_x2s_pk_off + 7] += 0x28; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1161 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1162 | } |
| 1163 | |
| 1164 | /* Server second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1165 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 1166 | buffer1 + c_a_off, c_a_len); |
| 1167 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1168 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1169 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1170 | } else { |
| 1171 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1172 | } |
| 1173 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1174 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1175 | buffer1 + c_x2s_pk_off, c_x2s_pk_len); |
| 1176 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1177 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1178 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1179 | } else { |
| 1180 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1181 | } |
| 1182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1183 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1184 | buffer1 + c_x2s_pr_off, c_x2s_pr_len); |
| 1185 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1186 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1187 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1188 | } else { |
| 1189 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1190 | } |
| 1191 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1192 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1193 | if (inject_error == 4) { |
| 1194 | TEST_ASSERT( |
| 1195 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1196 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1197 | |
| 1198 | break; |
| 1199 | |
| 1200 | } |
| 1201 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1202 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1203 | mbedtls_free(buffer0); |
| 1204 | mbedtls_free(buffer1); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1205 | } |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 1206 | #endif /* PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1207 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1208 | typedef enum { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 1209 | INJECT_ERR_NONE = 0, |
| 1210 | INJECT_ERR_UNINITIALIZED_ACCESS, |
| 1211 | INJECT_ERR_DUPLICATE_SETUP, |
| 1212 | INJECT_ERR_INVALID_USER, |
| 1213 | INJECT_ERR_INVALID_PEER, |
| 1214 | INJECT_ERR_SET_USER, |
| 1215 | INJECT_ERR_SET_PEER, |
| 1216 | INJECT_EMPTY_IO_BUFFER, |
| 1217 | INJECT_UNKNOWN_STEP, |
| 1218 | INJECT_INVALID_FIRST_STEP, |
| 1219 | INJECT_WRONG_BUFFER_SIZE, |
| 1220 | INJECT_VALID_OPERATION_AFTER_FAILURE, |
| 1221 | INJECT_ANTICIPATE_KEY_DERIVATION_1, |
| 1222 | INJECT_ANTICIPATE_KEY_DERIVATION_2, |
| 1223 | } ecjpake_injected_failure_t; |
| 1224 | |
Paul Elliott | 01885fa | 2023-02-09 12:07:30 +0000 | [diff] [blame] | 1225 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Paul Elliott | 1243f93 | 2023-02-07 11:21:10 +0000 | [diff] [blame] | 1226 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1227 | static void interruptible_signverify_get_minmax_completes(uint32_t max_ops, |
| 1228 | psa_status_t expected_status, |
| 1229 | size_t *min_completes, |
| 1230 | size_t *max_completes) |
| 1231 | { |
| 1232 | |
| 1233 | /* This is slightly contrived, but we only really know that with a minimum |
| 1234 | value of max_ops that a successful operation should take more than one op |
| 1235 | to complete, and likewise that with a max_ops of |
| 1236 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */ |
| 1237 | if (max_ops == 0 || max_ops == 1) { |
Paul Elliott | c86d45e | 2023-02-15 17:38:05 +0000 | [diff] [blame] | 1238 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1239 | if (expected_status == PSA_SUCCESS) { |
| 1240 | *min_completes = 2; |
| 1241 | } else { |
| 1242 | *min_completes = 1; |
| 1243 | } |
| 1244 | |
| 1245 | *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED; |
| 1246 | } else { |
| 1247 | *min_completes = 1; |
| 1248 | *max_completes = 1; |
| 1249 | } |
| 1250 | } |
Paul Elliott | 01885fa | 2023-02-09 12:07:30 +0000 | [diff] [blame] | 1251 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1252 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1253 | /* END_HEADER */ |
| 1254 | |
| 1255 | /* BEGIN_DEPENDENCIES |
| 1256 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1257 | * END_DEPENDENCIES |
| 1258 | */ |
| 1259 | |
| 1260 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 7abdf7e | 2023-03-09 11:17:43 +0100 | [diff] [blame] | 1261 | void psa_can_do_hash() |
| 1262 | { |
| 1263 | /* We can't test that this is specific to drivers until partial init has |
| 1264 | * been implemented, but we can at least test before/after full init. */ |
| 1265 | TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE)); |
| 1266 | PSA_INIT(); |
| 1267 | TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE)); |
| 1268 | PSA_DONE(); |
| 1269 | } |
| 1270 | /* END_CASE */ |
| 1271 | |
| 1272 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1273 | void static_checks() |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1274 | { |
| 1275 | size_t max_truncated_mac_size = |
| 1276 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1277 | |
| 1278 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1279 | * encoding. The shifted mask is the maximum truncated value. The |
| 1280 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1281 | TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size); |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1282 | } |
| 1283 | /* END_CASE */ |
| 1284 | |
| 1285 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1286 | void import_with_policy(int type_arg, |
| 1287 | int usage_arg, int alg_arg, |
| 1288 | int expected_status_arg) |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1289 | { |
| 1290 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1291 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1292 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1293 | psa_key_type_t type = type_arg; |
| 1294 | psa_key_usage_t usage = usage_arg; |
| 1295 | psa_algorithm_t alg = alg_arg; |
| 1296 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1297 | const uint8_t key_material[16] = { 0 }; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1298 | psa_status_t status; |
| 1299 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1300 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1301 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1302 | psa_set_key_type(&attributes, type); |
| 1303 | psa_set_key_usage_flags(&attributes, usage); |
| 1304 | psa_set_key_algorithm(&attributes, alg); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1306 | status = psa_import_key(&attributes, |
| 1307 | key_material, sizeof(key_material), |
| 1308 | &key); |
| 1309 | TEST_EQUAL(status, expected_status); |
| 1310 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1311 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1312 | } |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1314 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1315 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1316 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), |
| 1317 | mbedtls_test_update_key_usage_flags(usage)); |
| 1318 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 1319 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1320 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1321 | PSA_ASSERT(psa_destroy_key(key)); |
| 1322 | test_operations_on_invalid_key(key); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1323 | |
| 1324 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1325 | /* |
| 1326 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1327 | * thus reset them as required. |
| 1328 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1329 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1330 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1331 | psa_destroy_key(key); |
| 1332 | PSA_DONE(); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1333 | } |
| 1334 | /* END_CASE */ |
| 1335 | |
| 1336 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1337 | void import_with_data(data_t *data, int type_arg, |
| 1338 | int attr_bits_arg, |
| 1339 | int expected_status_arg) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1340 | { |
| 1341 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1342 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1343 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1344 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1345 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1346 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1347 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1349 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1350 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1351 | psa_set_key_type(&attributes, type); |
| 1352 | psa_set_key_bits(&attributes, attr_bits); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1354 | status = psa_import_key(&attributes, data->x, data->len, &key); |
| 1355 | TEST_EQUAL(status, expected_status); |
| 1356 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1357 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1358 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1359 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1360 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1361 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1362 | if (attr_bits != 0) { |
| 1363 | TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes)); |
| 1364 | } |
| 1365 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1367 | PSA_ASSERT(psa_destroy_key(key)); |
| 1368 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1369 | |
| 1370 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1371 | /* |
| 1372 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1373 | * thus reset them as required. |
| 1374 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1375 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1377 | psa_destroy_key(key); |
| 1378 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1379 | } |
| 1380 | /* END_CASE */ |
| 1381 | |
| 1382 | /* BEGIN_CASE */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1383 | /* Construct and attempt to import a large unstructured key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1384 | void import_large_key(int type_arg, int byte_size_arg, |
| 1385 | int expected_status_arg) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1386 | { |
| 1387 | psa_key_type_t type = type_arg; |
| 1388 | size_t byte_size = byte_size_arg; |
| 1389 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1390 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1391 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1392 | psa_status_t status; |
| 1393 | uint8_t *buffer = NULL; |
| 1394 | size_t buffer_size = byte_size + 1; |
| 1395 | size_t n; |
| 1396 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1397 | /* Skip the test case if the target running the test cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1398 | * accommodate large keys due to heap size constraints */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1399 | ASSERT_ALLOC_WEAK(buffer, buffer_size); |
| 1400 | memset(buffer, 'K', byte_size); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1401 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1402 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1403 | |
| 1404 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1405 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1406 | psa_set_key_type(&attributes, type); |
| 1407 | status = psa_import_key(&attributes, buffer, byte_size, &key); |
| 1408 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 1409 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1410 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1411 | if (status == PSA_SUCCESS) { |
| 1412 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1413 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 1414 | TEST_EQUAL(psa_get_key_bits(&attributes), |
| 1415 | PSA_BYTES_TO_BITS(byte_size)); |
| 1416 | ASSERT_NO_SLOT_NUMBER(&attributes); |
| 1417 | memset(buffer, 0, byte_size + 1); |
| 1418 | PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n)); |
| 1419 | for (n = 0; n < byte_size; n++) { |
| 1420 | TEST_EQUAL(buffer[n], 'K'); |
| 1421 | } |
| 1422 | for (n = byte_size; n < buffer_size; n++) { |
| 1423 | TEST_EQUAL(buffer[n], 0); |
| 1424 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1428 | /* |
| 1429 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1430 | * thus reset them as required. |
| 1431 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1432 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1433 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1434 | psa_destroy_key(key); |
| 1435 | PSA_DONE(); |
| 1436 | mbedtls_free(buffer); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1437 | } |
| 1438 | /* END_CASE */ |
| 1439 | |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 1440 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1441 | /* Import an RSA key with a valid structure (but not valid numbers |
| 1442 | * inside, beyond having sensible size and parity). This is expected to |
| 1443 | * fail for large keys. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1444 | 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] | 1445 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1446 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1447 | size_t bits = bits_arg; |
| 1448 | psa_status_t expected_status = expected_status_arg; |
| 1449 | psa_status_t status; |
| 1450 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1451 | 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] | 1452 | size_t buffer_size = /* Slight overapproximations */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1453 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1454 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1455 | unsigned char *p; |
| 1456 | int ret; |
| 1457 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1458 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1459 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1460 | PSA_ASSERT(psa_crypto_init()); |
| 1461 | ASSERT_ALLOC(buffer, buffer_size); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1462 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1463 | TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p, |
| 1464 | bits, keypair)) >= 0); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1465 | length = ret; |
| 1466 | |
| 1467 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1468 | psa_set_key_type(&attributes, type); |
| 1469 | status = psa_import_key(&attributes, p, length, &key); |
| 1470 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1471 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1472 | if (status == PSA_SUCCESS) { |
| 1473 | PSA_ASSERT(psa_destroy_key(key)); |
| 1474 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1475 | |
| 1476 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1477 | mbedtls_free(buffer); |
| 1478 | PSA_DONE(); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1479 | } |
| 1480 | /* END_CASE */ |
| 1481 | |
| 1482 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1483 | void import_export(data_t *data, |
| 1484 | int type_arg, |
| 1485 | int usage_arg, int alg_arg, |
| 1486 | int lifetime_arg, |
| 1487 | int expected_bits, |
| 1488 | int export_size_delta, |
| 1489 | int expected_export_status_arg, |
| 1490 | /*whether reexport must give the original input exactly*/ |
| 1491 | int canonical_input) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1492 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1493 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1494 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1495 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1496 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1497 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1498 | psa_key_lifetime_t lifetime = lifetime_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1499 | unsigned char *exported = NULL; |
| 1500 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1501 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1502 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1503 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1504 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1505 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame^] | 1506 | psa_status_t expected_import_result = PSA_SUCCESS; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1507 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1508 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1509 | ASSERT_ALLOC(exported, export_size); |
| 1510 | if (!canonical_input) { |
| 1511 | ASSERT_ALLOC(reexported, export_size); |
| 1512 | } |
| 1513 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1514 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1515 | psa_set_key_lifetime(&attributes, lifetime); |
| 1516 | psa_set_key_usage_flags(&attributes, usage_arg); |
| 1517 | psa_set_key_algorithm(&attributes, alg); |
| 1518 | psa_set_key_type(&attributes, type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1519 | |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame^] | 1520 | if (PSA_KEY_TYPE_IS_DH(type) && |
| 1521 | expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) { |
| 1522 | export_size -= 8; |
| 1523 | } |
| 1524 | |
| 1525 | if (PSA_KEY_TYPE_IS_DH(type) && |
| 1526 | (data->len != 256 && data->len != 384 && |
| 1527 | data->len != 512 && data->len != 768 && data->len != 1024)) { |
| 1528 | expected_import_result = PSA_ERROR_INVALID_ARGUMENT; |
| 1529 | } |
| 1530 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1531 | /* Import the key */ |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame^] | 1532 | TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key), |
| 1533 | expected_import_result); |
| 1534 | |
| 1535 | if (expected_import_result != PSA_SUCCESS) { |
| 1536 | goto exit; |
| 1537 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1538 | |
| 1539 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1540 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1541 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1542 | TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits); |
| 1543 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1544 | |
| 1545 | /* Export the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1546 | status = psa_export_key(key, exported, export_size, &exported_length); |
| 1547 | TEST_EQUAL(status, expected_export_status); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1548 | |
| 1549 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1550 | * and export_size. On errors, the exported length must be 0. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1551 | TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH); |
| 1552 | TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0); |
| 1553 | TEST_LE_U(exported_length, export_size); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1554 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1555 | TEST_ASSERT(mem_is_char(exported + exported_length, 0, |
| 1556 | export_size - exported_length)); |
| 1557 | if (status != PSA_SUCCESS) { |
| 1558 | TEST_EQUAL(exported_length, 0); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1559 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1560 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1561 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1562 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1563 | * this validates the canonical representations. For canonical inputs, |
| 1564 | * this doesn't directly validate the implementation, but it still helps |
| 1565 | * by cross-validating the test data with the sanity check code. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1566 | if (!psa_key_lifetime_is_external(lifetime)) { |
| 1567 | if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) { |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1568 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1569 | } |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1570 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1571 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1572 | if (canonical_input) { |
| 1573 | ASSERT_COMPARE(data->x, data->len, exported, exported_length); |
| 1574 | } else { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1575 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1576 | PSA_ASSERT(psa_import_key(&attributes, exported, exported_length, |
| 1577 | &key2)); |
| 1578 | PSA_ASSERT(psa_export_key(key2, |
| 1579 | reexported, |
| 1580 | export_size, |
| 1581 | &reexported_length)); |
| 1582 | ASSERT_COMPARE(exported, exported_length, |
| 1583 | reexported, reexported_length); |
| 1584 | PSA_ASSERT(psa_destroy_key(key2)); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1585 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1586 | TEST_LE_U(exported_length, |
| 1587 | PSA_EXPORT_KEY_OUTPUT_SIZE(type, |
| 1588 | psa_get_key_bits(&got_attributes))); |
| 1589 | TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1590 | |
| 1591 | destroy: |
| 1592 | /* Destroy the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1593 | PSA_ASSERT(psa_destroy_key(key)); |
| 1594 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1595 | |
| 1596 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1597 | /* |
| 1598 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1599 | * thus reset them as required. |
| 1600 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1601 | psa_reset_key_attributes(&got_attributes); |
| 1602 | psa_destroy_key(key); |
| 1603 | mbedtls_free(exported); |
| 1604 | mbedtls_free(reexported); |
| 1605 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1606 | } |
| 1607 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1608 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1609 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1610 | void import_export_public_key(data_t *data, |
| 1611 | int type_arg, // key pair or public key |
| 1612 | int alg_arg, |
| 1613 | int lifetime_arg, |
| 1614 | int export_size_delta, |
| 1615 | int expected_export_status_arg, |
| 1616 | data_t *expected_public_key) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1617 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1618 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1619 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1620 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1621 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1622 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1623 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1624 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1625 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1626 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1627 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1628 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1629 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1630 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1631 | psa_set_key_lifetime(&attributes, lifetime); |
| 1632 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1633 | psa_set_key_algorithm(&attributes, alg); |
| 1634 | psa_set_key_type(&attributes, type); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1635 | |
| 1636 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1637 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1638 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1639 | /* Export the public key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1640 | ASSERT_ALLOC(exported, export_size); |
| 1641 | status = psa_export_public_key(key, |
| 1642 | exported, export_size, |
| 1643 | &exported_length); |
| 1644 | TEST_EQUAL(status, expected_export_status); |
| 1645 | if (status == PSA_SUCCESS) { |
| 1646 | 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] | 1647 | size_t bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1648 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1649 | bits = psa_get_key_bits(&attributes); |
| 1650 | TEST_LE_U(expected_public_key->len, |
| 1651 | PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1652 | TEST_LE_U(expected_public_key->len, |
| 1653 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1654 | TEST_LE_U(expected_public_key->len, |
| 1655 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
| 1656 | ASSERT_COMPARE(expected_public_key->x, expected_public_key->len, |
| 1657 | exported, exported_length); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1658 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1659 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1660 | /* |
| 1661 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1662 | * thus reset them as required. |
| 1663 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1664 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1665 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1666 | mbedtls_free(exported); |
| 1667 | psa_destroy_key(key); |
| 1668 | PSA_DONE(); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1669 | } |
| 1670 | /* END_CASE */ |
| 1671 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1672 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1673 | void import_and_exercise_key(data_t *data, |
| 1674 | int type_arg, |
| 1675 | int bits_arg, |
| 1676 | int alg_arg) |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1677 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1678 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1679 | psa_key_type_t type = type_arg; |
| 1680 | size_t bits = bits_arg; |
| 1681 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1682 | 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] | 1683 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1684 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1685 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1686 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1687 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1688 | psa_set_key_usage_flags(&attributes, usage); |
| 1689 | psa_set_key_algorithm(&attributes, alg); |
| 1690 | psa_set_key_type(&attributes, type); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1691 | |
| 1692 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1693 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1694 | |
| 1695 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1696 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1697 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1698 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1699 | |
| 1700 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1701 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1702 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1703 | } |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1704 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1705 | PSA_ASSERT(psa_destroy_key(key)); |
| 1706 | test_operations_on_invalid_key(key); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1707 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1708 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1709 | /* |
| 1710 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1711 | * thus reset them as required. |
| 1712 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1713 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1714 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1715 | psa_reset_key_attributes(&attributes); |
| 1716 | psa_destroy_key(key); |
| 1717 | PSA_DONE(); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1718 | } |
| 1719 | /* END_CASE */ |
| 1720 | |
| 1721 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1722 | void effective_key_attributes(int type_arg, int expected_type_arg, |
| 1723 | int bits_arg, int expected_bits_arg, |
| 1724 | int usage_arg, int expected_usage_arg, |
| 1725 | int alg_arg, int expected_alg_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1726 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1727 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1728 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1729 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1730 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1731 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1732 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1733 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1734 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1735 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1736 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1737 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1738 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1739 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1740 | psa_set_key_usage_flags(&attributes, usage); |
| 1741 | psa_set_key_algorithm(&attributes, alg); |
| 1742 | psa_set_key_type(&attributes, key_type); |
| 1743 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1744 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1745 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
| 1746 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1747 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1748 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1749 | TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type); |
| 1750 | TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits); |
| 1751 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
| 1752 | TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1753 | |
| 1754 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1755 | /* |
| 1756 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1757 | * thus reset them as required. |
| 1758 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1759 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1760 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1761 | psa_destroy_key(key); |
| 1762 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1763 | } |
| 1764 | /* END_CASE */ |
| 1765 | |
| 1766 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1767 | void check_key_policy(int type_arg, int bits_arg, |
| 1768 | int usage_arg, int alg_arg) |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1769 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1770 | test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg, |
| 1771 | usage_arg, |
| 1772 | mbedtls_test_update_key_usage_flags(usage_arg), |
| 1773 | alg_arg, alg_arg); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1774 | goto exit; |
| 1775 | } |
| 1776 | /* END_CASE */ |
| 1777 | |
| 1778 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1779 | void key_attributes_init() |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1780 | { |
| 1781 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1782 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1783 | * 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] | 1784 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1785 | psa_key_attributes_t func = psa_key_attributes_init(); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1786 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1787 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1788 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1789 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1790 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1791 | TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE); |
| 1792 | TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE); |
| 1793 | TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1794 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1795 | TEST_EQUAL(psa_get_key_type(&func), 0); |
| 1796 | TEST_EQUAL(psa_get_key_type(&init), 0); |
| 1797 | TEST_EQUAL(psa_get_key_type(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1798 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1799 | TEST_EQUAL(psa_get_key_bits(&func), 0); |
| 1800 | TEST_EQUAL(psa_get_key_bits(&init), 0); |
| 1801 | TEST_EQUAL(psa_get_key_bits(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1802 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1803 | TEST_EQUAL(psa_get_key_usage_flags(&func), 0); |
| 1804 | TEST_EQUAL(psa_get_key_usage_flags(&init), 0); |
| 1805 | TEST_EQUAL(psa_get_key_usage_flags(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1806 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1807 | TEST_EQUAL(psa_get_key_algorithm(&func), 0); |
| 1808 | TEST_EQUAL(psa_get_key_algorithm(&init), 0); |
| 1809 | TEST_EQUAL(psa_get_key_algorithm(&zero), 0); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1810 | } |
| 1811 | /* END_CASE */ |
| 1812 | |
| 1813 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1814 | void mac_key_policy(int policy_usage_arg, |
| 1815 | int policy_alg_arg, |
| 1816 | int key_type_arg, |
| 1817 | data_t *key_data, |
| 1818 | int exercise_alg_arg, |
| 1819 | int expected_status_sign_arg, |
| 1820 | int expected_status_verify_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1821 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1822 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1823 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1824 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1825 | psa_key_type_t key_type = key_type_arg; |
| 1826 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 1827 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 1828 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1829 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1830 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 1831 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1832 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1833 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1834 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1835 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1836 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1837 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1838 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1839 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1840 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1841 | &key)); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1842 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1843 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 1844 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1845 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1846 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 1847 | TEST_EQUAL(status, expected_status_sign); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1848 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1849 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1850 | uint8_t input[128] = { 0 }; |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1851 | size_t mac_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1852 | TEST_EQUAL(psa_mac_compute(key, exercise_alg, |
| 1853 | input, 128, |
| 1854 | mac, PSA_MAC_MAX_SIZE, &mac_len), |
| 1855 | expected_status_sign); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1856 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1857 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1858 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 1859 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 1860 | if (status == PSA_SUCCESS) { |
| 1861 | status = psa_mac_update(&operation, input, 128); |
| 1862 | if (status == PSA_SUCCESS) { |
| 1863 | TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE, |
| 1864 | &mac_len), |
| 1865 | expected_status_sign); |
| 1866 | } else { |
| 1867 | TEST_EQUAL(status, expected_status_sign); |
| 1868 | } |
| 1869 | } else { |
| 1870 | TEST_EQUAL(status, expected_status_sign); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1871 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1872 | PSA_ASSERT(psa_mac_abort(&operation)); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1873 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1874 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1875 | status = psa_mac_verify(key, exercise_alg, input, 128, |
| 1876 | mac, mac_len); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1877 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1878 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 1879 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1880 | } else { |
| 1881 | TEST_EQUAL(status, expected_status_verify); |
| 1882 | } |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1883 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1884 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1885 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 1886 | if (status == PSA_SUCCESS) { |
| 1887 | status = psa_mac_update(&operation, input, 128); |
| 1888 | if (status == PSA_SUCCESS) { |
| 1889 | status = psa_mac_verify_finish(&operation, mac, mac_len); |
| 1890 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 1891 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1892 | } else { |
| 1893 | TEST_EQUAL(status, expected_status_verify); |
| 1894 | } |
| 1895 | } else { |
| 1896 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1897 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1898 | } else { |
| 1899 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1900 | } |
| 1901 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1902 | psa_mac_abort(&operation); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1903 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1904 | memset(mac, 0, sizeof(mac)); |
| 1905 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 1906 | TEST_EQUAL(status, expected_status_verify); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1907 | |
| 1908 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1909 | psa_mac_abort(&operation); |
| 1910 | psa_destroy_key(key); |
| 1911 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1912 | } |
| 1913 | /* END_CASE */ |
| 1914 | |
| 1915 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1916 | void cipher_key_policy(int policy_usage_arg, |
| 1917 | int policy_alg, |
| 1918 | int key_type, |
| 1919 | data_t *key_data, |
| 1920 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1921 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1922 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1923 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1924 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1925 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1926 | size_t output_buffer_size = 0; |
| 1927 | size_t input_buffer_size = 0; |
| 1928 | size_t output_length = 0; |
| 1929 | uint8_t *output = NULL; |
| 1930 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1931 | psa_status_t status; |
| 1932 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1933 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg); |
| 1934 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg, |
| 1935 | input_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1936 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1937 | ASSERT_ALLOC(input, input_buffer_size); |
| 1938 | ASSERT_ALLOC(output, output_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1939 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1940 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1941 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1942 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1943 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1944 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1945 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1946 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1947 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1948 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1949 | /* Check if no key usage flag implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1950 | TEST_EQUAL(policy_usage, |
| 1951 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1952 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1953 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1954 | status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size, |
| 1955 | output, output_buffer_size, |
| 1956 | &output_length); |
| 1957 | if (policy_alg == exercise_alg && |
| 1958 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1959 | PSA_ASSERT(status); |
| 1960 | } else { |
| 1961 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1962 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1963 | |
| 1964 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1965 | status = psa_cipher_encrypt_setup(&operation, key, exercise_alg); |
| 1966 | if (policy_alg == exercise_alg && |
| 1967 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1968 | PSA_ASSERT(status); |
| 1969 | } else { |
| 1970 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1971 | } |
| 1972 | psa_cipher_abort(&operation); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1973 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1974 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1975 | status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size, |
| 1976 | input, input_buffer_size, |
| 1977 | &output_length); |
| 1978 | if (policy_alg == exercise_alg && |
| 1979 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 1980 | PSA_ASSERT(status); |
| 1981 | } else { |
| 1982 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1983 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1984 | |
| 1985 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1986 | status = psa_cipher_decrypt_setup(&operation, key, exercise_alg); |
| 1987 | if (policy_alg == exercise_alg && |
| 1988 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 1989 | PSA_ASSERT(status); |
| 1990 | } else { |
| 1991 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1992 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1993 | |
| 1994 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1995 | psa_cipher_abort(&operation); |
| 1996 | mbedtls_free(input); |
| 1997 | mbedtls_free(output); |
| 1998 | psa_destroy_key(key); |
| 1999 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2000 | } |
| 2001 | /* END_CASE */ |
| 2002 | |
| 2003 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2004 | void aead_key_policy(int policy_usage_arg, |
| 2005 | int policy_alg, |
| 2006 | int key_type, |
| 2007 | data_t *key_data, |
| 2008 | int nonce_length_arg, |
| 2009 | int tag_length_arg, |
| 2010 | int exercise_alg, |
| 2011 | int expected_status_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2012 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2013 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2014 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2015 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2016 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2017 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2018 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2019 | unsigned char nonce[16] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2020 | size_t nonce_length = nonce_length_arg; |
| 2021 | unsigned char tag[16]; |
| 2022 | size_t tag_length = tag_length_arg; |
| 2023 | size_t output_length; |
| 2024 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2025 | TEST_LE_U(nonce_length, sizeof(nonce)); |
| 2026 | TEST_LE_U(tag_length, sizeof(tag)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2027 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2028 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2029 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2030 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2031 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2032 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2033 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2034 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2035 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2036 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2037 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2038 | TEST_EQUAL(policy_usage, |
| 2039 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2040 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2041 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2042 | status = psa_aead_encrypt(key, exercise_alg, |
| 2043 | nonce, nonce_length, |
| 2044 | NULL, 0, |
| 2045 | NULL, 0, |
| 2046 | tag, tag_length, |
| 2047 | &output_length); |
| 2048 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2049 | TEST_EQUAL(status, expected_status); |
| 2050 | } else { |
| 2051 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2052 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2053 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2054 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2055 | status = psa_aead_encrypt_setup(&operation, key, exercise_alg); |
| 2056 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2057 | TEST_EQUAL(status, expected_status); |
| 2058 | } else { |
| 2059 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2060 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2061 | |
| 2062 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2063 | memset(tag, 0, sizeof(tag)); |
| 2064 | status = psa_aead_decrypt(key, exercise_alg, |
| 2065 | nonce, nonce_length, |
| 2066 | NULL, 0, |
| 2067 | tag, tag_length, |
| 2068 | NULL, 0, |
| 2069 | &output_length); |
| 2070 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2071 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2072 | } else if (expected_status == PSA_SUCCESS) { |
| 2073 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2074 | } else { |
| 2075 | TEST_EQUAL(status, expected_status); |
| 2076 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2077 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2078 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2079 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2080 | status = psa_aead_decrypt_setup(&operation, key, exercise_alg); |
| 2081 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2082 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2083 | } else { |
| 2084 | TEST_EQUAL(status, expected_status); |
| 2085 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2086 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2087 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2088 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2089 | psa_destroy_key(key); |
| 2090 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2091 | } |
| 2092 | /* END_CASE */ |
| 2093 | |
| 2094 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2095 | void asymmetric_encryption_key_policy(int policy_usage_arg, |
| 2096 | int policy_alg, |
| 2097 | int key_type, |
| 2098 | data_t *key_data, |
| 2099 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2100 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2101 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2102 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2103 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2104 | psa_status_t status; |
| 2105 | size_t key_bits; |
| 2106 | size_t buffer_length; |
| 2107 | unsigned char *buffer = NULL; |
| 2108 | size_t output_length; |
| 2109 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2110 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2111 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2112 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2113 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2114 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2115 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2116 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2117 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2118 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2119 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2120 | TEST_EQUAL(policy_usage, |
| 2121 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2122 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2123 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 2124 | key_bits = psa_get_key_bits(&attributes); |
| 2125 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, |
| 2126 | exercise_alg); |
| 2127 | ASSERT_ALLOC(buffer, buffer_length); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2128 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2129 | status = psa_asymmetric_encrypt(key, exercise_alg, |
| 2130 | NULL, 0, |
| 2131 | NULL, 0, |
| 2132 | buffer, buffer_length, |
| 2133 | &output_length); |
| 2134 | if (policy_alg == exercise_alg && |
| 2135 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2136 | PSA_ASSERT(status); |
| 2137 | } else { |
| 2138 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2139 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2140 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2141 | if (buffer_length != 0) { |
| 2142 | memset(buffer, 0, buffer_length); |
| 2143 | } |
| 2144 | status = psa_asymmetric_decrypt(key, exercise_alg, |
| 2145 | buffer, buffer_length, |
| 2146 | NULL, 0, |
| 2147 | buffer, buffer_length, |
| 2148 | &output_length); |
| 2149 | if (policy_alg == exercise_alg && |
| 2150 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2151 | TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING); |
| 2152 | } else { |
| 2153 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2154 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2155 | |
| 2156 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2157 | /* |
| 2158 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2159 | * thus reset them as required. |
| 2160 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2161 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2162 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2163 | psa_destroy_key(key); |
| 2164 | PSA_DONE(); |
| 2165 | mbedtls_free(buffer); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2166 | } |
| 2167 | /* END_CASE */ |
| 2168 | |
| 2169 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2170 | void asymmetric_signature_key_policy(int policy_usage_arg, |
| 2171 | int policy_alg, |
| 2172 | int key_type, |
| 2173 | data_t *key_data, |
| 2174 | int exercise_alg, |
| 2175 | int payload_length_arg, |
| 2176 | int expected_usage_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2177 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2178 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2179 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2180 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 2181 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2182 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2183 | unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 }; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2184 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2185 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2186 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2187 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2188 | int compatible_alg = payload_length_arg > 0; |
| 2189 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2190 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2191 | size_t signature_length; |
| 2192 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2193 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2194 | in the expected usage flags. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2195 | TEST_EQUAL(expected_usage, |
| 2196 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2197 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2198 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2199 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2200 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2201 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2202 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2203 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2204 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2205 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2206 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2207 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_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 | status = psa_sign_hash(key, exercise_alg, |
| 2210 | payload, payload_length, |
| 2211 | signature, sizeof(signature), |
| 2212 | &signature_length); |
| 2213 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) { |
| 2214 | PSA_ASSERT(status); |
| 2215 | } else { |
| 2216 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2217 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2218 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2219 | memset(signature, 0, sizeof(signature)); |
| 2220 | status = psa_verify_hash(key, exercise_alg, |
| 2221 | payload, payload_length, |
| 2222 | signature, sizeof(signature)); |
| 2223 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) { |
| 2224 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2225 | } else { |
| 2226 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2227 | } |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2228 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2229 | if (PSA_ALG_IS_SIGN_HASH(exercise_alg) && |
| 2230 | PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) { |
| 2231 | status = psa_sign_message(key, exercise_alg, |
| 2232 | payload, payload_length, |
| 2233 | signature, sizeof(signature), |
| 2234 | &signature_length); |
| 2235 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) { |
| 2236 | PSA_ASSERT(status); |
| 2237 | } else { |
| 2238 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2239 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2240 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2241 | memset(signature, 0, sizeof(signature)); |
| 2242 | status = psa_verify_message(key, exercise_alg, |
| 2243 | payload, payload_length, |
| 2244 | signature, sizeof(signature)); |
| 2245 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) { |
| 2246 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2247 | } else { |
| 2248 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2249 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2250 | } |
| 2251 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2252 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2253 | psa_destroy_key(key); |
| 2254 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2255 | } |
| 2256 | /* END_CASE */ |
| 2257 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2258 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2259 | void derive_key_policy(int policy_usage, |
| 2260 | int policy_alg, |
| 2261 | int key_type, |
| 2262 | data_t *key_data, |
| 2263 | int exercise_alg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2264 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2265 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2266 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2267 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2268 | psa_status_t status; |
| 2269 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2270 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2271 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2272 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2273 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2274 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2275 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2276 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2277 | &key)); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2278 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2279 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2280 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2281 | if (PSA_ALG_IS_TLS12_PRF(exercise_alg) || |
| 2282 | PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) { |
| 2283 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 2284 | &operation, |
| 2285 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2286 | (const uint8_t *) "", 0)); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2287 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2288 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2289 | status = psa_key_derivation_input_key(&operation, |
| 2290 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 2291 | key); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2292 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2293 | if (policy_alg == exercise_alg && |
| 2294 | (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) { |
| 2295 | PSA_ASSERT(status); |
| 2296 | } else { |
| 2297 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2298 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2299 | |
| 2300 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2301 | psa_key_derivation_abort(&operation); |
| 2302 | psa_destroy_key(key); |
| 2303 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2304 | } |
| 2305 | /* END_CASE */ |
| 2306 | |
| 2307 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2308 | void agreement_key_policy(int policy_usage, |
| 2309 | int policy_alg, |
| 2310 | int key_type_arg, |
| 2311 | data_t *key_data, |
| 2312 | int exercise_alg, |
| 2313 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2314 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2315 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2316 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2317 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2318 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2319 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2320 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2321 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2322 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2323 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2324 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2325 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2326 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2327 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2328 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2329 | &key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2330 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2331 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
| 2332 | status = mbedtls_test_psa_key_agreement_with_self(&operation, key); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2333 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2334 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2335 | |
| 2336 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2337 | psa_key_derivation_abort(&operation); |
| 2338 | psa_destroy_key(key); |
| 2339 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2340 | } |
| 2341 | /* END_CASE */ |
| 2342 | |
| 2343 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2344 | void key_policy_alg2(int key_type_arg, data_t *key_data, |
| 2345 | int usage_arg, int alg_arg, int alg2_arg) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2346 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2347 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2348 | psa_key_type_t key_type = key_type_arg; |
| 2349 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2350 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2351 | psa_key_usage_t usage = usage_arg; |
| 2352 | psa_algorithm_t alg = alg_arg; |
| 2353 | psa_algorithm_t alg2 = alg2_arg; |
| 2354 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2355 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2356 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2357 | psa_set_key_usage_flags(&attributes, usage); |
| 2358 | psa_set_key_algorithm(&attributes, alg); |
| 2359 | psa_set_key_enrollment_algorithm(&attributes, alg2); |
| 2360 | psa_set_key_type(&attributes, key_type); |
| 2361 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2362 | &key)); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2363 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2364 | /* Update the usage flags to obtain implicit usage flags */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2365 | usage = mbedtls_test_update_key_usage_flags(usage); |
| 2366 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 2367 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage); |
| 2368 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 2369 | TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2371 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2372 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2373 | } |
| 2374 | if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2375 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2376 | } |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2377 | |
| 2378 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2379 | /* |
| 2380 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2381 | * thus reset them as required. |
| 2382 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2383 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2385 | psa_destroy_key(key); |
| 2386 | PSA_DONE(); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2387 | } |
| 2388 | /* END_CASE */ |
| 2389 | |
| 2390 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2391 | void raw_agreement_key_policy(int policy_usage, |
| 2392 | int policy_alg, |
| 2393 | int key_type_arg, |
| 2394 | data_t *key_data, |
| 2395 | int exercise_alg, |
| 2396 | int expected_status_arg) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2397 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2398 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2399 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2400 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2401 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2402 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2403 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2404 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2405 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2406 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2407 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2408 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2409 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2410 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2411 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2412 | &key)); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2413 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2414 | status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2415 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2416 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2417 | |
| 2418 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2419 | psa_key_derivation_abort(&operation); |
| 2420 | psa_destroy_key(key); |
| 2421 | PSA_DONE(); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2422 | } |
| 2423 | /* END_CASE */ |
| 2424 | |
| 2425 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2426 | void copy_success(int source_usage_arg, |
| 2427 | int source_alg_arg, int source_alg2_arg, |
| 2428 | unsigned int source_lifetime_arg, |
| 2429 | int type_arg, data_t *material, |
| 2430 | int copy_attributes, |
| 2431 | int target_usage_arg, |
| 2432 | int target_alg_arg, int target_alg2_arg, |
| 2433 | unsigned int target_lifetime_arg, |
| 2434 | int expected_usage_arg, |
| 2435 | int expected_alg_arg, int expected_alg2_arg) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2436 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2437 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2438 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2439 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2440 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2441 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2442 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 2443 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2444 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2445 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2446 | uint8_t *export_buffer = NULL; |
| 2447 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2448 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2449 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2450 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2451 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2452 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2453 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2454 | psa_set_key_type(&source_attributes, type_arg); |
| 2455 | psa_set_key_lifetime(&source_attributes, source_lifetime); |
| 2456 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2457 | material->x, material->len, |
| 2458 | &source_key)); |
| 2459 | PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes)); |
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 target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2462 | if (copy_attributes) { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2463 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2464 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2465 | psa_set_key_lifetime(&target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2466 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2467 | if (target_usage_arg != -1) { |
| 2468 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2469 | } |
| 2470 | if (target_alg_arg != -1) { |
| 2471 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2472 | } |
| 2473 | if (target_alg2_arg != -1) { |
| 2474 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
| 2475 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2476 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2477 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2478 | /* Copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2479 | PSA_ASSERT(psa_copy_key(source_key, |
| 2480 | &target_attributes, &target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2481 | |
| 2482 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2483 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2484 | |
| 2485 | /* Test that the target slot has the expected content and policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2486 | PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes)); |
| 2487 | TEST_EQUAL(psa_get_key_type(&source_attributes), |
| 2488 | psa_get_key_type(&target_attributes)); |
| 2489 | TEST_EQUAL(psa_get_key_bits(&source_attributes), |
| 2490 | psa_get_key_bits(&target_attributes)); |
| 2491 | TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes)); |
| 2492 | TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes)); |
| 2493 | TEST_EQUAL(expected_alg2, |
| 2494 | psa_get_key_enrollment_algorithm(&target_attributes)); |
| 2495 | if (expected_usage & PSA_KEY_USAGE_EXPORT) { |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2496 | size_t length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2497 | ASSERT_ALLOC(export_buffer, material->len); |
| 2498 | PSA_ASSERT(psa_export_key(target_key, export_buffer, |
| 2499 | material->len, &length)); |
| 2500 | ASSERT_COMPARE(material->x, material->len, |
| 2501 | export_buffer, length); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2502 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2503 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2504 | if (!psa_key_lifetime_is_external(target_lifetime)) { |
| 2505 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2506 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2507 | } |
| 2508 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2509 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2510 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2511 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2512 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2513 | PSA_ASSERT(psa_destroy_key(target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2514 | |
| 2515 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2516 | /* |
| 2517 | * Source and target key attributes may have been returned by |
| 2518 | * psa_get_key_attributes() thus reset them as required. |
| 2519 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2520 | psa_reset_key_attributes(&source_attributes); |
| 2521 | psa_reset_key_attributes(&target_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2522 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2523 | PSA_DONE(); |
| 2524 | mbedtls_free(export_buffer); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2525 | } |
| 2526 | /* END_CASE */ |
| 2527 | |
| 2528 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2529 | void copy_fail(int source_usage_arg, |
| 2530 | int source_alg_arg, int source_alg2_arg, |
| 2531 | int source_lifetime_arg, |
| 2532 | int type_arg, data_t *material, |
| 2533 | int target_type_arg, int target_bits_arg, |
| 2534 | int target_usage_arg, |
| 2535 | int target_alg_arg, int target_alg2_arg, |
| 2536 | int target_id_arg, int target_lifetime_arg, |
| 2537 | int expected_status_arg) |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2538 | { |
| 2539 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2540 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2541 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2542 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2543 | 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] | 2544 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2545 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2546 | |
| 2547 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2548 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2549 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2550 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2551 | psa_set_key_type(&source_attributes, type_arg); |
| 2552 | psa_set_key_lifetime(&source_attributes, source_lifetime_arg); |
| 2553 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2554 | material->x, material->len, |
| 2555 | &source_key)); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2556 | |
| 2557 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2558 | psa_set_key_id(&target_attributes, key_id); |
| 2559 | psa_set_key_lifetime(&target_attributes, target_lifetime_arg); |
| 2560 | psa_set_key_type(&target_attributes, target_type_arg); |
| 2561 | psa_set_key_bits(&target_attributes, target_bits_arg); |
| 2562 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2563 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2564 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2565 | |
| 2566 | /* Try to copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2567 | TEST_EQUAL(psa_copy_key(source_key, |
| 2568 | &target_attributes, &target_key), |
| 2569 | expected_status_arg); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2570 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2571 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2572 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2573 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2574 | psa_reset_key_attributes(&source_attributes); |
| 2575 | psa_reset_key_attributes(&target_attributes); |
| 2576 | PSA_DONE(); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2577 | } |
| 2578 | /* END_CASE */ |
| 2579 | |
| 2580 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2581 | void hash_operation_init() |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2582 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2583 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2584 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2585 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2586 | * 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] | 2587 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2588 | psa_hash_operation_t func = psa_hash_operation_init(); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2589 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2590 | psa_hash_operation_t zero; |
| 2591 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2592 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2593 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2594 | /* A freshly-initialized hash operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2595 | TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)), |
| 2596 | PSA_ERROR_BAD_STATE); |
| 2597 | TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)), |
| 2598 | PSA_ERROR_BAD_STATE); |
| 2599 | TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)), |
| 2600 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2601 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2602 | /* A default hash operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2603 | PSA_ASSERT(psa_hash_abort(&func)); |
| 2604 | PSA_ASSERT(psa_hash_abort(&init)); |
| 2605 | PSA_ASSERT(psa_hash_abort(&zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2606 | } |
| 2607 | /* END_CASE */ |
| 2608 | |
| 2609 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2610 | void hash_setup(int alg_arg, |
| 2611 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2612 | { |
| 2613 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2614 | uint8_t *output = NULL; |
| 2615 | size_t output_size = 0; |
| 2616 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2617 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2618 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2619 | psa_status_t status; |
| 2620 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2621 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2622 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2623 | /* Hash Setup, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2624 | output_size = PSA_HASH_LENGTH(alg); |
| 2625 | ASSERT_ALLOC(output, output_size); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2626 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2627 | status = psa_hash_compute(alg, NULL, 0, |
| 2628 | output, output_size, &output_length); |
| 2629 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2630 | |
| 2631 | /* Hash Setup, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2632 | status = psa_hash_setup(&operation, alg); |
| 2633 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2634 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2635 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2636 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2637 | |
| 2638 | /* If setup failed, reproduce the failure, so as to |
| 2639 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2640 | if (status != PSA_SUCCESS) { |
| 2641 | TEST_EQUAL(psa_hash_setup(&operation, alg), status); |
| 2642 | } |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2643 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2644 | /* Now the operation object should be reusable. */ |
| 2645 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2646 | PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG)); |
| 2647 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2648 | #endif |
| 2649 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2650 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2651 | mbedtls_free(output); |
| 2652 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2653 | } |
| 2654 | /* END_CASE */ |
| 2655 | |
| 2656 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2657 | void hash_compute_fail(int alg_arg, data_t *input, |
| 2658 | int output_size_arg, int expected_status_arg) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2659 | { |
| 2660 | psa_algorithm_t alg = alg_arg; |
| 2661 | uint8_t *output = NULL; |
| 2662 | size_t output_size = output_size_arg; |
| 2663 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2664 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2665 | psa_status_t expected_status = expected_status_arg; |
| 2666 | psa_status_t status; |
| 2667 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2668 | ASSERT_ALLOC(output, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2669 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2670 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2671 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2672 | /* Hash Compute, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2673 | status = psa_hash_compute(alg, input->x, input->len, |
| 2674 | output, output_size, &output_length); |
| 2675 | TEST_EQUAL(status, expected_status); |
| 2676 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2677 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2678 | /* Hash Compute, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2679 | status = psa_hash_setup(&operation, alg); |
| 2680 | if (status == PSA_SUCCESS) { |
| 2681 | status = psa_hash_update(&operation, input->x, input->len); |
| 2682 | if (status == PSA_SUCCESS) { |
| 2683 | status = psa_hash_finish(&operation, output, output_size, |
| 2684 | &output_length); |
| 2685 | if (status == PSA_SUCCESS) { |
| 2686 | TEST_LE_U(output_length, output_size); |
| 2687 | } else { |
| 2688 | TEST_EQUAL(status, expected_status); |
| 2689 | } |
| 2690 | } else { |
| 2691 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2692 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2693 | } else { |
| 2694 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2695 | } |
| 2696 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2697 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2698 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2699 | mbedtls_free(output); |
| 2700 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2701 | } |
| 2702 | /* END_CASE */ |
| 2703 | |
| 2704 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2705 | void hash_compare_fail(int alg_arg, data_t *input, |
| 2706 | data_t *reference_hash, |
| 2707 | int expected_status_arg) |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2708 | { |
| 2709 | psa_algorithm_t alg = alg_arg; |
| 2710 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2711 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2712 | psa_status_t status; |
| 2713 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2714 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2715 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2716 | /* Hash Compare, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2717 | status = psa_hash_compare(alg, input->x, input->len, |
| 2718 | reference_hash->x, reference_hash->len); |
| 2719 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2720 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2721 | /* Hash Compare, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2722 | status = psa_hash_setup(&operation, alg); |
| 2723 | if (status == PSA_SUCCESS) { |
| 2724 | status = psa_hash_update(&operation, input->x, input->len); |
| 2725 | if (status == PSA_SUCCESS) { |
| 2726 | status = psa_hash_verify(&operation, reference_hash->x, |
| 2727 | reference_hash->len); |
| 2728 | TEST_EQUAL(status, expected_status); |
| 2729 | } else { |
| 2730 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2731 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2732 | } else { |
| 2733 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2734 | } |
| 2735 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2736 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2737 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2738 | PSA_DONE(); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2739 | } |
| 2740 | /* END_CASE */ |
| 2741 | |
| 2742 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2743 | void hash_compute_compare(int alg_arg, data_t *input, |
| 2744 | data_t *expected_output) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2745 | { |
| 2746 | psa_algorithm_t alg = alg_arg; |
| 2747 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2748 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2749 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2750 | size_t i; |
| 2751 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2752 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2753 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2754 | /* Compute with tight buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2755 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 2756 | output, PSA_HASH_LENGTH(alg), |
| 2757 | &output_length)); |
| 2758 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2759 | ASSERT_COMPARE(output, output_length, |
| 2760 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2761 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2762 | /* Compute with tight buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2763 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2764 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2765 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 2766 | PSA_HASH_LENGTH(alg), |
| 2767 | &output_length)); |
| 2768 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2769 | ASSERT_COMPARE(output, output_length, |
| 2770 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2771 | |
| 2772 | /* Compute with larger buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2773 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 2774 | output, sizeof(output), |
| 2775 | &output_length)); |
| 2776 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2777 | ASSERT_COMPARE(output, output_length, |
| 2778 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2779 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2780 | /* Compute with larger buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2781 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2782 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2783 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 2784 | sizeof(output), &output_length)); |
| 2785 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2786 | ASSERT_COMPARE(output, output_length, |
| 2787 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2788 | |
| 2789 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2790 | PSA_ASSERT(psa_hash_compare(alg, input->x, input->len, |
| 2791 | output, output_length)); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2792 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2793 | /* Compare with correct hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2794 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2795 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2796 | PSA_ASSERT(psa_hash_verify(&operation, output, |
| 2797 | output_length)); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2798 | |
| 2799 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2800 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2801 | output, output_length + 1), |
| 2802 | PSA_ERROR_INVALID_SIGNATURE); |
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 trailing garbage, 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 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1), |
| 2808 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2809 | |
| 2810 | /* Compare with truncated hash, 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 truncated hash, 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 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2821 | /* Compare with corrupted value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2822 | for (i = 0; i < output_length; i++) { |
| 2823 | mbedtls_test_set_step(i); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2824 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2825 | |
| 2826 | /* One-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2827 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2828 | output, output_length), |
| 2829 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2830 | |
| 2831 | /* Multi-Part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2832 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2833 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2834 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length), |
| 2835 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2836 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2837 | output[i] ^= 1; |
| 2838 | } |
| 2839 | |
| 2840 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2841 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2842 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2843 | } |
| 2844 | /* END_CASE */ |
| 2845 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2846 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2847 | void hash_bad_order() |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2848 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2849 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2850 | unsigned char input[] = ""; |
| 2851 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2852 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2853 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2854 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2855 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 |
| 2856 | }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2857 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2858 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2859 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2860 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2861 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2862 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2863 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2864 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2865 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2866 | TEST_EQUAL(psa_hash_setup(&operation, alg), |
| 2867 | PSA_ERROR_BAD_STATE); |
| 2868 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2869 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2870 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2871 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2872 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2873 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2874 | PSA_ERROR_BAD_STATE); |
| 2875 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2876 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2877 | /* Check that update calls abort on error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2878 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 2879 | operation.id = UINT_MAX; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2880 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2881 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2882 | PSA_ERROR_BAD_STATE); |
| 2883 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2884 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2885 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2886 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2887 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2888 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2889 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2890 | hash, sizeof(hash), &hash_len)); |
| 2891 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2892 | PSA_ERROR_BAD_STATE); |
| 2893 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2894 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2895 | /* Call verify without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2896 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2897 | valid_hash, sizeof(valid_hash)), |
| 2898 | PSA_ERROR_BAD_STATE); |
| 2899 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2900 | |
| 2901 | /* Call verify after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2902 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2903 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2904 | hash, sizeof(hash), &hash_len)); |
| 2905 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2906 | valid_hash, sizeof(valid_hash)), |
| 2907 | PSA_ERROR_BAD_STATE); |
| 2908 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2909 | |
| 2910 | /* Call verify twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2911 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2912 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2913 | PSA_ASSERT(psa_hash_verify(&operation, |
| 2914 | valid_hash, sizeof(valid_hash))); |
| 2915 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2916 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2917 | valid_hash, sizeof(valid_hash)), |
| 2918 | PSA_ERROR_BAD_STATE); |
| 2919 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2920 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2921 | |
| 2922 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2923 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2924 | hash, sizeof(hash), &hash_len), |
| 2925 | PSA_ERROR_BAD_STATE); |
| 2926 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2927 | |
| 2928 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2929 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2930 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2931 | hash, sizeof(hash), &hash_len)); |
| 2932 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2933 | hash, sizeof(hash), &hash_len), |
| 2934 | PSA_ERROR_BAD_STATE); |
| 2935 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2936 | |
| 2937 | /* Call finish after calling verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2938 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2939 | PSA_ASSERT(psa_hash_verify(&operation, |
| 2940 | valid_hash, sizeof(valid_hash))); |
| 2941 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2942 | hash, sizeof(hash), &hash_len), |
| 2943 | PSA_ERROR_BAD_STATE); |
| 2944 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2945 | |
| 2946 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2947 | PSA_DONE(); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2948 | } |
| 2949 | /* END_CASE */ |
| 2950 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2951 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2952 | void hash_verify_bad_args() |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2953 | { |
| 2954 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2955 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2956 | * appended to it */ |
| 2957 | unsigned char hash[] = { |
| 2958 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2959 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2960 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb |
| 2961 | }; |
| 2962 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2963 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2964 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2965 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2966 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2967 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2968 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2969 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2970 | TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1), |
| 2971 | PSA_ERROR_INVALID_SIGNATURE); |
| 2972 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2973 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2974 | ASSERT_OPERATION_IS_INACTIVE(operation); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2975 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2976 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2977 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2978 | TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size), |
| 2979 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2980 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2981 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2982 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2983 | TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)), |
| 2984 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2985 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2986 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2987 | PSA_DONE(); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2988 | } |
| 2989 | /* END_CASE */ |
| 2990 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2991 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2992 | void hash_finish_bad_args() |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2993 | { |
| 2994 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2995 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2996 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2997 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2998 | size_t hash_len; |
| 2999 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3000 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3001 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3002 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3003 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3004 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3005 | hash, expected_size - 1, &hash_len), |
| 3006 | PSA_ERROR_BUFFER_TOO_SMALL); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3007 | |
| 3008 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3009 | PSA_DONE(); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3010 | } |
| 3011 | /* END_CASE */ |
| 3012 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3013 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3014 | void hash_clone_source_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3015 | { |
| 3016 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3017 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3018 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 3019 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3020 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3021 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3022 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3023 | size_t hash_len; |
| 3024 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3025 | PSA_ASSERT(psa_crypto_init()); |
| 3026 | PSA_ASSERT(psa_hash_setup(&op_source, alg)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3027 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3028 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3029 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3030 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3031 | hash, sizeof(hash), &hash_len)); |
| 3032 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3033 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3035 | TEST_EQUAL(psa_hash_clone(&op_source, &op_setup), |
| 3036 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3037 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3038 | PSA_ASSERT(psa_hash_clone(&op_source, &op_init)); |
| 3039 | PSA_ASSERT(psa_hash_finish(&op_init, |
| 3040 | hash, sizeof(hash), &hash_len)); |
| 3041 | PSA_ASSERT(psa_hash_clone(&op_source, &op_finished)); |
| 3042 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3043 | hash, sizeof(hash), &hash_len)); |
| 3044 | PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted)); |
| 3045 | PSA_ASSERT(psa_hash_finish(&op_aborted, |
| 3046 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3047 | |
| 3048 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3049 | psa_hash_abort(&op_source); |
| 3050 | psa_hash_abort(&op_init); |
| 3051 | psa_hash_abort(&op_setup); |
| 3052 | psa_hash_abort(&op_finished); |
| 3053 | psa_hash_abort(&op_aborted); |
| 3054 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3055 | } |
| 3056 | /* END_CASE */ |
| 3057 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3058 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3059 | void hash_clone_target_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3060 | { |
| 3061 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3062 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3063 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3064 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3065 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3066 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3067 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 3068 | size_t hash_len; |
| 3069 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3070 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3071 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3072 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3073 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3074 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3075 | hash, sizeof(hash), &hash_len)); |
| 3076 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3077 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3078 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3079 | PSA_ASSERT(psa_hash_clone(&op_setup, &op_target)); |
| 3080 | PSA_ASSERT(psa_hash_finish(&op_target, |
| 3081 | hash, sizeof(hash), &hash_len)); |
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 | TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE); |
| 3084 | TEST_EQUAL(psa_hash_clone(&op_finished, &op_target), |
| 3085 | PSA_ERROR_BAD_STATE); |
| 3086 | TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target), |
| 3087 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3088 | |
| 3089 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3090 | psa_hash_abort(&op_target); |
| 3091 | psa_hash_abort(&op_init); |
| 3092 | psa_hash_abort(&op_setup); |
| 3093 | psa_hash_abort(&op_finished); |
| 3094 | psa_hash_abort(&op_aborted); |
| 3095 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3096 | } |
| 3097 | /* END_CASE */ |
| 3098 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3099 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3100 | void mac_operation_init() |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3101 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3102 | const uint8_t input[1] = { 0 }; |
| 3103 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3104 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3105 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3106 | * 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] | 3107 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3108 | psa_mac_operation_t func = psa_mac_operation_init(); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3109 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 3110 | psa_mac_operation_t zero; |
| 3111 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3112 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3113 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3114 | /* A freshly-initialized MAC operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3115 | TEST_EQUAL(psa_mac_update(&func, |
| 3116 | input, sizeof(input)), |
| 3117 | PSA_ERROR_BAD_STATE); |
| 3118 | TEST_EQUAL(psa_mac_update(&init, |
| 3119 | input, sizeof(input)), |
| 3120 | PSA_ERROR_BAD_STATE); |
| 3121 | TEST_EQUAL(psa_mac_update(&zero, |
| 3122 | input, sizeof(input)), |
| 3123 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3124 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3125 | /* A default MAC operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3126 | PSA_ASSERT(psa_mac_abort(&func)); |
| 3127 | PSA_ASSERT(psa_mac_abort(&init)); |
| 3128 | PSA_ASSERT(psa_mac_abort(&zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3129 | } |
| 3130 | /* END_CASE */ |
| 3131 | |
| 3132 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3133 | void mac_setup(int key_type_arg, |
| 3134 | data_t *key, |
| 3135 | int alg_arg, |
| 3136 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3137 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3138 | psa_key_type_t key_type = key_type_arg; |
| 3139 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3140 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3141 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3142 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3143 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3144 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3145 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3146 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3147 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3149 | if (!exercise_mac_setup(key_type, key->x, key->len, alg, |
| 3150 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3151 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3152 | } |
| 3153 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3154 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3155 | /* The operation object should be reusable. */ |
| 3156 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3157 | if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 3158 | smoke_test_key_data, |
| 3159 | sizeof(smoke_test_key_data), |
| 3160 | KNOWN_SUPPORTED_MAC_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, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3165 | #endif |
| 3166 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3167 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3168 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3169 | } |
| 3170 | /* END_CASE */ |
| 3171 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3172 | /* 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] | 3173 | void mac_bad_order() |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3174 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3175 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3176 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 3177 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3178 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3179 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3180 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3181 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa |
| 3182 | }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3183 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3184 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3185 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 3186 | size_t sign_mac_length = 0; |
| 3187 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3188 | const uint8_t verify_mac[] = { |
| 3189 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 3190 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3191 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 |
| 3192 | }; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3193 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3194 | PSA_ASSERT(psa_crypto_init()); |
| 3195 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 3196 | psa_set_key_algorithm(&attributes, alg); |
| 3197 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3198 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3199 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3200 | &key)); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3201 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3202 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3203 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3204 | PSA_ERROR_BAD_STATE); |
| 3205 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3206 | |
| 3207 | /* Call sign finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3208 | TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac), |
| 3209 | &sign_mac_length), |
| 3210 | PSA_ERROR_BAD_STATE); |
| 3211 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3212 | |
| 3213 | /* Call verify finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3214 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3215 | verify_mac, sizeof(verify_mac)), |
| 3216 | PSA_ERROR_BAD_STATE); |
| 3217 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3218 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3219 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3220 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3221 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3222 | TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg), |
| 3223 | PSA_ERROR_BAD_STATE); |
| 3224 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3225 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3226 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3227 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3228 | /* Call update after sign finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3229 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3230 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3231 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3232 | sign_mac, sizeof(sign_mac), |
| 3233 | &sign_mac_length)); |
| 3234 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3235 | PSA_ERROR_BAD_STATE); |
| 3236 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3237 | |
| 3238 | /* Call update after verify finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3239 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3240 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3241 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3242 | verify_mac, sizeof(verify_mac))); |
| 3243 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3244 | PSA_ERROR_BAD_STATE); |
| 3245 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3246 | |
| 3247 | /* Call sign finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3248 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3249 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3250 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3251 | sign_mac, sizeof(sign_mac), |
| 3252 | &sign_mac_length)); |
| 3253 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3254 | sign_mac, sizeof(sign_mac), |
| 3255 | &sign_mac_length), |
| 3256 | PSA_ERROR_BAD_STATE); |
| 3257 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3258 | |
| 3259 | /* Call verify finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3260 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3261 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3262 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3263 | verify_mac, sizeof(verify_mac))); |
| 3264 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3265 | verify_mac, sizeof(verify_mac)), |
| 3266 | PSA_ERROR_BAD_STATE); |
| 3267 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3268 | |
| 3269 | /* Setup sign but try verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3270 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3271 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3272 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3273 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3274 | verify_mac, sizeof(verify_mac)), |
| 3275 | PSA_ERROR_BAD_STATE); |
| 3276 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3277 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3278 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3279 | |
| 3280 | /* Setup verify but try sign. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3281 | PSA_ASSERT(psa_mac_verify_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_sign_finish(&operation, |
| 3285 | sign_mac, sizeof(sign_mac), |
| 3286 | &sign_mac_length), |
| 3287 | PSA_ERROR_BAD_STATE); |
| 3288 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3289 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3290 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3291 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3292 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3293 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3294 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3295 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3296 | } |
| 3297 | /* END_CASE */ |
| 3298 | |
| 3299 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3300 | void mac_sign_verify_multi(int key_type_arg, |
| 3301 | data_t *key_data, |
| 3302 | int alg_arg, |
| 3303 | data_t *input, |
| 3304 | int is_verify, |
| 3305 | data_t *expected_mac) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3306 | { |
| 3307 | size_t data_part_len = 0; |
| 3308 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3309 | 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] | 3310 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3311 | mbedtls_test_set_step(2000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3312 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3313 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3314 | alg_arg, |
| 3315 | input, data_part_len, |
| 3316 | expected_mac, |
| 3317 | is_verify, 0) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3318 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3319 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3320 | |
| 3321 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3322 | mbedtls_test_set_step(3000 + 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, 1) == 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 | |
| 3333 | /* Goto is required to silence warnings about unused labels, as we |
| 3334 | * don't actually do any test assertions in this function. */ |
| 3335 | goto exit; |
| 3336 | } |
| 3337 | /* END_CASE */ |
| 3338 | |
| 3339 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3340 | void mac_sign(int key_type_arg, |
| 3341 | data_t *key_data, |
| 3342 | int alg_arg, |
| 3343 | data_t *input, |
| 3344 | data_t *expected_mac) |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3345 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3346 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3347 | psa_key_type_t key_type = key_type_arg; |
| 3348 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3349 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3350 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3351 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3352 | size_t mac_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3353 | 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] | 3354 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3355 | const size_t output_sizes_to_test[] = { |
| 3356 | 0, |
| 3357 | 1, |
| 3358 | expected_mac->len - 1, |
| 3359 | expected_mac->len, |
| 3360 | expected_mac->len + 1, |
| 3361 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3362 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3363 | TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3364 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3365 | TEST_ASSERT(expected_mac->len == mac_buffer_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3367 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3368 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3369 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 3370 | psa_set_key_algorithm(&attributes, alg); |
| 3371 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3372 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3373 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3374 | &key)); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3375 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3376 | 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] | 3377 | const size_t output_size = output_sizes_to_test[i]; |
| 3378 | psa_status_t expected_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3379 | (output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3380 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3381 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3382 | mbedtls_test_set_step(output_size); |
| 3383 | ASSERT_ALLOC(actual_mac, output_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3384 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3385 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3386 | TEST_EQUAL(psa_mac_compute(key, alg, |
| 3387 | input->x, input->len, |
| 3388 | actual_mac, output_size, &mac_length), |
| 3389 | expected_status); |
| 3390 | if (expected_status == PSA_SUCCESS) { |
| 3391 | ASSERT_COMPARE(expected_mac->x, expected_mac->len, |
| 3392 | actual_mac, mac_length); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3393 | } |
| 3394 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3395 | if (output_size > 0) { |
| 3396 | memset(actual_mac, 0, output_size); |
| 3397 | } |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3398 | |
| 3399 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3400 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3401 | PSA_ASSERT(psa_mac_update(&operation, |
| 3402 | input->x, input->len)); |
| 3403 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3404 | actual_mac, output_size, |
| 3405 | &mac_length), |
| 3406 | expected_status); |
| 3407 | PSA_ASSERT(psa_mac_abort(&operation)); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3408 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3409 | if (expected_status == PSA_SUCCESS) { |
| 3410 | ASSERT_COMPARE(expected_mac->x, expected_mac->len, |
| 3411 | actual_mac, mac_length); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3412 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3413 | mbedtls_free(actual_mac); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3414 | actual_mac = NULL; |
| 3415 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3416 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3417 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3418 | psa_mac_abort(&operation); |
| 3419 | psa_destroy_key(key); |
| 3420 | PSA_DONE(); |
| 3421 | mbedtls_free(actual_mac); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3422 | } |
| 3423 | /* END_CASE */ |
| 3424 | |
| 3425 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3426 | void mac_verify(int key_type_arg, |
| 3427 | data_t *key_data, |
| 3428 | int alg_arg, |
| 3429 | data_t *input, |
| 3430 | data_t *expected_mac) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3431 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3432 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3433 | psa_key_type_t key_type = key_type_arg; |
| 3434 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3435 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3436 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3437 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3438 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3439 | TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3440 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3441 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3442 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3443 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 3444 | psa_set_key_algorithm(&attributes, alg); |
| 3445 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3446 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3447 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3448 | &key)); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3449 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3450 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3451 | PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len, |
| 3452 | expected_mac->x, expected_mac->len)); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3453 | |
| 3454 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3455 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3456 | PSA_ASSERT(psa_mac_update(&operation, |
| 3457 | input->x, input->len)); |
| 3458 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3459 | expected_mac->x, |
| 3460 | expected_mac->len)); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3461 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3462 | /* Test a MAC that's too short, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3463 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3464 | input->x, input->len, |
| 3465 | expected_mac->x, |
| 3466 | expected_mac->len - 1), |
| 3467 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3468 | |
| 3469 | /* Test a MAC that's too short, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3470 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3471 | PSA_ASSERT(psa_mac_update(&operation, |
| 3472 | input->x, input->len)); |
| 3473 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3474 | expected_mac->x, |
| 3475 | expected_mac->len - 1), |
| 3476 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3477 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3478 | /* Test a MAC that's too long, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3479 | ASSERT_ALLOC(perturbed_mac, expected_mac->len + 1); |
| 3480 | memcpy(perturbed_mac, expected_mac->x, expected_mac->len); |
| 3481 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3482 | input->x, input->len, |
| 3483 | perturbed_mac, expected_mac->len + 1), |
| 3484 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3485 | |
| 3486 | /* Test a MAC that's too long, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3487 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3488 | PSA_ASSERT(psa_mac_update(&operation, |
| 3489 | input->x, input->len)); |
| 3490 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3491 | perturbed_mac, |
| 3492 | expected_mac->len + 1), |
| 3493 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3494 | |
| 3495 | /* Test changing one byte. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3496 | for (size_t i = 0; i < expected_mac->len; i++) { |
| 3497 | mbedtls_test_set_step(i); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3498 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3499 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3500 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3501 | input->x, input->len, |
| 3502 | perturbed_mac, expected_mac->len), |
| 3503 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3504 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3505 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3506 | PSA_ASSERT(psa_mac_update(&operation, |
| 3507 | input->x, input->len)); |
| 3508 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3509 | perturbed_mac, |
| 3510 | expected_mac->len), |
| 3511 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3512 | perturbed_mac[i] ^= 1; |
| 3513 | } |
| 3514 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3515 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3516 | psa_mac_abort(&operation); |
| 3517 | psa_destroy_key(key); |
| 3518 | PSA_DONE(); |
| 3519 | mbedtls_free(perturbed_mac); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3520 | } |
| 3521 | /* END_CASE */ |
| 3522 | |
| 3523 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3524 | void cipher_operation_init() |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3525 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3526 | const uint8_t input[1] = { 0 }; |
| 3527 | unsigned char output[1] = { 0 }; |
| 3528 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3529 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3530 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3531 | * 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] | 3532 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3533 | psa_cipher_operation_t func = psa_cipher_operation_init(); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3534 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3535 | psa_cipher_operation_t zero; |
| 3536 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3537 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3538 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3539 | /* A freshly-initialized cipher operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3540 | TEST_EQUAL(psa_cipher_update(&func, |
| 3541 | input, sizeof(input), |
| 3542 | output, sizeof(output), |
| 3543 | &output_length), |
| 3544 | PSA_ERROR_BAD_STATE); |
| 3545 | TEST_EQUAL(psa_cipher_update(&init, |
| 3546 | input, sizeof(input), |
| 3547 | output, sizeof(output), |
| 3548 | &output_length), |
| 3549 | PSA_ERROR_BAD_STATE); |
| 3550 | TEST_EQUAL(psa_cipher_update(&zero, |
| 3551 | input, sizeof(input), |
| 3552 | output, sizeof(output), |
| 3553 | &output_length), |
| 3554 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3555 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3556 | /* A default cipher operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3557 | PSA_ASSERT(psa_cipher_abort(&func)); |
| 3558 | PSA_ASSERT(psa_cipher_abort(&init)); |
| 3559 | PSA_ASSERT(psa_cipher_abort(&zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3560 | } |
| 3561 | /* END_CASE */ |
| 3562 | |
| 3563 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3564 | void cipher_setup(int key_type_arg, |
| 3565 | data_t *key, |
| 3566 | int alg_arg, |
| 3567 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3568 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3569 | psa_key_type_t key_type = key_type_arg; |
| 3570 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3571 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3572 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3573 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3574 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3575 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3576 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3577 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3578 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3579 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3580 | if (!exercise_cipher_setup(key_type, key->x, key->len, alg, |
| 3581 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3582 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3583 | } |
| 3584 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3585 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3586 | /* The operation object should be reusable. */ |
| 3587 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3588 | if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3589 | smoke_test_key_data, |
| 3590 | sizeof(smoke_test_key_data), |
| 3591 | KNOWN_SUPPORTED_CIPHER_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, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3596 | #endif |
| 3597 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3598 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3599 | psa_cipher_abort(&operation); |
| 3600 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3601 | } |
| 3602 | /* END_CASE */ |
| 3603 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3604 | /* 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] | 3605 | void cipher_bad_order() |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3606 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3607 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3608 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3609 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3610 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3611 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3612 | 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] | 3613 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3614 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3615 | 0xaa, 0xaa, 0xaa, 0xaa |
| 3616 | }; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3617 | const uint8_t text[] = { |
| 3618 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3619 | 0xbb, 0xbb, 0xbb, 0xbb |
| 3620 | }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3621 | 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] | 3622 | size_t length = 0; |
| 3623 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3624 | PSA_ASSERT(psa_crypto_init()); |
| 3625 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3626 | psa_set_key_algorithm(&attributes, alg); |
| 3627 | psa_set_key_type(&attributes, key_type); |
| 3628 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3629 | &key)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3630 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3631 | /* Call encrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3632 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3633 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3634 | TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg), |
| 3635 | PSA_ERROR_BAD_STATE); |
| 3636 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3637 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3638 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3639 | |
| 3640 | /* Call decrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3641 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3642 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3643 | TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg), |
| 3644 | PSA_ERROR_BAD_STATE); |
| 3645 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3646 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3647 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3648 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3649 | /* Generate an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3650 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3651 | buffer, sizeof(buffer), |
| 3652 | &length), |
| 3653 | PSA_ERROR_BAD_STATE); |
| 3654 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3655 | |
| 3656 | /* Generate an IV twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3657 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3658 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3659 | buffer, sizeof(buffer), |
| 3660 | &length)); |
| 3661 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3662 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3663 | buffer, sizeof(buffer), |
| 3664 | &length), |
| 3665 | PSA_ERROR_BAD_STATE); |
| 3666 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3667 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3668 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3669 | |
| 3670 | /* Generate an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3671 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3672 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3673 | iv, sizeof(iv))); |
| 3674 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3675 | buffer, sizeof(buffer), |
| 3676 | &length), |
| 3677 | PSA_ERROR_BAD_STATE); |
| 3678 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3679 | |
| 3680 | /* Set an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3681 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3682 | iv, sizeof(iv)), |
| 3683 | PSA_ERROR_BAD_STATE); |
| 3684 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3685 | |
| 3686 | /* Set an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3687 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3688 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3689 | iv, sizeof(iv))); |
| 3690 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3691 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3692 | iv, sizeof(iv)), |
| 3693 | PSA_ERROR_BAD_STATE); |
| 3694 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3695 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3696 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3697 | |
| 3698 | /* Set an IV after it's already generated. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3699 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3700 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3701 | buffer, sizeof(buffer), |
| 3702 | &length)); |
| 3703 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3704 | iv, sizeof(iv)), |
| 3705 | PSA_ERROR_BAD_STATE); |
| 3706 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3707 | |
| 3708 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3709 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3710 | text, sizeof(text), |
| 3711 | buffer, sizeof(buffer), |
| 3712 | &length), |
| 3713 | PSA_ERROR_BAD_STATE); |
| 3714 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3715 | |
| 3716 | /* Call update without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3717 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3718 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3719 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3720 | text, sizeof(text), |
| 3721 | buffer, sizeof(buffer), |
| 3722 | &length), |
| 3723 | PSA_ERROR_BAD_STATE); |
| 3724 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3725 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3726 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3727 | |
| 3728 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3729 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3730 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3731 | iv, sizeof(iv))); |
| 3732 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3733 | buffer, sizeof(buffer), &length)); |
| 3734 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3735 | text, sizeof(text), |
| 3736 | buffer, sizeof(buffer), |
| 3737 | &length), |
| 3738 | PSA_ERROR_BAD_STATE); |
| 3739 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3740 | |
| 3741 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3742 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3743 | buffer, sizeof(buffer), &length), |
| 3744 | PSA_ERROR_BAD_STATE); |
| 3745 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3746 | |
| 3747 | /* Call finish without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3748 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3749 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3750 | * for cipher modes with padding. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3751 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3752 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3753 | buffer, sizeof(buffer), &length), |
| 3754 | PSA_ERROR_BAD_STATE); |
| 3755 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3756 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3757 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3758 | |
| 3759 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3760 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3761 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3762 | iv, sizeof(iv))); |
| 3763 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3764 | buffer, sizeof(buffer), &length)); |
| 3765 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3766 | buffer, sizeof(buffer), &length), |
| 3767 | PSA_ERROR_BAD_STATE); |
| 3768 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3769 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3770 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3771 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3772 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3773 | psa_cipher_abort(&operation); |
| 3774 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3775 | } |
| 3776 | /* END_CASE */ |
| 3777 | |
| 3778 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3779 | void cipher_encrypt_fail(int alg_arg, |
| 3780 | int key_type_arg, |
| 3781 | data_t *key_data, |
| 3782 | data_t *input, |
| 3783 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3784 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3785 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3786 | psa_status_t status; |
| 3787 | psa_key_type_t key_type = key_type_arg; |
| 3788 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3789 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3790 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 }; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3791 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; |
| 3792 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3793 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3794 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3795 | size_t output_length = 0; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3796 | size_t function_output_length; |
| 3797 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3798 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3799 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3800 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 3801 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3802 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3803 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3804 | psa_set_key_algorithm(&attributes, alg); |
| 3805 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3806 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3807 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 3808 | input->len); |
| 3809 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3810 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3811 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3812 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3813 | } |
| 3814 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3815 | /* Encrypt, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3816 | status = psa_cipher_encrypt(key, alg, input->x, input->len, output, |
| 3817 | output_buffer_size, &output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3818 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3819 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3820 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3821 | /* Encrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3822 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
| 3823 | if (status == PSA_SUCCESS) { |
| 3824 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 3825 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3826 | iv, iv_size, |
| 3827 | &iv_length)); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3828 | } |
| 3829 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3830 | status = psa_cipher_update(&operation, input->x, input->len, |
| 3831 | output, output_buffer_size, |
| 3832 | &function_output_length); |
| 3833 | if (status == PSA_SUCCESS) { |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3834 | output_length += function_output_length; |
| 3835 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3836 | status = psa_cipher_finish(&operation, output + output_length, |
| 3837 | output_buffer_size - output_length, |
| 3838 | &function_output_length); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3839 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3840 | TEST_EQUAL(status, expected_status); |
| 3841 | } else { |
| 3842 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3843 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3844 | } else { |
| 3845 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3846 | } |
| 3847 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3848 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3849 | psa_cipher_abort(&operation); |
| 3850 | mbedtls_free(output); |
| 3851 | psa_destroy_key(key); |
| 3852 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3853 | } |
| 3854 | /* END_CASE */ |
| 3855 | |
| 3856 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3857 | void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data, |
| 3858 | data_t *input, int iv_length, |
| 3859 | int expected_result) |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3860 | { |
| 3861 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3862 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3863 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3864 | size_t output_buffer_size = 0; |
| 3865 | unsigned char *output = NULL; |
| 3866 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3867 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 3868 | ASSERT_ALLOC(output, output_buffer_size); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3869 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3870 | PSA_ASSERT(psa_crypto_init()); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3871 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3872 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3873 | psa_set_key_algorithm(&attributes, alg); |
| 3874 | psa_set_key_type(&attributes, key_type); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3875 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3876 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3877 | &key)); |
| 3878 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3879 | TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output, |
| 3880 | iv_length)); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3881 | |
| 3882 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3883 | psa_cipher_abort(&operation); |
| 3884 | mbedtls_free(output); |
| 3885 | psa_destroy_key(key); |
| 3886 | PSA_DONE(); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3887 | } |
| 3888 | /* END_CASE */ |
| 3889 | |
| 3890 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3891 | void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data, |
| 3892 | data_t *plaintext, data_t *ciphertext) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3893 | { |
| 3894 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3895 | psa_key_type_t key_type = key_type_arg; |
| 3896 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3897 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3898 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3899 | unsigned char *output = NULL; |
| 3900 | size_t output_buffer_size = 0; |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3901 | size_t output_length, length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3902 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3903 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3904 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3905 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3906 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3907 | TEST_LE_U(ciphertext->len, |
| 3908 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len)); |
| 3909 | TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len), |
| 3910 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len)); |
| 3911 | TEST_LE_U(plaintext->len, |
| 3912 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len)); |
| 3913 | TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len), |
| 3914 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len)); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3915 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3916 | |
| 3917 | /* Set up key and output buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3918 | psa_set_key_usage_flags(&attributes, |
| 3919 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3920 | psa_set_key_algorithm(&attributes, alg); |
| 3921 | psa_set_key_type(&attributes, key_type); |
| 3922 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3923 | &key)); |
| 3924 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 3925 | plaintext->len); |
| 3926 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3927 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3928 | /* set_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3929 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3930 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 3931 | PSA_ERROR_BAD_STATE); |
| 3932 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3933 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 3934 | PSA_ERROR_BAD_STATE); |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3935 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3936 | /* generate_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3937 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3938 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 3939 | &length), |
| 3940 | PSA_ERROR_BAD_STATE); |
| 3941 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3942 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 3943 | &length), |
| 3944 | PSA_ERROR_BAD_STATE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3945 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3946 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3947 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3948 | output_length = 0; |
| 3949 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3950 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3951 | plaintext->x, plaintext->len, |
| 3952 | output, output_buffer_size, |
| 3953 | &length)); |
| 3954 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3955 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3956 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3957 | mbedtls_buffer_offset(output, output_length), |
| 3958 | output_buffer_size - output_length, |
| 3959 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3960 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3961 | ASSERT_COMPARE(ciphertext->x, ciphertext->len, |
| 3962 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3963 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3964 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3965 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3966 | output_length = 0; |
| 3967 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3968 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3969 | ciphertext->x, ciphertext->len, |
| 3970 | output, output_buffer_size, |
| 3971 | &length)); |
| 3972 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3973 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3974 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3975 | mbedtls_buffer_offset(output, output_length), |
| 3976 | output_buffer_size - output_length, |
| 3977 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3978 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3979 | ASSERT_COMPARE(plaintext->x, plaintext->len, |
| 3980 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3981 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3982 | /* One-shot encryption */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3983 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3984 | PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len, |
| 3985 | output, output_buffer_size, |
| 3986 | &output_length)); |
| 3987 | ASSERT_COMPARE(ciphertext->x, ciphertext->len, |
| 3988 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3989 | |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3990 | /* One-shot decryption */ |
| 3991 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3992 | PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len, |
| 3993 | output, output_buffer_size, |
| 3994 | &output_length)); |
| 3995 | ASSERT_COMPARE(plaintext->x, plaintext->len, |
| 3996 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3997 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3998 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3999 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4000 | mbedtls_free(output); |
| 4001 | psa_cipher_abort(&operation); |
| 4002 | psa_destroy_key(key); |
| 4003 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4004 | } |
| 4005 | /* END_CASE */ |
| 4006 | |
| 4007 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4008 | 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] | 4009 | { |
| 4010 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4011 | psa_algorithm_t alg = alg_arg; |
| 4012 | psa_key_type_t key_type = key_type_arg; |
| 4013 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4014 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4015 | psa_status_t status; |
| 4016 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4017 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4018 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4019 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4020 | psa_set_key_algorithm(&attributes, alg); |
| 4021 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4022 | |
| 4023 | /* Usage of either of these two size macros would cause divide by zero |
| 4024 | * with incorrect key types previously. Input length should be irrelevant |
| 4025 | * here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4026 | TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16), |
| 4027 | 0); |
| 4028 | TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4029 | |
| 4030 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4031 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4032 | &key)); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4033 | |
| 4034 | /* Should fail due to invalid alg type (to support invalid key type). |
| 4035 | * Encrypt or decrypt will end up in the same place. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4036 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4037 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4038 | TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4039 | |
| 4040 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4041 | psa_cipher_abort(&operation); |
| 4042 | psa_destroy_key(key); |
| 4043 | PSA_DONE(); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4044 | } |
| 4045 | /* END_CASE */ |
| 4046 | |
| 4047 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4048 | void cipher_encrypt_validation(int alg_arg, |
| 4049 | int key_type_arg, |
| 4050 | data_t *key_data, |
| 4051 | data_t *input) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4052 | { |
| 4053 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4054 | psa_key_type_t key_type = key_type_arg; |
| 4055 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4056 | size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4057 | unsigned char *output1 = NULL; |
| 4058 | size_t output1_buffer_size = 0; |
| 4059 | size_t output1_length = 0; |
| 4060 | unsigned char *output2 = NULL; |
| 4061 | size_t output2_buffer_size = 0; |
| 4062 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4063 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4064 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4065 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4066 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4067 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4068 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4069 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4070 | psa_set_key_algorithm(&attributes, alg); |
| 4071 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4072 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4073 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4074 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4075 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4076 | ASSERT_ALLOC(output1, output1_buffer_size); |
| 4077 | ASSERT_ALLOC(output2, output2_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4078 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4079 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4080 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4081 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 4082 | /* The one-shot cipher encryption uses generated iv so validating |
| 4083 | the output is not possible. Validating with multipart encryption. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4084 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1, |
| 4085 | output1_buffer_size, &output1_length)); |
| 4086 | TEST_LE_U(output1_length, |
| 4087 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4088 | TEST_LE_U(output1_length, |
| 4089 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4090 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4091 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4092 | PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4093 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4094 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4095 | input->x, input->len, |
| 4096 | output2, output2_buffer_size, |
| 4097 | &function_output_length)); |
| 4098 | TEST_LE_U(function_output_length, |
| 4099 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4100 | TEST_LE_U(function_output_length, |
| 4101 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4102 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4103 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4104 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4105 | output2 + output2_length, |
| 4106 | output2_buffer_size - output2_length, |
| 4107 | &function_output_length)); |
| 4108 | TEST_LE_U(function_output_length, |
| 4109 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4110 | TEST_LE_U(function_output_length, |
| 4111 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4112 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4114 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4115 | ASSERT_COMPARE(output1 + iv_size, output1_length - iv_size, |
| 4116 | output2, output2_length); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4117 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4118 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4119 | psa_cipher_abort(&operation); |
| 4120 | mbedtls_free(output1); |
| 4121 | mbedtls_free(output2); |
| 4122 | psa_destroy_key(key); |
| 4123 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4124 | } |
| 4125 | /* END_CASE */ |
| 4126 | |
| 4127 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4128 | void cipher_encrypt_multipart(int alg_arg, int key_type_arg, |
| 4129 | data_t *key_data, data_t *iv, |
| 4130 | data_t *input, |
| 4131 | int first_part_size_arg, |
| 4132 | int output1_length_arg, int output2_length_arg, |
| 4133 | data_t *expected_output, |
| 4134 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4135 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4136 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4137 | psa_key_type_t key_type = key_type_arg; |
| 4138 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4139 | psa_status_t status; |
| 4140 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4141 | size_t first_part_size = first_part_size_arg; |
| 4142 | size_t output1_length = output1_length_arg; |
| 4143 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4144 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4145 | size_t output_buffer_size = 0; |
| 4146 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4147 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4148 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4149 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4151 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4153 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4154 | psa_set_key_algorithm(&attributes, alg); |
| 4155 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4156 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4157 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4158 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4159 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4160 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
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 | if (iv->len > 0) { |
| 4163 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4164 | } |
| 4165 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4166 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4167 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4168 | ASSERT_ALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4169 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4170 | TEST_LE_U(first_part_size, input->len); |
| 4171 | PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size, |
| 4172 | output, output_buffer_size, |
| 4173 | &function_output_length)); |
| 4174 | TEST_ASSERT(function_output_length == output1_length); |
| 4175 | TEST_LE_U(function_output_length, |
| 4176 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4177 | TEST_LE_U(function_output_length, |
| 4178 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4179 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4181 | if (first_part_size < input->len) { |
| 4182 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4183 | input->x + first_part_size, |
| 4184 | input->len - first_part_size, |
| 4185 | (output_buffer_size == 0 ? NULL : |
| 4186 | output + total_output_length), |
| 4187 | output_buffer_size - total_output_length, |
| 4188 | &function_output_length)); |
| 4189 | TEST_ASSERT(function_output_length == output2_length); |
| 4190 | TEST_LE_U(function_output_length, |
| 4191 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4192 | alg, |
| 4193 | input->len - first_part_size)); |
| 4194 | TEST_LE_U(function_output_length, |
| 4195 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4196 | total_output_length += function_output_length; |
| 4197 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4198 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4199 | status = psa_cipher_finish(&operation, |
| 4200 | (output_buffer_size == 0 ? NULL : |
| 4201 | output + total_output_length), |
| 4202 | output_buffer_size - total_output_length, |
| 4203 | &function_output_length); |
| 4204 | TEST_LE_U(function_output_length, |
| 4205 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4206 | TEST_LE_U(function_output_length, |
| 4207 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4208 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4209 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4210 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4211 | if (expected_status == PSA_SUCCESS) { |
| 4212 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4213 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4214 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 4215 | output, total_output_length); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4216 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4217 | |
| 4218 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4219 | psa_cipher_abort(&operation); |
| 4220 | mbedtls_free(output); |
| 4221 | psa_destroy_key(key); |
| 4222 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4223 | } |
| 4224 | /* END_CASE */ |
| 4225 | |
| 4226 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4227 | void cipher_decrypt_multipart(int alg_arg, int key_type_arg, |
| 4228 | data_t *key_data, data_t *iv, |
| 4229 | data_t *input, |
| 4230 | int first_part_size_arg, |
| 4231 | int output1_length_arg, int output2_length_arg, |
| 4232 | data_t *expected_output, |
| 4233 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4234 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4235 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4236 | psa_key_type_t key_type = key_type_arg; |
| 4237 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4238 | psa_status_t status; |
| 4239 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4240 | size_t first_part_size = first_part_size_arg; |
| 4241 | size_t output1_length = output1_length_arg; |
| 4242 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4243 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4244 | size_t output_buffer_size = 0; |
| 4245 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4246 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4247 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4248 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4249 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4250 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4252 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4253 | psa_set_key_algorithm(&attributes, alg); |
| 4254 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4256 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4257 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4258 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4259 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
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 | if (iv->len > 0) { |
| 4262 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4263 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4264 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4265 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4266 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4267 | ASSERT_ALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4268 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4269 | TEST_LE_U(first_part_size, input->len); |
| 4270 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4271 | input->x, first_part_size, |
| 4272 | output, output_buffer_size, |
| 4273 | &function_output_length)); |
| 4274 | TEST_ASSERT(function_output_length == output1_length); |
| 4275 | TEST_LE_U(function_output_length, |
| 4276 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4277 | TEST_LE_U(function_output_length, |
| 4278 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4279 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4280 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4281 | if (first_part_size < input->len) { |
| 4282 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4283 | input->x + first_part_size, |
| 4284 | input->len - first_part_size, |
| 4285 | (output_buffer_size == 0 ? NULL : |
| 4286 | output + total_output_length), |
| 4287 | output_buffer_size - total_output_length, |
| 4288 | &function_output_length)); |
| 4289 | TEST_ASSERT(function_output_length == output2_length); |
| 4290 | TEST_LE_U(function_output_length, |
| 4291 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4292 | alg, |
| 4293 | input->len - first_part_size)); |
| 4294 | TEST_LE_U(function_output_length, |
| 4295 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4296 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4297 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4298 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4299 | status = psa_cipher_finish(&operation, |
| 4300 | (output_buffer_size == 0 ? NULL : |
| 4301 | output + total_output_length), |
| 4302 | output_buffer_size - total_output_length, |
| 4303 | &function_output_length); |
| 4304 | TEST_LE_U(function_output_length, |
| 4305 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4306 | TEST_LE_U(function_output_length, |
| 4307 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4308 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4309 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4310 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4311 | if (expected_status == PSA_SUCCESS) { |
| 4312 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4314 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 4315 | output, total_output_length); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4316 | } |
| 4317 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4318 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4319 | psa_cipher_abort(&operation); |
| 4320 | mbedtls_free(output); |
| 4321 | psa_destroy_key(key); |
| 4322 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4323 | } |
| 4324 | /* END_CASE */ |
| 4325 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4326 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4327 | void cipher_decrypt_fail(int alg_arg, |
| 4328 | int key_type_arg, |
| 4329 | data_t *key_data, |
| 4330 | data_t *iv, |
| 4331 | data_t *input_arg, |
| 4332 | int expected_status_arg) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4333 | { |
| 4334 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4335 | psa_status_t status; |
| 4336 | psa_key_type_t key_type = key_type_arg; |
| 4337 | psa_algorithm_t alg = alg_arg; |
| 4338 | psa_status_t expected_status = expected_status_arg; |
| 4339 | unsigned char *input = NULL; |
| 4340 | size_t input_buffer_size = 0; |
| 4341 | unsigned char *output = NULL; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4342 | unsigned char *output_multi = NULL; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4343 | size_t output_buffer_size = 0; |
| 4344 | size_t output_length = 0; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4345 | size_t function_output_length; |
| 4346 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4347 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4349 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 4350 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4352 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4353 | psa_set_key_algorithm(&attributes, alg); |
| 4354 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4356 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4357 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4358 | } |
| 4359 | |
| 4360 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4361 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4362 | if (input_buffer_size > 0) { |
| 4363 | ASSERT_ALLOC(input, input_buffer_size); |
| 4364 | memcpy(input, iv->x, iv->len); |
| 4365 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4366 | } |
| 4367 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4368 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size); |
| 4369 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4370 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4371 | /* Decrypt, one-short */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4372 | status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4373 | output_buffer_size, &output_length); |
| 4374 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4375 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4376 | /* Decrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4377 | status = psa_cipher_decrypt_setup(&operation, key, alg); |
| 4378 | if (status == PSA_SUCCESS) { |
| 4379 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 4380 | input_arg->len) + |
| 4381 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4382 | ASSERT_ALLOC(output_multi, output_buffer_size); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4383 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4384 | if (iv->len > 0) { |
| 4385 | status = psa_cipher_set_iv(&operation, iv->x, iv->len); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4386 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4387 | if (status != PSA_SUCCESS) { |
| 4388 | TEST_EQUAL(status, expected_status); |
| 4389 | } |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4390 | } |
| 4391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4392 | if (status == PSA_SUCCESS) { |
| 4393 | status = psa_cipher_update(&operation, |
| 4394 | input_arg->x, input_arg->len, |
| 4395 | output_multi, output_buffer_size, |
| 4396 | &function_output_length); |
| 4397 | if (status == PSA_SUCCESS) { |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4398 | output_length = function_output_length; |
| 4399 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4400 | status = psa_cipher_finish(&operation, |
| 4401 | output_multi + output_length, |
| 4402 | output_buffer_size - output_length, |
| 4403 | &function_output_length); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4404 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4405 | TEST_EQUAL(status, expected_status); |
| 4406 | } else { |
| 4407 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4408 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4409 | } else { |
| 4410 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4411 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4412 | } else { |
| 4413 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4414 | } |
| 4415 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4416 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4417 | psa_cipher_abort(&operation); |
| 4418 | mbedtls_free(input); |
| 4419 | mbedtls_free(output); |
| 4420 | mbedtls_free(output_multi); |
| 4421 | psa_destroy_key(key); |
| 4422 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4423 | } |
| 4424 | /* END_CASE */ |
| 4425 | |
| 4426 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4427 | void cipher_decrypt(int alg_arg, |
| 4428 | int key_type_arg, |
| 4429 | data_t *key_data, |
| 4430 | data_t *iv, |
| 4431 | data_t *input_arg, |
| 4432 | data_t *expected_output) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4433 | { |
| 4434 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4435 | psa_key_type_t key_type = key_type_arg; |
| 4436 | psa_algorithm_t alg = alg_arg; |
| 4437 | unsigned char *input = NULL; |
| 4438 | size_t input_buffer_size = 0; |
| 4439 | unsigned char *output = NULL; |
| 4440 | size_t output_buffer_size = 0; |
| 4441 | size_t output_length = 0; |
| 4442 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4444 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4445 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4446 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4447 | psa_set_key_algorithm(&attributes, alg); |
| 4448 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4449 | |
| 4450 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4451 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4452 | if (input_buffer_size > 0) { |
| 4453 | ASSERT_ALLOC(input, input_buffer_size); |
| 4454 | memcpy(input, iv->x, iv->len); |
| 4455 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4456 | } |
| 4457 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4458 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size); |
| 4459 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4460 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4461 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4462 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4463 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4464 | PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4465 | output_buffer_size, &output_length)); |
| 4466 | TEST_LE_U(output_length, |
| 4467 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size)); |
| 4468 | TEST_LE_U(output_length, |
| 4469 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4470 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4471 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 4472 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4473 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4474 | mbedtls_free(input); |
| 4475 | mbedtls_free(output); |
| 4476 | psa_destroy_key(key); |
| 4477 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4478 | } |
| 4479 | /* END_CASE */ |
| 4480 | |
| 4481 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4482 | void cipher_verify_output(int alg_arg, |
| 4483 | int key_type_arg, |
| 4484 | data_t *key_data, |
| 4485 | data_t *input) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4486 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4487 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4488 | psa_key_type_t key_type = key_type_arg; |
| 4489 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4490 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4491 | size_t output1_size = 0; |
| 4492 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4493 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4494 | size_t output2_size = 0; |
| 4495 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4496 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4497 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4498 | PSA_ASSERT(psa_crypto_init()); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4499 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4500 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4501 | psa_set_key_algorithm(&attributes, alg); |
| 4502 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4503 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4504 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4505 | &key)); |
| 4506 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4507 | ASSERT_ALLOC(output1, output1_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4508 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4509 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, |
| 4510 | output1, output1_size, |
| 4511 | &output1_length)); |
| 4512 | TEST_LE_U(output1_length, |
| 4513 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4514 | TEST_LE_U(output1_length, |
| 4515 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4516 | |
| 4517 | output2_size = output1_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4518 | ASSERT_ALLOC(output2, output2_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_decrypt(key, alg, output1, output1_length, |
| 4521 | output2, output2_size, |
| 4522 | &output2_length)); |
| 4523 | TEST_LE_U(output2_length, |
| 4524 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4525 | TEST_LE_U(output2_length, |
| 4526 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4527 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4528 | ASSERT_COMPARE(input->x, input->len, output2, output2_length); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4529 | |
| 4530 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4531 | mbedtls_free(output1); |
| 4532 | mbedtls_free(output2); |
| 4533 | psa_destroy_key(key); |
| 4534 | PSA_DONE(); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4535 | } |
| 4536 | /* END_CASE */ |
| 4537 | |
| 4538 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4539 | void cipher_verify_output_multipart(int alg_arg, |
| 4540 | int key_type_arg, |
| 4541 | data_t *key_data, |
| 4542 | data_t *input, |
| 4543 | int first_part_size_arg) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4544 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4545 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4546 | psa_key_type_t key_type = key_type_arg; |
| 4547 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4548 | size_t first_part_size = first_part_size_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4549 | unsigned char iv[16] = { 0 }; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4550 | size_t iv_size = 16; |
| 4551 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4552 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4553 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4554 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4555 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4556 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4557 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4558 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4559 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 4560 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4561 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4562 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4563 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4564 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4565 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4566 | psa_set_key_algorithm(&attributes, alg); |
| 4567 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4568 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4569 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4570 | &key)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4571 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4572 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg)); |
| 4573 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4574 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4575 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 4576 | PSA_ASSERT(psa_cipher_generate_iv(&operation1, |
| 4577 | iv, iv_size, |
| 4578 | &iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4579 | } |
| 4580 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4581 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4582 | TEST_LE_U(output1_buffer_size, |
| 4583 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
| 4584 | ASSERT_ALLOC(output1, output1_buffer_size); |
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 | TEST_LE_U(first_part_size, input->len); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4587 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4588 | PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size, |
| 4589 | output1, output1_buffer_size, |
| 4590 | &function_output_length)); |
| 4591 | TEST_LE_U(function_output_length, |
| 4592 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4593 | TEST_LE_U(function_output_length, |
| 4594 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4595 | output1_length += function_output_length; |
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 | PSA_ASSERT(psa_cipher_update(&operation1, |
| 4598 | input->x + first_part_size, |
| 4599 | input->len - 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, |
| 4604 | alg, |
| 4605 | input->len - first_part_size)); |
| 4606 | TEST_LE_U(function_output_length, |
| 4607 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4608 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4609 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4610 | PSA_ASSERT(psa_cipher_finish(&operation1, |
| 4611 | output1 + output1_length, |
| 4612 | output1_buffer_size - output1_length, |
| 4613 | &function_output_length)); |
| 4614 | TEST_LE_U(function_output_length, |
| 4615 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4616 | TEST_LE_U(function_output_length, |
| 4617 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4618 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4619 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4620 | PSA_ASSERT(psa_cipher_abort(&operation1)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4621 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4622 | output2_buffer_size = output1_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4623 | TEST_LE_U(output2_buffer_size, |
| 4624 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4625 | TEST_LE_U(output2_buffer_size, |
| 4626 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
| 4627 | ASSERT_ALLOC(output2, output2_buffer_size); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4628 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4629 | if (iv_length > 0) { |
| 4630 | PSA_ASSERT(psa_cipher_set_iv(&operation2, |
| 4631 | iv, iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4632 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4633 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4634 | PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size, |
| 4635 | output2, output2_buffer_size, |
| 4636 | &function_output_length)); |
| 4637 | TEST_LE_U(function_output_length, |
| 4638 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4639 | TEST_LE_U(function_output_length, |
| 4640 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4641 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4642 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4643 | PSA_ASSERT(psa_cipher_update(&operation2, |
| 4644 | output1 + first_part_size, |
| 4645 | output1_length - 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, |
| 4650 | alg, |
| 4651 | output1_length - first_part_size)); |
| 4652 | TEST_LE_U(function_output_length, |
| 4653 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4654 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4655 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4656 | PSA_ASSERT(psa_cipher_finish(&operation2, |
| 4657 | output2 + output2_length, |
| 4658 | output2_buffer_size - output2_length, |
| 4659 | &function_output_length)); |
| 4660 | TEST_LE_U(function_output_length, |
| 4661 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4662 | TEST_LE_U(function_output_length, |
| 4663 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4664 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4665 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4666 | PSA_ASSERT(psa_cipher_abort(&operation2)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4667 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4668 | ASSERT_COMPARE(input->x, input->len, output2, output2_length); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4669 | |
| 4670 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4671 | psa_cipher_abort(&operation1); |
| 4672 | psa_cipher_abort(&operation2); |
| 4673 | mbedtls_free(output1); |
| 4674 | mbedtls_free(output2); |
| 4675 | psa_destroy_key(key); |
| 4676 | PSA_DONE(); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4677 | } |
| 4678 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 4679 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4680 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4681 | void aead_encrypt_decrypt(int key_type_arg, data_t *key_data, |
| 4682 | int alg_arg, |
| 4683 | data_t *nonce, |
| 4684 | data_t *additional_data, |
| 4685 | data_t *input_data, |
| 4686 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4687 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4688 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4689 | psa_key_type_t key_type = key_type_arg; |
| 4690 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4691 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4692 | unsigned char *output_data = NULL; |
| 4693 | size_t output_size = 0; |
| 4694 | size_t output_length = 0; |
| 4695 | unsigned char *output_data2 = NULL; |
| 4696 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4697 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4698 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4699 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4700 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4701 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4702 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4703 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4704 | psa_set_key_algorithm(&attributes, alg); |
| 4705 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4706 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4707 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4708 | &key)); |
| 4709 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4710 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4711 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4712 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4713 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4714 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4715 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4716 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4717 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
| 4718 | TEST_EQUAL(output_size, |
| 4719 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4720 | TEST_LE_U(output_size, |
| 4721 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
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 | ASSERT_ALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4724 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4725 | status = psa_aead_encrypt(key, alg, |
| 4726 | nonce->x, nonce->len, |
| 4727 | additional_data->x, |
| 4728 | additional_data->len, |
| 4729 | input_data->x, input_data->len, |
| 4730 | output_data, output_size, |
| 4731 | &output_length); |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4732 | |
| 4733 | /* If the operation is not supported, just skip and not fail in case the |
| 4734 | * encryption involves a common limitation of cryptography hardwares and |
| 4735 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4736 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4737 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4738 | 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] | 4739 | } |
| 4740 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4741 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4742 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4743 | if (PSA_SUCCESS == expected_result) { |
| 4744 | ASSERT_ALLOC(output_data2, output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4745 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4746 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4747 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4748 | TEST_EQUAL(input_data->len, |
| 4749 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length)); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4750 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4751 | TEST_LE_U(input_data->len, |
| 4752 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length)); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4753 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4754 | TEST_EQUAL(psa_aead_decrypt(key, alg, |
| 4755 | nonce->x, nonce->len, |
| 4756 | additional_data->x, |
| 4757 | additional_data->len, |
| 4758 | output_data, output_length, |
| 4759 | output_data2, output_length, |
| 4760 | &output_length2), |
| 4761 | expected_result); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4762 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4763 | ASSERT_COMPARE(input_data->x, input_data->len, |
| 4764 | output_data2, output_length2); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4765 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4766 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4767 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4768 | psa_destroy_key(key); |
| 4769 | mbedtls_free(output_data); |
| 4770 | mbedtls_free(output_data2); |
| 4771 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4772 | } |
| 4773 | /* END_CASE */ |
| 4774 | |
| 4775 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4776 | void aead_encrypt(int key_type_arg, data_t *key_data, |
| 4777 | int alg_arg, |
| 4778 | data_t *nonce, |
| 4779 | data_t *additional_data, |
| 4780 | data_t *input_data, |
| 4781 | data_t *expected_result) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4782 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4783 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4784 | psa_key_type_t key_type = key_type_arg; |
| 4785 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4786 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4787 | unsigned char *output_data = NULL; |
| 4788 | size_t output_size = 0; |
| 4789 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4790 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4791 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4792 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4793 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4794 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4795 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4796 | psa_set_key_algorithm(&attributes, alg); |
| 4797 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4798 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4799 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4800 | &key)); |
| 4801 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4802 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4803 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4804 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4805 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4806 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4807 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4808 | TEST_EQUAL(output_size, |
| 4809 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4810 | TEST_LE_U(output_size, |
| 4811 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
| 4812 | ASSERT_ALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4813 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4814 | status = psa_aead_encrypt(key, alg, |
| 4815 | nonce->x, nonce->len, |
| 4816 | additional_data->x, additional_data->len, |
| 4817 | input_data->x, input_data->len, |
| 4818 | output_data, output_size, |
| 4819 | &output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4820 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4821 | /* If the operation is not supported, just skip and not fail in case the |
| 4822 | * encryption involves a common limitation of cryptography hardwares and |
| 4823 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4824 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4825 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4826 | 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] | 4827 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4828 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4829 | PSA_ASSERT(status); |
| 4830 | ASSERT_COMPARE(expected_result->x, expected_result->len, |
| 4831 | output_data, output_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4832 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4833 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4834 | psa_destroy_key(key); |
| 4835 | mbedtls_free(output_data); |
| 4836 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4837 | } |
| 4838 | /* END_CASE */ |
| 4839 | |
| 4840 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4841 | void aead_decrypt(int key_type_arg, data_t *key_data, |
| 4842 | int alg_arg, |
| 4843 | data_t *nonce, |
| 4844 | data_t *additional_data, |
| 4845 | data_t *input_data, |
| 4846 | data_t *expected_data, |
| 4847 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4848 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4849 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4850 | psa_key_type_t key_type = key_type_arg; |
| 4851 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4852 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4853 | unsigned char *output_data = NULL; |
| 4854 | size_t output_size = 0; |
| 4855 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4856 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4857 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4858 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4859 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4860 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4861 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4862 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4863 | psa_set_key_algorithm(&attributes, alg); |
| 4864 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4865 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4866 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4867 | &key)); |
| 4868 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4869 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4870 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4871 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4872 | alg); |
| 4873 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4874 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4875 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4876 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4877 | TEST_EQUAL(output_size, |
| 4878 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4879 | TEST_LE_U(output_size, |
| 4880 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
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 | ASSERT_ALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4883 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4884 | status = psa_aead_decrypt(key, alg, |
| 4885 | nonce->x, nonce->len, |
| 4886 | additional_data->x, |
| 4887 | additional_data->len, |
| 4888 | input_data->x, input_data->len, |
| 4889 | output_data, output_size, |
| 4890 | &output_length); |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4891 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4892 | /* If the operation is not supported, just skip and not fail in case the |
| 4893 | * decryption involves a common limitation of cryptography hardwares and |
| 4894 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4895 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4896 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4897 | 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] | 4898 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4899 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4900 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4901 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4902 | if (expected_result == PSA_SUCCESS) { |
| 4903 | ASSERT_COMPARE(expected_data->x, expected_data->len, |
| 4904 | output_data, output_length); |
| 4905 | } |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4906 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4907 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4908 | psa_destroy_key(key); |
| 4909 | mbedtls_free(output_data); |
| 4910 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4911 | } |
| 4912 | /* END_CASE */ |
| 4913 | |
| 4914 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4915 | void aead_multipart_encrypt(int key_type_arg, data_t *key_data, |
| 4916 | int alg_arg, |
| 4917 | data_t *nonce, |
| 4918 | data_t *additional_data, |
| 4919 | data_t *input_data, |
| 4920 | int do_set_lengths, |
| 4921 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4922 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4923 | size_t ad_part_len = 0; |
| 4924 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4925 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4926 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4927 | for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) { |
| 4928 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4929 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4930 | if (do_set_lengths) { |
| 4931 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4932 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4933 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4934 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4935 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4936 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4937 | |
| 4938 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4939 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4940 | alg_arg, nonce, |
| 4941 | additional_data, |
| 4942 | ad_part_len, |
| 4943 | input_data, -1, |
| 4944 | set_lengths_method, |
| 4945 | expected_output, |
| 4946 | 1, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4947 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4948 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4949 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4950 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 4951 | mbedtls_test_set_step(1000 + ad_part_len); |
| 4952 | |
| 4953 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4954 | alg_arg, nonce, |
| 4955 | additional_data, |
| 4956 | ad_part_len, |
| 4957 | input_data, -1, |
| 4958 | set_lengths_method, |
| 4959 | expected_output, |
| 4960 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4961 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4962 | } |
| 4963 | } |
| 4964 | |
| 4965 | for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) { |
| 4966 | /* Split data into length(data_part_len) parts. */ |
| 4967 | mbedtls_test_set_step(2000 + data_part_len); |
| 4968 | |
| 4969 | if (do_set_lengths) { |
| 4970 | if (data_part_len & 0x01) { |
| 4971 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4972 | } else { |
| 4973 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
| 4974 | } |
| 4975 | } |
| 4976 | |
| 4977 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4978 | alg_arg, nonce, |
| 4979 | additional_data, -1, |
| 4980 | input_data, data_part_len, |
| 4981 | set_lengths_method, |
| 4982 | expected_output, |
| 4983 | 1, 0)) { |
| 4984 | break; |
| 4985 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4986 | |
| 4987 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4988 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4989 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4990 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4991 | alg_arg, nonce, |
| 4992 | additional_data, -1, |
| 4993 | input_data, data_part_len, |
| 4994 | set_lengths_method, |
| 4995 | expected_output, |
| 4996 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4997 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4998 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4999 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5000 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5001 | /* Goto is required to silence warnings about unused labels, as we |
| 5002 | * don't actually do any test assertions in this function. */ |
| 5003 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5004 | } |
| 5005 | /* END_CASE */ |
| 5006 | |
| 5007 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5008 | void aead_multipart_decrypt(int key_type_arg, data_t *key_data, |
| 5009 | int alg_arg, |
| 5010 | data_t *nonce, |
| 5011 | data_t *additional_data, |
| 5012 | data_t *input_data, |
| 5013 | int do_set_lengths, |
| 5014 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5015 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5016 | size_t ad_part_len = 0; |
| 5017 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5018 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5019 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5020 | 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] | 5021 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5022 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5023 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5024 | if (do_set_lengths) { |
| 5025 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5026 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5027 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5028 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5029 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5030 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5031 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5032 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5033 | alg_arg, nonce, |
| 5034 | additional_data, |
| 5035 | ad_part_len, |
| 5036 | input_data, -1, |
| 5037 | set_lengths_method, |
| 5038 | expected_output, |
| 5039 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5040 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5041 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5042 | |
| 5043 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5044 | mbedtls_test_set_step(1000 + ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5045 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5046 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5047 | alg_arg, nonce, |
| 5048 | additional_data, |
| 5049 | ad_part_len, |
| 5050 | input_data, -1, |
| 5051 | set_lengths_method, |
| 5052 | expected_output, |
| 5053 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5054 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5055 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5056 | } |
| 5057 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5058 | 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] | 5059 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5060 | mbedtls_test_set_step(2000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5061 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5062 | if (do_set_lengths) { |
| 5063 | if (data_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5064 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5065 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5066 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5067 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5068 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5069 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5070 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5071 | alg_arg, nonce, |
| 5072 | additional_data, -1, |
| 5073 | input_data, data_part_len, |
| 5074 | set_lengths_method, |
| 5075 | expected_output, |
| 5076 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5077 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5078 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5079 | |
| 5080 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5081 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5082 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5083 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5084 | alg_arg, nonce, |
| 5085 | additional_data, -1, |
| 5086 | input_data, data_part_len, |
| 5087 | set_lengths_method, |
| 5088 | expected_output, |
| 5089 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5090 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5091 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5092 | } |
| 5093 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5094 | /* Goto is required to silence warnings about unused labels, as we |
| 5095 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5096 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5097 | } |
| 5098 | /* END_CASE */ |
| 5099 | |
| 5100 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5101 | void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data, |
| 5102 | int alg_arg, |
| 5103 | int nonce_length, |
| 5104 | int expected_nonce_length_arg, |
| 5105 | data_t *additional_data, |
| 5106 | data_t *input_data, |
| 5107 | int expected_status_arg) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5108 | { |
| 5109 | |
| 5110 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5111 | psa_key_type_t key_type = key_type_arg; |
| 5112 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5113 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5114 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5115 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5116 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5117 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5118 | size_t actual_nonce_length = 0; |
| 5119 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 5120 | unsigned char *output = NULL; |
| 5121 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5122 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5123 | size_t ciphertext_size = 0; |
| 5124 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5125 | size_t tag_length = 0; |
| 5126 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5127 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5128 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5129 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5130 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5131 | psa_set_key_algorithm(&attributes, alg); |
| 5132 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5133 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5134 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5135 | &key)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5136 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5137 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
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 | 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] | 5140 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5141 | ASSERT_ALLOC(output, output_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5142 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5143 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5144 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5145 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5146 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5147 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5149 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5150 | |
| 5151 | /* If the operation is not supported, just skip and not fail in case the |
| 5152 | * encryption involves a common limitation of cryptography hardwares and |
| 5153 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5154 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5155 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5156 | 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] | 5157 | } |
| 5158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5159 | PSA_ASSERT(status); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5160 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5161 | status = psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5162 | nonce_length, |
| 5163 | &actual_nonce_length); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5164 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5165 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5166 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5167 | TEST_EQUAL(actual_nonce_length, expected_nonce_length); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 5168 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5169 | if (expected_status == PSA_SUCCESS) { |
| 5170 | TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type, |
| 5171 | alg)); |
| 5172 | } |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 5173 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5174 | TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 5175 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5176 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5177 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5178 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5179 | input_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5181 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5182 | additional_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5183 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5184 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5185 | output, output_size, |
| 5186 | &ciphertext_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5187 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5188 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5189 | &ciphertext_length, tag_buffer, |
| 5190 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5191 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5192 | |
| 5193 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5194 | psa_destroy_key(key); |
| 5195 | mbedtls_free(output); |
| 5196 | mbedtls_free(ciphertext); |
| 5197 | psa_aead_abort(&operation); |
| 5198 | PSA_DONE(); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5199 | } |
| 5200 | /* END_CASE */ |
| 5201 | |
| 5202 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5203 | void aead_multipart_set_nonce(int key_type_arg, data_t *key_data, |
| 5204 | int alg_arg, |
| 5205 | int nonce_length_arg, |
| 5206 | int set_lengths_method_arg, |
| 5207 | data_t *additional_data, |
| 5208 | data_t *input_data, |
| 5209 | int expected_status_arg) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5210 | { |
| 5211 | |
| 5212 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5213 | psa_key_type_t key_type = key_type_arg; |
| 5214 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5215 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5216 | uint8_t *nonce_buffer = NULL; |
| 5217 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5218 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5219 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5220 | unsigned char *output = NULL; |
| 5221 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5222 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5223 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5224 | size_t ciphertext_size = 0; |
| 5225 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5226 | size_t tag_length = 0; |
| 5227 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5228 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5229 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5230 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5231 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5232 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5233 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5234 | psa_set_key_algorithm(&attributes, alg); |
| 5235 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5236 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5237 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5238 | &key)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5239 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5240 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
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 | 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] | 5243 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5244 | ASSERT_ALLOC(output, output_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5245 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5246 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
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 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5249 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5250 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5252 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5253 | |
| 5254 | /* If the operation is not supported, just skip and not fail in case the |
| 5255 | * encryption involves a common limitation of cryptography hardwares and |
| 5256 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5257 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5258 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5259 | 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] | 5260 | } |
| 5261 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5262 | PSA_ASSERT(status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5263 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5264 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5265 | if (nonce_length_arg == -1) { |
| 5266 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
| 5267 | ASSERT_ALLOC(nonce_buffer, 4); |
| 5268 | nonce_length = 0; |
| 5269 | } else { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5270 | /* If length is zero, then this will return NULL. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5271 | nonce_length = (size_t) nonce_length_arg; |
| 5272 | ASSERT_ALLOC(nonce_buffer, nonce_length); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5273 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5274 | if (nonce_buffer) { |
| 5275 | for (index = 0; index < nonce_length - 1; ++index) { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5276 | nonce_buffer[index] = 'a' + index; |
| 5277 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5278 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5279 | } |
| 5280 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5281 | if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { |
| 5282 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5283 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5284 | } |
| 5285 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5286 | status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5287 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5288 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5289 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5290 | if (expected_status == PSA_SUCCESS) { |
| 5291 | if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) { |
| 5292 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5293 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5294 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5295 | 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] | 5296 | expected_status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5297 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5298 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5299 | /* 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] | 5300 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5301 | additional_data->len), |
| 5302 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5303 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5304 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5305 | output, output_size, |
| 5306 | &ciphertext_length), |
| 5307 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5308 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5309 | TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5310 | &ciphertext_length, tag_buffer, |
| 5311 | PSA_AEAD_TAG_MAX_SIZE, &tag_length), |
| 5312 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5313 | } |
| 5314 | |
| 5315 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5316 | psa_destroy_key(key); |
| 5317 | mbedtls_free(output); |
| 5318 | mbedtls_free(ciphertext); |
| 5319 | mbedtls_free(nonce_buffer); |
| 5320 | psa_aead_abort(&operation); |
| 5321 | PSA_DONE(); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5322 | } |
| 5323 | /* END_CASE */ |
| 5324 | |
| 5325 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5326 | 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] | 5327 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5328 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5329 | data_t *nonce, |
| 5330 | data_t *additional_data, |
| 5331 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5332 | int expected_status_arg) |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5333 | { |
| 5334 | |
| 5335 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5336 | psa_key_type_t key_type = key_type_arg; |
| 5337 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5338 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5339 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5340 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5341 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5342 | unsigned char *output = NULL; |
| 5343 | unsigned char *ciphertext = NULL; |
| 5344 | size_t output_size = output_size_arg; |
| 5345 | size_t ciphertext_size = 0; |
| 5346 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5347 | size_t tag_length = 0; |
| 5348 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5349 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5350 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5352 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5353 | psa_set_key_algorithm(&attributes, alg); |
| 5354 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5356 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5357 | &key)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5358 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5359 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5360 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5361 | ASSERT_ALLOC(output, output_size); |
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 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5364 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5365 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5367 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5368 | |
| 5369 | /* If the operation is not supported, just skip and not fail in case the |
| 5370 | * encryption involves a common limitation of cryptography hardwares and |
| 5371 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5372 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5373 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5374 | 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] | 5375 | } |
| 5376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5377 | PSA_ASSERT(status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5378 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5379 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5380 | input_data->len)); |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5381 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5382 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5383 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5384 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5385 | additional_data->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5386 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5387 | status = psa_aead_update(&operation, input_data->x, input_data->len, |
| 5388 | output, output_size, &ciphertext_length); |
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 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5392 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5393 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5394 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5395 | &ciphertext_length, tag_buffer, |
| 5396 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5397 | } |
| 5398 | |
| 5399 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5400 | psa_destroy_key(key); |
| 5401 | mbedtls_free(output); |
| 5402 | mbedtls_free(ciphertext); |
| 5403 | psa_aead_abort(&operation); |
| 5404 | PSA_DONE(); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5405 | } |
| 5406 | /* END_CASE */ |
| 5407 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5408 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5409 | void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data, |
| 5410 | int alg_arg, |
| 5411 | int finish_ciphertext_size_arg, |
| 5412 | int tag_size_arg, |
| 5413 | data_t *nonce, |
| 5414 | data_t *additional_data, |
| 5415 | data_t *input_data, |
| 5416 | int expected_status_arg) |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5417 | { |
| 5418 | |
| 5419 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5420 | psa_key_type_t key_type = key_type_arg; |
| 5421 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5422 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5423 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5424 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5425 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5426 | unsigned char *ciphertext = NULL; |
| 5427 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5428 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5429 | size_t ciphertext_size = 0; |
| 5430 | size_t ciphertext_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5431 | size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg; |
| 5432 | size_t tag_size = (size_t) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5433 | size_t tag_length = 0; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5434 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5435 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5436 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5437 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5438 | psa_set_key_algorithm(&attributes, alg); |
| 5439 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5440 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5441 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5442 | &key)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5444 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
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 | 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] | 5447 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5448 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5449 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5450 | ASSERT_ALLOC(finish_ciphertext, finish_ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5451 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5452 | ASSERT_ALLOC(tag_buffer, tag_size); |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5453 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5454 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5455 | |
| 5456 | /* If the operation is not supported, just skip and not fail in case the |
| 5457 | * encryption involves a common limitation of cryptography hardwares and |
| 5458 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5459 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5460 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5461 | 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] | 5462 | } |
| 5463 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5464 | PSA_ASSERT(status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5465 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5466 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5467 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5468 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5469 | input_data->len)); |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5470 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5471 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5472 | additional_data->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5473 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5474 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5475 | ciphertext, ciphertext_size, &ciphertext_length)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5476 | |
| 5477 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5478 | status = psa_aead_finish(&operation, finish_ciphertext, |
| 5479 | finish_ciphertext_size, |
| 5480 | &ciphertext_length, tag_buffer, |
| 5481 | tag_size, &tag_length); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5482 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5483 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5484 | |
| 5485 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5486 | psa_destroy_key(key); |
| 5487 | mbedtls_free(ciphertext); |
| 5488 | mbedtls_free(finish_ciphertext); |
| 5489 | mbedtls_free(tag_buffer); |
| 5490 | psa_aead_abort(&operation); |
| 5491 | PSA_DONE(); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5492 | } |
| 5493 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5494 | |
| 5495 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5496 | void aead_multipart_verify(int key_type_arg, data_t *key_data, |
| 5497 | int alg_arg, |
| 5498 | data_t *nonce, |
| 5499 | data_t *additional_data, |
| 5500 | data_t *input_data, |
| 5501 | data_t *tag, |
| 5502 | int tag_usage_arg, |
| 5503 | int expected_setup_status_arg, |
| 5504 | int expected_status_arg) |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5505 | { |
| 5506 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5507 | psa_key_type_t key_type = key_type_arg; |
| 5508 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5509 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5510 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5511 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5512 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5513 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5514 | unsigned char *plaintext = NULL; |
| 5515 | unsigned char *finish_plaintext = NULL; |
| 5516 | size_t plaintext_size = 0; |
| 5517 | size_t plaintext_length = 0; |
| 5518 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5519 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5520 | unsigned char *tag_buffer = NULL; |
| 5521 | size_t tag_size = 0; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5522 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5523 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5524 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5525 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 5526 | psa_set_key_algorithm(&attributes, alg); |
| 5527 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5528 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5529 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5530 | &key)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5531 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5532 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
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 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 5535 | input_data->len); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5536 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5537 | ASSERT_ALLOC(plaintext, plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5538 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5539 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5540 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5541 | ASSERT_ALLOC(finish_plaintext, verify_plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5542 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5543 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5544 | |
| 5545 | /* If the operation is not supported, just skip and not fail in case the |
| 5546 | * encryption involves a common limitation of cryptography hardwares and |
| 5547 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5548 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5549 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5550 | 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] | 5551 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5552 | TEST_EQUAL(status, expected_setup_status); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5553 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5554 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5555 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5556 | } |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5557 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5558 | PSA_ASSERT(status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5559 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5560 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5562 | status = psa_aead_set_lengths(&operation, additional_data->len, |
| 5563 | input_data->len); |
| 5564 | PSA_ASSERT(status); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5565 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5566 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5567 | additional_data->len)); |
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(psa_aead_update(&operation, input_data->x, |
| 5570 | input_data->len, |
| 5571 | plaintext, plaintext_size, |
| 5572 | &plaintext_length)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5573 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5574 | if (tag_usage == USE_GIVEN_TAG) { |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5575 | tag_buffer = tag->x; |
| 5576 | tag_size = tag->len; |
| 5577 | } |
| 5578 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5579 | status = psa_aead_verify(&operation, finish_plaintext, |
| 5580 | verify_plaintext_size, |
| 5581 | &plaintext_length, |
| 5582 | tag_buffer, tag_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5583 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5584 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5585 | |
| 5586 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5587 | psa_destroy_key(key); |
| 5588 | mbedtls_free(plaintext); |
| 5589 | mbedtls_free(finish_plaintext); |
| 5590 | psa_aead_abort(&operation); |
| 5591 | PSA_DONE(); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5592 | } |
| 5593 | /* END_CASE */ |
| 5594 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5595 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5596 | void aead_multipart_setup(int key_type_arg, data_t *key_data, |
| 5597 | int alg_arg, int expected_status_arg) |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5598 | { |
| 5599 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5600 | psa_key_type_t key_type = key_type_arg; |
| 5601 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5602 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5603 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5604 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5605 | psa_status_t expected_status = expected_status_arg; |
| 5606 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5607 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5608 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5609 | psa_set_key_usage_flags(&attributes, |
| 5610 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5611 | psa_set_key_algorithm(&attributes, alg); |
| 5612 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5613 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5614 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5615 | &key)); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5616 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5617 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5618 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5619 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5620 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5621 | psa_aead_abort(&operation); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5622 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5623 | status = psa_aead_decrypt_setup(&operation, key, alg); |
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 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5626 | |
| 5627 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5628 | psa_destroy_key(key); |
| 5629 | psa_aead_abort(&operation); |
| 5630 | PSA_DONE(); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5631 | } |
| 5632 | /* END_CASE */ |
| 5633 | |
| 5634 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5635 | void aead_multipart_state_test(int key_type_arg, data_t *key_data, |
| 5636 | int alg_arg, |
| 5637 | data_t *nonce, |
| 5638 | data_t *additional_data, |
| 5639 | data_t *input_data) |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5640 | { |
| 5641 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5642 | psa_key_type_t key_type = key_type_arg; |
| 5643 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5644 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5645 | unsigned char *output_data = NULL; |
| 5646 | unsigned char *final_data = NULL; |
| 5647 | size_t output_size = 0; |
| 5648 | size_t finish_output_size = 0; |
| 5649 | size_t output_length = 0; |
| 5650 | size_t key_bits = 0; |
| 5651 | size_t tag_length = 0; |
| 5652 | size_t tag_size = 0; |
| 5653 | size_t nonce_length = 0; |
| 5654 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5655 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5656 | size_t output_part_length = 0; |
| 5657 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5658 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5659 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5660 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5661 | psa_set_key_usage_flags(&attributes, |
| 5662 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5663 | psa_set_key_algorithm(&attributes, alg); |
| 5664 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5665 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5666 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5667 | &key)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5668 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5669 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5670 | key_bits = psa_get_key_bits(&attributes); |
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 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5673 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5674 | TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5676 | 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] | 5677 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5678 | ASSERT_ALLOC(output_data, output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5679 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5680 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5681 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5682 | TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5683 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5684 | ASSERT_ALLOC(final_data, finish_output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5685 | |
| 5686 | /* Test all operations error without calling setup first. */ |
| 5687 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5688 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5689 | PSA_ERROR_BAD_STATE); |
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 | psa_aead_abort(&operation); |
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_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5694 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5695 | &nonce_length), |
| 5696 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5697 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5698 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5699 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5700 | /* ------------------------------------------------------- */ |
| 5701 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5702 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 5703 | input_data->len), |
| 5704 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5705 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5706 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5707 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5708 | /* ------------------------------------------------------- */ |
| 5709 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5710 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5711 | additional_data->len), |
| 5712 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5713 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5714 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5715 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5716 | /* ------------------------------------------------------- */ |
| 5717 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5718 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 5719 | input_data->len, output_data, |
| 5720 | output_size, &output_length), |
| 5721 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5722 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5723 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5724 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5725 | /* ------------------------------------------------------- */ |
| 5726 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5727 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 5728 | finish_output_size, |
| 5729 | &output_part_length, |
| 5730 | tag_buffer, tag_length, |
| 5731 | &tag_size), |
| 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_verify(&operation, final_data, |
| 5739 | finish_output_size, |
| 5740 | &output_part_length, |
| 5741 | tag_buffer, |
| 5742 | tag_length), |
| 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 | |
| 5747 | /* Test for double setups. */ |
| 5748 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5749 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5750 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5751 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 5752 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5753 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5754 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5755 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5756 | /* ------------------------------------------------------- */ |
| 5757 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5758 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5759 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5760 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 5761 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5762 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5763 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5764 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5765 | /* ------------------------------------------------------- */ |
| 5766 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5767 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5768 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5769 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 5770 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5771 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5772 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5773 | |
| 5774 | /* ------------------------------------------------------- */ |
| 5775 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5776 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5777 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5778 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 5779 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5780 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5781 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5782 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5783 | /* Test for not setting a nonce. */ |
| 5784 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5785 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5786 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5787 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5788 | additional_data->len), |
| 5789 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5790 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5791 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5792 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5793 | /* ------------------------------------------------------- */ |
| 5794 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5795 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5796 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5797 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 5798 | input_data->len, output_data, |
| 5799 | output_size, &output_length), |
| 5800 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5801 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5802 | psa_aead_abort(&operation); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5803 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +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 | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5807 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5808 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 5809 | finish_output_size, |
| 5810 | &output_part_length, |
| 5811 | tag_buffer, tag_length, |
| 5812 | &tag_size), |
| 5813 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5814 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5815 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5816 | |
| 5817 | /* ------------------------------------------------------- */ |
| 5818 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5819 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5820 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5821 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 5822 | finish_output_size, |
| 5823 | &output_part_length, |
| 5824 | tag_buffer, |
| 5825 | tag_length), |
| 5826 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5827 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5828 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5829 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5830 | /* Test for double setting nonce. */ |
| 5831 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5832 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5833 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5834 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5835 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5836 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5837 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5838 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5839 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5840 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5841 | /* Test for double generating 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 | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5844 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5845 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5846 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5847 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5848 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5849 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5850 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5851 | &nonce_length), |
| 5852 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5853 | |
| 5854 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5855 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5856 | |
| 5857 | /* Test for generate nonce then set and vice versa */ |
| 5858 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5859 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5860 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5861 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5862 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5863 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5864 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5865 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5866 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5867 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5868 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5869 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5870 | /* Test for generating nonce after calling set lengths */ |
| 5871 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5872 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5873 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5874 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5875 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5876 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5877 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5878 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5879 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5880 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5881 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5882 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5883 | /* 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] | 5884 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5885 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5886 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5887 | if (operation.alg == PSA_ALG_CCM) { |
| 5888 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5889 | input_data->len), |
| 5890 | PSA_ERROR_INVALID_ARGUMENT); |
| 5891 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5892 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5893 | &nonce_length), |
| 5894 | PSA_ERROR_BAD_STATE); |
| 5895 | } else { |
| 5896 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5897 | input_data->len)); |
| 5898 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5899 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5900 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5901 | } |
| 5902 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5903 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5904 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5905 | /* 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] | 5906 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5907 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5908 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5909 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 5910 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5911 | input_data->len), |
| 5912 | PSA_ERROR_INVALID_ARGUMENT); |
| 5913 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5914 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5915 | &nonce_length), |
| 5916 | PSA_ERROR_BAD_STATE); |
| 5917 | } else { |
| 5918 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5919 | input_data->len)); |
| 5920 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5921 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5922 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5923 | } |
| 5924 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5925 | psa_aead_abort(&operation); |
Dave Rodgman | d26d744 | 2023-02-11 17:14:54 +0000 | [diff] [blame] | 5926 | #endif |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5927 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5928 | /* 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] | 5929 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5930 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5931 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5932 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5933 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5934 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5935 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5936 | if (operation.alg == PSA_ALG_CCM) { |
| 5937 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5938 | input_data->len), |
| 5939 | PSA_ERROR_INVALID_ARGUMENT); |
| 5940 | } else { |
| 5941 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5942 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5943 | } |
| 5944 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5945 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5946 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5947 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5948 | /* Test for setting nonce after calling set lengths */ |
| 5949 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5950 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5951 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5952 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5953 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5954 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5955 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5956 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5957 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5958 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5959 | /* 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] | 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 | if (operation.alg == PSA_ALG_CCM) { |
| 5964 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5965 | input_data->len), |
| 5966 | PSA_ERROR_INVALID_ARGUMENT); |
| 5967 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5968 | PSA_ERROR_BAD_STATE); |
| 5969 | } else { |
| 5970 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5971 | input_data->len)); |
| 5972 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5973 | } |
| 5974 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5975 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5976 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5977 | /* 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] | 5978 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5979 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5980 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5981 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 5982 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5983 | input_data->len), |
| 5984 | PSA_ERROR_INVALID_ARGUMENT); |
| 5985 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5986 | PSA_ERROR_BAD_STATE); |
| 5987 | } else { |
| 5988 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5989 | input_data->len)); |
| 5990 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5991 | } |
| 5992 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5993 | psa_aead_abort(&operation); |
Dave Rodgman | a476363 | 2023-02-11 18:36:23 +0000 | [diff] [blame] | 5994 | #endif |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5995 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5996 | /* 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] | 5997 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5998 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5999 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6000 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6001 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6002 | if (operation.alg == PSA_ALG_CCM) { |
| 6003 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6004 | input_data->len), |
| 6005 | PSA_ERROR_INVALID_ARGUMENT); |
| 6006 | } else { |
| 6007 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6008 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6009 | } |
| 6010 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6011 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6012 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6013 | /* 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] | 6014 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6015 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6016 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6017 | if (operation.alg == PSA_ALG_GCM) { |
| 6018 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6019 | SIZE_MAX), |
| 6020 | PSA_ERROR_INVALID_ARGUMENT); |
| 6021 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6022 | PSA_ERROR_BAD_STATE); |
| 6023 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6024 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6025 | SIZE_MAX)); |
| 6026 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6027 | } |
| 6028 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6029 | psa_aead_abort(&operation); |
Dave Rodgman | 91e8321 | 2023-02-11 20:07:43 +0000 | [diff] [blame] | 6030 | #endif |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6031 | |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 6032 | /* 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] | 6033 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6034 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6035 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6036 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6037 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6038 | if (operation.alg == PSA_ALG_GCM) { |
| 6039 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6040 | SIZE_MAX), |
| 6041 | PSA_ERROR_INVALID_ARGUMENT); |
| 6042 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6043 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6044 | SIZE_MAX)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6045 | } |
| 6046 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6047 | psa_aead_abort(&operation); |
Dave Rodgman | 641288b | 2023-02-11 22:02:04 +0000 | [diff] [blame] | 6048 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6049 | |
| 6050 | /* ------------------------------------------------------- */ |
| 6051 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6052 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6053 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6054 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6055 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6056 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6057 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6058 | &nonce_length), |
| 6059 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6060 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6061 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6062 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6063 | /* Test for generating nonce in decrypt setup. */ |
| 6064 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6065 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +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 | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6071 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6072 | psa_aead_abort(&operation); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6073 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6074 | /* Test for setting lengths twice. */ |
| 6075 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6076 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6077 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6078 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6079 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6080 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6081 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6082 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6083 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6084 | input_data->len), |
| 6085 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6086 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6087 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6088 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6089 | /* Test for setting lengths after setting nonce + already starting data. */ |
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_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6092 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6093 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6094 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6095 | if (operation.alg == PSA_ALG_CCM) { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6096 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6097 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6098 | additional_data->len), |
| 6099 | PSA_ERROR_BAD_STATE); |
| 6100 | } else { |
| 6101 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6102 | additional_data->len)); |
| 6103 | |
| 6104 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6105 | input_data->len), |
| 6106 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6107 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6108 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6109 | |
| 6110 | /* ------------------------------------------------------- */ |
| 6111 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6112 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6114 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6115 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6116 | if (operation.alg == PSA_ALG_CCM) { |
| 6117 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6118 | input_data->len, output_data, |
| 6119 | output_size, &output_length), |
| 6120 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6121 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6122 | } else { |
| 6123 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6124 | input_data->len, output_data, |
| 6125 | output_size, &output_length)); |
| 6126 | |
| 6127 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6128 | input_data->len), |
| 6129 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6130 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6131 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6132 | |
| 6133 | /* ------------------------------------------------------- */ |
| 6134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6135 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6136 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6137 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6138 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6139 | if (operation.alg == PSA_ALG_CCM) { |
| 6140 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6141 | finish_output_size, |
| 6142 | &output_part_length, |
| 6143 | tag_buffer, tag_length, |
| 6144 | &tag_size)); |
| 6145 | } else { |
| 6146 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6147 | finish_output_size, |
| 6148 | &output_part_length, |
| 6149 | tag_buffer, tag_length, |
| 6150 | &tag_size)); |
| 6151 | |
| 6152 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6153 | input_data->len), |
| 6154 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6155 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6156 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6157 | |
| 6158 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 6159 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6160 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6161 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6162 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6163 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6164 | &nonce_length)); |
| 6165 | if (operation.alg == PSA_ALG_CCM) { |
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 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6168 | additional_data->len), |
| 6169 | PSA_ERROR_BAD_STATE); |
| 6170 | } else { |
| 6171 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6172 | additional_data->len)); |
| 6173 | |
| 6174 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6175 | input_data->len), |
| 6176 | PSA_ERROR_BAD_STATE); |
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 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6179 | |
| 6180 | /* ------------------------------------------------------- */ |
| 6181 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6182 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6183 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6184 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6185 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6186 | &nonce_length)); |
| 6187 | if (operation.alg == PSA_ALG_CCM) { |
| 6188 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6189 | input_data->len, output_data, |
| 6190 | output_size, &output_length), |
| 6191 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6192 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6193 | } else { |
| 6194 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6195 | input_data->len, output_data, |
| 6196 | output_size, &output_length)); |
| 6197 | |
| 6198 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6199 | input_data->len), |
| 6200 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6201 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6202 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6203 | |
| 6204 | /* ------------------------------------------------------- */ |
| 6205 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6206 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6207 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6208 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6209 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6210 | &nonce_length)); |
| 6211 | if (operation.alg == PSA_ALG_CCM) { |
| 6212 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6213 | finish_output_size, |
| 6214 | &output_part_length, |
| 6215 | tag_buffer, tag_length, |
| 6216 | &tag_size)); |
| 6217 | } else { |
| 6218 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6219 | finish_output_size, |
| 6220 | &output_part_length, |
| 6221 | tag_buffer, tag_length, |
| 6222 | &tag_size)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6223 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6224 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6225 | input_data->len), |
| 6226 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6227 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6228 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6229 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6230 | /* Test for not sending any additional data or data after setting non zero |
| 6231 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6232 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6233 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6234 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6235 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6236 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6237 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6238 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6239 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6240 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6241 | finish_output_size, |
| 6242 | &output_part_length, |
| 6243 | tag_buffer, tag_length, |
| 6244 | &tag_size), |
| 6245 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6246 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6247 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6248 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6249 | /* Test for not sending any additional data or data after setting non-zero |
| 6250 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6252 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6253 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6254 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6256 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6257 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6258 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6259 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6260 | finish_output_size, |
| 6261 | &output_part_length, |
| 6262 | tag_buffer, |
| 6263 | tag_length), |
| 6264 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6266 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6267 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6268 | /* Test for not sending any additional data after setting a non-zero length |
| 6269 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6270 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6271 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6272 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6273 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6274 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6275 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6276 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6277 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6278 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6279 | input_data->len, output_data, |
| 6280 | output_size, &output_length), |
| 6281 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6282 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6283 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6284 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6285 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 6286 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6287 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6288 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6289 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6290 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6291 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6292 | input_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6293 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6294 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6295 | additional_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6296 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6297 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6298 | finish_output_size, |
| 6299 | &output_part_length, |
| 6300 | tag_buffer, tag_length, |
| 6301 | &tag_size), |
| 6302 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6303 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6304 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6305 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6306 | /* Test for sending too much additional data after setting lengths. */ |
| 6307 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6308 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6309 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6310 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6311 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6312 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6313 | |
| 6314 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6315 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6316 | additional_data->len), |
| 6317 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6318 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6319 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6320 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6321 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6322 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6323 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6324 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6325 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6326 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6327 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6328 | input_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6329 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6330 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6331 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6332 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6333 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6334 | 1), |
| 6335 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6336 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6337 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6338 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6339 | /* Test for sending too much data after setting lengths. */ |
| 6340 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6341 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6342 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6343 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6344 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6345 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6346 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6347 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6348 | input_data->len, output_data, |
| 6349 | output_size, &output_length), |
| 6350 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6352 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6353 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6354 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6356 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6358 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6359 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6360 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6361 | input_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6362 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6363 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6364 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6365 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6366 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6367 | input_data->len, output_data, |
| 6368 | output_size, &output_length)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6370 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6371 | 1, output_data, |
| 6372 | output_size, &output_length), |
| 6373 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6374 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6375 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6376 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6377 | /* Test sending additional data after data. */ |
| 6378 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6379 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6380 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6381 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6382 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6383 | if (operation.alg != PSA_ALG_CCM) { |
| 6384 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6385 | input_data->len, output_data, |
| 6386 | output_size, &output_length)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6387 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6388 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6389 | additional_data->len), |
| 6390 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6391 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6392 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6393 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6394 | /* Test calling finish on decryption. */ |
| 6395 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6396 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6397 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6398 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6399 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6400 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6401 | finish_output_size, |
| 6402 | &output_part_length, |
| 6403 | tag_buffer, tag_length, |
| 6404 | &tag_size), |
| 6405 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6406 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6407 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6408 | |
| 6409 | /* Test calling verify on encryption. */ |
| 6410 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6411 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6412 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6413 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6414 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6415 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6416 | finish_output_size, |
| 6417 | &output_part_length, |
| 6418 | tag_buffer, |
| 6419 | tag_length), |
| 6420 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6421 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6422 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6423 | |
| 6424 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6425 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6426 | psa_destroy_key(key); |
| 6427 | psa_aead_abort(&operation); |
| 6428 | mbedtls_free(output_data); |
| 6429 | mbedtls_free(final_data); |
| 6430 | PSA_DONE(); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6431 | } |
| 6432 | /* END_CASE */ |
| 6433 | |
| 6434 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6435 | void signature_size(int type_arg, |
| 6436 | int bits, |
| 6437 | int alg_arg, |
| 6438 | int expected_size_arg) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6439 | { |
| 6440 | psa_key_type_t type = type_arg; |
| 6441 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6442 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6444 | TEST_EQUAL(actual_size, (size_t) expected_size_arg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6445 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6446 | exit: |
| 6447 | ; |
| 6448 | } |
| 6449 | /* END_CASE */ |
| 6450 | |
| 6451 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6452 | void sign_hash_deterministic(int key_type_arg, data_t *key_data, |
| 6453 | int alg_arg, data_t *input_data, |
| 6454 | data_t *output_data) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6455 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6456 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6457 | psa_key_type_t key_type = key_type_arg; |
| 6458 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6459 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6460 | unsigned char *signature = NULL; |
| 6461 | size_t signature_size; |
| 6462 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6463 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6464 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6465 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6466 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6467 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6468 | psa_set_key_algorithm(&attributes, alg); |
| 6469 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6470 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6471 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6472 | &key)); |
| 6473 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6474 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6475 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6476 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6477 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6478 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6479 | key_bits, alg); |
| 6480 | TEST_ASSERT(signature_size != 0); |
| 6481 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6482 | ASSERT_ALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6483 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6484 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6485 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6486 | input_data->x, input_data->len, |
| 6487 | signature, signature_size, |
| 6488 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6489 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6490 | ASSERT_COMPARE(output_data->x, output_data->len, |
| 6491 | signature, signature_length); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6492 | |
| 6493 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6494 | /* |
| 6495 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6496 | * thus reset them as required. |
| 6497 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6498 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6499 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6500 | psa_destroy_key(key); |
| 6501 | mbedtls_free(signature); |
| 6502 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6503 | } |
| 6504 | /* END_CASE */ |
| 6505 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6506 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6507 | /** |
| 6508 | * sign_hash_interruptible() test intentions: |
| 6509 | * |
| 6510 | * Note: This test can currently only handle ECDSA. |
| 6511 | * |
| 6512 | * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 6513 | * and private keys / keypairs only). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6514 | * |
| 6515 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 6516 | * expected for different max_ops values. |
| 6517 | * |
| 6518 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 6519 | * and that each successful stage completes some ops (this is not mandated by |
| 6520 | * the PSA specification, but is currently the case). |
| 6521 | * |
| 6522 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 6523 | * complete() calls does not alter the number of ops returned. |
| 6524 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6525 | void sign_hash_interruptible(int key_type_arg, data_t *key_data, |
| 6526 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6527 | data_t *output_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6528 | { |
| 6529 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6530 | psa_key_type_t key_type = key_type_arg; |
| 6531 | psa_algorithm_t alg = alg_arg; |
| 6532 | size_t key_bits; |
| 6533 | unsigned char *signature = NULL; |
| 6534 | size_t signature_size; |
| 6535 | size_t signature_length = 0xdeadbeef; |
| 6536 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6537 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6538 | uint32_t num_ops = 0; |
| 6539 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6540 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6541 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6542 | size_t min_completes = 0; |
| 6543 | size_t max_completes = 0; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6544 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6545 | psa_sign_hash_interruptible_operation_t operation = |
| 6546 | psa_sign_hash_interruptible_operation_init(); |
| 6547 | |
| 6548 | PSA_ASSERT(psa_crypto_init()); |
| 6549 | |
| 6550 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6551 | psa_set_key_algorithm(&attributes, alg); |
| 6552 | psa_set_key_type(&attributes, key_type); |
| 6553 | |
| 6554 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6555 | &key)); |
| 6556 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6557 | key_bits = psa_get_key_bits(&attributes); |
| 6558 | |
| 6559 | /* Allocate a buffer which has the size advertised by the |
| 6560 | * library. */ |
| 6561 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6562 | key_bits, alg); |
| 6563 | TEST_ASSERT(signature_size != 0); |
| 6564 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6565 | ASSERT_ALLOC(signature, signature_size); |
| 6566 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6567 | psa_interruptible_set_max_ops(max_ops); |
| 6568 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6569 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6570 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6571 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6572 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6573 | TEST_ASSERT(num_ops_prior == 0); |
| 6574 | |
| 6575 | /* Start performing the signature. */ |
| 6576 | PSA_ASSERT(psa_sign_hash_start(&operation, key, alg, |
| 6577 | input_data->x, input_data->len)); |
| 6578 | |
| 6579 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6580 | TEST_ASSERT(num_ops_prior == 0); |
| 6581 | |
| 6582 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6583 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6584 | status = psa_sign_hash_complete(&operation, signature, signature_size, |
| 6585 | &signature_length); |
| 6586 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6587 | num_completes++; |
| 6588 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6589 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 6590 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6591 | /* We are asserting here that every complete makes progress |
| 6592 | * (completes some ops), which is true of the internal |
| 6593 | * implementation and probably any implementation, however this is |
| 6594 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6595 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6596 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6597 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 6598 | |
| 6599 | /* Ensure calling get_num_ops() twice still returns the same |
| 6600 | * number of ops as previously reported. */ |
| 6601 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6602 | |
| 6603 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6604 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6605 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6606 | |
| 6607 | TEST_ASSERT(status == PSA_SUCCESS); |
| 6608 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6609 | TEST_LE_U(min_completes, num_completes); |
| 6610 | TEST_LE_U(num_completes, max_completes); |
| 6611 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6612 | /* Verify that the signature is what is expected. */ |
| 6613 | ASSERT_COMPARE(output_data->x, output_data->len, |
| 6614 | signature, signature_length); |
| 6615 | |
| 6616 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6617 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6618 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6619 | TEST_ASSERT(num_ops == 0); |
| 6620 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6621 | exit: |
| 6622 | |
| 6623 | /* |
| 6624 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6625 | * thus reset them as required. |
| 6626 | */ |
| 6627 | psa_reset_key_attributes(&attributes); |
| 6628 | |
| 6629 | psa_destroy_key(key); |
| 6630 | mbedtls_free(signature); |
| 6631 | PSA_DONE(); |
| 6632 | } |
| 6633 | /* END_CASE */ |
| 6634 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6635 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6636 | void sign_hash_fail(int key_type_arg, data_t *key_data, |
| 6637 | int alg_arg, data_t *input_data, |
| 6638 | int signature_size_arg, int expected_status_arg) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6639 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6640 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6641 | psa_key_type_t key_type = key_type_arg; |
| 6642 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6643 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6644 | psa_status_t actual_status; |
| 6645 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 6646 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 6647 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6648 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6649 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6650 | ASSERT_ALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6651 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6652 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6653 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6654 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6655 | psa_set_key_algorithm(&attributes, alg); |
| 6656 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6657 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6658 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6659 | &key)); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6660 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6661 | actual_status = psa_sign_hash(key, alg, |
| 6662 | input_data->x, input_data->len, |
| 6663 | signature, signature_size, |
| 6664 | &signature_length); |
| 6665 | TEST_EQUAL(actual_status, expected_status); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6666 | /* The value of *signature_length is unspecified on error, but |
| 6667 | * whatever it is, it should be less than signature_size, so that |
| 6668 | * if the caller tries to read *signature_length bytes without |
| 6669 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6670 | TEST_LE_U(signature_length, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6671 | |
| 6672 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6673 | psa_reset_key_attributes(&attributes); |
| 6674 | psa_destroy_key(key); |
| 6675 | mbedtls_free(signature); |
| 6676 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6677 | } |
| 6678 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6679 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6680 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6681 | /** |
| 6682 | * sign_hash_fail_interruptible() test intentions: |
| 6683 | * |
| 6684 | * Note: This test can currently only handle ECDSA. |
| 6685 | * |
| 6686 | * 1. Test that various failure cases for interruptible sign hash fail with the |
| 6687 | * correct error codes, and at the correct point (at start or during |
| 6688 | * complete). |
| 6689 | * |
| 6690 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 6691 | * expected for different max_ops values. |
| 6692 | * |
| 6693 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 6694 | * and that each successful stage completes some ops (this is not mandated by |
| 6695 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 6696 | * |
| 6697 | * 4. Check that calling complete() when start() fails and complete() |
| 6698 | * after completion results in a BAD_STATE error. |
| 6699 | * |
| 6700 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 6701 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6702 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6703 | void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 6704 | int alg_arg, data_t *input_data, |
| 6705 | int signature_size_arg, |
| 6706 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6707 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6708 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6709 | { |
| 6710 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6711 | psa_key_type_t key_type = key_type_arg; |
| 6712 | psa_algorithm_t alg = alg_arg; |
| 6713 | size_t signature_size = signature_size_arg; |
| 6714 | psa_status_t actual_status; |
| 6715 | psa_status_t expected_start_status = expected_start_status_arg; |
| 6716 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 6717 | unsigned char *signature = NULL; |
| 6718 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6719 | uint32_t num_ops = 0; |
| 6720 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6721 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6722 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6723 | size_t min_completes = 0; |
| 6724 | size_t max_completes = 0; |
| 6725 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6726 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6727 | psa_sign_hash_interruptible_operation_t operation = |
| 6728 | psa_sign_hash_interruptible_operation_init(); |
| 6729 | |
| 6730 | ASSERT_ALLOC(signature, signature_size); |
| 6731 | |
| 6732 | PSA_ASSERT(psa_crypto_init()); |
| 6733 | |
| 6734 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6735 | psa_set_key_algorithm(&attributes, alg); |
| 6736 | psa_set_key_type(&attributes, key_type); |
| 6737 | |
| 6738 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6739 | &key)); |
| 6740 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6741 | psa_interruptible_set_max_ops(max_ops); |
| 6742 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6743 | interruptible_signverify_get_minmax_completes(max_ops, |
| 6744 | expected_complete_status, |
| 6745 | &min_completes, |
| 6746 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6747 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6748 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6749 | TEST_ASSERT(num_ops_prior == 0); |
| 6750 | |
| 6751 | /* Start performing the signature. */ |
| 6752 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 6753 | input_data->x, input_data->len); |
| 6754 | |
| 6755 | TEST_EQUAL(actual_status, expected_start_status); |
| 6756 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6757 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 6758 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 6759 | * start failed. */ |
| 6760 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6761 | signature_size, |
| 6762 | &signature_length); |
| 6763 | |
| 6764 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 6765 | |
| 6766 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6767 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 6768 | input_data->x, input_data->len); |
| 6769 | |
| 6770 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 6771 | } |
| 6772 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6773 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6774 | TEST_ASSERT(num_ops_prior == 0); |
| 6775 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6776 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6777 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6778 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6779 | signature_size, |
| 6780 | &signature_length); |
| 6781 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6782 | num_completes++; |
| 6783 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6784 | if (actual_status == PSA_SUCCESS || |
| 6785 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6786 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6787 | /* We are asserting here that every complete makes progress |
| 6788 | * (completes some ops), which is true of the internal |
| 6789 | * implementation and probably any implementation, however this is |
| 6790 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6791 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6792 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6793 | num_ops_prior = num_ops; |
| 6794 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6795 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6796 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6797 | TEST_EQUAL(actual_status, expected_complete_status); |
| 6798 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 6799 | /* Check that another complete returns BAD_STATE. */ |
| 6800 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6801 | signature_size, |
| 6802 | &signature_length); |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6803 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 6804 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6805 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6806 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6807 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6808 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6809 | TEST_ASSERT(num_ops == 0); |
| 6810 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6811 | /* The value of *signature_length is unspecified on error, but |
| 6812 | * whatever it is, it should be less than signature_size, so that |
| 6813 | * if the caller tries to read *signature_length bytes without |
| 6814 | * checking the error code then they don't overflow a buffer. */ |
| 6815 | TEST_LE_U(signature_length, signature_size); |
| 6816 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6817 | TEST_LE_U(min_completes, num_completes); |
| 6818 | TEST_LE_U(num_completes, max_completes); |
| 6819 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6820 | exit: |
| 6821 | psa_reset_key_attributes(&attributes); |
| 6822 | psa_destroy_key(key); |
| 6823 | mbedtls_free(signature); |
| 6824 | PSA_DONE(); |
| 6825 | } |
| 6826 | /* END_CASE */ |
| 6827 | |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6828 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6829 | void sign_verify_hash(int key_type_arg, data_t *key_data, |
| 6830 | int alg_arg, data_t *input_data) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6831 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6832 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6833 | psa_key_type_t key_type = key_type_arg; |
| 6834 | psa_algorithm_t alg = alg_arg; |
| 6835 | size_t key_bits; |
| 6836 | unsigned char *signature = NULL; |
| 6837 | size_t signature_size; |
| 6838 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6839 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6840 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6841 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6842 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6843 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 6844 | psa_set_key_algorithm(&attributes, alg); |
| 6845 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6846 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6847 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6848 | &key)); |
| 6849 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6850 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6851 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6852 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6853 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6854 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6855 | key_bits, alg); |
| 6856 | TEST_ASSERT(signature_size != 0); |
| 6857 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6858 | ASSERT_ALLOC(signature, signature_size); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6859 | |
| 6860 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6861 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6862 | input_data->x, input_data->len, |
| 6863 | signature, signature_size, |
| 6864 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6865 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6866 | TEST_LE_U(signature_length, signature_size); |
| 6867 | TEST_ASSERT(signature_length > 0); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6868 | |
| 6869 | /* Use the library to verify that the signature is correct. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6870 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 6871 | input_data->x, input_data->len, |
| 6872 | signature, signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6873 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6874 | if (input_data->len != 0) { |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6875 | /* Flip a bit in the input and verify that the signature is now |
| 6876 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6877 | * because ECDSA may ignore the last few bits of the input. */ |
| 6878 | input_data->x[0] ^= 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6879 | TEST_EQUAL(psa_verify_hash(key, alg, |
| 6880 | input_data->x, input_data->len, |
| 6881 | signature, signature_length), |
| 6882 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6883 | } |
| 6884 | |
| 6885 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6886 | /* |
| 6887 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6888 | * thus reset them as required. |
| 6889 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6890 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6891 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6892 | psa_destroy_key(key); |
| 6893 | mbedtls_free(signature); |
| 6894 | PSA_DONE(); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6895 | } |
| 6896 | /* END_CASE */ |
| 6897 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6898 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6899 | /** |
| 6900 | * sign_verify_hash_interruptible() test intentions: |
| 6901 | * |
| 6902 | * Note: This test can currently only handle ECDSA. |
| 6903 | * |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 6904 | * 1. Test that we can sign an input hash with the given keypair and then |
| 6905 | * afterwards verify that signature. This is currently the only way to test |
| 6906 | * non deterministic ECDSA, but this test can also handle deterministic. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6907 | * |
| 6908 | * 2. Test that after corrupting the hash, the verification detects an invalid |
| 6909 | * signature. |
| 6910 | * |
| 6911 | * 3. Test the number of calls to psa_sign_hash_complete() required are as |
| 6912 | * expected for different max_ops values. |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6913 | * |
| 6914 | * 4. Test that the number of ops done prior to starting signing and after abort |
| 6915 | * is zero and that each successful signing stage completes some ops (this is |
| 6916 | * not mandated by the PSA specification, but is currently the case). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6917 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6918 | 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] | 6919 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6920 | int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6921 | { |
| 6922 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6923 | psa_key_type_t key_type = key_type_arg; |
| 6924 | psa_algorithm_t alg = alg_arg; |
| 6925 | size_t key_bits; |
| 6926 | unsigned char *signature = NULL; |
| 6927 | size_t signature_size; |
| 6928 | size_t signature_length = 0xdeadbeef; |
| 6929 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6930 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6931 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6932 | uint32_t num_ops = 0; |
| 6933 | uint32_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6934 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6935 | size_t min_completes = 0; |
| 6936 | size_t max_completes = 0; |
| 6937 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6938 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 6939 | psa_sign_hash_interruptible_operation_init(); |
| 6940 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 6941 | psa_verify_hash_interruptible_operation_init(); |
| 6942 | |
| 6943 | PSA_ASSERT(psa_crypto_init()); |
| 6944 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6945 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 6946 | PSA_KEY_USAGE_VERIFY_HASH); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6947 | psa_set_key_algorithm(&attributes, alg); |
| 6948 | psa_set_key_type(&attributes, key_type); |
| 6949 | |
| 6950 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6951 | &key)); |
| 6952 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6953 | key_bits = psa_get_key_bits(&attributes); |
| 6954 | |
| 6955 | /* Allocate a buffer which has the size advertised by the |
| 6956 | * library. */ |
| 6957 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6958 | key_bits, alg); |
| 6959 | TEST_ASSERT(signature_size != 0); |
| 6960 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6961 | ASSERT_ALLOC(signature, signature_size); |
| 6962 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6963 | psa_interruptible_set_max_ops(max_ops); |
| 6964 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6965 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6966 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6967 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6968 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); |
| 6969 | TEST_ASSERT(num_ops_prior == 0); |
| 6970 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6971 | /* Start performing the signature. */ |
| 6972 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 6973 | input_data->x, input_data->len)); |
| 6974 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6975 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); |
| 6976 | TEST_ASSERT(num_ops_prior == 0); |
| 6977 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6978 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6979 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6980 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6981 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 6982 | signature_size, |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6983 | &signature_length); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6984 | |
| 6985 | num_completes++; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6986 | |
| 6987 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 6988 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 6989 | /* We are asserting here that every complete makes progress |
| 6990 | * (completes some ops), which is true of the internal |
| 6991 | * implementation and probably any implementation, however this is |
| 6992 | * not mandated by the PSA specification. */ |
| 6993 | TEST_ASSERT(num_ops > num_ops_prior); |
| 6994 | |
| 6995 | num_ops_prior = num_ops; |
| 6996 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6997 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6998 | |
| 6999 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7000 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7001 | TEST_LE_U(min_completes, num_completes); |
| 7002 | TEST_LE_U(num_completes, max_completes); |
| 7003 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7004 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7005 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7006 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7007 | TEST_ASSERT(num_ops == 0); |
| 7008 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7009 | /* Check that the signature length looks sensible. */ |
| 7010 | TEST_LE_U(signature_length, signature_size); |
| 7011 | TEST_ASSERT(signature_length > 0); |
| 7012 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7013 | num_completes = 0; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7014 | |
| 7015 | /* Start verification. */ |
| 7016 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7017 | input_data->x, input_data->len, |
| 7018 | signature, signature_length)); |
| 7019 | |
| 7020 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7021 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7022 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7023 | |
| 7024 | num_completes++; |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7025 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7026 | |
| 7027 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7028 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7029 | TEST_LE_U(min_completes, num_completes); |
| 7030 | TEST_LE_U(num_completes, max_completes); |
| 7031 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7032 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7033 | |
| 7034 | verify_operation = psa_verify_hash_interruptible_operation_init(); |
| 7035 | |
| 7036 | if (input_data->len != 0) { |
| 7037 | /* Flip a bit in the input and verify that the signature is now |
| 7038 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 7039 | * because ECDSA may ignore the last few bits of the input. */ |
| 7040 | input_data->x[0] ^= 1; |
| 7041 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7042 | /* Start verification. */ |
| 7043 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7044 | input_data->x, input_data->len, |
| 7045 | signature, signature_length)); |
| 7046 | |
| 7047 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7048 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7049 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7050 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7051 | |
| 7052 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7053 | } |
| 7054 | |
| 7055 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7056 | |
| 7057 | exit: |
| 7058 | /* |
| 7059 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7060 | * thus reset them as required. |
| 7061 | */ |
| 7062 | psa_reset_key_attributes(&attributes); |
| 7063 | |
| 7064 | psa_destroy_key(key); |
| 7065 | mbedtls_free(signature); |
| 7066 | PSA_DONE(); |
| 7067 | } |
| 7068 | /* END_CASE */ |
| 7069 | |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7070 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7071 | void verify_hash(int key_type_arg, data_t *key_data, |
| 7072 | int alg_arg, data_t *hash_data, |
| 7073 | data_t *signature_data) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7074 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7075 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7076 | psa_key_type_t key_type = key_type_arg; |
| 7077 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7078 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7079 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7080 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 7081 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7082 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7083 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7084 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7085 | psa_set_key_algorithm(&attributes, alg); |
| 7086 | psa_set_key_type(&attributes, key_type); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7087 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7088 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7089 | &key)); |
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 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 7092 | hash_data->x, hash_data->len, |
| 7093 | signature_data->x, signature_data->len)); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 7094 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7095 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7096 | psa_reset_key_attributes(&attributes); |
| 7097 | psa_destroy_key(key); |
| 7098 | PSA_DONE(); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7099 | } |
| 7100 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7101 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7102 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7103 | /** |
| 7104 | * verify_hash_interruptible() test intentions: |
| 7105 | * |
| 7106 | * Note: This test can currently only handle ECDSA. |
| 7107 | * |
| 7108 | * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 7109 | * only). Given this test only does verification it can accept public keys as |
| 7110 | * well as private keys / keypairs. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7111 | * |
| 7112 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7113 | * expected for different max_ops values. |
| 7114 | * |
| 7115 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7116 | * and that each successful stage completes some ops (this is not mandated by |
| 7117 | * the PSA specification, but is currently the case). |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7118 | * |
| 7119 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 7120 | * complete() calls does not alter the number of ops returned. |
| 7121 | * |
| 7122 | * 5. Test that after corrupting the hash, the verification detects an invalid |
| 7123 | * signature. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7124 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7125 | void verify_hash_interruptible(int key_type_arg, data_t *key_data, |
| 7126 | int alg_arg, data_t *hash_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7127 | data_t *signature_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7128 | { |
| 7129 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7130 | psa_key_type_t key_type = key_type_arg; |
| 7131 | psa_algorithm_t alg = alg_arg; |
| 7132 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7133 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7134 | uint32_t num_ops = 0; |
| 7135 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7136 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7137 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7138 | size_t min_completes = 0; |
| 7139 | size_t max_completes = 0; |
| 7140 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7141 | psa_verify_hash_interruptible_operation_t operation = |
| 7142 | psa_verify_hash_interruptible_operation_init(); |
| 7143 | |
| 7144 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 7145 | |
| 7146 | PSA_ASSERT(psa_crypto_init()); |
| 7147 | |
| 7148 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7149 | psa_set_key_algorithm(&attributes, alg); |
| 7150 | psa_set_key_type(&attributes, key_type); |
| 7151 | |
| 7152 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7153 | &key)); |
| 7154 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7155 | psa_interruptible_set_max_ops(max_ops); |
| 7156 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7157 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 7158 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7159 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7160 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7161 | |
| 7162 | TEST_ASSERT(num_ops_prior == 0); |
| 7163 | |
| 7164 | /* Start verification. */ |
| 7165 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7166 | hash_data->x, hash_data->len, |
| 7167 | signature_data->x, signature_data->len) |
| 7168 | ); |
| 7169 | |
| 7170 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7171 | |
| 7172 | TEST_ASSERT(num_ops_prior == 0); |
| 7173 | |
| 7174 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7175 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7176 | status = psa_verify_hash_complete(&operation); |
| 7177 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7178 | num_completes++; |
| 7179 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7180 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 7181 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7182 | /* We are asserting here that every complete makes progress |
| 7183 | * (completes some ops), which is true of the internal |
| 7184 | * implementation and probably any implementation, however this is |
| 7185 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7186 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7187 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7188 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 7189 | |
| 7190 | /* Ensure calling get_num_ops() twice still returns the same |
| 7191 | * number of ops as previously reported. */ |
| 7192 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7193 | |
| 7194 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7195 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7196 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7197 | |
| 7198 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7199 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7200 | TEST_LE_U(min_completes, num_completes); |
| 7201 | TEST_LE_U(num_completes, max_completes); |
| 7202 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7203 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7204 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7205 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7206 | TEST_ASSERT(num_ops == 0); |
| 7207 | |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7208 | if (hash_data->len != 0) { |
| 7209 | /* Flip a bit in the hash and verify that the signature is now detected |
| 7210 | * as invalid. Flip a bit at the beginning, not at the end, because |
| 7211 | * ECDSA may ignore the last few bits of the input. */ |
| 7212 | hash_data->x[0] ^= 1; |
| 7213 | |
| 7214 | /* Start verification. */ |
| 7215 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7216 | hash_data->x, hash_data->len, |
| 7217 | signature_data->x, signature_data->len)); |
| 7218 | |
| 7219 | /* Continue performing the signature until complete. */ |
| 7220 | do { |
| 7221 | status = psa_verify_hash_complete(&operation); |
| 7222 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7223 | |
| 7224 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7225 | } |
| 7226 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7227 | exit: |
| 7228 | psa_reset_key_attributes(&attributes); |
| 7229 | psa_destroy_key(key); |
| 7230 | PSA_DONE(); |
| 7231 | } |
| 7232 | /* END_CASE */ |
| 7233 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7234 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7235 | void verify_hash_fail(int key_type_arg, data_t *key_data, |
| 7236 | int alg_arg, data_t *hash_data, |
| 7237 | data_t *signature_data, |
| 7238 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7239 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7240 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7241 | psa_key_type_t key_type = key_type_arg; |
| 7242 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7243 | psa_status_t actual_status; |
| 7244 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7245 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7246 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7247 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7248 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7249 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7250 | psa_set_key_algorithm(&attributes, alg); |
| 7251 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7252 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7253 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7254 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7256 | actual_status = psa_verify_hash(key, alg, |
| 7257 | hash_data->x, hash_data->len, |
| 7258 | signature_data->x, signature_data->len); |
| 7259 | TEST_EQUAL(actual_status, expected_status); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7260 | |
| 7261 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7262 | psa_reset_key_attributes(&attributes); |
| 7263 | psa_destroy_key(key); |
| 7264 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7265 | } |
| 7266 | /* END_CASE */ |
| 7267 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7268 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7269 | /** |
| 7270 | * verify_hash_fail_interruptible() test intentions: |
| 7271 | * |
| 7272 | * Note: This test can currently only handle ECDSA. |
| 7273 | * |
| 7274 | * 1. Test that various failure cases for interruptible verify hash fail with |
| 7275 | * the correct error codes, and at the correct point (at start or during |
| 7276 | * complete). |
| 7277 | * |
| 7278 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7279 | * expected for different max_ops values. |
| 7280 | * |
| 7281 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7282 | * and that each successful stage completes some ops (this is not mandated by |
| 7283 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7284 | * |
| 7285 | * 4. Check that calling complete() when start() fails and complete() |
| 7286 | * after completion results in a BAD_STATE error. |
| 7287 | * |
| 7288 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 7289 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7290 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7291 | void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 7292 | int alg_arg, data_t *hash_data, |
| 7293 | data_t *signature_data, |
| 7294 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7295 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7296 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7297 | { |
| 7298 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7299 | psa_key_type_t key_type = key_type_arg; |
| 7300 | psa_algorithm_t alg = alg_arg; |
| 7301 | psa_status_t actual_status; |
| 7302 | psa_status_t expected_start_status = expected_start_status_arg; |
| 7303 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 7304 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7305 | uint32_t num_ops = 0; |
| 7306 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7307 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7308 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7309 | size_t min_completes = 0; |
| 7310 | size_t max_completes = 0; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7311 | psa_verify_hash_interruptible_operation_t operation = |
| 7312 | psa_verify_hash_interruptible_operation_init(); |
| 7313 | |
| 7314 | PSA_ASSERT(psa_crypto_init()); |
| 7315 | |
| 7316 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7317 | psa_set_key_algorithm(&attributes, alg); |
| 7318 | psa_set_key_type(&attributes, key_type); |
| 7319 | |
| 7320 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7321 | &key)); |
| 7322 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7323 | psa_interruptible_set_max_ops(max_ops); |
| 7324 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7325 | interruptible_signverify_get_minmax_completes(max_ops, |
| 7326 | expected_complete_status, |
| 7327 | &min_completes, |
| 7328 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7329 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7330 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7331 | TEST_ASSERT(num_ops_prior == 0); |
| 7332 | |
| 7333 | /* Start verification. */ |
| 7334 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7335 | hash_data->x, hash_data->len, |
| 7336 | signature_data->x, |
| 7337 | signature_data->len); |
| 7338 | |
| 7339 | TEST_EQUAL(actual_status, expected_start_status); |
| 7340 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7341 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 7342 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7343 | * start failed. */ |
| 7344 | actual_status = psa_verify_hash_complete(&operation); |
| 7345 | |
| 7346 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7347 | |
| 7348 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7349 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7350 | hash_data->x, hash_data->len, |
| 7351 | signature_data->x, |
| 7352 | signature_data->len); |
| 7353 | |
| 7354 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7355 | } |
| 7356 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7357 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7358 | TEST_ASSERT(num_ops_prior == 0); |
| 7359 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7360 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7361 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7362 | actual_status = psa_verify_hash_complete(&operation); |
| 7363 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7364 | num_completes++; |
| 7365 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7366 | if (actual_status == PSA_SUCCESS || |
| 7367 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7368 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7369 | /* We are asserting here that every complete makes progress |
| 7370 | * (completes some ops), which is true of the internal |
| 7371 | * implementation and probably any implementation, however this is |
| 7372 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7373 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7374 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7375 | num_ops_prior = num_ops; |
| 7376 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7377 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7378 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7379 | TEST_EQUAL(actual_status, expected_complete_status); |
| 7380 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 7381 | /* Check that another complete returns BAD_STATE. */ |
| 7382 | actual_status = psa_verify_hash_complete(&operation); |
| 7383 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7384 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7385 | TEST_LE_U(min_completes, num_completes); |
| 7386 | TEST_LE_U(num_completes, max_completes); |
| 7387 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7388 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7389 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7390 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7391 | TEST_ASSERT(num_ops == 0); |
| 7392 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7393 | exit: |
| 7394 | psa_reset_key_attributes(&attributes); |
| 7395 | psa_destroy_key(key); |
| 7396 | PSA_DONE(); |
| 7397 | } |
| 7398 | /* END_CASE */ |
| 7399 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7400 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7401 | /** |
| 7402 | * interruptible_signverify_hash_state_test() test intentions: |
| 7403 | * |
| 7404 | * Note: This test can currently only handle ECDSA. |
| 7405 | * |
| 7406 | * 1. Test that calling the various interruptible sign and verify hash functions |
| 7407 | * in incorrect orders returns BAD_STATE errors. |
| 7408 | */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7409 | void interruptible_signverify_hash_state_test(int key_type_arg, |
| 7410 | data_t *key_data, int alg_arg, data_t *input_data) |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7411 | { |
| 7412 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7413 | psa_key_type_t key_type = key_type_arg; |
| 7414 | psa_algorithm_t alg = alg_arg; |
| 7415 | size_t key_bits; |
| 7416 | unsigned char *signature = NULL; |
| 7417 | size_t signature_size; |
| 7418 | size_t signature_length = 0xdeadbeef; |
| 7419 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7420 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7421 | psa_sign_hash_interruptible_operation_init(); |
| 7422 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7423 | psa_verify_hash_interruptible_operation_init(); |
| 7424 | |
| 7425 | PSA_ASSERT(psa_crypto_init()); |
| 7426 | |
| 7427 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7428 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7429 | psa_set_key_algorithm(&attributes, alg); |
| 7430 | psa_set_key_type(&attributes, key_type); |
| 7431 | |
| 7432 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7433 | &key)); |
| 7434 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7435 | key_bits = psa_get_key_bits(&attributes); |
| 7436 | |
| 7437 | /* Allocate a buffer which has the size advertised by the |
| 7438 | * library. */ |
| 7439 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7440 | key_bits, alg); |
| 7441 | TEST_ASSERT(signature_size != 0); |
| 7442 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7443 | ASSERT_ALLOC(signature, signature_size); |
| 7444 | |
| 7445 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7446 | |
| 7447 | /* --- Attempt completes prior to starts --- */ |
| 7448 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7449 | signature_size, |
| 7450 | &signature_length), |
| 7451 | PSA_ERROR_BAD_STATE); |
| 7452 | |
| 7453 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7454 | |
| 7455 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7456 | PSA_ERROR_BAD_STATE); |
| 7457 | |
| 7458 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7459 | |
| 7460 | /* --- Aborts in all other places. --- */ |
| 7461 | psa_sign_hash_abort(&sign_operation); |
| 7462 | |
| 7463 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7464 | input_data->x, input_data->len)); |
| 7465 | |
| 7466 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7467 | |
| 7468 | psa_interruptible_set_max_ops(1); |
| 7469 | |
| 7470 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7471 | input_data->x, input_data->len)); |
| 7472 | |
| 7473 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7474 | signature_size, |
| 7475 | &signature_length), |
| 7476 | PSA_OPERATION_INCOMPLETE); |
| 7477 | |
| 7478 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7479 | |
| 7480 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7481 | |
| 7482 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7483 | input_data->x, input_data->len)); |
| 7484 | |
| 7485 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7486 | signature_size, |
| 7487 | &signature_length)); |
| 7488 | |
| 7489 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7490 | |
| 7491 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7492 | |
| 7493 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7494 | input_data->x, input_data->len, |
| 7495 | signature, signature_length)); |
| 7496 | |
| 7497 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7498 | |
| 7499 | psa_interruptible_set_max_ops(1); |
| 7500 | |
| 7501 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7502 | input_data->x, input_data->len, |
| 7503 | signature, signature_length)); |
| 7504 | |
| 7505 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7506 | PSA_OPERATION_INCOMPLETE); |
| 7507 | |
| 7508 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7509 | |
| 7510 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 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 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7517 | |
| 7518 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7519 | |
| 7520 | /* --- Attempt double starts. --- */ |
| 7521 | |
| 7522 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7523 | input_data->x, input_data->len)); |
| 7524 | |
| 7525 | TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg, |
| 7526 | input_data->x, input_data->len), |
| 7527 | PSA_ERROR_BAD_STATE); |
| 7528 | |
| 7529 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7530 | |
| 7531 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7532 | input_data->x, input_data->len, |
| 7533 | signature, signature_length)); |
| 7534 | |
| 7535 | TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg, |
| 7536 | input_data->x, input_data->len, |
| 7537 | signature, signature_length), |
| 7538 | PSA_ERROR_BAD_STATE); |
| 7539 | |
| 7540 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7541 | |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7542 | exit: |
| 7543 | /* |
| 7544 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7545 | * thus reset them as required. |
| 7546 | */ |
| 7547 | psa_reset_key_attributes(&attributes); |
| 7548 | |
| 7549 | psa_destroy_key(key); |
| 7550 | mbedtls_free(signature); |
| 7551 | PSA_DONE(); |
| 7552 | } |
| 7553 | /* END_CASE */ |
| 7554 | |
| 7555 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7556 | /** |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7557 | * interruptible_signverify_hash_edgecase_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7558 | * |
| 7559 | * Note: This test can currently only handle ECDSA. |
| 7560 | * |
| 7561 | * 1. Test various edge cases in the interruptible sign and verify hash |
| 7562 | * interfaces. |
| 7563 | */ |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7564 | void interruptible_signverify_hash_edgecase_tests(int key_type_arg, |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7565 | data_t *key_data, int alg_arg, data_t *input_data) |
| 7566 | { |
| 7567 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7568 | psa_key_type_t key_type = key_type_arg; |
| 7569 | psa_algorithm_t alg = alg_arg; |
| 7570 | size_t key_bits; |
| 7571 | unsigned char *signature = NULL; |
| 7572 | size_t signature_size; |
| 7573 | size_t signature_length = 0xdeadbeef; |
| 7574 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7575 | uint8_t *input_buffer = NULL; |
| 7576 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7577 | psa_sign_hash_interruptible_operation_init(); |
| 7578 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7579 | psa_verify_hash_interruptible_operation_init(); |
| 7580 | |
| 7581 | PSA_ASSERT(psa_crypto_init()); |
| 7582 | |
| 7583 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7584 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7585 | psa_set_key_algorithm(&attributes, alg); |
| 7586 | psa_set_key_type(&attributes, key_type); |
| 7587 | |
| 7588 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7589 | &key)); |
| 7590 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7591 | key_bits = psa_get_key_bits(&attributes); |
| 7592 | |
| 7593 | /* Allocate a buffer which has the size advertised by the |
| 7594 | * library. */ |
| 7595 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7596 | key_bits, alg); |
| 7597 | TEST_ASSERT(signature_size != 0); |
| 7598 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7599 | ASSERT_ALLOC(signature, signature_size); |
| 7600 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7601 | /* --- Change function inputs mid run, to cause an error (sign only, |
| 7602 | * verify passes all inputs to start. --- */ |
| 7603 | |
| 7604 | psa_interruptible_set_max_ops(1); |
| 7605 | |
| 7606 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7607 | input_data->x, input_data->len)); |
| 7608 | |
| 7609 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7610 | signature_size, |
| 7611 | &signature_length), |
| 7612 | PSA_OPERATION_INCOMPLETE); |
| 7613 | |
| 7614 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7615 | 0, |
| 7616 | &signature_length), |
| 7617 | PSA_ERROR_BUFFER_TOO_SMALL); |
| 7618 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7619 | /* And test that this invalidates the operation. */ |
| 7620 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7621 | 0, |
| 7622 | &signature_length), |
| 7623 | PSA_ERROR_BAD_STATE); |
| 7624 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7625 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7626 | |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7627 | /* Trash the hash buffer in between start and complete, to ensure |
| 7628 | * no reliance on external buffers. */ |
| 7629 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7630 | |
| 7631 | input_buffer = mbedtls_calloc(1, input_data->len); |
| 7632 | TEST_ASSERT(input_buffer != NULL); |
| 7633 | |
| 7634 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7635 | |
| 7636 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7637 | input_buffer, input_data->len)); |
| 7638 | |
| 7639 | memset(input_buffer, '!', input_data->len); |
| 7640 | mbedtls_free(input_buffer); |
| 7641 | input_buffer = NULL; |
| 7642 | |
| 7643 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7644 | signature_size, |
| 7645 | &signature_length)); |
| 7646 | |
| 7647 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7648 | |
| 7649 | input_buffer = mbedtls_calloc(1, input_data->len); |
| 7650 | TEST_ASSERT(input_buffer != NULL); |
| 7651 | |
| 7652 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7653 | |
| 7654 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7655 | input_buffer, input_data->len, |
| 7656 | signature, signature_length)); |
| 7657 | |
| 7658 | memset(input_buffer, '!', input_data->len); |
| 7659 | mbedtls_free(input_buffer); |
| 7660 | input_buffer = NULL; |
| 7661 | |
| 7662 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7663 | |
| 7664 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7665 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7666 | exit: |
| 7667 | /* |
| 7668 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7669 | * thus reset them as required. |
| 7670 | */ |
| 7671 | psa_reset_key_attributes(&attributes); |
| 7672 | |
| 7673 | psa_destroy_key(key); |
| 7674 | mbedtls_free(signature); |
| 7675 | PSA_DONE(); |
| 7676 | } |
| 7677 | /* END_CASE */ |
| 7678 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7679 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7680 | /** |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 7681 | * interruptible_signverify_hash_ops_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7682 | * |
| 7683 | * Note: This test can currently only handle ECDSA. |
| 7684 | * |
| 7685 | * 1. Test that setting max ops is reflected in both interruptible sign and |
| 7686 | * verify hash |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7687 | * 2. Test that changing the value of max_ops to unlimited during an operation |
| 7688 | * causes that operation to complete in the next call. |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7689 | * |
| 7690 | * 3. Test that calling get_num_ops() between complete calls gives the same |
| 7691 | * 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] | 7692 | */ |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 7693 | void interruptible_signverify_hash_ops_tests(int key_type_arg, |
| 7694 | data_t *key_data, int alg_arg, |
| 7695 | data_t *input_data) |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7696 | { |
| 7697 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7698 | psa_key_type_t key_type = key_type_arg; |
| 7699 | psa_algorithm_t alg = alg_arg; |
| 7700 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7701 | size_t key_bits; |
| 7702 | unsigned char *signature = NULL; |
| 7703 | size_t signature_size; |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7704 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7705 | uint32_t num_ops = 0; |
| 7706 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 7707 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7708 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7709 | psa_sign_hash_interruptible_operation_init(); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7710 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7711 | psa_verify_hash_interruptible_operation_init(); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7712 | |
| 7713 | PSA_ASSERT(psa_crypto_init()); |
| 7714 | |
| 7715 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7716 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7717 | psa_set_key_algorithm(&attributes, alg); |
| 7718 | psa_set_key_type(&attributes, key_type); |
| 7719 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7720 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key)); |
| 7721 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7722 | key_bits = psa_get_key_bits(&attributes); |
| 7723 | |
| 7724 | /* Allocate a buffer which has the size advertised by the |
| 7725 | * library. */ |
| 7726 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7727 | |
| 7728 | TEST_ASSERT(signature_size != 0); |
| 7729 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7730 | ASSERT_ALLOC(signature, signature_size); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7731 | |
| 7732 | /* Check that default max ops gets set if we don't set it. */ |
| 7733 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7734 | input_data->x, input_data->len)); |
| 7735 | |
| 7736 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 7737 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7738 | |
| 7739 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7740 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7741 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7742 | input_data->x, input_data->len, |
| 7743 | signature, signature_size)); |
| 7744 | |
| 7745 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 7746 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7747 | |
| 7748 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7749 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7750 | /* Check that max ops gets set properly. */ |
| 7751 | |
| 7752 | psa_interruptible_set_max_ops(0xbeef); |
| 7753 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7754 | TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7755 | |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7756 | /* --- Ensure changing the max ops mid operation works (operation should |
| 7757 | * complete successfully after setting max ops to unlimited --- */ |
| 7758 | psa_interruptible_set_max_ops(1); |
| 7759 | |
| 7760 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7761 | input_data->x, input_data->len)); |
| 7762 | |
| 7763 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7764 | signature_size, |
| 7765 | &signature_length), |
| 7766 | PSA_OPERATION_INCOMPLETE); |
| 7767 | |
| 7768 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7769 | |
| 7770 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7771 | signature_size, |
| 7772 | &signature_length)); |
| 7773 | |
| 7774 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7775 | |
| 7776 | psa_interruptible_set_max_ops(1); |
| 7777 | |
| 7778 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7779 | input_data->x, input_data->len, |
| 7780 | signature, signature_length)); |
| 7781 | |
| 7782 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7783 | PSA_OPERATION_INCOMPLETE); |
| 7784 | |
| 7785 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7786 | |
| 7787 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7788 | |
| 7789 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7790 | |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7791 | /* --- Test that not calling get_num_ops inbetween complete calls does not |
| 7792 | * result in lost ops. ---*/ |
| 7793 | |
| 7794 | psa_interruptible_set_max_ops(1); |
| 7795 | |
| 7796 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7797 | input_data->x, input_data->len)); |
| 7798 | |
| 7799 | /* Continue performing the signature until complete. */ |
| 7800 | do { |
| 7801 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7802 | signature_size, |
| 7803 | &signature_length); |
| 7804 | |
| 7805 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7806 | |
| 7807 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7808 | |
| 7809 | PSA_ASSERT(status); |
| 7810 | |
| 7811 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7812 | |
| 7813 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7814 | input_data->x, input_data->len)); |
| 7815 | |
| 7816 | /* Continue performing the signature until complete. */ |
| 7817 | do { |
| 7818 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7819 | signature_size, |
| 7820 | &signature_length); |
| 7821 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7822 | |
| 7823 | PSA_ASSERT(status); |
| 7824 | |
| 7825 | TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation)); |
| 7826 | |
| 7827 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7828 | |
| 7829 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7830 | input_data->x, input_data->len, |
| 7831 | signature, signature_length)); |
| 7832 | |
| 7833 | /* Continue performing the verification until complete. */ |
| 7834 | do { |
| 7835 | status = psa_verify_hash_complete(&verify_operation); |
| 7836 | |
| 7837 | num_ops = psa_verify_hash_get_num_ops(&verify_operation); |
| 7838 | |
| 7839 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7840 | |
| 7841 | PSA_ASSERT(status); |
| 7842 | |
| 7843 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7844 | |
| 7845 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7846 | input_data->x, input_data->len, |
| 7847 | signature, signature_length)); |
| 7848 | |
| 7849 | /* Continue performing the verification until complete. */ |
| 7850 | do { |
| 7851 | status = psa_verify_hash_complete(&verify_operation); |
| 7852 | |
| 7853 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7854 | |
| 7855 | PSA_ASSERT(status); |
| 7856 | |
| 7857 | TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation)); |
| 7858 | |
| 7859 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7860 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7861 | exit: |
| 7862 | /* |
| 7863 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7864 | * thus reset them as required. |
| 7865 | */ |
| 7866 | psa_reset_key_attributes(&attributes); |
| 7867 | |
| 7868 | psa_destroy_key(key); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7869 | mbedtls_free(signature); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7870 | PSA_DONE(); |
| 7871 | } |
| 7872 | /* END_CASE */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7873 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7874 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7875 | void sign_message_deterministic(int key_type_arg, |
| 7876 | data_t *key_data, |
| 7877 | int alg_arg, |
| 7878 | data_t *input_data, |
| 7879 | data_t *output_data) |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7880 | { |
| 7881 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7882 | psa_key_type_t key_type = key_type_arg; |
| 7883 | psa_algorithm_t alg = alg_arg; |
| 7884 | size_t key_bits; |
| 7885 | unsigned char *signature = NULL; |
| 7886 | size_t signature_size; |
| 7887 | size_t signature_length = 0xdeadbeef; |
| 7888 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7889 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7890 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7891 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7892 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 7893 | psa_set_key_algorithm(&attributes, alg); |
| 7894 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7895 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7896 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7897 | &key)); |
| 7898 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7899 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7900 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7901 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7902 | TEST_ASSERT(signature_size != 0); |
| 7903 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7904 | ASSERT_ALLOC(signature, signature_size); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7905 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7906 | PSA_ASSERT(psa_sign_message(key, alg, |
| 7907 | input_data->x, input_data->len, |
| 7908 | signature, signature_size, |
| 7909 | &signature_length)); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7910 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7911 | ASSERT_COMPARE(output_data->x, output_data->len, |
| 7912 | signature, signature_length); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7913 | |
| 7914 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7915 | psa_reset_key_attributes(&attributes); |
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_destroy_key(key); |
| 7918 | mbedtls_free(signature); |
| 7919 | PSA_DONE(); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7920 | |
| 7921 | } |
| 7922 | /* END_CASE */ |
| 7923 | |
| 7924 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7925 | void sign_message_fail(int key_type_arg, |
| 7926 | data_t *key_data, |
| 7927 | int alg_arg, |
| 7928 | data_t *input_data, |
| 7929 | int signature_size_arg, |
| 7930 | int expected_status_arg) |
| 7931 | { |
| 7932 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7933 | psa_key_type_t key_type = key_type_arg; |
| 7934 | psa_algorithm_t alg = alg_arg; |
| 7935 | size_t signature_size = signature_size_arg; |
| 7936 | psa_status_t actual_status; |
| 7937 | psa_status_t expected_status = expected_status_arg; |
| 7938 | unsigned char *signature = NULL; |
| 7939 | size_t signature_length = 0xdeadbeef; |
| 7940 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7941 | |
| 7942 | ASSERT_ALLOC(signature, signature_size); |
| 7943 | |
| 7944 | PSA_ASSERT(psa_crypto_init()); |
| 7945 | |
| 7946 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 7947 | psa_set_key_algorithm(&attributes, alg); |
| 7948 | psa_set_key_type(&attributes, key_type); |
| 7949 | |
| 7950 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7951 | &key)); |
| 7952 | |
| 7953 | actual_status = psa_sign_message(key, alg, |
| 7954 | input_data->x, input_data->len, |
| 7955 | signature, signature_size, |
| 7956 | &signature_length); |
| 7957 | TEST_EQUAL(actual_status, expected_status); |
| 7958 | /* The value of *signature_length is unspecified on error, but |
| 7959 | * whatever it is, it should be less than signature_size, so that |
| 7960 | * if the caller tries to read *signature_length bytes without |
| 7961 | * checking the error code then they don't overflow a buffer. */ |
| 7962 | TEST_LE_U(signature_length, signature_size); |
| 7963 | |
| 7964 | exit: |
| 7965 | psa_reset_key_attributes(&attributes); |
| 7966 | psa_destroy_key(key); |
| 7967 | mbedtls_free(signature); |
| 7968 | PSA_DONE(); |
| 7969 | } |
| 7970 | /* END_CASE */ |
| 7971 | |
| 7972 | /* BEGIN_CASE */ |
| 7973 | void sign_verify_message(int key_type_arg, |
| 7974 | data_t *key_data, |
| 7975 | int alg_arg, |
| 7976 | data_t *input_data) |
| 7977 | { |
| 7978 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7979 | psa_key_type_t key_type = key_type_arg; |
| 7980 | psa_algorithm_t alg = alg_arg; |
| 7981 | size_t key_bits; |
| 7982 | unsigned char *signature = NULL; |
| 7983 | size_t signature_size; |
| 7984 | size_t signature_length = 0xdeadbeef; |
| 7985 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7986 | |
| 7987 | PSA_ASSERT(psa_crypto_init()); |
| 7988 | |
| 7989 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 7990 | PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 7991 | psa_set_key_algorithm(&attributes, alg); |
| 7992 | psa_set_key_type(&attributes, key_type); |
| 7993 | |
| 7994 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7995 | &key)); |
| 7996 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7997 | key_bits = psa_get_key_bits(&attributes); |
| 7998 | |
| 7999 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8000 | TEST_ASSERT(signature_size != 0); |
| 8001 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 8002 | ASSERT_ALLOC(signature, signature_size); |
| 8003 | |
| 8004 | PSA_ASSERT(psa_sign_message(key, alg, |
| 8005 | input_data->x, input_data->len, |
| 8006 | signature, signature_size, |
| 8007 | &signature_length)); |
| 8008 | TEST_LE_U(signature_length, signature_size); |
| 8009 | TEST_ASSERT(signature_length > 0); |
| 8010 | |
| 8011 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8012 | input_data->x, input_data->len, |
| 8013 | signature, signature_length)); |
| 8014 | |
| 8015 | if (input_data->len != 0) { |
| 8016 | /* Flip a bit in the input and verify that the signature is now |
| 8017 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 8018 | * because ECDSA may ignore the last few bits of the input. */ |
| 8019 | input_data->x[0] ^= 1; |
| 8020 | TEST_EQUAL(psa_verify_message(key, alg, |
| 8021 | input_data->x, input_data->len, |
| 8022 | signature, signature_length), |
| 8023 | PSA_ERROR_INVALID_SIGNATURE); |
| 8024 | } |
| 8025 | |
| 8026 | exit: |
| 8027 | psa_reset_key_attributes(&attributes); |
| 8028 | |
| 8029 | psa_destroy_key(key); |
| 8030 | mbedtls_free(signature); |
| 8031 | PSA_DONE(); |
| 8032 | } |
| 8033 | /* END_CASE */ |
| 8034 | |
| 8035 | /* BEGIN_CASE */ |
| 8036 | void verify_message(int key_type_arg, |
| 8037 | data_t *key_data, |
| 8038 | int alg_arg, |
| 8039 | data_t *input_data, |
| 8040 | data_t *signature_data) |
| 8041 | { |
| 8042 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8043 | psa_key_type_t key_type = key_type_arg; |
| 8044 | psa_algorithm_t alg = alg_arg; |
| 8045 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8046 | |
| 8047 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 8048 | |
| 8049 | PSA_ASSERT(psa_crypto_init()); |
| 8050 | |
| 8051 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8052 | psa_set_key_algorithm(&attributes, alg); |
| 8053 | psa_set_key_type(&attributes, key_type); |
| 8054 | |
| 8055 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8056 | &key)); |
| 8057 | |
| 8058 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8059 | input_data->x, input_data->len, |
| 8060 | signature_data->x, signature_data->len)); |
| 8061 | |
| 8062 | exit: |
| 8063 | psa_reset_key_attributes(&attributes); |
| 8064 | psa_destroy_key(key); |
| 8065 | PSA_DONE(); |
| 8066 | } |
| 8067 | /* END_CASE */ |
| 8068 | |
| 8069 | /* BEGIN_CASE */ |
| 8070 | void verify_message_fail(int key_type_arg, |
| 8071 | data_t *key_data, |
| 8072 | int alg_arg, |
| 8073 | data_t *hash_data, |
| 8074 | data_t *signature_data, |
| 8075 | int expected_status_arg) |
| 8076 | { |
| 8077 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8078 | psa_key_type_t key_type = key_type_arg; |
| 8079 | psa_algorithm_t alg = alg_arg; |
| 8080 | psa_status_t actual_status; |
| 8081 | psa_status_t expected_status = expected_status_arg; |
| 8082 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8083 | |
| 8084 | PSA_ASSERT(psa_crypto_init()); |
| 8085 | |
| 8086 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8087 | psa_set_key_algorithm(&attributes, alg); |
| 8088 | psa_set_key_type(&attributes, key_type); |
| 8089 | |
| 8090 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8091 | &key)); |
| 8092 | |
| 8093 | actual_status = psa_verify_message(key, alg, |
| 8094 | hash_data->x, hash_data->len, |
| 8095 | signature_data->x, |
| 8096 | signature_data->len); |
| 8097 | TEST_EQUAL(actual_status, expected_status); |
| 8098 | |
| 8099 | exit: |
| 8100 | psa_reset_key_attributes(&attributes); |
| 8101 | psa_destroy_key(key); |
| 8102 | PSA_DONE(); |
| 8103 | } |
| 8104 | /* END_CASE */ |
| 8105 | |
| 8106 | /* BEGIN_CASE */ |
| 8107 | void asymmetric_encrypt(int key_type_arg, |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8108 | data_t *key_data, |
| 8109 | int alg_arg, |
| 8110 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8111 | data_t *label, |
| 8112 | int expected_output_length_arg, |
| 8113 | int expected_status_arg) |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8114 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8115 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8116 | psa_key_type_t key_type = key_type_arg; |
| 8117 | psa_algorithm_t alg = alg_arg; |
| 8118 | size_t expected_output_length = expected_output_length_arg; |
| 8119 | size_t key_bits; |
| 8120 | unsigned char *output = NULL; |
| 8121 | size_t output_size; |
| 8122 | size_t output_length = ~0; |
| 8123 | psa_status_t actual_status; |
| 8124 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8125 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8126 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8127 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 8128 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8129 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8130 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 8131 | psa_set_key_algorithm(&attributes, alg); |
| 8132 | psa_set_key_type(&attributes, key_type); |
| 8133 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8134 | &key)); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8135 | |
| 8136 | /* Determine the maximum output length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8137 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8138 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8139 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8140 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8141 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
| 8142 | ASSERT_ALLOC(output, output_size); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8143 | |
| 8144 | /* Encrypt the input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8145 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8146 | input_data->x, input_data->len, |
| 8147 | label->x, label->len, |
| 8148 | output, output_size, |
| 8149 | &output_length); |
| 8150 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8151 | if (actual_status == PSA_SUCCESS) { |
| 8152 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8153 | } else { |
| 8154 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8155 | } |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8156 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8157 | /* If the label is empty, the test framework puts a non-null pointer |
| 8158 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8159 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8160 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8161 | if (output_size != 0) { |
| 8162 | memset(output, 0, output_size); |
| 8163 | } |
| 8164 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8165 | input_data->x, input_data->len, |
| 8166 | NULL, label->len, |
| 8167 | output, output_size, |
| 8168 | &output_length); |
| 8169 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8170 | if (actual_status == PSA_SUCCESS) { |
| 8171 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8172 | } else { |
| 8173 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8174 | } |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8175 | } |
| 8176 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8177 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8178 | /* |
| 8179 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8180 | * thus reset them as required. |
| 8181 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8182 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8183 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8184 | psa_destroy_key(key); |
| 8185 | mbedtls_free(output); |
| 8186 | PSA_DONE(); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8187 | } |
| 8188 | /* END_CASE */ |
| 8189 | |
| 8190 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8191 | void asymmetric_encrypt_decrypt(int key_type_arg, |
| 8192 | data_t *key_data, |
| 8193 | int alg_arg, |
| 8194 | data_t *input_data, |
| 8195 | data_t *label) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8196 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8197 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8198 | psa_key_type_t key_type = key_type_arg; |
| 8199 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8200 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8201 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8202 | size_t output_size; |
| 8203 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8204 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8205 | size_t output2_size; |
| 8206 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8207 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8208 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8209 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8210 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8211 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 8212 | psa_set_key_algorithm(&attributes, alg); |
| 8213 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8215 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8216 | &key)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8217 | |
| 8218 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8219 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8220 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8221 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8222 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8223 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
| 8224 | ASSERT_ALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8225 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8226 | output2_size = input_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8227 | TEST_LE_U(output2_size, |
| 8228 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)); |
| 8229 | TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
| 8230 | ASSERT_ALLOC(output2, output2_size); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8231 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 8232 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 8233 | * the original plaintext because of the non-optional random |
| 8234 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8235 | PSA_ASSERT(psa_asymmetric_encrypt(key, alg, |
| 8236 | input_data->x, input_data->len, |
| 8237 | label->x, label->len, |
| 8238 | output, output_size, |
| 8239 | &output_length)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8240 | /* We don't know what ciphertext length to expect, but check that |
| 8241 | * it looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8242 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8243 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8244 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8245 | output, output_length, |
| 8246 | label->x, label->len, |
| 8247 | output2, output2_size, |
| 8248 | &output2_length)); |
| 8249 | ASSERT_COMPARE(input_data->x, input_data->len, |
| 8250 | output2, output2_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8251 | |
| 8252 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8253 | /* |
| 8254 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8255 | * thus reset them as required. |
| 8256 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8257 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8258 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8259 | psa_destroy_key(key); |
| 8260 | mbedtls_free(output); |
| 8261 | mbedtls_free(output2); |
| 8262 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8263 | } |
| 8264 | /* END_CASE */ |
| 8265 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8266 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8267 | void asymmetric_decrypt(int key_type_arg, |
| 8268 | data_t *key_data, |
| 8269 | int alg_arg, |
| 8270 | data_t *input_data, |
| 8271 | data_t *label, |
| 8272 | data_t *expected_data) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8273 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8274 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8275 | psa_key_type_t key_type = key_type_arg; |
| 8276 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8277 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8278 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 8279 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8280 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8281 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8282 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8283 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8284 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8285 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8286 | psa_set_key_algorithm(&attributes, alg); |
| 8287 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8288 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8289 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8290 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8291 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8292 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8293 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8294 | |
| 8295 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8296 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8297 | TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
| 8298 | ASSERT_ALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8299 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8300 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8301 | input_data->x, input_data->len, |
| 8302 | label->x, label->len, |
| 8303 | output, |
| 8304 | output_size, |
| 8305 | &output_length)); |
| 8306 | ASSERT_COMPARE(expected_data->x, expected_data->len, |
| 8307 | output, output_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8308 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8309 | /* If the label is empty, the test framework puts a non-null pointer |
| 8310 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8311 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8312 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8313 | if (output_size != 0) { |
| 8314 | memset(output, 0, output_size); |
| 8315 | } |
| 8316 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8317 | input_data->x, input_data->len, |
| 8318 | NULL, label->len, |
| 8319 | output, |
| 8320 | output_size, |
| 8321 | &output_length)); |
| 8322 | ASSERT_COMPARE(expected_data->x, expected_data->len, |
| 8323 | output, output_length); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8324 | } |
| 8325 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8326 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8327 | psa_reset_key_attributes(&attributes); |
| 8328 | psa_destroy_key(key); |
| 8329 | mbedtls_free(output); |
| 8330 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8331 | } |
| 8332 | /* END_CASE */ |
| 8333 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8334 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8335 | void asymmetric_decrypt_fail(int key_type_arg, |
| 8336 | data_t *key_data, |
| 8337 | int alg_arg, |
| 8338 | data_t *input_data, |
| 8339 | data_t *label, |
| 8340 | int output_size_arg, |
| 8341 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8342 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8343 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8344 | psa_key_type_t key_type = key_type_arg; |
| 8345 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8346 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 8347 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8348 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8349 | psa_status_t actual_status; |
| 8350 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8351 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8353 | ASSERT_ALLOC(output, output_size); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8354 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8355 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8356 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8357 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8358 | psa_set_key_algorithm(&attributes, alg); |
| 8359 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8360 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8361 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8362 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8363 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8364 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8365 | input_data->x, input_data->len, |
| 8366 | label->x, label->len, |
| 8367 | output, output_size, |
| 8368 | &output_length); |
| 8369 | TEST_EQUAL(actual_status, expected_status); |
| 8370 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8371 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8372 | /* If the label is empty, the test framework puts a non-null pointer |
| 8373 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8374 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8375 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8376 | if (output_size != 0) { |
| 8377 | memset(output, 0, output_size); |
| 8378 | } |
| 8379 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8380 | input_data->x, input_data->len, |
| 8381 | NULL, label->len, |
| 8382 | output, output_size, |
| 8383 | &output_length); |
| 8384 | TEST_EQUAL(actual_status, expected_status); |
| 8385 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8386 | } |
| 8387 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8388 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8389 | psa_reset_key_attributes(&attributes); |
| 8390 | psa_destroy_key(key); |
| 8391 | mbedtls_free(output); |
| 8392 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8393 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8394 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8395 | |
| 8396 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8397 | void key_derivation_init() |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8398 | { |
| 8399 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 8400 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 8401 | * 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] | 8402 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8403 | size_t capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8404 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init(); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 8405 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8406 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8407 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8408 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8409 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8410 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8411 | TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity), |
| 8412 | PSA_ERROR_BAD_STATE); |
| 8413 | TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity), |
| 8414 | PSA_ERROR_BAD_STATE); |
| 8415 | TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity), |
| 8416 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8417 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8418 | /* A default operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8419 | PSA_ASSERT(psa_key_derivation_abort(&func)); |
| 8420 | PSA_ASSERT(psa_key_derivation_abort(&init)); |
| 8421 | PSA_ASSERT(psa_key_derivation_abort(&zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8422 | } |
| 8423 | /* END_CASE */ |
| 8424 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 8425 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8426 | void derive_setup(int alg_arg, int expected_status_arg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8427 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8428 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8429 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8430 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8431 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8432 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8433 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8434 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8435 | expected_status); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8436 | |
| 8437 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8438 | psa_key_derivation_abort(&operation); |
| 8439 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8440 | } |
| 8441 | /* END_CASE */ |
| 8442 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8443 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8444 | void derive_set_capacity(int alg_arg, int capacity_arg, |
| 8445 | int expected_status_arg) |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8446 | { |
| 8447 | psa_algorithm_t alg = alg_arg; |
| 8448 | size_t capacity = capacity_arg; |
| 8449 | psa_status_t expected_status = expected_status_arg; |
| 8450 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8451 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8452 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8453 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8454 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8455 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8456 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 8457 | expected_status); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8458 | |
| 8459 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8460 | psa_key_derivation_abort(&operation); |
| 8461 | PSA_DONE(); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8462 | } |
| 8463 | /* END_CASE */ |
| 8464 | |
| 8465 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8466 | void derive_input(int alg_arg, |
| 8467 | int step_arg1, int key_type_arg1, data_t *input1, |
| 8468 | int expected_status_arg1, |
| 8469 | int step_arg2, int key_type_arg2, data_t *input2, |
| 8470 | int expected_status_arg2, |
| 8471 | int step_arg3, int key_type_arg3, data_t *input3, |
| 8472 | int expected_status_arg3, |
| 8473 | int output_key_type_arg, int expected_output_status_arg) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8474 | { |
| 8475 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8476 | psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 }; |
| 8477 | psa_key_type_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 }; |
| 8478 | psa_status_t expected_statuses[] = { expected_status_arg1, |
| 8479 | expected_status_arg2, |
| 8480 | expected_status_arg3 }; |
| 8481 | data_t *inputs[] = { input1, input2, input3 }; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8482 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8483 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8484 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8485 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8486 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8487 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8488 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8489 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8490 | psa_status_t expected_output_status = expected_output_status_arg; |
| 8491 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8492 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8493 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8494 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8495 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8496 | psa_set_key_algorithm(&attributes, alg); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8497 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8498 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8499 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8500 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8501 | mbedtls_test_set_step(i); |
| 8502 | if (steps[i] == 0) { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 8503 | /* Skip this step */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8504 | } else if (key_types[i] != PSA_KEY_TYPE_NONE) { |
| 8505 | psa_set_key_type(&attributes, key_types[i]); |
| 8506 | PSA_ASSERT(psa_import_key(&attributes, |
| 8507 | inputs[i]->x, inputs[i]->len, |
| 8508 | &keys[i])); |
| 8509 | if (PSA_KEY_TYPE_IS_KEY_PAIR(key_types[i]) && |
| 8510 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8511 | // When taking a private key as secret input, use key agreement |
| 8512 | // to add the shared secret to the derivation |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8513 | TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self( |
| 8514 | &operation, keys[i]), |
| 8515 | expected_statuses[i]); |
| 8516 | } else { |
| 8517 | TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i], |
| 8518 | keys[i]), |
| 8519 | expected_statuses[i]); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8520 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8521 | } else { |
| 8522 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8523 | &operation, steps[i], |
| 8524 | inputs[i]->x, inputs[i]->len), |
| 8525 | expected_statuses[i]); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8526 | } |
| 8527 | } |
| 8528 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8529 | if (output_key_type != PSA_KEY_TYPE_NONE) { |
| 8530 | psa_reset_key_attributes(&attributes); |
| 8531 | psa_set_key_type(&attributes, output_key_type); |
| 8532 | psa_set_key_bits(&attributes, 8); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8533 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8534 | psa_key_derivation_output_key(&attributes, &operation, |
| 8535 | &output_key); |
| 8536 | } else { |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8537 | uint8_t buffer[1]; |
| 8538 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8539 | psa_key_derivation_output_bytes(&operation, |
| 8540 | buffer, sizeof(buffer)); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8541 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8542 | TEST_EQUAL(actual_output_status, expected_output_status); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8543 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8544 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8545 | psa_key_derivation_abort(&operation); |
| 8546 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8547 | psa_destroy_key(keys[i]); |
| 8548 | } |
| 8549 | psa_destroy_key(output_key); |
| 8550 | PSA_DONE(); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8551 | } |
| 8552 | /* END_CASE */ |
| 8553 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8554 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8555 | void derive_over_capacity(int alg_arg) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8556 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8557 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8558 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 8559 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8560 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8561 | unsigned char input1[] = "Input 1"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8562 | size_t input1_length = sizeof(input1); |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8563 | unsigned char input2[] = "Input 2"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8564 | size_t input2_length = sizeof(input2); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8565 | uint8_t buffer[42]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8566 | size_t capacity = sizeof(buffer); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 8567 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 8568 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8569 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8570 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8571 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8572 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8573 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8574 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8575 | psa_set_key_algorithm(&attributes, alg); |
| 8576 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8577 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8578 | PSA_ASSERT(psa_import_key(&attributes, |
| 8579 | key_data, sizeof(key_data), |
| 8580 | &key)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8581 | |
| 8582 | /* valid key derivation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8583 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8584 | input1, input1_length, |
| 8585 | input2, input2_length, |
| 8586 | capacity)) { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8587 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8588 | } |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8589 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8590 | /* state of operation shouldn't allow additional generation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8591 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8592 | PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8593 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8594 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8595 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8596 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity), |
| 8597 | PSA_ERROR_INSUFFICIENT_DATA); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8598 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8599 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8600 | psa_key_derivation_abort(&operation); |
| 8601 | psa_destroy_key(key); |
| 8602 | PSA_DONE(); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8603 | } |
| 8604 | /* END_CASE */ |
| 8605 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8606 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8607 | void derive_actions_without_setup() |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8608 | { |
| 8609 | uint8_t output_buffer[16]; |
| 8610 | size_t buffer_size = 16; |
| 8611 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8612 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8613 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8614 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8615 | output_buffer, buffer_size) |
| 8616 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8617 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8618 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8619 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8620 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8621 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8622 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8623 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8624 | output_buffer, buffer_size) |
| 8625 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8626 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8627 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8628 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8629 | |
| 8630 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8631 | psa_key_derivation_abort(&operation); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8632 | } |
| 8633 | /* END_CASE */ |
| 8634 | |
| 8635 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8636 | void derive_output(int alg_arg, |
| 8637 | int step1_arg, data_t *input1, int expected_status_arg1, |
| 8638 | int step2_arg, data_t *input2, int expected_status_arg2, |
| 8639 | int step3_arg, data_t *input3, int expected_status_arg3, |
| 8640 | int step4_arg, data_t *input4, int expected_status_arg4, |
| 8641 | data_t *key_agreement_peer_key, |
| 8642 | int requested_capacity_arg, |
| 8643 | data_t *expected_output1, |
| 8644 | data_t *expected_output2, |
| 8645 | int other_key_input_type, |
| 8646 | int key_input_type, |
| 8647 | int derive_type) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8648 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8649 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8650 | psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg }; |
| 8651 | data_t *inputs[] = { input1, input2, input3, input4 }; |
| 8652 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8653 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8654 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8655 | MBEDTLS_SVC_KEY_ID_INIT }; |
| 8656 | psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2, |
| 8657 | expected_status_arg3, expected_status_arg4 }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8658 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8659 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8660 | uint8_t *expected_outputs[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8661 | { expected_output1->x, expected_output2->x }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8662 | size_t output_sizes[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8663 | { expected_output1->len, expected_output2->len }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8664 | size_t output_buffer_size = 0; |
| 8665 | uint8_t *output_buffer = NULL; |
| 8666 | size_t expected_capacity; |
| 8667 | size_t current_capacity; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8668 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; |
| 8669 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; |
| 8670 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; |
| 8671 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8672 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8673 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8674 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8676 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
| 8677 | if (output_sizes[i] > output_buffer_size) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8678 | output_buffer_size = output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8679 | } |
| 8680 | if (output_sizes[i] == 0) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8681 | expected_outputs[i] = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8682 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8683 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8684 | ASSERT_ALLOC(output_buffer, output_buffer_size); |
| 8685 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8686 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8687 | /* Extraction phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8688 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8689 | PSA_ASSERT(psa_key_derivation_set_capacity(&operation, |
| 8690 | requested_capacity)); |
| 8691 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8692 | switch (steps[i]) { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8693 | case 0: |
| 8694 | break; |
| 8695 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8696 | switch (key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8697 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8698 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8699 | &operation, steps[i], |
| 8700 | inputs[i]->x, inputs[i]->len), |
| 8701 | statuses[i]); |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8702 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8703 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8704 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8705 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8706 | break; |
| 8707 | case 1: // input key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8708 | psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE); |
| 8709 | psa_set_key_algorithm(&attributes1, alg); |
| 8710 | psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8711 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8712 | PSA_ASSERT(psa_import_key(&attributes1, |
| 8713 | inputs[i]->x, inputs[i]->len, |
| 8714 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8715 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8716 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 8717 | PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1)); |
| 8718 | TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)), |
| 8719 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8720 | } |
| 8721 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8722 | PSA_ASSERT(psa_key_derivation_input_key(&operation, |
| 8723 | steps[i], |
| 8724 | keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8725 | break; |
| 8726 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8727 | TEST_ASSERT(!"default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8728 | break; |
| 8729 | } |
| 8730 | break; |
| 8731 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8732 | switch (other_key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8733 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8734 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 8735 | steps[i], |
| 8736 | inputs[i]->x, |
| 8737 | inputs[i]->len), |
| 8738 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8739 | break; |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 8740 | case 1: // input key, type DERIVE |
| 8741 | case 11: // input key, type RAW |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8742 | psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE); |
| 8743 | psa_set_key_algorithm(&attributes2, alg); |
| 8744 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8745 | |
| 8746 | // other secret of type RAW_DATA passed with input_key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8747 | if (other_key_input_type == 11) { |
| 8748 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA); |
| 8749 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8750 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8751 | PSA_ASSERT(psa_import_key(&attributes2, |
| 8752 | inputs[i]->x, inputs[i]->len, |
| 8753 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8754 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8755 | TEST_EQUAL(psa_key_derivation_input_key(&operation, |
| 8756 | steps[i], |
| 8757 | keys[i]), |
| 8758 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8759 | break; |
| 8760 | case 2: // key agreement |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8761 | psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE); |
| 8762 | psa_set_key_algorithm(&attributes3, alg); |
| 8763 | psa_set_key_type(&attributes3, |
| 8764 | PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8765 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8766 | PSA_ASSERT(psa_import_key(&attributes3, |
| 8767 | inputs[i]->x, inputs[i]->len, |
| 8768 | &keys[i])); |
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 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 8771 | &operation, |
| 8772 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 8773 | keys[i], key_agreement_peer_key->x, |
| 8774 | key_agreement_peer_key->len), statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8775 | break; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8776 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8777 | TEST_ASSERT(!"default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8778 | break; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8779 | } |
| 8780 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8781 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8782 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8783 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8784 | break; |
| 8785 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8786 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8787 | &operation, steps[i], |
| 8788 | inputs[i]->x, inputs[i]->len), statuses[i]); |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 8789 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8790 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 8791 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8792 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8793 | break; |
| 8794 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 8795 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8796 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8797 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8798 | ¤t_capacity)); |
| 8799 | TEST_EQUAL(current_capacity, requested_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8800 | expected_capacity = requested_capacity; |
| 8801 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8802 | if (derive_type == 1) { // output key |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8803 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; |
| 8804 | |
| 8805 | /* For output key derivation secret must be provided using |
| 8806 | input key, otherwise operation is not permitted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8807 | if (key_input_type == 1) { |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8808 | expected_status = PSA_SUCCESS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8809 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8810 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8811 | psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT); |
| 8812 | psa_set_key_algorithm(&attributes4, alg); |
| 8813 | psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE); |
| 8814 | psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8815 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8816 | TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation, |
| 8817 | &derived_key), expected_status); |
| 8818 | } else { // output bytes |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8819 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8820 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8821 | /* Read some bytes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8822 | status = psa_key_derivation_output_bytes(&operation, |
| 8823 | output_buffer, output_sizes[i]); |
| 8824 | if (expected_capacity == 0 && output_sizes[i] == 0) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8825 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8826 | TEST_ASSERT(status == PSA_SUCCESS || |
| 8827 | status == PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8828 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8829 | } else if (expected_capacity == 0 || |
| 8830 | output_sizes[i] > expected_capacity) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8831 | /* Capacity exceeded. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8832 | TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8833 | expected_capacity = 0; |
| 8834 | continue; |
| 8835 | } |
| 8836 | /* Success. Check the read data. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8837 | PSA_ASSERT(status); |
| 8838 | if (output_sizes[i] != 0) { |
| 8839 | ASSERT_COMPARE(output_buffer, output_sizes[i], |
| 8840 | expected_outputs[i], output_sizes[i]); |
| 8841 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8842 | /* Check the operation status. */ |
| 8843 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8844 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8845 | ¤t_capacity)); |
| 8846 | TEST_EQUAL(expected_capacity, current_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8847 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8848 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8849 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8850 | |
| 8851 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8852 | mbedtls_free(output_buffer); |
| 8853 | psa_key_derivation_abort(&operation); |
| 8854 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8855 | psa_destroy_key(keys[i]); |
| 8856 | } |
| 8857 | psa_destroy_key(derived_key); |
| 8858 | PSA_DONE(); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8859 | } |
| 8860 | /* END_CASE */ |
| 8861 | |
| 8862 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8863 | void derive_full(int alg_arg, |
| 8864 | data_t *key_data, |
| 8865 | data_t *input1, |
| 8866 | data_t *input2, |
| 8867 | int requested_capacity_arg) |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8868 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8869 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8870 | psa_algorithm_t alg = alg_arg; |
| 8871 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8872 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8873 | unsigned char output_buffer[16]; |
| 8874 | size_t expected_capacity = requested_capacity; |
| 8875 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8876 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8877 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8878 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8879 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8880 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8881 | psa_set_key_algorithm(&attributes, alg); |
| 8882 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8883 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8884 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8885 | &key)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8886 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8887 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8888 | input1->x, input1->len, |
| 8889 | input2->x, input2->len, |
| 8890 | requested_capacity)) { |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 8891 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8892 | } |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 8893 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8894 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8895 | ¤t_capacity)); |
| 8896 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8897 | |
| 8898 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8899 | while (current_capacity > 0) { |
| 8900 | size_t read_size = sizeof(output_buffer); |
| 8901 | if (read_size > current_capacity) { |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8902 | read_size = current_capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8903 | } |
| 8904 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8905 | output_buffer, |
| 8906 | read_size)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8907 | expected_capacity -= read_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8908 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8909 | ¤t_capacity)); |
| 8910 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8911 | } |
| 8912 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8913 | /* Check that the operation refuses to go over capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8914 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1), |
| 8915 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8916 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8917 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8918 | |
| 8919 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8920 | psa_key_derivation_abort(&operation); |
| 8921 | psa_destroy_key(key); |
| 8922 | PSA_DONE(); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8923 | } |
| 8924 | /* END_CASE */ |
| 8925 | |
Przemek Stekiel | 8258ea7 | 2022-10-19 12:17:19 +0200 | [diff] [blame] | 8926 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8927 | void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg, |
| 8928 | int derivation_step, |
| 8929 | int capacity, int expected_capacity_status_arg, |
| 8930 | data_t *expected_output, |
| 8931 | int expected_output_status_arg) |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8932 | { |
| 8933 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 8934 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | d378504 | 2022-09-16 06:45:44 -0400 | [diff] [blame] | 8935 | 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] | 8936 | uint8_t *output_buffer = NULL; |
| 8937 | psa_status_t status; |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 8938 | psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg; |
| 8939 | psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg; |
| 8940 | 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] | 8941 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8942 | ASSERT_ALLOC(output_buffer, expected_output->len); |
| 8943 | PSA_ASSERT(psa_crypto_init()); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8944 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8945 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8946 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 8947 | expected_capacity_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8948 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8949 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 8950 | step, input->x, input->len), |
| 8951 | expected_input_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8952 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8953 | if (((psa_status_t) expected_input_status) != PSA_SUCCESS) { |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8954 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8955 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8956 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8957 | status = psa_key_derivation_output_bytes(&operation, output_buffer, |
| 8958 | expected_output->len); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8959 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8960 | TEST_EQUAL(status, expected_output_status); |
| 8961 | if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) { |
| 8962 | ASSERT_COMPARE(output_buffer, expected_output->len, expected_output->x, |
| 8963 | expected_output->len); |
| 8964 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8965 | |
| 8966 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8967 | mbedtls_free(output_buffer); |
| 8968 | psa_key_derivation_abort(&operation); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8969 | PSA_DONE(); |
| 8970 | } |
| 8971 | /* END_CASE */ |
| 8972 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 8973 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8974 | void derive_key_exercise(int alg_arg, |
| 8975 | data_t *key_data, |
| 8976 | data_t *input1, |
| 8977 | data_t *input2, |
| 8978 | int derived_type_arg, |
| 8979 | int derived_bits_arg, |
| 8980 | int derived_usage_arg, |
| 8981 | int derived_alg_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8982 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8983 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8984 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8985 | psa_algorithm_t alg = alg_arg; |
| 8986 | psa_key_type_t derived_type = derived_type_arg; |
| 8987 | size_t derived_bits = derived_bits_arg; |
| 8988 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 8989 | psa_algorithm_t derived_alg = derived_alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8990 | size_t capacity = PSA_BITS_TO_BYTES(derived_bits); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8991 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8992 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8993 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8994 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8995 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8996 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8997 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8998 | psa_set_key_algorithm(&attributes, alg); |
| 8999 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
| 9000 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 9001 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9002 | |
| 9003 | /* Derive a key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9004 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9005 | input1->x, input1->len, |
| 9006 | input2->x, input2->len, |
| 9007 | capacity)) { |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9008 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9009 | } |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9010 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9011 | psa_set_key_usage_flags(&attributes, derived_usage); |
| 9012 | psa_set_key_algorithm(&attributes, derived_alg); |
| 9013 | psa_set_key_type(&attributes, derived_type); |
| 9014 | psa_set_key_bits(&attributes, derived_bits); |
| 9015 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation, |
| 9016 | &derived_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9017 | |
| 9018 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9019 | PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes)); |
| 9020 | TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type); |
| 9021 | TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9022 | |
| 9023 | /* Exercise the derived key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9024 | if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) { |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9025 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9026 | } |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9027 | |
| 9028 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9029 | /* |
| 9030 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9031 | * thus reset them as required. |
| 9032 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9033 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9035 | psa_key_derivation_abort(&operation); |
| 9036 | psa_destroy_key(base_key); |
| 9037 | psa_destroy_key(derived_key); |
| 9038 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9039 | } |
| 9040 | /* END_CASE */ |
| 9041 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9042 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9043 | void derive_key_export(int alg_arg, |
| 9044 | data_t *key_data, |
| 9045 | data_t *input1, |
| 9046 | data_t *input2, |
| 9047 | int bytes1_arg, |
| 9048 | int bytes2_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9049 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9050 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9051 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9052 | psa_algorithm_t alg = alg_arg; |
| 9053 | size_t bytes1 = bytes1_arg; |
| 9054 | size_t bytes2 = bytes2_arg; |
| 9055 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9056 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9057 | uint8_t *output_buffer = NULL; |
| 9058 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9059 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9060 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9061 | size_t length; |
| 9062 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9063 | ASSERT_ALLOC(output_buffer, capacity); |
| 9064 | ASSERT_ALLOC(export_buffer, capacity); |
| 9065 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9066 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9067 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9068 | psa_set_key_algorithm(&base_attributes, alg); |
| 9069 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9070 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9071 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9072 | |
| 9073 | /* Derive some material and output it. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9074 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9075 | input1->x, input1->len, |
| 9076 | input2->x, input2->len, |
| 9077 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9078 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9079 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9080 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9081 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9082 | output_buffer, |
| 9083 | capacity)); |
| 9084 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9085 | |
| 9086 | /* 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] | 9087 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9088 | input1->x, input1->len, |
| 9089 | input2->x, input2->len, |
| 9090 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9091 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9092 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9093 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9094 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9095 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9096 | psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA); |
| 9097 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1)); |
| 9098 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9099 | &derived_key)); |
| 9100 | PSA_ASSERT(psa_export_key(derived_key, |
| 9101 | export_buffer, bytes1, |
| 9102 | &length)); |
| 9103 | TEST_EQUAL(length, bytes1); |
| 9104 | PSA_ASSERT(psa_destroy_key(derived_key)); |
| 9105 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2)); |
| 9106 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9107 | &derived_key)); |
| 9108 | PSA_ASSERT(psa_export_key(derived_key, |
| 9109 | export_buffer + bytes1, bytes2, |
| 9110 | &length)); |
| 9111 | TEST_EQUAL(length, bytes2); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9112 | |
| 9113 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9114 | ASSERT_COMPARE(output_buffer, bytes1 + bytes2, |
| 9115 | export_buffer, capacity); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9116 | |
| 9117 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9118 | mbedtls_free(output_buffer); |
| 9119 | mbedtls_free(export_buffer); |
| 9120 | psa_key_derivation_abort(&operation); |
| 9121 | psa_destroy_key(base_key); |
| 9122 | psa_destroy_key(derived_key); |
| 9123 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9124 | } |
| 9125 | /* END_CASE */ |
| 9126 | |
| 9127 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9128 | void derive_key_type(int alg_arg, |
| 9129 | data_t *key_data, |
| 9130 | data_t *input1, |
| 9131 | data_t *input2, |
| 9132 | int key_type_arg, int bits_arg, |
| 9133 | data_t *expected_export) |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9134 | { |
| 9135 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9136 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9137 | const psa_algorithm_t alg = alg_arg; |
| 9138 | const psa_key_type_t key_type = key_type_arg; |
| 9139 | const size_t bits = bits_arg; |
| 9140 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9141 | const size_t export_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9142 | PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9143 | uint8_t *export_buffer = NULL; |
| 9144 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9145 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9146 | size_t export_length; |
| 9147 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9148 | ASSERT_ALLOC(export_buffer, export_buffer_size); |
| 9149 | PSA_ASSERT(psa_crypto_init()); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9151 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9152 | psa_set_key_algorithm(&base_attributes, alg); |
| 9153 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9154 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9155 | &base_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9156 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9157 | if (mbedtls_test_psa_setup_key_derivation_wrap( |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9158 | &operation, base_key, alg, |
| 9159 | input1->x, input1->len, |
| 9160 | input2->x, input2->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9161 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) { |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9162 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9163 | } |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9164 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9165 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9166 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9167 | psa_set_key_type(&derived_attributes, key_type); |
| 9168 | psa_set_key_bits(&derived_attributes, bits); |
| 9169 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9170 | &derived_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9172 | PSA_ASSERT(psa_export_key(derived_key, |
| 9173 | export_buffer, export_buffer_size, |
| 9174 | &export_length)); |
| 9175 | ASSERT_COMPARE(export_buffer, export_length, |
| 9176 | expected_export->x, expected_export->len); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9177 | |
| 9178 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9179 | mbedtls_free(export_buffer); |
| 9180 | psa_key_derivation_abort(&operation); |
| 9181 | psa_destroy_key(base_key); |
| 9182 | psa_destroy_key(derived_key); |
| 9183 | PSA_DONE(); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9184 | } |
| 9185 | /* END_CASE */ |
| 9186 | |
| 9187 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9188 | void derive_key(int alg_arg, |
| 9189 | data_t *key_data, data_t *input1, data_t *input2, |
| 9190 | int type_arg, int bits_arg, |
| 9191 | int expected_status_arg, |
| 9192 | int is_large_output) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9193 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9194 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9195 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9196 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 9197 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9198 | size_t bits = bits_arg; |
| 9199 | psa_status_t expected_status = expected_status_arg; |
| 9200 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9201 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9202 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9203 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9204 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9205 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9206 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9207 | psa_set_key_algorithm(&base_attributes, alg); |
| 9208 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9209 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9210 | &base_key)); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9211 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9212 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9213 | input1->x, input1->len, |
| 9214 | input2->x, input2->len, |
| 9215 | SIZE_MAX)) { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9216 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9217 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9218 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9219 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9220 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9221 | psa_set_key_type(&derived_attributes, type); |
| 9222 | psa_set_key_bits(&derived_attributes, bits); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9223 | |
| 9224 | psa_status_t status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9225 | psa_key_derivation_output_key(&derived_attributes, |
| 9226 | &operation, |
| 9227 | &derived_key); |
| 9228 | if (is_large_output > 0) { |
| 9229 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9230 | } |
| 9231 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9232 | |
| 9233 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9234 | psa_key_derivation_abort(&operation); |
| 9235 | psa_destroy_key(base_key); |
| 9236 | psa_destroy_key(derived_key); |
| 9237 | PSA_DONE(); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9238 | } |
| 9239 | /* END_CASE */ |
| 9240 | |
| 9241 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9242 | void key_agreement_setup(int alg_arg, |
| 9243 | int our_key_type_arg, int our_key_alg_arg, |
| 9244 | data_t *our_key_data, data_t *peer_key_data, |
| 9245 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9246 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9247 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9248 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 9249 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9250 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9251 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9252 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9253 | psa_status_t expected_status = expected_status_arg; |
| 9254 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9256 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9257 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9258 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9259 | psa_set_key_algorithm(&attributes, our_key_alg); |
| 9260 | psa_set_key_type(&attributes, our_key_type); |
| 9261 | PSA_ASSERT(psa_import_key(&attributes, |
| 9262 | our_key_data->x, our_key_data->len, |
| 9263 | &our_key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9264 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9265 | /* The tests currently include inputs that should fail at either step. |
| 9266 | * Test cases that fail at the setup step should be changed to call |
| 9267 | * key_derivation_setup instead, and this function should be renamed |
| 9268 | * to key_agreement_fail. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9269 | status = psa_key_derivation_setup(&operation, alg); |
| 9270 | if (status == PSA_SUCCESS) { |
| 9271 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 9272 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 9273 | our_key, |
| 9274 | peer_key_data->x, peer_key_data->len), |
| 9275 | expected_status); |
| 9276 | } else { |
| 9277 | TEST_ASSERT(status == expected_status); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9278 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9279 | |
| 9280 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9281 | psa_key_derivation_abort(&operation); |
| 9282 | psa_destroy_key(our_key); |
| 9283 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9284 | } |
| 9285 | /* END_CASE */ |
| 9286 | |
| 9287 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9288 | void raw_key_agreement(int alg_arg, |
| 9289 | int our_key_type_arg, data_t *our_key_data, |
| 9290 | data_t *peer_key_data, |
| 9291 | data_t *expected_output) |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9292 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9293 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9294 | psa_algorithm_t alg = alg_arg; |
| 9295 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9296 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9297 | unsigned char *output = NULL; |
| 9298 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9299 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9300 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9301 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9302 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9303 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9304 | psa_set_key_algorithm(&attributes, alg); |
| 9305 | psa_set_key_type(&attributes, our_key_type); |
| 9306 | PSA_ASSERT(psa_import_key(&attributes, |
| 9307 | our_key_data->x, our_key_data->len, |
| 9308 | &our_key)); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9309 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9310 | PSA_ASSERT(psa_get_key_attributes(our_key, &attributes)); |
| 9311 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9312 | |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9313 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9314 | TEST_LE_U(expected_output->len, |
| 9315 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits)); |
| 9316 | TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits), |
| 9317 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9318 | |
| 9319 | /* Good case with exact output size */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9320 | ASSERT_ALLOC(output, expected_output->len); |
| 9321 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9322 | peer_key_data->x, peer_key_data->len, |
| 9323 | output, expected_output->len, |
| 9324 | &output_length)); |
| 9325 | ASSERT_COMPARE(output, output_length, |
| 9326 | expected_output->x, expected_output->len); |
| 9327 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9328 | output = NULL; |
| 9329 | output_length = ~0; |
| 9330 | |
| 9331 | /* Larger buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9332 | ASSERT_ALLOC(output, expected_output->len + 1); |
| 9333 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9334 | peer_key_data->x, peer_key_data->len, |
| 9335 | output, expected_output->len + 1, |
| 9336 | &output_length)); |
| 9337 | ASSERT_COMPARE(output, output_length, |
| 9338 | expected_output->x, expected_output->len); |
| 9339 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9340 | output = NULL; |
| 9341 | output_length = ~0; |
| 9342 | |
| 9343 | /* Buffer too small */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9344 | ASSERT_ALLOC(output, expected_output->len - 1); |
| 9345 | TEST_EQUAL(psa_raw_key_agreement(alg, our_key, |
| 9346 | peer_key_data->x, peer_key_data->len, |
| 9347 | output, expected_output->len - 1, |
| 9348 | &output_length), |
| 9349 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9350 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9351 | TEST_LE_U(output_length, expected_output->len - 1); |
| 9352 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9353 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9354 | |
| 9355 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9356 | mbedtls_free(output); |
| 9357 | psa_destroy_key(our_key); |
| 9358 | PSA_DONE(); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9359 | } |
| 9360 | /* END_CASE */ |
| 9361 | |
| 9362 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9363 | void key_agreement_capacity(int alg_arg, |
| 9364 | int our_key_type_arg, data_t *our_key_data, |
| 9365 | data_t *peer_key_data, |
| 9366 | int expected_capacity_arg) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9367 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9368 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9369 | psa_algorithm_t alg = alg_arg; |
| 9370 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9371 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9372 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9373 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9374 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9375 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9376 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9377 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9378 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9379 | psa_set_key_algorithm(&attributes, alg); |
| 9380 | psa_set_key_type(&attributes, our_key_type); |
| 9381 | PSA_ASSERT(psa_import_key(&attributes, |
| 9382 | our_key_data->x, our_key_data->len, |
| 9383 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9385 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9386 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9387 | &operation, |
| 9388 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9389 | peer_key_data->x, peer_key_data->len)); |
| 9390 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9391 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9392 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9393 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9394 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9395 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9396 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 9397 | /* Test the advertised capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9398 | PSA_ASSERT(psa_key_derivation_get_capacity( |
| 9399 | &operation, &actual_capacity)); |
| 9400 | TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9401 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9402 | /* Test the actual capacity by reading the output. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9403 | while (actual_capacity > sizeof(output)) { |
| 9404 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9405 | output, sizeof(output))); |
| 9406 | actual_capacity -= sizeof(output); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9407 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9408 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9409 | output, actual_capacity)); |
| 9410 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1), |
| 9411 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9412 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9413 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9414 | psa_key_derivation_abort(&operation); |
| 9415 | psa_destroy_key(our_key); |
| 9416 | PSA_DONE(); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9417 | } |
| 9418 | /* END_CASE */ |
| 9419 | |
| 9420 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9421 | void key_agreement_output(int alg_arg, |
| 9422 | int our_key_type_arg, data_t *our_key_data, |
| 9423 | data_t *peer_key_data, |
| 9424 | data_t *expected_output1, data_t *expected_output2) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9425 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9426 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9427 | psa_algorithm_t alg = alg_arg; |
| 9428 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9429 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9430 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9431 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9433 | ASSERT_ALLOC(actual_output, MAX(expected_output1->len, |
| 9434 | expected_output2->len)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9435 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9436 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9437 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9438 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9439 | psa_set_key_algorithm(&attributes, alg); |
| 9440 | psa_set_key_type(&attributes, our_key_type); |
| 9441 | PSA_ASSERT(psa_import_key(&attributes, |
| 9442 | our_key_data->x, our_key_data->len, |
| 9443 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9444 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9445 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9446 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9447 | &operation, |
| 9448 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9449 | peer_key_data->x, peer_key_data->len)); |
| 9450 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9451 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9452 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9453 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9454 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9455 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9456 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9457 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9458 | actual_output, |
| 9459 | expected_output1->len)); |
| 9460 | ASSERT_COMPARE(actual_output, expected_output1->len, |
| 9461 | expected_output1->x, expected_output1->len); |
| 9462 | if (expected_output2->len != 0) { |
| 9463 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9464 | actual_output, |
| 9465 | expected_output2->len)); |
| 9466 | ASSERT_COMPARE(actual_output, expected_output2->len, |
| 9467 | expected_output2->x, expected_output2->len); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9468 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9469 | |
| 9470 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9471 | psa_key_derivation_abort(&operation); |
| 9472 | psa_destroy_key(our_key); |
| 9473 | PSA_DONE(); |
| 9474 | mbedtls_free(actual_output); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9475 | } |
| 9476 | /* END_CASE */ |
| 9477 | |
| 9478 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9479 | void generate_random(int bytes_arg) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9480 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9481 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9482 | unsigned char *output = NULL; |
| 9483 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9484 | size_t i; |
| 9485 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9486 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9487 | TEST_ASSERT(bytes_arg >= 0); |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 9488 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9489 | ASSERT_ALLOC(output, bytes); |
| 9490 | ASSERT_ALLOC(changed, bytes); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9491 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9492 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9493 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9494 | /* Run several times, to ensure that every output byte will be |
| 9495 | * nonzero at least once with overwhelming probability |
| 9496 | * (2^(-8*number_of_runs)). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9497 | for (run = 0; run < 10; run++) { |
| 9498 | if (bytes != 0) { |
| 9499 | memset(output, 0, bytes); |
| 9500 | } |
| 9501 | PSA_ASSERT(psa_generate_random(output, bytes)); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9502 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9503 | for (i = 0; i < bytes; i++) { |
| 9504 | if (output[i] != 0) { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9505 | ++changed[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9506 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9507 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9508 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9509 | |
| 9510 | /* Check that every byte was changed to nonzero at least once. This |
| 9511 | * validates that psa_generate_random is overwriting every byte of |
| 9512 | * the output buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9513 | for (i = 0; i < bytes; i++) { |
| 9514 | TEST_ASSERT(changed[i] != 0); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9515 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9516 | |
| 9517 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9518 | PSA_DONE(); |
| 9519 | mbedtls_free(output); |
| 9520 | mbedtls_free(changed); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9521 | } |
| 9522 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9523 | |
| 9524 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9525 | void generate_key(int type_arg, |
| 9526 | int bits_arg, |
| 9527 | int usage_arg, |
| 9528 | int alg_arg, |
| 9529 | int expected_status_arg, |
| 9530 | int is_large_key) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9531 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9532 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9533 | psa_key_type_t type = type_arg; |
| 9534 | psa_key_usage_t usage = usage_arg; |
| 9535 | size_t bits = bits_arg; |
| 9536 | psa_algorithm_t alg = alg_arg; |
| 9537 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9538 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 9539 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9540 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9541 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9542 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9543 | psa_set_key_usage_flags(&attributes, usage); |
| 9544 | psa_set_key_algorithm(&attributes, alg); |
| 9545 | psa_set_key_type(&attributes, type); |
| 9546 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9547 | |
| 9548 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9549 | psa_status_t status = psa_generate_key(&attributes, &key); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9550 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9551 | if (is_large_key > 0) { |
| 9552 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9553 | } |
| 9554 | TEST_EQUAL(status, expected_status); |
| 9555 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9556 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9557 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9558 | |
| 9559 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9560 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 9561 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 9562 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9563 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 9564 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9565 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 9566 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9567 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9568 | |
| 9569 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9570 | /* |
| 9571 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9572 | * thus reset them as required. |
| 9573 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9574 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9575 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9576 | psa_destroy_key(key); |
| 9577 | PSA_DONE(); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9578 | } |
| 9579 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 9580 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 9581 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR:PSA_WANT_ALG_RSA_PKCS1V15_CRYPT:PSA_WANT_ALG_RSA_PKCS1V15_SIGN:MBEDTLS_GENPRIME */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9582 | void generate_key_rsa(int bits_arg, |
| 9583 | data_t *e_arg, |
| 9584 | int expected_status_arg) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9585 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9586 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 9587 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9588 | size_t bits = bits_arg; |
| 9589 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 9590 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 9591 | psa_status_t expected_status = expected_status_arg; |
| 9592 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9593 | uint8_t *exported = NULL; |
| 9594 | size_t exported_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9595 | PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9596 | size_t exported_length = SIZE_MAX; |
| 9597 | uint8_t *e_read_buffer = NULL; |
| 9598 | int is_default_public_exponent = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9599 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9600 | size_t e_read_length = SIZE_MAX; |
| 9601 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9602 | if (e_arg->len == 0 || |
| 9603 | (e_arg->len == 3 && |
| 9604 | 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] | 9605 | is_default_public_exponent = 1; |
| 9606 | e_read_size = 0; |
| 9607 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9608 | ASSERT_ALLOC(e_read_buffer, e_read_size); |
| 9609 | ASSERT_ALLOC(exported, exported_size); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9610 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9611 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9612 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9613 | psa_set_key_usage_flags(&attributes, usage); |
| 9614 | psa_set_key_algorithm(&attributes, alg); |
| 9615 | PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type, |
| 9616 | e_arg->x, e_arg->len)); |
| 9617 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9618 | |
| 9619 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9620 | TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status); |
| 9621 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9622 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9623 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9624 | |
| 9625 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9626 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 9627 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 9628 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 9629 | PSA_ASSERT(psa_get_key_domain_parameters(&attributes, |
| 9630 | e_read_buffer, e_read_size, |
| 9631 | &e_read_length)); |
| 9632 | if (is_default_public_exponent) { |
| 9633 | TEST_EQUAL(e_read_length, 0); |
| 9634 | } else { |
| 9635 | ASSERT_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len); |
| 9636 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9637 | |
| 9638 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9639 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9640 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9641 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9642 | |
| 9643 | /* Export the key and check the public exponent. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9644 | PSA_ASSERT(psa_export_public_key(key, |
| 9645 | exported, exported_size, |
| 9646 | &exported_length)); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9647 | { |
| 9648 | uint8_t *p = exported; |
| 9649 | uint8_t *end = exported + exported_length; |
| 9650 | size_t len; |
| 9651 | /* RSAPublicKey ::= SEQUENCE { |
| 9652 | * modulus INTEGER, -- n |
| 9653 | * publicExponent INTEGER } -- e |
| 9654 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9655 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 9656 | MBEDTLS_ASN1_SEQUENCE | |
| 9657 | MBEDTLS_ASN1_CONSTRUCTED)); |
| 9658 | TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1)); |
| 9659 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 9660 | MBEDTLS_ASN1_INTEGER)); |
| 9661 | if (len >= 1 && p[0] == 0) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9662 | ++p; |
| 9663 | --len; |
| 9664 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9665 | if (e_arg->len == 0) { |
| 9666 | TEST_EQUAL(len, 3); |
| 9667 | TEST_EQUAL(p[0], 1); |
| 9668 | TEST_EQUAL(p[1], 0); |
| 9669 | TEST_EQUAL(p[2], 1); |
| 9670 | } else { |
| 9671 | ASSERT_COMPARE(p, len, e_arg->x, e_arg->len); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9672 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9673 | } |
| 9674 | |
| 9675 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9676 | /* |
| 9677 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 9678 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 9679 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9680 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9681 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9682 | psa_destroy_key(key); |
| 9683 | PSA_DONE(); |
| 9684 | mbedtls_free(e_read_buffer); |
| 9685 | mbedtls_free(exported); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9686 | } |
| 9687 | /* END_CASE */ |
| 9688 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9689 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9690 | void persistent_key_load_key_from_storage(data_t *data, |
| 9691 | int type_arg, int bits_arg, |
| 9692 | int usage_flags_arg, int alg_arg, |
| 9693 | int generation_method) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9694 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9695 | 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] | 9696 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9697 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9698 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9699 | psa_key_type_t type = type_arg; |
| 9700 | size_t bits = bits_arg; |
| 9701 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 9702 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9703 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9704 | unsigned char *first_export = NULL; |
| 9705 | unsigned char *second_export = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9706 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9707 | size_t first_exported_length; |
| 9708 | size_t second_exported_length; |
| 9709 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9710 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9711 | ASSERT_ALLOC(first_export, export_size); |
| 9712 | ASSERT_ALLOC(second_export, export_size); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9713 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9714 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9715 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9716 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9717 | psa_set_key_id(&attributes, key_id); |
| 9718 | psa_set_key_usage_flags(&attributes, usage_flags); |
| 9719 | psa_set_key_algorithm(&attributes, alg); |
| 9720 | psa_set_key_type(&attributes, type); |
| 9721 | psa_set_key_bits(&attributes, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9722 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9723 | switch (generation_method) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9724 | case IMPORT_KEY: |
| 9725 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9726 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, |
| 9727 | &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9728 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9729 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9730 | case GENERATE_KEY: |
| 9731 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9732 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9733 | break; |
| 9734 | |
| 9735 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 9736 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9737 | { |
| 9738 | /* Create base key */ |
| 9739 | psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256); |
| 9740 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9741 | psa_set_key_usage_flags(&base_attributes, |
| 9742 | PSA_KEY_USAGE_DERIVE); |
| 9743 | psa_set_key_algorithm(&base_attributes, derive_alg); |
| 9744 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9745 | PSA_ASSERT(psa_import_key(&base_attributes, |
| 9746 | data->x, data->len, |
| 9747 | &base_key)); |
| 9748 | /* Derive a key. */ |
| 9749 | PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg)); |
| 9750 | PSA_ASSERT(psa_key_derivation_input_key( |
| 9751 | &operation, |
| 9752 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key)); |
| 9753 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 9754 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
| 9755 | NULL, 0)); |
| 9756 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, |
| 9757 | &operation, |
| 9758 | &key)); |
| 9759 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
| 9760 | PSA_ASSERT(psa_destroy_key(base_key)); |
| 9761 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9762 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9763 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9764 | TEST_ASSUME(!"KDF not supported in this configuration"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9765 | #endif |
| 9766 | break; |
| 9767 | |
| 9768 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9769 | TEST_ASSERT(!"generation_method not implemented in test"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9770 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9771 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9772 | psa_reset_key_attributes(&attributes); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9773 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9774 | /* Export the key if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9775 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9776 | PSA_ASSERT(psa_export_key(key, |
| 9777 | first_export, export_size, |
| 9778 | &first_exported_length)); |
| 9779 | if (generation_method == IMPORT_KEY) { |
| 9780 | ASSERT_COMPARE(data->x, data->len, |
| 9781 | first_export, first_exported_length); |
| 9782 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9783 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9784 | |
| 9785 | /* Shutdown and restart */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9786 | PSA_ASSERT(psa_purge_key(key)); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 9787 | PSA_DONE(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9788 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9789 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9790 | /* Check key slot still contains key data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9791 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 9792 | TEST_ASSERT(mbedtls_svc_key_id_equal( |
| 9793 | psa_get_key_id(&attributes), key_id)); |
| 9794 | TEST_EQUAL(psa_get_key_lifetime(&attributes), |
| 9795 | PSA_KEY_LIFETIME_PERSISTENT); |
| 9796 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 9797 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 9798 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 9799 | mbedtls_test_update_key_usage_flags(usage_flags)); |
| 9800 | TEST_EQUAL(psa_get_key_algorithm(&attributes), alg); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9801 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9802 | /* Export the key again if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9803 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9804 | PSA_ASSERT(psa_export_key(key, |
| 9805 | second_export, export_size, |
| 9806 | &second_exported_length)); |
| 9807 | ASSERT_COMPARE(first_export, first_exported_length, |
| 9808 | second_export, second_exported_length); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9809 | } |
| 9810 | |
| 9811 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9812 | if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9813 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9814 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9815 | |
| 9816 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9817 | /* |
| 9818 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9819 | * thus reset them as required. |
| 9820 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9821 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9822 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9823 | mbedtls_free(first_export); |
| 9824 | mbedtls_free(second_export); |
| 9825 | psa_key_derivation_abort(&operation); |
| 9826 | psa_destroy_key(base_key); |
| 9827 | psa_destroy_key(key); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 9828 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9829 | } |
| 9830 | /* END_CASE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9831 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 9832 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9833 | void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg, |
| 9834 | int primitive_arg, int hash_arg, int role_arg, |
| 9835 | int test_input, data_t *pw_data, |
| 9836 | int inj_err_type_arg, |
| 9837 | int expected_error_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9838 | { |
| 9839 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 9840 | psa_pake_operation_t operation = psa_pake_operation_init(); |
| 9841 | psa_algorithm_t alg = alg_arg; |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9842 | psa_pake_primitive_t primitive = primitive_arg; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 9843 | psa_key_type_t key_type_pw = key_type_pw_arg; |
| 9844 | psa_key_usage_t key_usage_pw = key_usage_pw_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9845 | psa_algorithm_t hash_alg = hash_arg; |
| 9846 | psa_pake_role_t role = role_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9847 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9848 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9849 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
| 9850 | psa_status_t expected_error = expected_error_arg; |
| 9851 | psa_status_t status; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9852 | unsigned char *output_buffer = NULL; |
| 9853 | size_t output_len = 0; |
| 9854 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9855 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9856 | |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9857 | size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9858 | PSA_PAKE_STEP_KEY_SHARE); |
| 9859 | ASSERT_ALLOC(output_buffer, buf_size); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9860 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9861 | if (pw_data->len > 0) { |
| 9862 | psa_set_key_usage_flags(&attributes, key_usage_pw); |
| 9863 | psa_set_key_algorithm(&attributes, alg); |
| 9864 | psa_set_key_type(&attributes, key_type_pw); |
| 9865 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 9866 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9867 | } |
| 9868 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9869 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 9870 | psa_pake_cs_set_primitive(&cipher_suite, primitive); |
| 9871 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9872 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9873 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 9874 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9875 | if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) { |
| 9876 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 9877 | expected_error); |
| 9878 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9879 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 9880 | expected_error); |
| 9881 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9882 | TEST_EQUAL(psa_pake_set_password_key(&operation, key), |
| 9883 | expected_error); |
| 9884 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9885 | TEST_EQUAL(psa_pake_set_role(&operation, role), |
| 9886 | expected_error); |
| 9887 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9888 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 9889 | NULL, 0, NULL), |
| 9890 | expected_error); |
| 9891 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9892 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0), |
| 9893 | expected_error); |
| 9894 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9895 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9896 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9897 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9898 | status = psa_pake_setup(&operation, &cipher_suite); |
| 9899 | if (status != PSA_SUCCESS) { |
| 9900 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9901 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9902 | } |
| 9903 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9904 | if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) { |
| 9905 | TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite), |
| 9906 | expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9907 | goto exit; |
| 9908 | } |
| 9909 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9910 | status = psa_pake_set_role(&operation, role); |
| 9911 | if (status != PSA_SUCCESS) { |
| 9912 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9913 | goto exit; |
| 9914 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9915 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9916 | if (pw_data->len > 0) { |
| 9917 | status = psa_pake_set_password_key(&operation, key); |
| 9918 | if (status != PSA_SUCCESS) { |
| 9919 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9920 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9921 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9922 | } |
| 9923 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9924 | if (inj_err_type == INJECT_ERR_INVALID_USER) { |
| 9925 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 9926 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9927 | goto exit; |
| 9928 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9929 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9930 | if (inj_err_type == INJECT_ERR_INVALID_PEER) { |
| 9931 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 9932 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9933 | goto exit; |
| 9934 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9935 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9936 | if (inj_err_type == INJECT_ERR_SET_USER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9937 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9938 | TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4), |
| 9939 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9940 | goto exit; |
| 9941 | } |
| 9942 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9943 | if (inj_err_type == INJECT_ERR_SET_PEER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9944 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9945 | TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4), |
| 9946 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9947 | goto exit; |
| 9948 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9949 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9950 | const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 9951 | PSA_PAKE_STEP_KEY_SHARE); |
| 9952 | const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 9953 | PSA_PAKE_STEP_ZK_PUBLIC); |
| 9954 | const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 9955 | PSA_PAKE_STEP_ZK_PROOF); |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9956 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9957 | if (test_input) { |
| 9958 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 9959 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0), |
| 9960 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9961 | goto exit; |
| 9962 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 9963 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9964 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 9965 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 9966 | output_buffer, size_zk_proof), |
| 9967 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9968 | goto exit; |
| 9969 | } |
| 9970 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9971 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 9972 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 9973 | output_buffer, size_zk_proof), |
| 9974 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9975 | goto exit; |
| 9976 | } |
| 9977 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9978 | status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 9979 | output_buffer, size_key_share); |
| 9980 | if (status != PSA_SUCCESS) { |
| 9981 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9982 | goto exit; |
| 9983 | } |
| 9984 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9985 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 9986 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9987 | output_buffer, size_zk_public + 1), |
| 9988 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9989 | goto exit; |
| 9990 | } |
| 9991 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9992 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9993 | // 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] | 9994 | psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9995 | output_buffer, size_zk_public + 1); |
| 9996 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9997 | output_buffer, size_zk_public), |
| 9998 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9999 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10000 | } |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10001 | } else { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10002 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 10003 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10004 | NULL, 0, NULL), |
| 10005 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10006 | goto exit; |
| 10007 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10008 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10009 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 10010 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 10011 | output_buffer, buf_size, &output_len), |
| 10012 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10013 | goto exit; |
| 10014 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10015 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10016 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 10017 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10018 | output_buffer, buf_size, &output_len), |
| 10019 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10020 | goto exit; |
| 10021 | } |
| 10022 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10023 | status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10024 | output_buffer, buf_size, &output_len); |
| 10025 | if (status != PSA_SUCCESS) { |
| 10026 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10027 | goto exit; |
| 10028 | } |
| 10029 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10030 | TEST_ASSERT(output_len > 0); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10031 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10032 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 10033 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10034 | output_buffer, size_zk_public - 1, &output_len), |
| 10035 | PSA_ERROR_BUFFER_TOO_SMALL); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10036 | goto exit; |
| 10037 | } |
| 10038 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10039 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10040 | // 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] | 10041 | psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10042 | output_buffer, size_zk_public - 1, &output_len); |
| 10043 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10044 | output_buffer, buf_size, &output_len), |
| 10045 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10046 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10047 | } |
| 10048 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10049 | |
| 10050 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10051 | PSA_ASSERT(psa_destroy_key(key)); |
| 10052 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10053 | mbedtls_free(output_buffer); |
| 10054 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10055 | } |
| 10056 | /* END_CASE */ |
| 10057 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 10058 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10059 | void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg, |
| 10060 | int client_input_first, int inject_error, |
| 10061 | data_t *pw_data) |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10062 | { |
| 10063 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10064 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10065 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10066 | psa_algorithm_t alg = alg_arg; |
| 10067 | psa_algorithm_t hash_alg = hash_arg; |
| 10068 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10069 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10070 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10071 | PSA_INIT(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10072 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10073 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10074 | psa_set_key_algorithm(&attributes, alg); |
| 10075 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10076 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10077 | &key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10078 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10079 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10080 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10081 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10082 | |
| 10083 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10084 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10085 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10086 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10087 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10088 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10089 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10090 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10091 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10092 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10093 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10094 | client_input_first, 1, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10095 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10096 | if (inject_error == 1 || inject_error == 2) { |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10097 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10098 | } |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10099 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10100 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10101 | client_input_first, 2, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10102 | |
| 10103 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10104 | psa_destroy_key(key); |
| 10105 | psa_pake_abort(&server); |
| 10106 | psa_pake_abort(&client); |
| 10107 | PSA_DONE(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10108 | } |
| 10109 | /* END_CASE */ |
| 10110 | |
| 10111 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10112 | void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg, |
| 10113 | int derive_alg_arg, data_t *pw_data, |
| 10114 | int client_input_first, int inj_err_type_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10115 | { |
| 10116 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10117 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10118 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10119 | psa_algorithm_t alg = alg_arg; |
| 10120 | psa_algorithm_t hash_alg = hash_arg; |
| 10121 | psa_algorithm_t derive_alg = derive_alg_arg; |
| 10122 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10123 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10124 | psa_key_derivation_operation_t server_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10125 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10126 | psa_key_derivation_operation_t client_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10127 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10128 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10129 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10130 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10131 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10132 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10133 | psa_set_key_algorithm(&attributes, alg); |
| 10134 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10135 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10136 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10137 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10138 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10139 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10140 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10141 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10142 | /* Get shared key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10143 | PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg)); |
| 10144 | PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10145 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10146 | if (PSA_ALG_IS_TLS12_PRF(derive_alg) || |
| 10147 | PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) { |
| 10148 | PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive, |
| 10149 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10150 | (const uint8_t *) "", 0)); |
| 10151 | PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive, |
| 10152 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10153 | (const uint8_t *) "", 0)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10154 | } |
| 10155 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10156 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10157 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10159 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10160 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10161 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10162 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10163 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10164 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10165 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) { |
| 10166 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10167 | PSA_ERROR_BAD_STATE); |
| 10168 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10169 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10170 | goto exit; |
| 10171 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10172 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10173 | /* First round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10174 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10175 | client_input_first, 1, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10176 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10177 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) { |
| 10178 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10179 | PSA_ERROR_BAD_STATE); |
| 10180 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10181 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10182 | goto exit; |
| 10183 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10184 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10185 | /* Second round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10186 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10187 | client_input_first, 2, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10188 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10189 | PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive)); |
| 10190 | PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10191 | |
| 10192 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10193 | psa_key_derivation_abort(&server_derive); |
| 10194 | psa_key_derivation_abort(&client_derive); |
| 10195 | psa_destroy_key(key); |
| 10196 | psa_pake_abort(&server); |
| 10197 | psa_pake_abort(&client); |
| 10198 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10199 | } |
| 10200 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10201 | |
| 10202 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10203 | void ecjpake_size_macros() |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10204 | { |
| 10205 | const psa_algorithm_t alg = PSA_ALG_JPAKE; |
| 10206 | const size_t bits = 256; |
| 10207 | const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10208 | 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] | 10209 | 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] | 10210 | PSA_ECC_FAMILY_SECP_R1); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10211 | |
| 10212 | // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types |
| 10213 | /* 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] | 10214 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10215 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
| 10216 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10217 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10218 | /* The output for ZK_PROOF is the same bitsize as the curve */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10219 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10220 | PSA_BITS_TO_BYTES(bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10221 | |
| 10222 | /* Input sizes are the same as output sizes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10223 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10224 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE)); |
| 10225 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10226 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC)); |
| 10227 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10228 | 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] | 10229 | |
| 10230 | /* These inequalities will always hold even when other PAKEs are added */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10231 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10232 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10233 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10234 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10235 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10236 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10237 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10238 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10239 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10240 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10241 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10242 | PSA_PAKE_INPUT_MAX_SIZE); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10243 | } |
| 10244 | /* END_CASE */ |