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; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1506 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1507 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1508 | ASSERT_ALLOC(exported, export_size); |
| 1509 | if (!canonical_input) { |
| 1510 | ASSERT_ALLOC(reexported, export_size); |
| 1511 | } |
| 1512 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1513 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1514 | psa_set_key_lifetime(&attributes, lifetime); |
| 1515 | psa_set_key_usage_flags(&attributes, usage_arg); |
| 1516 | psa_set_key_algorithm(&attributes, alg); |
| 1517 | psa_set_key_type(&attributes, type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1518 | |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1519 | if (PSA_KEY_TYPE_IS_DH(type) && |
| 1520 | expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) { |
Przemek Stekiel | 654bef0 | 2022-12-15 13:28:02 +0100 | [diff] [blame] | 1521 | /* Simulate that buffer is too small, by decreasing its size by 1 byte. */ |
| 1522 | export_size -= 1; |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1523 | } |
| 1524 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1525 | /* Import the key */ |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1526 | TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key), |
Przemek Stekiel | 2e7c33d | 2023-04-27 12:29:45 +0200 | [diff] [blame^] | 1527 | PSA_SUCCESS); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1528 | |
| 1529 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1530 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1531 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1532 | TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits); |
| 1533 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1534 | |
| 1535 | /* Export the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1536 | status = psa_export_key(key, exported, export_size, &exported_length); |
| 1537 | TEST_EQUAL(status, expected_export_status); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1538 | |
| 1539 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1540 | * and export_size. On errors, the exported length must be 0. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1541 | TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH); |
| 1542 | TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0); |
| 1543 | TEST_LE_U(exported_length, export_size); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1544 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1545 | TEST_ASSERT(mem_is_char(exported + exported_length, 0, |
| 1546 | export_size - exported_length)); |
| 1547 | if (status != PSA_SUCCESS) { |
| 1548 | TEST_EQUAL(exported_length, 0); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1549 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1550 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1551 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1552 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1553 | * this validates the canonical representations. For canonical inputs, |
| 1554 | * this doesn't directly validate the implementation, but it still helps |
| 1555 | * by cross-validating the test data with the sanity check code. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1556 | if (!psa_key_lifetime_is_external(lifetime)) { |
| 1557 | if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) { |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1558 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1559 | } |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1560 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1562 | if (canonical_input) { |
| 1563 | ASSERT_COMPARE(data->x, data->len, exported, exported_length); |
| 1564 | } else { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1565 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1566 | PSA_ASSERT(psa_import_key(&attributes, exported, exported_length, |
| 1567 | &key2)); |
| 1568 | PSA_ASSERT(psa_export_key(key2, |
| 1569 | reexported, |
| 1570 | export_size, |
| 1571 | &reexported_length)); |
| 1572 | ASSERT_COMPARE(exported, exported_length, |
| 1573 | reexported, reexported_length); |
| 1574 | PSA_ASSERT(psa_destroy_key(key2)); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1575 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1576 | TEST_LE_U(exported_length, |
| 1577 | PSA_EXPORT_KEY_OUTPUT_SIZE(type, |
| 1578 | psa_get_key_bits(&got_attributes))); |
| 1579 | TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1580 | |
| 1581 | destroy: |
| 1582 | /* Destroy the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1583 | PSA_ASSERT(psa_destroy_key(key)); |
| 1584 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1585 | |
| 1586 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1587 | /* |
| 1588 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1589 | * thus reset them as required. |
| 1590 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1591 | psa_reset_key_attributes(&got_attributes); |
| 1592 | psa_destroy_key(key); |
| 1593 | mbedtls_free(exported); |
| 1594 | mbedtls_free(reexported); |
| 1595 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1596 | } |
| 1597 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1598 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1599 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1600 | void import_export_public_key(data_t *data, |
| 1601 | int type_arg, // key pair or public key |
| 1602 | int alg_arg, |
| 1603 | int lifetime_arg, |
| 1604 | int export_size_delta, |
| 1605 | int expected_export_status_arg, |
| 1606 | data_t *expected_public_key) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1607 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1608 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1609 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1610 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1611 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1612 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1613 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1614 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1615 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1616 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1617 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1618 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1619 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1620 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1621 | psa_set_key_lifetime(&attributes, lifetime); |
| 1622 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1623 | psa_set_key_algorithm(&attributes, alg); |
| 1624 | psa_set_key_type(&attributes, type); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1625 | |
| 1626 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1627 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1628 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1629 | /* Export the public key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1630 | ASSERT_ALLOC(exported, export_size); |
| 1631 | status = psa_export_public_key(key, |
| 1632 | exported, export_size, |
| 1633 | &exported_length); |
| 1634 | TEST_EQUAL(status, expected_export_status); |
| 1635 | if (status == PSA_SUCCESS) { |
| 1636 | 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] | 1637 | size_t bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1638 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1639 | bits = psa_get_key_bits(&attributes); |
| 1640 | TEST_LE_U(expected_public_key->len, |
| 1641 | PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1642 | TEST_LE_U(expected_public_key->len, |
| 1643 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1644 | TEST_LE_U(expected_public_key->len, |
| 1645 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
| 1646 | ASSERT_COMPARE(expected_public_key->x, expected_public_key->len, |
| 1647 | exported, exported_length); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1648 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1649 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1650 | /* |
| 1651 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1652 | * thus reset them as required. |
| 1653 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1654 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1655 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1656 | mbedtls_free(exported); |
| 1657 | psa_destroy_key(key); |
| 1658 | PSA_DONE(); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1659 | } |
| 1660 | /* END_CASE */ |
| 1661 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1662 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1663 | void import_and_exercise_key(data_t *data, |
| 1664 | int type_arg, |
| 1665 | int bits_arg, |
| 1666 | int alg_arg) |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1667 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1668 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1669 | psa_key_type_t type = type_arg; |
| 1670 | size_t bits = bits_arg; |
| 1671 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1672 | 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] | 1673 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1674 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1676 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1677 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1678 | psa_set_key_usage_flags(&attributes, usage); |
| 1679 | psa_set_key_algorithm(&attributes, alg); |
| 1680 | psa_set_key_type(&attributes, type); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1681 | |
| 1682 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1683 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1684 | |
| 1685 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1686 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1687 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1688 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1689 | |
| 1690 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1691 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1692 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1693 | } |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1694 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1695 | PSA_ASSERT(psa_destroy_key(key)); |
| 1696 | test_operations_on_invalid_key(key); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1697 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1698 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1699 | /* |
| 1700 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1701 | * thus reset them as required. |
| 1702 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1703 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1704 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1705 | psa_reset_key_attributes(&attributes); |
| 1706 | psa_destroy_key(key); |
| 1707 | PSA_DONE(); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1708 | } |
| 1709 | /* END_CASE */ |
| 1710 | |
| 1711 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1712 | void effective_key_attributes(int type_arg, int expected_type_arg, |
| 1713 | int bits_arg, int expected_bits_arg, |
| 1714 | int usage_arg, int expected_usage_arg, |
| 1715 | int alg_arg, int expected_alg_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1716 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1717 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1718 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1719 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1720 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1721 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1722 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1723 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1724 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1725 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1726 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1727 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1728 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1729 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1730 | psa_set_key_usage_flags(&attributes, usage); |
| 1731 | psa_set_key_algorithm(&attributes, alg); |
| 1732 | psa_set_key_type(&attributes, key_type); |
| 1733 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1734 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1735 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
| 1736 | psa_reset_key_attributes(&attributes); |
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_get_key_attributes(key, &attributes)); |
| 1739 | TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type); |
| 1740 | TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits); |
| 1741 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
| 1742 | TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1743 | |
| 1744 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1745 | /* |
| 1746 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1747 | * thus reset them as required. |
| 1748 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1749 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1750 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1751 | psa_destroy_key(key); |
| 1752 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1753 | } |
| 1754 | /* END_CASE */ |
| 1755 | |
| 1756 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1757 | void check_key_policy(int type_arg, int bits_arg, |
| 1758 | int usage_arg, int alg_arg) |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1759 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1760 | test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg, |
| 1761 | usage_arg, |
| 1762 | mbedtls_test_update_key_usage_flags(usage_arg), |
| 1763 | alg_arg, alg_arg); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1764 | goto exit; |
| 1765 | } |
| 1766 | /* END_CASE */ |
| 1767 | |
| 1768 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1769 | void key_attributes_init() |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1770 | { |
| 1771 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1772 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1773 | * 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] | 1774 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1775 | psa_key_attributes_t func = psa_key_attributes_init(); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1776 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1777 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1778 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1779 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1780 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1781 | TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE); |
| 1782 | TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE); |
| 1783 | TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1784 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1785 | TEST_EQUAL(psa_get_key_type(&func), 0); |
| 1786 | TEST_EQUAL(psa_get_key_type(&init), 0); |
| 1787 | TEST_EQUAL(psa_get_key_type(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1788 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1789 | TEST_EQUAL(psa_get_key_bits(&func), 0); |
| 1790 | TEST_EQUAL(psa_get_key_bits(&init), 0); |
| 1791 | TEST_EQUAL(psa_get_key_bits(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1792 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1793 | TEST_EQUAL(psa_get_key_usage_flags(&func), 0); |
| 1794 | TEST_EQUAL(psa_get_key_usage_flags(&init), 0); |
| 1795 | TEST_EQUAL(psa_get_key_usage_flags(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1796 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1797 | TEST_EQUAL(psa_get_key_algorithm(&func), 0); |
| 1798 | TEST_EQUAL(psa_get_key_algorithm(&init), 0); |
| 1799 | TEST_EQUAL(psa_get_key_algorithm(&zero), 0); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1800 | } |
| 1801 | /* END_CASE */ |
| 1802 | |
| 1803 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1804 | void mac_key_policy(int policy_usage_arg, |
| 1805 | int policy_alg_arg, |
| 1806 | int key_type_arg, |
| 1807 | data_t *key_data, |
| 1808 | int exercise_alg_arg, |
| 1809 | int expected_status_sign_arg, |
| 1810 | int expected_status_verify_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1811 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1812 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1813 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1814 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1815 | psa_key_type_t key_type = key_type_arg; |
| 1816 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 1817 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 1818 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1819 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1820 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 1821 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1822 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1823 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1824 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1825 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1826 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1827 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1828 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1829 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1830 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1831 | &key)); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1832 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1833 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 1834 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1835 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1836 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 1837 | TEST_EQUAL(status, expected_status_sign); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1838 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1839 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1840 | uint8_t input[128] = { 0 }; |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1841 | size_t mac_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1842 | TEST_EQUAL(psa_mac_compute(key, exercise_alg, |
| 1843 | input, 128, |
| 1844 | mac, PSA_MAC_MAX_SIZE, &mac_len), |
| 1845 | expected_status_sign); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1846 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1847 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1848 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 1849 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 1850 | if (status == PSA_SUCCESS) { |
| 1851 | status = psa_mac_update(&operation, input, 128); |
| 1852 | if (status == PSA_SUCCESS) { |
| 1853 | TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE, |
| 1854 | &mac_len), |
| 1855 | expected_status_sign); |
| 1856 | } else { |
| 1857 | TEST_EQUAL(status, expected_status_sign); |
| 1858 | } |
| 1859 | } else { |
| 1860 | TEST_EQUAL(status, expected_status_sign); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1861 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1862 | PSA_ASSERT(psa_mac_abort(&operation)); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1863 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1864 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1865 | status = psa_mac_verify(key, exercise_alg, input, 128, |
| 1866 | mac, mac_len); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1867 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1868 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 1869 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1870 | } else { |
| 1871 | TEST_EQUAL(status, expected_status_verify); |
| 1872 | } |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1873 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1874 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1875 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 1876 | if (status == PSA_SUCCESS) { |
| 1877 | status = psa_mac_update(&operation, input, 128); |
| 1878 | if (status == PSA_SUCCESS) { |
| 1879 | status = psa_mac_verify_finish(&operation, mac, mac_len); |
| 1880 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 1881 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 1882 | } else { |
| 1883 | TEST_EQUAL(status, expected_status_verify); |
| 1884 | } |
| 1885 | } else { |
| 1886 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1887 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1888 | } else { |
| 1889 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1890 | } |
| 1891 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1892 | psa_mac_abort(&operation); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1893 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1894 | memset(mac, 0, sizeof(mac)); |
| 1895 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 1896 | TEST_EQUAL(status, expected_status_verify); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1897 | |
| 1898 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1899 | psa_mac_abort(&operation); |
| 1900 | psa_destroy_key(key); |
| 1901 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1902 | } |
| 1903 | /* END_CASE */ |
| 1904 | |
| 1905 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1906 | void cipher_key_policy(int policy_usage_arg, |
| 1907 | int policy_alg, |
| 1908 | int key_type, |
| 1909 | data_t *key_data, |
| 1910 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1911 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1912 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1913 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1914 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1915 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1916 | size_t output_buffer_size = 0; |
| 1917 | size_t input_buffer_size = 0; |
| 1918 | size_t output_length = 0; |
| 1919 | uint8_t *output = NULL; |
| 1920 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1921 | psa_status_t status; |
| 1922 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1923 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg); |
| 1924 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg, |
| 1925 | input_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1926 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1927 | ASSERT_ALLOC(input, input_buffer_size); |
| 1928 | ASSERT_ALLOC(output, output_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1929 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1930 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1931 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1932 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 1933 | psa_set_key_algorithm(&attributes, policy_alg); |
| 1934 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1935 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1936 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 1937 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1938 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1939 | /* Check if no key usage flag implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1940 | TEST_EQUAL(policy_usage, |
| 1941 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1942 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1943 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1944 | status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size, |
| 1945 | output, output_buffer_size, |
| 1946 | &output_length); |
| 1947 | if (policy_alg == exercise_alg && |
| 1948 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1949 | PSA_ASSERT(status); |
| 1950 | } else { |
| 1951 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1952 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1953 | |
| 1954 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1955 | status = psa_cipher_encrypt_setup(&operation, key, exercise_alg); |
| 1956 | if (policy_alg == exercise_alg && |
| 1957 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 1958 | PSA_ASSERT(status); |
| 1959 | } else { |
| 1960 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1961 | } |
| 1962 | psa_cipher_abort(&operation); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1963 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1964 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1965 | status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size, |
| 1966 | input, input_buffer_size, |
| 1967 | &output_length); |
| 1968 | if (policy_alg == exercise_alg && |
| 1969 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 1970 | PSA_ASSERT(status); |
| 1971 | } else { |
| 1972 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1973 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1974 | |
| 1975 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1976 | status = psa_cipher_decrypt_setup(&operation, key, exercise_alg); |
| 1977 | if (policy_alg == exercise_alg && |
| 1978 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 1979 | PSA_ASSERT(status); |
| 1980 | } else { |
| 1981 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 1982 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1983 | |
| 1984 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1985 | psa_cipher_abort(&operation); |
| 1986 | mbedtls_free(input); |
| 1987 | mbedtls_free(output); |
| 1988 | psa_destroy_key(key); |
| 1989 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1990 | } |
| 1991 | /* END_CASE */ |
| 1992 | |
| 1993 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1994 | void aead_key_policy(int policy_usage_arg, |
| 1995 | int policy_alg, |
| 1996 | int key_type, |
| 1997 | data_t *key_data, |
| 1998 | int nonce_length_arg, |
| 1999 | int tag_length_arg, |
| 2000 | int exercise_alg, |
| 2001 | int expected_status_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2002 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2003 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2004 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2005 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2006 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2007 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2008 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2009 | unsigned char nonce[16] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2010 | size_t nonce_length = nonce_length_arg; |
| 2011 | unsigned char tag[16]; |
| 2012 | size_t tag_length = tag_length_arg; |
| 2013 | size_t output_length; |
| 2014 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2015 | TEST_LE_U(nonce_length, sizeof(nonce)); |
| 2016 | TEST_LE_U(tag_length, sizeof(tag)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2017 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2018 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2019 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2020 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2021 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2022 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2023 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2024 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2025 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2026 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2027 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2028 | TEST_EQUAL(policy_usage, |
| 2029 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2030 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2031 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2032 | status = psa_aead_encrypt(key, exercise_alg, |
| 2033 | nonce, nonce_length, |
| 2034 | NULL, 0, |
| 2035 | NULL, 0, |
| 2036 | tag, tag_length, |
| 2037 | &output_length); |
| 2038 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2039 | TEST_EQUAL(status, expected_status); |
| 2040 | } else { |
| 2041 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2042 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2043 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2044 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2045 | status = psa_aead_encrypt_setup(&operation, key, exercise_alg); |
| 2046 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2047 | TEST_EQUAL(status, expected_status); |
| 2048 | } else { |
| 2049 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2050 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2051 | |
| 2052 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2053 | memset(tag, 0, sizeof(tag)); |
| 2054 | status = psa_aead_decrypt(key, exercise_alg, |
| 2055 | nonce, nonce_length, |
| 2056 | NULL, 0, |
| 2057 | tag, tag_length, |
| 2058 | NULL, 0, |
| 2059 | &output_length); |
| 2060 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2061 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2062 | } else if (expected_status == PSA_SUCCESS) { |
| 2063 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2064 | } else { |
| 2065 | TEST_EQUAL(status, expected_status); |
| 2066 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2067 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2068 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2069 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2070 | status = psa_aead_decrypt_setup(&operation, key, exercise_alg); |
| 2071 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2072 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2073 | } else { |
| 2074 | TEST_EQUAL(status, expected_status); |
| 2075 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2076 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2077 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2078 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2079 | psa_destroy_key(key); |
| 2080 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2081 | } |
| 2082 | /* END_CASE */ |
| 2083 | |
| 2084 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2085 | void asymmetric_encryption_key_policy(int policy_usage_arg, |
| 2086 | int policy_alg, |
| 2087 | int key_type, |
| 2088 | data_t *key_data, |
| 2089 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2090 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2091 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2092 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2093 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2094 | psa_status_t status; |
| 2095 | size_t key_bits; |
| 2096 | size_t buffer_length; |
| 2097 | unsigned char *buffer = NULL; |
| 2098 | size_t output_length; |
| 2099 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2100 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2101 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2102 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2103 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2104 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2105 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2106 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2107 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2108 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2109 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2110 | TEST_EQUAL(policy_usage, |
| 2111 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2112 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2113 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 2114 | key_bits = psa_get_key_bits(&attributes); |
| 2115 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, |
| 2116 | exercise_alg); |
| 2117 | ASSERT_ALLOC(buffer, buffer_length); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2118 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2119 | status = psa_asymmetric_encrypt(key, exercise_alg, |
| 2120 | NULL, 0, |
| 2121 | NULL, 0, |
| 2122 | buffer, buffer_length, |
| 2123 | &output_length); |
| 2124 | if (policy_alg == exercise_alg && |
| 2125 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2126 | PSA_ASSERT(status); |
| 2127 | } else { |
| 2128 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2129 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2130 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2131 | if (buffer_length != 0) { |
| 2132 | memset(buffer, 0, buffer_length); |
| 2133 | } |
| 2134 | status = psa_asymmetric_decrypt(key, exercise_alg, |
| 2135 | buffer, buffer_length, |
| 2136 | NULL, 0, |
| 2137 | buffer, buffer_length, |
| 2138 | &output_length); |
| 2139 | if (policy_alg == exercise_alg && |
| 2140 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2141 | TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING); |
| 2142 | } else { |
| 2143 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2144 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2145 | |
| 2146 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2147 | /* |
| 2148 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2149 | * thus reset them as required. |
| 2150 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2151 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2153 | psa_destroy_key(key); |
| 2154 | PSA_DONE(); |
| 2155 | mbedtls_free(buffer); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2156 | } |
| 2157 | /* END_CASE */ |
| 2158 | |
| 2159 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2160 | void asymmetric_signature_key_policy(int policy_usage_arg, |
| 2161 | int policy_alg, |
| 2162 | int key_type, |
| 2163 | data_t *key_data, |
| 2164 | int exercise_alg, |
| 2165 | int payload_length_arg, |
| 2166 | int expected_usage_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2167 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2168 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2169 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2170 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 2171 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2172 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2173 | unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 }; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2174 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2175 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2176 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2177 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2178 | int compatible_alg = payload_length_arg > 0; |
| 2179 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2180 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2181 | size_t signature_length; |
| 2182 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2183 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2184 | in the expected usage flags. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2185 | TEST_EQUAL(expected_usage, |
| 2186 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2187 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2188 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2189 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2190 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2191 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2192 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2193 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2194 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2195 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2196 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2197 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2198 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2199 | status = psa_sign_hash(key, exercise_alg, |
| 2200 | payload, payload_length, |
| 2201 | signature, sizeof(signature), |
| 2202 | &signature_length); |
| 2203 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) { |
| 2204 | PSA_ASSERT(status); |
| 2205 | } else { |
| 2206 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2207 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2208 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2209 | memset(signature, 0, sizeof(signature)); |
| 2210 | status = psa_verify_hash(key, exercise_alg, |
| 2211 | payload, payload_length, |
| 2212 | signature, sizeof(signature)); |
| 2213 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) { |
| 2214 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2215 | } else { |
| 2216 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2217 | } |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2218 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2219 | if (PSA_ALG_IS_SIGN_HASH(exercise_alg) && |
| 2220 | PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) { |
| 2221 | status = psa_sign_message(key, exercise_alg, |
| 2222 | payload, payload_length, |
| 2223 | signature, sizeof(signature), |
| 2224 | &signature_length); |
| 2225 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) { |
| 2226 | PSA_ASSERT(status); |
| 2227 | } else { |
| 2228 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2229 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2230 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2231 | memset(signature, 0, sizeof(signature)); |
| 2232 | status = psa_verify_message(key, exercise_alg, |
| 2233 | payload, payload_length, |
| 2234 | signature, sizeof(signature)); |
| 2235 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) { |
| 2236 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 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 | } |
| 2241 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2242 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2243 | psa_destroy_key(key); |
| 2244 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2245 | } |
| 2246 | /* END_CASE */ |
| 2247 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2248 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2249 | void derive_key_policy(int policy_usage, |
| 2250 | int policy_alg, |
| 2251 | int key_type, |
| 2252 | data_t *key_data, |
| 2253 | int exercise_alg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2254 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2255 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2256 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2257 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2258 | psa_status_t status; |
| 2259 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2260 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2261 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2262 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2263 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2264 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2266 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2267 | &key)); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2268 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2269 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2270 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2271 | if (PSA_ALG_IS_TLS12_PRF(exercise_alg) || |
| 2272 | PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) { |
| 2273 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 2274 | &operation, |
| 2275 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2276 | (const uint8_t *) "", 0)); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2277 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2278 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2279 | status = psa_key_derivation_input_key(&operation, |
| 2280 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 2281 | key); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2282 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2283 | if (policy_alg == exercise_alg && |
| 2284 | (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) { |
| 2285 | PSA_ASSERT(status); |
| 2286 | } else { |
| 2287 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2288 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2289 | |
| 2290 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2291 | psa_key_derivation_abort(&operation); |
| 2292 | psa_destroy_key(key); |
| 2293 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2294 | } |
| 2295 | /* END_CASE */ |
| 2296 | |
| 2297 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2298 | void agreement_key_policy(int policy_usage, |
| 2299 | int policy_alg, |
| 2300 | int key_type_arg, |
| 2301 | data_t *key_data, |
| 2302 | int exercise_alg, |
| 2303 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2304 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2305 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2306 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2307 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2308 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2309 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2310 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2311 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2312 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2314 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2315 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2316 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2317 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2318 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2319 | &key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2320 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2321 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
| 2322 | status = mbedtls_test_psa_key_agreement_with_self(&operation, key); |
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 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2325 | |
| 2326 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2327 | psa_key_derivation_abort(&operation); |
| 2328 | psa_destroy_key(key); |
| 2329 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2330 | } |
| 2331 | /* END_CASE */ |
| 2332 | |
| 2333 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2334 | void key_policy_alg2(int key_type_arg, data_t *key_data, |
| 2335 | int usage_arg, int alg_arg, int alg2_arg) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2336 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2337 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2338 | psa_key_type_t key_type = key_type_arg; |
| 2339 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2340 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2341 | psa_key_usage_t usage = usage_arg; |
| 2342 | psa_algorithm_t alg = alg_arg; |
| 2343 | psa_algorithm_t alg2 = alg2_arg; |
| 2344 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2345 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2346 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2347 | psa_set_key_usage_flags(&attributes, usage); |
| 2348 | psa_set_key_algorithm(&attributes, alg); |
| 2349 | psa_set_key_enrollment_algorithm(&attributes, alg2); |
| 2350 | psa_set_key_type(&attributes, key_type); |
| 2351 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2352 | &key)); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2353 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2354 | /* Update the usage flags to obtain implicit usage flags */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2355 | usage = mbedtls_test_update_key_usage_flags(usage); |
| 2356 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 2357 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage); |
| 2358 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 2359 | TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2360 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2361 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2362 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2363 | } |
| 2364 | if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2365 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2366 | } |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2367 | |
| 2368 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2369 | /* |
| 2370 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2371 | * thus reset them as required. |
| 2372 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2373 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2374 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2375 | psa_destroy_key(key); |
| 2376 | PSA_DONE(); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2377 | } |
| 2378 | /* END_CASE */ |
| 2379 | |
| 2380 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2381 | void raw_agreement_key_policy(int policy_usage, |
| 2382 | int policy_alg, |
| 2383 | int key_type_arg, |
| 2384 | data_t *key_data, |
| 2385 | int exercise_alg, |
| 2386 | int expected_status_arg) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2387 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2388 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2389 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2390 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2391 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2392 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2393 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2394 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2395 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2396 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2397 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2398 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2399 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2400 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2401 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2402 | &key)); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2403 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2404 | status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2405 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2406 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2407 | |
| 2408 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2409 | psa_key_derivation_abort(&operation); |
| 2410 | psa_destroy_key(key); |
| 2411 | PSA_DONE(); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2412 | } |
| 2413 | /* END_CASE */ |
| 2414 | |
| 2415 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2416 | void copy_success(int source_usage_arg, |
| 2417 | int source_alg_arg, int source_alg2_arg, |
| 2418 | unsigned int source_lifetime_arg, |
| 2419 | int type_arg, data_t *material, |
| 2420 | int copy_attributes, |
| 2421 | int target_usage_arg, |
| 2422 | int target_alg_arg, int target_alg2_arg, |
| 2423 | unsigned int target_lifetime_arg, |
| 2424 | int expected_usage_arg, |
| 2425 | int expected_alg_arg, int expected_alg2_arg) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2426 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2427 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2428 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2429 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2430 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2431 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2432 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 2433 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2434 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2435 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2436 | uint8_t *export_buffer = NULL; |
| 2437 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2438 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2439 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2440 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2441 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2442 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2443 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2444 | psa_set_key_type(&source_attributes, type_arg); |
| 2445 | psa_set_key_lifetime(&source_attributes, source_lifetime); |
| 2446 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2447 | material->x, material->len, |
| 2448 | &source_key)); |
| 2449 | PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2450 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2451 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2452 | if (copy_attributes) { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2453 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2454 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2455 | psa_set_key_lifetime(&target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2456 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2457 | if (target_usage_arg != -1) { |
| 2458 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2459 | } |
| 2460 | if (target_alg_arg != -1) { |
| 2461 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2462 | } |
| 2463 | if (target_alg2_arg != -1) { |
| 2464 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
| 2465 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2466 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2467 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2468 | /* Copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2469 | PSA_ASSERT(psa_copy_key(source_key, |
| 2470 | &target_attributes, &target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2471 | |
| 2472 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2473 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2474 | |
| 2475 | /* Test that the target slot has the expected content and policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2476 | PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes)); |
| 2477 | TEST_EQUAL(psa_get_key_type(&source_attributes), |
| 2478 | psa_get_key_type(&target_attributes)); |
| 2479 | TEST_EQUAL(psa_get_key_bits(&source_attributes), |
| 2480 | psa_get_key_bits(&target_attributes)); |
| 2481 | TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes)); |
| 2482 | TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes)); |
| 2483 | TEST_EQUAL(expected_alg2, |
| 2484 | psa_get_key_enrollment_algorithm(&target_attributes)); |
| 2485 | if (expected_usage & PSA_KEY_USAGE_EXPORT) { |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2486 | size_t length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2487 | ASSERT_ALLOC(export_buffer, material->len); |
| 2488 | PSA_ASSERT(psa_export_key(target_key, export_buffer, |
| 2489 | material->len, &length)); |
| 2490 | ASSERT_COMPARE(material->x, material->len, |
| 2491 | export_buffer, length); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2492 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2493 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2494 | if (!psa_key_lifetime_is_external(target_lifetime)) { |
| 2495 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2496 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2497 | } |
| 2498 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2499 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2500 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2501 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2502 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2503 | PSA_ASSERT(psa_destroy_key(target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2504 | |
| 2505 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2506 | /* |
| 2507 | * Source and target key attributes may have been returned by |
| 2508 | * psa_get_key_attributes() thus reset them as required. |
| 2509 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2510 | psa_reset_key_attributes(&source_attributes); |
| 2511 | psa_reset_key_attributes(&target_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2512 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2513 | PSA_DONE(); |
| 2514 | mbedtls_free(export_buffer); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2515 | } |
| 2516 | /* END_CASE */ |
| 2517 | |
| 2518 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2519 | void copy_fail(int source_usage_arg, |
| 2520 | int source_alg_arg, int source_alg2_arg, |
| 2521 | int source_lifetime_arg, |
| 2522 | int type_arg, data_t *material, |
| 2523 | int target_type_arg, int target_bits_arg, |
| 2524 | int target_usage_arg, |
| 2525 | int target_alg_arg, int target_alg2_arg, |
| 2526 | int target_id_arg, int target_lifetime_arg, |
| 2527 | int expected_status_arg) |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2528 | { |
| 2529 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2530 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2531 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2532 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2533 | 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] | 2534 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2535 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2536 | |
| 2537 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2538 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2539 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2540 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2541 | psa_set_key_type(&source_attributes, type_arg); |
| 2542 | psa_set_key_lifetime(&source_attributes, source_lifetime_arg); |
| 2543 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2544 | material->x, material->len, |
| 2545 | &source_key)); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2546 | |
| 2547 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2548 | psa_set_key_id(&target_attributes, key_id); |
| 2549 | psa_set_key_lifetime(&target_attributes, target_lifetime_arg); |
| 2550 | psa_set_key_type(&target_attributes, target_type_arg); |
| 2551 | psa_set_key_bits(&target_attributes, target_bits_arg); |
| 2552 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2553 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2554 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2555 | |
| 2556 | /* Try to copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2557 | TEST_EQUAL(psa_copy_key(source_key, |
| 2558 | &target_attributes, &target_key), |
| 2559 | expected_status_arg); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2560 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2561 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2562 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2563 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2564 | psa_reset_key_attributes(&source_attributes); |
| 2565 | psa_reset_key_attributes(&target_attributes); |
| 2566 | PSA_DONE(); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2567 | } |
| 2568 | /* END_CASE */ |
| 2569 | |
| 2570 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2571 | void hash_operation_init() |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2572 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2573 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2574 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2575 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2576 | * 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] | 2577 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2578 | psa_hash_operation_t func = psa_hash_operation_init(); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2579 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2580 | psa_hash_operation_t zero; |
| 2581 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2582 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2583 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2584 | /* A freshly-initialized hash operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2585 | TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)), |
| 2586 | PSA_ERROR_BAD_STATE); |
| 2587 | TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)), |
| 2588 | PSA_ERROR_BAD_STATE); |
| 2589 | TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)), |
| 2590 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2591 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2592 | /* A default hash operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2593 | PSA_ASSERT(psa_hash_abort(&func)); |
| 2594 | PSA_ASSERT(psa_hash_abort(&init)); |
| 2595 | PSA_ASSERT(psa_hash_abort(&zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2596 | } |
| 2597 | /* END_CASE */ |
| 2598 | |
| 2599 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2600 | void hash_setup(int alg_arg, |
| 2601 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2602 | { |
| 2603 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2604 | uint8_t *output = NULL; |
| 2605 | size_t output_size = 0; |
| 2606 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2607 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2608 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2609 | psa_status_t status; |
| 2610 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2611 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2612 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2613 | /* Hash Setup, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2614 | output_size = PSA_HASH_LENGTH(alg); |
| 2615 | ASSERT_ALLOC(output, output_size); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2616 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2617 | status = psa_hash_compute(alg, NULL, 0, |
| 2618 | output, output_size, &output_length); |
| 2619 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2620 | |
| 2621 | /* Hash Setup, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2622 | status = psa_hash_setup(&operation, alg); |
| 2623 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2624 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2625 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2626 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2627 | |
| 2628 | /* If setup failed, reproduce the failure, so as to |
| 2629 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2630 | if (status != PSA_SUCCESS) { |
| 2631 | TEST_EQUAL(psa_hash_setup(&operation, alg), status); |
| 2632 | } |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2633 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2634 | /* Now the operation object should be reusable. */ |
| 2635 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2636 | PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG)); |
| 2637 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2638 | #endif |
| 2639 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2640 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2641 | mbedtls_free(output); |
| 2642 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2643 | } |
| 2644 | /* END_CASE */ |
| 2645 | |
| 2646 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2647 | void hash_compute_fail(int alg_arg, data_t *input, |
| 2648 | int output_size_arg, int expected_status_arg) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2649 | { |
| 2650 | psa_algorithm_t alg = alg_arg; |
| 2651 | uint8_t *output = NULL; |
| 2652 | size_t output_size = output_size_arg; |
| 2653 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2654 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2655 | psa_status_t expected_status = expected_status_arg; |
| 2656 | psa_status_t status; |
| 2657 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2658 | ASSERT_ALLOC(output, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2659 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2660 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2661 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2662 | /* Hash Compute, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2663 | status = psa_hash_compute(alg, input->x, input->len, |
| 2664 | output, output_size, &output_length); |
| 2665 | TEST_EQUAL(status, expected_status); |
| 2666 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2667 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2668 | /* Hash Compute, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2669 | status = psa_hash_setup(&operation, alg); |
| 2670 | if (status == PSA_SUCCESS) { |
| 2671 | status = psa_hash_update(&operation, input->x, input->len); |
| 2672 | if (status == PSA_SUCCESS) { |
| 2673 | status = psa_hash_finish(&operation, output, output_size, |
| 2674 | &output_length); |
| 2675 | if (status == PSA_SUCCESS) { |
| 2676 | TEST_LE_U(output_length, output_size); |
| 2677 | } else { |
| 2678 | TEST_EQUAL(status, expected_status); |
| 2679 | } |
| 2680 | } else { |
| 2681 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2682 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2683 | } else { |
| 2684 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2685 | } |
| 2686 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2687 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2688 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2689 | mbedtls_free(output); |
| 2690 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2691 | } |
| 2692 | /* END_CASE */ |
| 2693 | |
| 2694 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2695 | void hash_compare_fail(int alg_arg, data_t *input, |
| 2696 | data_t *reference_hash, |
| 2697 | int expected_status_arg) |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2698 | { |
| 2699 | psa_algorithm_t alg = alg_arg; |
| 2700 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2701 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2702 | psa_status_t status; |
| 2703 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2704 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2705 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2706 | /* Hash Compare, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2707 | status = psa_hash_compare(alg, input->x, input->len, |
| 2708 | reference_hash->x, reference_hash->len); |
| 2709 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2710 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2711 | /* Hash Compare, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2712 | status = psa_hash_setup(&operation, alg); |
| 2713 | if (status == PSA_SUCCESS) { |
| 2714 | status = psa_hash_update(&operation, input->x, input->len); |
| 2715 | if (status == PSA_SUCCESS) { |
| 2716 | status = psa_hash_verify(&operation, reference_hash->x, |
| 2717 | reference_hash->len); |
| 2718 | TEST_EQUAL(status, expected_status); |
| 2719 | } else { |
| 2720 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2721 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2722 | } else { |
| 2723 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2724 | } |
| 2725 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2726 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2727 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2728 | PSA_DONE(); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2729 | } |
| 2730 | /* END_CASE */ |
| 2731 | |
| 2732 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2733 | void hash_compute_compare(int alg_arg, data_t *input, |
| 2734 | data_t *expected_output) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2735 | { |
| 2736 | psa_algorithm_t alg = alg_arg; |
| 2737 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2738 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2739 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2740 | size_t i; |
| 2741 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2742 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2743 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2744 | /* Compute with tight buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2745 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 2746 | output, PSA_HASH_LENGTH(alg), |
| 2747 | &output_length)); |
| 2748 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2749 | ASSERT_COMPARE(output, output_length, |
| 2750 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2751 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2752 | /* Compute with tight buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2753 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2754 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2755 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 2756 | 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); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2761 | |
| 2762 | /* Compute with larger buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2763 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 2764 | output, sizeof(output), |
| 2765 | &output_length)); |
| 2766 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2767 | ASSERT_COMPARE(output, output_length, |
| 2768 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2769 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2770 | /* Compute with larger buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2771 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2772 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2773 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 2774 | sizeof(output), &output_length)); |
| 2775 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
| 2776 | ASSERT_COMPARE(output, output_length, |
| 2777 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2778 | |
| 2779 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2780 | PSA_ASSERT(psa_hash_compare(alg, input->x, input->len, |
| 2781 | output, output_length)); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2782 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2783 | /* Compare with correct hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2784 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2785 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2786 | PSA_ASSERT(psa_hash_verify(&operation, output, |
| 2787 | output_length)); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2788 | |
| 2789 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2790 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2791 | output, output_length + 1), |
| 2792 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2793 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2794 | /* Compare with trailing garbage, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2795 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2796 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2797 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1), |
| 2798 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2799 | |
| 2800 | /* Compare with truncated hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2801 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2802 | output, output_length - 1), |
| 2803 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2804 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2805 | /* Compare with truncated hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2806 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2807 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2808 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1), |
| 2809 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2810 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2811 | /* Compare with corrupted value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2812 | for (i = 0; i < output_length; i++) { |
| 2813 | mbedtls_test_set_step(i); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2814 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2815 | |
| 2816 | /* One-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2817 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 2818 | output, output_length), |
| 2819 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2820 | |
| 2821 | /* Multi-Part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2822 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2823 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 2824 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length), |
| 2825 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2826 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2827 | output[i] ^= 1; |
| 2828 | } |
| 2829 | |
| 2830 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2831 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2832 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2833 | } |
| 2834 | /* END_CASE */ |
| 2835 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2836 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2837 | void hash_bad_order() |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2838 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2839 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2840 | unsigned char input[] = ""; |
| 2841 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2842 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2843 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2844 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2845 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 |
| 2846 | }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2847 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2848 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2849 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2850 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2851 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2852 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2853 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2854 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2855 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2856 | TEST_EQUAL(psa_hash_setup(&operation, alg), |
| 2857 | PSA_ERROR_BAD_STATE); |
| 2858 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2859 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2860 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2861 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2862 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2863 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2864 | PSA_ERROR_BAD_STATE); |
| 2865 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2866 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2867 | /* Check that update calls abort on error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2868 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 2869 | operation.id = UINT_MAX; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2870 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2871 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2872 | PSA_ERROR_BAD_STATE); |
| 2873 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2874 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2875 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2876 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2877 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2878 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2879 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2880 | hash, sizeof(hash), &hash_len)); |
| 2881 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 2882 | PSA_ERROR_BAD_STATE); |
| 2883 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2884 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2885 | /* Call verify without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2886 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2887 | valid_hash, sizeof(valid_hash)), |
| 2888 | PSA_ERROR_BAD_STATE); |
| 2889 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2890 | |
| 2891 | /* Call verify after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2892 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2893 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2894 | hash, sizeof(hash), &hash_len)); |
| 2895 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2896 | valid_hash, sizeof(valid_hash)), |
| 2897 | PSA_ERROR_BAD_STATE); |
| 2898 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2899 | |
| 2900 | /* Call verify twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2901 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2902 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2903 | PSA_ASSERT(psa_hash_verify(&operation, |
| 2904 | valid_hash, sizeof(valid_hash))); |
| 2905 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2906 | TEST_EQUAL(psa_hash_verify(&operation, |
| 2907 | valid_hash, sizeof(valid_hash)), |
| 2908 | PSA_ERROR_BAD_STATE); |
| 2909 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2910 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2911 | |
| 2912 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2913 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2914 | hash, sizeof(hash), &hash_len), |
| 2915 | PSA_ERROR_BAD_STATE); |
| 2916 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2917 | |
| 2918 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2919 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2920 | PSA_ASSERT(psa_hash_finish(&operation, |
| 2921 | hash, sizeof(hash), &hash_len)); |
| 2922 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2923 | hash, sizeof(hash), &hash_len), |
| 2924 | PSA_ERROR_BAD_STATE); |
| 2925 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2926 | |
| 2927 | /* Call finish after calling verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2928 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2929 | PSA_ASSERT(psa_hash_verify(&operation, |
| 2930 | valid_hash, sizeof(valid_hash))); |
| 2931 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2932 | hash, sizeof(hash), &hash_len), |
| 2933 | PSA_ERROR_BAD_STATE); |
| 2934 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2935 | |
| 2936 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2937 | PSA_DONE(); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2938 | } |
| 2939 | /* END_CASE */ |
| 2940 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2941 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2942 | void hash_verify_bad_args() |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2943 | { |
| 2944 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2945 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2946 | * appended to it */ |
| 2947 | unsigned char hash[] = { |
| 2948 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2949 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2950 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb |
| 2951 | }; |
| 2952 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2953 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2954 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2955 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2956 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2957 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2958 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2959 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 2960 | TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1), |
| 2961 | PSA_ERROR_INVALID_SIGNATURE); |
| 2962 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 2963 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 2964 | ASSERT_OPERATION_IS_INACTIVE(operation); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2965 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2966 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2967 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2968 | TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size), |
| 2969 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2970 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2971 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2972 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2973 | TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)), |
| 2974 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2975 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2976 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2977 | PSA_DONE(); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2978 | } |
| 2979 | /* END_CASE */ |
| 2980 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2981 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2982 | void hash_finish_bad_args() |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2983 | { |
| 2984 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2985 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2986 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2987 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2988 | size_t hash_len; |
| 2989 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2990 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2991 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2992 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2993 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 2994 | TEST_EQUAL(psa_hash_finish(&operation, |
| 2995 | hash, expected_size - 1, &hash_len), |
| 2996 | PSA_ERROR_BUFFER_TOO_SMALL); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2997 | |
| 2998 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2999 | PSA_DONE(); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3000 | } |
| 3001 | /* END_CASE */ |
| 3002 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3003 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3004 | void hash_clone_source_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3005 | { |
| 3006 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3007 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3008 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 3009 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3010 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3011 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3012 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3013 | size_t hash_len; |
| 3014 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3015 | PSA_ASSERT(psa_crypto_init()); |
| 3016 | PSA_ASSERT(psa_hash_setup(&op_source, alg)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3017 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3018 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3019 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3020 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3021 | hash, sizeof(hash), &hash_len)); |
| 3022 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3023 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3024 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3025 | TEST_EQUAL(psa_hash_clone(&op_source, &op_setup), |
| 3026 | PSA_ERROR_BAD_STATE); |
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_clone(&op_source, &op_init)); |
| 3029 | PSA_ASSERT(psa_hash_finish(&op_init, |
| 3030 | hash, sizeof(hash), &hash_len)); |
| 3031 | PSA_ASSERT(psa_hash_clone(&op_source, &op_finished)); |
| 3032 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3033 | hash, sizeof(hash), &hash_len)); |
| 3034 | PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted)); |
| 3035 | PSA_ASSERT(psa_hash_finish(&op_aborted, |
| 3036 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3037 | |
| 3038 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3039 | psa_hash_abort(&op_source); |
| 3040 | psa_hash_abort(&op_init); |
| 3041 | psa_hash_abort(&op_setup); |
| 3042 | psa_hash_abort(&op_finished); |
| 3043 | psa_hash_abort(&op_aborted); |
| 3044 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3045 | } |
| 3046 | /* END_CASE */ |
| 3047 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3048 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3049 | void hash_clone_target_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3050 | { |
| 3051 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3052 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3053 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3054 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3055 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3056 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3057 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 3058 | size_t hash_len; |
| 3059 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3060 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3061 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3062 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3063 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3064 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3065 | hash, sizeof(hash), &hash_len)); |
| 3066 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3067 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3068 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3069 | PSA_ASSERT(psa_hash_clone(&op_setup, &op_target)); |
| 3070 | PSA_ASSERT(psa_hash_finish(&op_target, |
| 3071 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3072 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3073 | TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE); |
| 3074 | TEST_EQUAL(psa_hash_clone(&op_finished, &op_target), |
| 3075 | PSA_ERROR_BAD_STATE); |
| 3076 | TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target), |
| 3077 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3078 | |
| 3079 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3080 | psa_hash_abort(&op_target); |
| 3081 | psa_hash_abort(&op_init); |
| 3082 | psa_hash_abort(&op_setup); |
| 3083 | psa_hash_abort(&op_finished); |
| 3084 | psa_hash_abort(&op_aborted); |
| 3085 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3086 | } |
| 3087 | /* END_CASE */ |
| 3088 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3089 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3090 | void mac_operation_init() |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3091 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3092 | const uint8_t input[1] = { 0 }; |
| 3093 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3094 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3095 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3096 | * 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] | 3097 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3098 | psa_mac_operation_t func = psa_mac_operation_init(); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3099 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 3100 | psa_mac_operation_t zero; |
| 3101 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3102 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3103 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3104 | /* A freshly-initialized MAC operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3105 | TEST_EQUAL(psa_mac_update(&func, |
| 3106 | input, sizeof(input)), |
| 3107 | PSA_ERROR_BAD_STATE); |
| 3108 | TEST_EQUAL(psa_mac_update(&init, |
| 3109 | input, sizeof(input)), |
| 3110 | PSA_ERROR_BAD_STATE); |
| 3111 | TEST_EQUAL(psa_mac_update(&zero, |
| 3112 | input, sizeof(input)), |
| 3113 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3114 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3115 | /* A default MAC operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3116 | PSA_ASSERT(psa_mac_abort(&func)); |
| 3117 | PSA_ASSERT(psa_mac_abort(&init)); |
| 3118 | PSA_ASSERT(psa_mac_abort(&zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3119 | } |
| 3120 | /* END_CASE */ |
| 3121 | |
| 3122 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3123 | void mac_setup(int key_type_arg, |
| 3124 | data_t *key, |
| 3125 | int alg_arg, |
| 3126 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3127 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3128 | psa_key_type_t key_type = key_type_arg; |
| 3129 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3130 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3131 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3132 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3133 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3134 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3135 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3136 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3137 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3138 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3139 | if (!exercise_mac_setup(key_type, key->x, key->len, alg, |
| 3140 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3141 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3142 | } |
| 3143 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3144 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3145 | /* The operation object should be reusable. */ |
| 3146 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3147 | if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 3148 | smoke_test_key_data, |
| 3149 | sizeof(smoke_test_key_data), |
| 3150 | KNOWN_SUPPORTED_MAC_ALG, |
| 3151 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3152 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3153 | } |
| 3154 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3155 | #endif |
| 3156 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3157 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3158 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3159 | } |
| 3160 | /* END_CASE */ |
| 3161 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3162 | /* 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] | 3163 | void mac_bad_order() |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3164 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3165 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3166 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 3167 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3168 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3169 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3170 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3171 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa |
| 3172 | }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3173 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3174 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3175 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 3176 | size_t sign_mac_length = 0; |
| 3177 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3178 | const uint8_t verify_mac[] = { |
| 3179 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 3180 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3181 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 |
| 3182 | }; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3183 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3184 | PSA_ASSERT(psa_crypto_init()); |
| 3185 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 3186 | psa_set_key_algorithm(&attributes, alg); |
| 3187 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3188 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3189 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3190 | &key)); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3191 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3192 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3193 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3194 | PSA_ERROR_BAD_STATE); |
| 3195 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3196 | |
| 3197 | /* Call sign finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3198 | TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac), |
| 3199 | &sign_mac_length), |
| 3200 | PSA_ERROR_BAD_STATE); |
| 3201 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3202 | |
| 3203 | /* Call verify finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3204 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3205 | verify_mac, sizeof(verify_mac)), |
| 3206 | PSA_ERROR_BAD_STATE); |
| 3207 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3208 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3209 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3210 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3211 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3212 | TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg), |
| 3213 | PSA_ERROR_BAD_STATE); |
| 3214 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3215 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3216 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3217 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3218 | /* Call update after sign finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3219 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3220 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3221 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3222 | sign_mac, sizeof(sign_mac), |
| 3223 | &sign_mac_length)); |
| 3224 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3225 | PSA_ERROR_BAD_STATE); |
| 3226 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3227 | |
| 3228 | /* Call update after verify finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3229 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3230 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3231 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3232 | verify_mac, sizeof(verify_mac))); |
| 3233 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3234 | PSA_ERROR_BAD_STATE); |
| 3235 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3236 | |
| 3237 | /* Call sign finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3238 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3239 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3240 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3241 | sign_mac, sizeof(sign_mac), |
| 3242 | &sign_mac_length)); |
| 3243 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3244 | sign_mac, sizeof(sign_mac), |
| 3245 | &sign_mac_length), |
| 3246 | PSA_ERROR_BAD_STATE); |
| 3247 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3248 | |
| 3249 | /* Call verify finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3250 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3251 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3252 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3253 | verify_mac, sizeof(verify_mac))); |
| 3254 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3255 | verify_mac, sizeof(verify_mac)), |
| 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 | /* Setup sign but try verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3260 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3261 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3262 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3263 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3264 | verify_mac, sizeof(verify_mac)), |
| 3265 | PSA_ERROR_BAD_STATE); |
| 3266 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3267 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3268 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3269 | |
| 3270 | /* Setup verify but try sign. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3271 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3272 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3273 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3274 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3275 | sign_mac, sizeof(sign_mac), |
| 3276 | &sign_mac_length), |
| 3277 | PSA_ERROR_BAD_STATE); |
| 3278 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3279 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3280 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3282 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3283 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3284 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3285 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3286 | } |
| 3287 | /* END_CASE */ |
| 3288 | |
| 3289 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3290 | void mac_sign_verify_multi(int key_type_arg, |
| 3291 | data_t *key_data, |
| 3292 | int alg_arg, |
| 3293 | data_t *input, |
| 3294 | int is_verify, |
| 3295 | data_t *expected_mac) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3296 | { |
| 3297 | size_t data_part_len = 0; |
| 3298 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3299 | 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] | 3300 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3301 | mbedtls_test_set_step(2000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3302 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3303 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3304 | alg_arg, |
| 3305 | input, data_part_len, |
| 3306 | expected_mac, |
| 3307 | is_verify, 0) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3308 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3309 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3310 | |
| 3311 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3312 | mbedtls_test_set_step(3000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3314 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3315 | alg_arg, |
| 3316 | input, data_part_len, |
| 3317 | expected_mac, |
| 3318 | is_verify, 1) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3319 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3320 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3321 | } |
| 3322 | |
| 3323 | /* Goto is required to silence warnings about unused labels, as we |
| 3324 | * don't actually do any test assertions in this function. */ |
| 3325 | goto exit; |
| 3326 | } |
| 3327 | /* END_CASE */ |
| 3328 | |
| 3329 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3330 | void mac_sign(int key_type_arg, |
| 3331 | data_t *key_data, |
| 3332 | int alg_arg, |
| 3333 | data_t *input, |
| 3334 | data_t *expected_mac) |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3335 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3336 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3337 | psa_key_type_t key_type = key_type_arg; |
| 3338 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3339 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3340 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3341 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3342 | size_t mac_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3343 | 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] | 3344 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3345 | const size_t output_sizes_to_test[] = { |
| 3346 | 0, |
| 3347 | 1, |
| 3348 | expected_mac->len - 1, |
| 3349 | expected_mac->len, |
| 3350 | expected_mac->len + 1, |
| 3351 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3353 | TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3354 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3355 | TEST_ASSERT(expected_mac->len == mac_buffer_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3356 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3357 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3358 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3359 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 3360 | psa_set_key_algorithm(&attributes, alg); |
| 3361 | psa_set_key_type(&attributes, key_type); |
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 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3364 | &key)); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3365 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3366 | 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] | 3367 | const size_t output_size = output_sizes_to_test[i]; |
| 3368 | psa_status_t expected_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3369 | (output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3370 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3371 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3372 | mbedtls_test_set_step(output_size); |
| 3373 | ASSERT_ALLOC(actual_mac, output_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3374 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3375 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3376 | TEST_EQUAL(psa_mac_compute(key, alg, |
| 3377 | input->x, input->len, |
| 3378 | actual_mac, output_size, &mac_length), |
| 3379 | expected_status); |
| 3380 | if (expected_status == PSA_SUCCESS) { |
| 3381 | ASSERT_COMPARE(expected_mac->x, expected_mac->len, |
| 3382 | actual_mac, mac_length); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3383 | } |
| 3384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3385 | if (output_size > 0) { |
| 3386 | memset(actual_mac, 0, output_size); |
| 3387 | } |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3388 | |
| 3389 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3390 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3391 | PSA_ASSERT(psa_mac_update(&operation, |
| 3392 | input->x, input->len)); |
| 3393 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3394 | actual_mac, output_size, |
| 3395 | &mac_length), |
| 3396 | expected_status); |
| 3397 | PSA_ASSERT(psa_mac_abort(&operation)); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3398 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3399 | if (expected_status == PSA_SUCCESS) { |
| 3400 | ASSERT_COMPARE(expected_mac->x, expected_mac->len, |
| 3401 | actual_mac, mac_length); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3402 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3403 | mbedtls_free(actual_mac); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3404 | actual_mac = NULL; |
| 3405 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3406 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3407 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3408 | psa_mac_abort(&operation); |
| 3409 | psa_destroy_key(key); |
| 3410 | PSA_DONE(); |
| 3411 | mbedtls_free(actual_mac); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3412 | } |
| 3413 | /* END_CASE */ |
| 3414 | |
| 3415 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3416 | void mac_verify(int key_type_arg, |
| 3417 | data_t *key_data, |
| 3418 | int alg_arg, |
| 3419 | data_t *input, |
| 3420 | data_t *expected_mac) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3421 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3422 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3423 | psa_key_type_t key_type = key_type_arg; |
| 3424 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3425 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3426 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3427 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3428 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3429 | TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3430 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3431 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3433 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 3434 | psa_set_key_algorithm(&attributes, alg); |
| 3435 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3436 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3437 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3438 | &key)); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3439 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3440 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3441 | PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len, |
| 3442 | expected_mac->x, expected_mac->len)); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3443 | |
| 3444 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3445 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3446 | PSA_ASSERT(psa_mac_update(&operation, |
| 3447 | input->x, input->len)); |
| 3448 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3449 | expected_mac->x, |
| 3450 | expected_mac->len)); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3451 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3452 | /* Test a MAC that's too short, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3453 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3454 | input->x, input->len, |
| 3455 | expected_mac->x, |
| 3456 | expected_mac->len - 1), |
| 3457 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3458 | |
| 3459 | /* Test a MAC that's too short, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3460 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3461 | PSA_ASSERT(psa_mac_update(&operation, |
| 3462 | input->x, input->len)); |
| 3463 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3464 | expected_mac->x, |
| 3465 | expected_mac->len - 1), |
| 3466 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3467 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3468 | /* Test a MAC that's too long, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3469 | ASSERT_ALLOC(perturbed_mac, expected_mac->len + 1); |
| 3470 | memcpy(perturbed_mac, expected_mac->x, expected_mac->len); |
| 3471 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3472 | input->x, input->len, |
| 3473 | perturbed_mac, expected_mac->len + 1), |
| 3474 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3475 | |
| 3476 | /* Test a MAC that's too long, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3477 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3478 | PSA_ASSERT(psa_mac_update(&operation, |
| 3479 | input->x, input->len)); |
| 3480 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3481 | perturbed_mac, |
| 3482 | expected_mac->len + 1), |
| 3483 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3484 | |
| 3485 | /* Test changing one byte. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3486 | for (size_t i = 0; i < expected_mac->len; i++) { |
| 3487 | mbedtls_test_set_step(i); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3488 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3489 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3490 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3491 | input->x, input->len, |
| 3492 | perturbed_mac, expected_mac->len), |
| 3493 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3494 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3495 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3496 | PSA_ASSERT(psa_mac_update(&operation, |
| 3497 | input->x, input->len)); |
| 3498 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3499 | perturbed_mac, |
| 3500 | expected_mac->len), |
| 3501 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3502 | perturbed_mac[i] ^= 1; |
| 3503 | } |
| 3504 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3505 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3506 | psa_mac_abort(&operation); |
| 3507 | psa_destroy_key(key); |
| 3508 | PSA_DONE(); |
| 3509 | mbedtls_free(perturbed_mac); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3510 | } |
| 3511 | /* END_CASE */ |
| 3512 | |
| 3513 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3514 | void cipher_operation_init() |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3515 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3516 | const uint8_t input[1] = { 0 }; |
| 3517 | unsigned char output[1] = { 0 }; |
| 3518 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3519 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3520 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3521 | * 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] | 3522 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3523 | psa_cipher_operation_t func = psa_cipher_operation_init(); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3524 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3525 | psa_cipher_operation_t zero; |
| 3526 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3527 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3528 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3529 | /* A freshly-initialized cipher operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3530 | TEST_EQUAL(psa_cipher_update(&func, |
| 3531 | input, sizeof(input), |
| 3532 | output, sizeof(output), |
| 3533 | &output_length), |
| 3534 | PSA_ERROR_BAD_STATE); |
| 3535 | TEST_EQUAL(psa_cipher_update(&init, |
| 3536 | input, sizeof(input), |
| 3537 | output, sizeof(output), |
| 3538 | &output_length), |
| 3539 | PSA_ERROR_BAD_STATE); |
| 3540 | TEST_EQUAL(psa_cipher_update(&zero, |
| 3541 | input, sizeof(input), |
| 3542 | output, sizeof(output), |
| 3543 | &output_length), |
| 3544 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3545 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3546 | /* A default cipher operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3547 | PSA_ASSERT(psa_cipher_abort(&func)); |
| 3548 | PSA_ASSERT(psa_cipher_abort(&init)); |
| 3549 | PSA_ASSERT(psa_cipher_abort(&zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3550 | } |
| 3551 | /* END_CASE */ |
| 3552 | |
| 3553 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3554 | void cipher_setup(int key_type_arg, |
| 3555 | data_t *key, |
| 3556 | int alg_arg, |
| 3557 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3558 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3559 | psa_key_type_t key_type = key_type_arg; |
| 3560 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3561 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3562 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3563 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3564 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3565 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3566 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3567 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3568 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3569 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3570 | if (!exercise_cipher_setup(key_type, key->x, key->len, alg, |
| 3571 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3572 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3573 | } |
| 3574 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3575 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3576 | /* The operation object should be reusable. */ |
| 3577 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3578 | if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3579 | smoke_test_key_data, |
| 3580 | sizeof(smoke_test_key_data), |
| 3581 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3582 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3583 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3584 | } |
| 3585 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3586 | #endif |
| 3587 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3588 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3589 | psa_cipher_abort(&operation); |
| 3590 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3591 | } |
| 3592 | /* END_CASE */ |
| 3593 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3594 | /* 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] | 3595 | void cipher_bad_order() |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3596 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3597 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3598 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3599 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3600 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3601 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3602 | 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] | 3603 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3604 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3605 | 0xaa, 0xaa, 0xaa, 0xaa |
| 3606 | }; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3607 | const uint8_t text[] = { |
| 3608 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3609 | 0xbb, 0xbb, 0xbb, 0xbb |
| 3610 | }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3611 | 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] | 3612 | size_t length = 0; |
| 3613 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3614 | PSA_ASSERT(psa_crypto_init()); |
| 3615 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3616 | psa_set_key_algorithm(&attributes, alg); |
| 3617 | psa_set_key_type(&attributes, key_type); |
| 3618 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3619 | &key)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3620 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3621 | /* Call encrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3622 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3623 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3624 | TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg), |
| 3625 | PSA_ERROR_BAD_STATE); |
| 3626 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3627 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3628 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3629 | |
| 3630 | /* Call decrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3631 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3632 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3633 | TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg), |
| 3634 | PSA_ERROR_BAD_STATE); |
| 3635 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3636 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3637 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3638 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3639 | /* Generate an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3640 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3641 | buffer, sizeof(buffer), |
| 3642 | &length), |
| 3643 | PSA_ERROR_BAD_STATE); |
| 3644 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3645 | |
| 3646 | /* Generate an IV twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3647 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3648 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3649 | buffer, sizeof(buffer), |
| 3650 | &length)); |
| 3651 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3652 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3653 | buffer, sizeof(buffer), |
| 3654 | &length), |
| 3655 | PSA_ERROR_BAD_STATE); |
| 3656 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3657 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3658 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3659 | |
| 3660 | /* Generate an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3661 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3662 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3663 | iv, sizeof(iv))); |
| 3664 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3665 | buffer, sizeof(buffer), |
| 3666 | &length), |
| 3667 | PSA_ERROR_BAD_STATE); |
| 3668 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3669 | |
| 3670 | /* Set an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3671 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3672 | iv, sizeof(iv)), |
| 3673 | PSA_ERROR_BAD_STATE); |
| 3674 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3675 | |
| 3676 | /* Set an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3677 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3678 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3679 | iv, sizeof(iv))); |
| 3680 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3681 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3682 | iv, sizeof(iv)), |
| 3683 | PSA_ERROR_BAD_STATE); |
| 3684 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3685 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3686 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3687 | |
| 3688 | /* Set an IV after it's already generated. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3689 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3690 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3691 | buffer, sizeof(buffer), |
| 3692 | &length)); |
| 3693 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 3694 | iv, sizeof(iv)), |
| 3695 | PSA_ERROR_BAD_STATE); |
| 3696 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3697 | |
| 3698 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3699 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3700 | text, sizeof(text), |
| 3701 | buffer, sizeof(buffer), |
| 3702 | &length), |
| 3703 | PSA_ERROR_BAD_STATE); |
| 3704 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3705 | |
| 3706 | /* Call update without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3707 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3708 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3709 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3710 | text, sizeof(text), |
| 3711 | buffer, sizeof(buffer), |
| 3712 | &length), |
| 3713 | PSA_ERROR_BAD_STATE); |
| 3714 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3715 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3716 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3717 | |
| 3718 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3719 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3720 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3721 | iv, sizeof(iv))); |
| 3722 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3723 | buffer, sizeof(buffer), &length)); |
| 3724 | TEST_EQUAL(psa_cipher_update(&operation, |
| 3725 | text, sizeof(text), |
| 3726 | buffer, sizeof(buffer), |
| 3727 | &length), |
| 3728 | PSA_ERROR_BAD_STATE); |
| 3729 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3730 | |
| 3731 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3732 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3733 | buffer, sizeof(buffer), &length), |
| 3734 | PSA_ERROR_BAD_STATE); |
| 3735 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3736 | |
| 3737 | /* Call finish without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3738 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3739 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3740 | * for cipher modes with padding. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3741 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3742 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3743 | buffer, sizeof(buffer), &length), |
| 3744 | PSA_ERROR_BAD_STATE); |
| 3745 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3746 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3747 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3748 | |
| 3749 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3750 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3751 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 3752 | iv, sizeof(iv))); |
| 3753 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3754 | buffer, sizeof(buffer), &length)); |
| 3755 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 3756 | buffer, sizeof(buffer), &length), |
| 3757 | PSA_ERROR_BAD_STATE); |
| 3758 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3759 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3760 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3761 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3762 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3763 | psa_cipher_abort(&operation); |
| 3764 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3765 | } |
| 3766 | /* END_CASE */ |
| 3767 | |
| 3768 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3769 | void cipher_encrypt_fail(int alg_arg, |
| 3770 | int key_type_arg, |
| 3771 | data_t *key_data, |
| 3772 | data_t *input, |
| 3773 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3774 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3775 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3776 | psa_status_t status; |
| 3777 | psa_key_type_t key_type = key_type_arg; |
| 3778 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3779 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3780 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 }; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3781 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; |
| 3782 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3783 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3784 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3785 | size_t output_length = 0; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3786 | size_t function_output_length; |
| 3787 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3788 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3789 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3790 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 3791 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3792 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3793 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3794 | psa_set_key_algorithm(&attributes, alg); |
| 3795 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3796 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3797 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 3798 | input->len); |
| 3799 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3800 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3801 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3802 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3803 | } |
| 3804 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3805 | /* Encrypt, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3806 | status = psa_cipher_encrypt(key, alg, input->x, input->len, output, |
| 3807 | output_buffer_size, &output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3808 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3809 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3810 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3811 | /* Encrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3812 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
| 3813 | if (status == PSA_SUCCESS) { |
| 3814 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 3815 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3816 | iv, iv_size, |
| 3817 | &iv_length)); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3818 | } |
| 3819 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3820 | status = psa_cipher_update(&operation, input->x, input->len, |
| 3821 | output, output_buffer_size, |
| 3822 | &function_output_length); |
| 3823 | if (status == PSA_SUCCESS) { |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3824 | output_length += function_output_length; |
| 3825 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3826 | status = psa_cipher_finish(&operation, output + output_length, |
| 3827 | output_buffer_size - output_length, |
| 3828 | &function_output_length); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3829 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3830 | TEST_EQUAL(status, expected_status); |
| 3831 | } else { |
| 3832 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3833 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3834 | } else { |
| 3835 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3836 | } |
| 3837 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3838 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3839 | psa_cipher_abort(&operation); |
| 3840 | mbedtls_free(output); |
| 3841 | psa_destroy_key(key); |
| 3842 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3843 | } |
| 3844 | /* END_CASE */ |
| 3845 | |
| 3846 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3847 | void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data, |
| 3848 | data_t *input, int iv_length, |
| 3849 | int expected_result) |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3850 | { |
| 3851 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3852 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3853 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3854 | size_t output_buffer_size = 0; |
| 3855 | unsigned char *output = NULL; |
| 3856 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3857 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 3858 | ASSERT_ALLOC(output, output_buffer_size); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3859 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3860 | PSA_ASSERT(psa_crypto_init()); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3861 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3862 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 3863 | psa_set_key_algorithm(&attributes, alg); |
| 3864 | psa_set_key_type(&attributes, key_type); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3865 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3866 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3867 | &key)); |
| 3868 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3869 | TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output, |
| 3870 | iv_length)); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3871 | |
| 3872 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3873 | psa_cipher_abort(&operation); |
| 3874 | mbedtls_free(output); |
| 3875 | psa_destroy_key(key); |
| 3876 | PSA_DONE(); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3877 | } |
| 3878 | /* END_CASE */ |
| 3879 | |
| 3880 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3881 | void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data, |
| 3882 | data_t *plaintext, data_t *ciphertext) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3883 | { |
| 3884 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3885 | psa_key_type_t key_type = key_type_arg; |
| 3886 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3887 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3888 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3889 | unsigned char *output = NULL; |
| 3890 | size_t output_buffer_size = 0; |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3891 | size_t output_length, length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3892 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3893 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3894 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3895 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3896 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3897 | TEST_LE_U(ciphertext->len, |
| 3898 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len)); |
| 3899 | TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len), |
| 3900 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len)); |
| 3901 | TEST_LE_U(plaintext->len, |
| 3902 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len)); |
| 3903 | TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len), |
| 3904 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len)); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3905 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3906 | |
| 3907 | /* Set up key and output buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3908 | psa_set_key_usage_flags(&attributes, |
| 3909 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3910 | psa_set_key_algorithm(&attributes, alg); |
| 3911 | psa_set_key_type(&attributes, key_type); |
| 3912 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3913 | &key)); |
| 3914 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 3915 | plaintext->len); |
| 3916 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3917 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3918 | /* set_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3919 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3920 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 3921 | PSA_ERROR_BAD_STATE); |
| 3922 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3923 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 3924 | PSA_ERROR_BAD_STATE); |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3925 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3926 | /* generate_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3927 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3928 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 3929 | &length), |
| 3930 | PSA_ERROR_BAD_STATE); |
| 3931 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3932 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 3933 | &length), |
| 3934 | PSA_ERROR_BAD_STATE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3935 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3936 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3937 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3938 | output_length = 0; |
| 3939 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3940 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3941 | plaintext->x, plaintext->len, |
| 3942 | output, output_buffer_size, |
| 3943 | &length)); |
| 3944 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3945 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3946 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3947 | mbedtls_buffer_offset(output, output_length), |
| 3948 | output_buffer_size - output_length, |
| 3949 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3950 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3951 | ASSERT_COMPARE(ciphertext->x, ciphertext->len, |
| 3952 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3953 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3954 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3955 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3956 | output_length = 0; |
| 3957 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3958 | PSA_ASSERT(psa_cipher_update(&operation, |
| 3959 | ciphertext->x, ciphertext->len, |
| 3960 | output, output_buffer_size, |
| 3961 | &length)); |
| 3962 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3963 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3964 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 3965 | mbedtls_buffer_offset(output, output_length), |
| 3966 | output_buffer_size - output_length, |
| 3967 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3968 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3969 | ASSERT_COMPARE(plaintext->x, plaintext->len, |
| 3970 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3971 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3972 | /* One-shot encryption */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3973 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3974 | PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len, |
| 3975 | output, output_buffer_size, |
| 3976 | &output_length)); |
| 3977 | ASSERT_COMPARE(ciphertext->x, ciphertext->len, |
| 3978 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3979 | |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3980 | /* One-shot decryption */ |
| 3981 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3982 | PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len, |
| 3983 | output, output_buffer_size, |
| 3984 | &output_length)); |
| 3985 | ASSERT_COMPARE(plaintext->x, plaintext->len, |
| 3986 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3987 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3988 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3989 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3990 | mbedtls_free(output); |
| 3991 | psa_cipher_abort(&operation); |
| 3992 | psa_destroy_key(key); |
| 3993 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3994 | } |
| 3995 | /* END_CASE */ |
| 3996 | |
| 3997 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3998 | 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] | 3999 | { |
| 4000 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4001 | psa_algorithm_t alg = alg_arg; |
| 4002 | psa_key_type_t key_type = key_type_arg; |
| 4003 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4004 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4005 | psa_status_t status; |
| 4006 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4007 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4008 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4009 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4010 | psa_set_key_algorithm(&attributes, alg); |
| 4011 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4012 | |
| 4013 | /* Usage of either of these two size macros would cause divide by zero |
| 4014 | * with incorrect key types previously. Input length should be irrelevant |
| 4015 | * here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4016 | TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16), |
| 4017 | 0); |
| 4018 | TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4019 | |
| 4020 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4021 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4022 | &key)); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4023 | |
| 4024 | /* Should fail due to invalid alg type (to support invalid key type). |
| 4025 | * Encrypt or decrypt will end up in the same place. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4026 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4027 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4028 | TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4029 | |
| 4030 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4031 | psa_cipher_abort(&operation); |
| 4032 | psa_destroy_key(key); |
| 4033 | PSA_DONE(); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4034 | } |
| 4035 | /* END_CASE */ |
| 4036 | |
| 4037 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4038 | void cipher_encrypt_validation(int alg_arg, |
| 4039 | int key_type_arg, |
| 4040 | data_t *key_data, |
| 4041 | data_t *input) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4042 | { |
| 4043 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4044 | psa_key_type_t key_type = key_type_arg; |
| 4045 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4046 | size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4047 | unsigned char *output1 = NULL; |
| 4048 | size_t output1_buffer_size = 0; |
| 4049 | size_t output1_length = 0; |
| 4050 | unsigned char *output2 = NULL; |
| 4051 | size_t output2_buffer_size = 0; |
| 4052 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4053 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4054 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4055 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4056 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4057 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4058 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4059 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4060 | psa_set_key_algorithm(&attributes, alg); |
| 4061 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4062 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4063 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4064 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4065 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4066 | ASSERT_ALLOC(output1, output1_buffer_size); |
| 4067 | ASSERT_ALLOC(output2, output2_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4068 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4069 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4070 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4071 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 4072 | /* The one-shot cipher encryption uses generated iv so validating |
| 4073 | the output is not possible. Validating with multipart encryption. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4074 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1, |
| 4075 | output1_buffer_size, &output1_length)); |
| 4076 | TEST_LE_U(output1_length, |
| 4077 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4078 | TEST_LE_U(output1_length, |
| 4079 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4080 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4081 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4082 | PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4083 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4084 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4085 | input->x, input->len, |
| 4086 | output2, output2_buffer_size, |
| 4087 | &function_output_length)); |
| 4088 | TEST_LE_U(function_output_length, |
| 4089 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4090 | TEST_LE_U(function_output_length, |
| 4091 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4092 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4093 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4094 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4095 | output2 + output2_length, |
| 4096 | output2_buffer_size - output2_length, |
| 4097 | &function_output_length)); |
| 4098 | TEST_LE_U(function_output_length, |
| 4099 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4100 | TEST_LE_U(function_output_length, |
| 4101 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4102 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4103 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4104 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4105 | ASSERT_COMPARE(output1 + iv_size, output1_length - iv_size, |
| 4106 | output2, output2_length); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4107 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4108 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4109 | psa_cipher_abort(&operation); |
| 4110 | mbedtls_free(output1); |
| 4111 | mbedtls_free(output2); |
| 4112 | psa_destroy_key(key); |
| 4113 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4114 | } |
| 4115 | /* END_CASE */ |
| 4116 | |
| 4117 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4118 | void cipher_encrypt_multipart(int alg_arg, int key_type_arg, |
| 4119 | data_t *key_data, data_t *iv, |
| 4120 | data_t *input, |
| 4121 | int first_part_size_arg, |
| 4122 | int output1_length_arg, int output2_length_arg, |
| 4123 | data_t *expected_output, |
| 4124 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4125 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4126 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4127 | psa_key_type_t key_type = key_type_arg; |
| 4128 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4129 | psa_status_t status; |
| 4130 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4131 | size_t first_part_size = first_part_size_arg; |
| 4132 | size_t output1_length = output1_length_arg; |
| 4133 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4134 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4135 | size_t output_buffer_size = 0; |
| 4136 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4137 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4138 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4139 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4140 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4141 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4142 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4143 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4144 | psa_set_key_algorithm(&attributes, alg); |
| 4145 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4146 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4147 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4148 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4150 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4151 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4152 | if (iv->len > 0) { |
| 4153 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4154 | } |
| 4155 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4156 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4157 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4158 | ASSERT_ALLOC(output, output_buffer_size); |
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 | TEST_LE_U(first_part_size, input->len); |
| 4161 | PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size, |
| 4162 | output, output_buffer_size, |
| 4163 | &function_output_length)); |
| 4164 | TEST_ASSERT(function_output_length == output1_length); |
| 4165 | TEST_LE_U(function_output_length, |
| 4166 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4167 | TEST_LE_U(function_output_length, |
| 4168 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4169 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4170 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4171 | if (first_part_size < input->len) { |
| 4172 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4173 | input->x + first_part_size, |
| 4174 | input->len - first_part_size, |
| 4175 | (output_buffer_size == 0 ? NULL : |
| 4176 | output + total_output_length), |
| 4177 | output_buffer_size - total_output_length, |
| 4178 | &function_output_length)); |
| 4179 | TEST_ASSERT(function_output_length == output2_length); |
| 4180 | TEST_LE_U(function_output_length, |
| 4181 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4182 | alg, |
| 4183 | input->len - first_part_size)); |
| 4184 | TEST_LE_U(function_output_length, |
| 4185 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4186 | total_output_length += function_output_length; |
| 4187 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4188 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4189 | status = psa_cipher_finish(&operation, |
| 4190 | (output_buffer_size == 0 ? NULL : |
| 4191 | output + total_output_length), |
| 4192 | output_buffer_size - total_output_length, |
| 4193 | &function_output_length); |
| 4194 | TEST_LE_U(function_output_length, |
| 4195 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4196 | TEST_LE_U(function_output_length, |
| 4197 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4198 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4199 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4200 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4201 | if (expected_status == PSA_SUCCESS) { |
| 4202 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4203 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4204 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 4205 | output, total_output_length); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4206 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4207 | |
| 4208 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4209 | psa_cipher_abort(&operation); |
| 4210 | mbedtls_free(output); |
| 4211 | psa_destroy_key(key); |
| 4212 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4213 | } |
| 4214 | /* END_CASE */ |
| 4215 | |
| 4216 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4217 | void cipher_decrypt_multipart(int alg_arg, int key_type_arg, |
| 4218 | data_t *key_data, data_t *iv, |
| 4219 | data_t *input, |
| 4220 | int first_part_size_arg, |
| 4221 | int output1_length_arg, int output2_length_arg, |
| 4222 | data_t *expected_output, |
| 4223 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4224 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4225 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4226 | psa_key_type_t key_type = key_type_arg; |
| 4227 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4228 | psa_status_t status; |
| 4229 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4230 | size_t first_part_size = first_part_size_arg; |
| 4231 | size_t output1_length = output1_length_arg; |
| 4232 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4233 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4234 | size_t output_buffer_size = 0; |
| 4235 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4236 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4237 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4238 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4239 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4240 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4241 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4242 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4243 | psa_set_key_algorithm(&attributes, alg); |
| 4244 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4245 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4246 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4247 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4248 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4249 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4250 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4251 | if (iv->len > 0) { |
| 4252 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4253 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4254 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4255 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4256 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4257 | ASSERT_ALLOC(output, output_buffer_size); |
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 | TEST_LE_U(first_part_size, input->len); |
| 4260 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4261 | input->x, first_part_size, |
| 4262 | output, output_buffer_size, |
| 4263 | &function_output_length)); |
| 4264 | TEST_ASSERT(function_output_length == output1_length); |
| 4265 | TEST_LE_U(function_output_length, |
| 4266 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4267 | TEST_LE_U(function_output_length, |
| 4268 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4269 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4270 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4271 | if (first_part_size < input->len) { |
| 4272 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4273 | input->x + first_part_size, |
| 4274 | input->len - first_part_size, |
| 4275 | (output_buffer_size == 0 ? NULL : |
| 4276 | output + total_output_length), |
| 4277 | output_buffer_size - total_output_length, |
| 4278 | &function_output_length)); |
| 4279 | TEST_ASSERT(function_output_length == output2_length); |
| 4280 | TEST_LE_U(function_output_length, |
| 4281 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4282 | alg, |
| 4283 | input->len - first_part_size)); |
| 4284 | TEST_LE_U(function_output_length, |
| 4285 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4286 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4287 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4288 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4289 | status = psa_cipher_finish(&operation, |
| 4290 | (output_buffer_size == 0 ? NULL : |
| 4291 | output + total_output_length), |
| 4292 | output_buffer_size - total_output_length, |
| 4293 | &function_output_length); |
| 4294 | TEST_LE_U(function_output_length, |
| 4295 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4296 | TEST_LE_U(function_output_length, |
| 4297 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4298 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4299 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4300 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4301 | if (expected_status == PSA_SUCCESS) { |
| 4302 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4303 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4304 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 4305 | output, total_output_length); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4306 | } |
| 4307 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4308 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4309 | psa_cipher_abort(&operation); |
| 4310 | mbedtls_free(output); |
| 4311 | psa_destroy_key(key); |
| 4312 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4313 | } |
| 4314 | /* END_CASE */ |
| 4315 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4316 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4317 | void cipher_decrypt_fail(int alg_arg, |
| 4318 | int key_type_arg, |
| 4319 | data_t *key_data, |
| 4320 | data_t *iv, |
| 4321 | data_t *input_arg, |
| 4322 | int expected_status_arg) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4323 | { |
| 4324 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4325 | psa_status_t status; |
| 4326 | psa_key_type_t key_type = key_type_arg; |
| 4327 | psa_algorithm_t alg = alg_arg; |
| 4328 | psa_status_t expected_status = expected_status_arg; |
| 4329 | unsigned char *input = NULL; |
| 4330 | size_t input_buffer_size = 0; |
| 4331 | unsigned char *output = NULL; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4332 | unsigned char *output_multi = NULL; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4333 | size_t output_buffer_size = 0; |
| 4334 | size_t output_length = 0; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4335 | size_t function_output_length; |
| 4336 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4337 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4338 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4339 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 4340 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4341 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4342 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4343 | psa_set_key_algorithm(&attributes, alg); |
| 4344 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4345 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4346 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4347 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4348 | } |
| 4349 | |
| 4350 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4351 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4352 | if (input_buffer_size > 0) { |
| 4353 | ASSERT_ALLOC(input, input_buffer_size); |
| 4354 | memcpy(input, iv->x, iv->len); |
| 4355 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4356 | } |
| 4357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4358 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size); |
| 4359 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4360 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4361 | /* Decrypt, one-short */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4362 | status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4363 | output_buffer_size, &output_length); |
| 4364 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4365 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4366 | /* Decrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4367 | status = psa_cipher_decrypt_setup(&operation, key, alg); |
| 4368 | if (status == PSA_SUCCESS) { |
| 4369 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 4370 | input_arg->len) + |
| 4371 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
| 4372 | ASSERT_ALLOC(output_multi, output_buffer_size); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4374 | if (iv->len > 0) { |
| 4375 | status = psa_cipher_set_iv(&operation, iv->x, iv->len); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4377 | if (status != PSA_SUCCESS) { |
| 4378 | TEST_EQUAL(status, expected_status); |
| 4379 | } |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4380 | } |
| 4381 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4382 | if (status == PSA_SUCCESS) { |
| 4383 | status = psa_cipher_update(&operation, |
| 4384 | input_arg->x, input_arg->len, |
| 4385 | output_multi, output_buffer_size, |
| 4386 | &function_output_length); |
| 4387 | if (status == PSA_SUCCESS) { |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4388 | output_length = function_output_length; |
| 4389 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4390 | status = psa_cipher_finish(&operation, |
| 4391 | output_multi + output_length, |
| 4392 | output_buffer_size - output_length, |
| 4393 | &function_output_length); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4394 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4395 | TEST_EQUAL(status, expected_status); |
| 4396 | } else { |
| 4397 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4398 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4399 | } else { |
| 4400 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4401 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4402 | } else { |
| 4403 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4404 | } |
| 4405 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4406 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4407 | psa_cipher_abort(&operation); |
| 4408 | mbedtls_free(input); |
| 4409 | mbedtls_free(output); |
| 4410 | mbedtls_free(output_multi); |
| 4411 | psa_destroy_key(key); |
| 4412 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4413 | } |
| 4414 | /* END_CASE */ |
| 4415 | |
| 4416 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4417 | void cipher_decrypt(int alg_arg, |
| 4418 | int key_type_arg, |
| 4419 | data_t *key_data, |
| 4420 | data_t *iv, |
| 4421 | data_t *input_arg, |
| 4422 | data_t *expected_output) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4423 | { |
| 4424 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4425 | psa_key_type_t key_type = key_type_arg; |
| 4426 | psa_algorithm_t alg = alg_arg; |
| 4427 | unsigned char *input = NULL; |
| 4428 | size_t input_buffer_size = 0; |
| 4429 | unsigned char *output = NULL; |
| 4430 | size_t output_buffer_size = 0; |
| 4431 | size_t output_length = 0; |
| 4432 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4433 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4434 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4435 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4436 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4437 | psa_set_key_algorithm(&attributes, alg); |
| 4438 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4439 | |
| 4440 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4441 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4442 | if (input_buffer_size > 0) { |
| 4443 | ASSERT_ALLOC(input, input_buffer_size); |
| 4444 | memcpy(input, iv->x, iv->len); |
| 4445 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4446 | } |
| 4447 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4448 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size); |
| 4449 | ASSERT_ALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4450 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4451 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4452 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4453 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4454 | PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4455 | output_buffer_size, &output_length)); |
| 4456 | TEST_LE_U(output_length, |
| 4457 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size)); |
| 4458 | TEST_LE_U(output_length, |
| 4459 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_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 | ASSERT_COMPARE(expected_output->x, expected_output->len, |
| 4462 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4463 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4464 | mbedtls_free(input); |
| 4465 | mbedtls_free(output); |
| 4466 | psa_destroy_key(key); |
| 4467 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4468 | } |
| 4469 | /* END_CASE */ |
| 4470 | |
| 4471 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4472 | void cipher_verify_output(int alg_arg, |
| 4473 | int key_type_arg, |
| 4474 | data_t *key_data, |
| 4475 | data_t *input) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4476 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4477 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4478 | psa_key_type_t key_type = key_type_arg; |
| 4479 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4480 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4481 | size_t output1_size = 0; |
| 4482 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4483 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4484 | size_t output2_size = 0; |
| 4485 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4486 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4487 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4488 | PSA_ASSERT(psa_crypto_init()); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4489 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4490 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4491 | psa_set_key_algorithm(&attributes, alg); |
| 4492 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4493 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4494 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4495 | &key)); |
| 4496 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4497 | ASSERT_ALLOC(output1, output1_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4498 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4499 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, |
| 4500 | output1, output1_size, |
| 4501 | &output1_length)); |
| 4502 | TEST_LE_U(output1_length, |
| 4503 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4504 | TEST_LE_U(output1_length, |
| 4505 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4506 | |
| 4507 | output2_size = output1_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4508 | ASSERT_ALLOC(output2, output2_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4509 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4510 | PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length, |
| 4511 | output2, output2_size, |
| 4512 | &output2_length)); |
| 4513 | TEST_LE_U(output2_length, |
| 4514 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4515 | TEST_LE_U(output2_length, |
| 4516 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4517 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4518 | ASSERT_COMPARE(input->x, input->len, output2, output2_length); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4519 | |
| 4520 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4521 | mbedtls_free(output1); |
| 4522 | mbedtls_free(output2); |
| 4523 | psa_destroy_key(key); |
| 4524 | PSA_DONE(); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4525 | } |
| 4526 | /* END_CASE */ |
| 4527 | |
| 4528 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4529 | void cipher_verify_output_multipart(int alg_arg, |
| 4530 | int key_type_arg, |
| 4531 | data_t *key_data, |
| 4532 | data_t *input, |
| 4533 | int first_part_size_arg) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4534 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4535 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4536 | psa_key_type_t key_type = key_type_arg; |
| 4537 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4538 | size_t first_part_size = first_part_size_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4539 | unsigned char iv[16] = { 0 }; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4540 | size_t iv_size = 16; |
| 4541 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4542 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4543 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4544 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4545 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4546 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4547 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4548 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4549 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 4550 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4551 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4552 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4553 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4554 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4555 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4556 | psa_set_key_algorithm(&attributes, alg); |
| 4557 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4558 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4559 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4560 | &key)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4562 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg)); |
| 4563 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg)); |
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 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 4566 | PSA_ASSERT(psa_cipher_generate_iv(&operation1, |
| 4567 | iv, iv_size, |
| 4568 | &iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4569 | } |
| 4570 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4571 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4572 | TEST_LE_U(output1_buffer_size, |
| 4573 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
| 4574 | ASSERT_ALLOC(output1, output1_buffer_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4575 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4576 | TEST_LE_U(first_part_size, input->len); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4577 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4578 | PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size, |
| 4579 | output1, output1_buffer_size, |
| 4580 | &function_output_length)); |
| 4581 | TEST_LE_U(function_output_length, |
| 4582 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4583 | TEST_LE_U(function_output_length, |
| 4584 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4585 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4586 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4587 | PSA_ASSERT(psa_cipher_update(&operation1, |
| 4588 | input->x + first_part_size, |
| 4589 | input->len - first_part_size, |
| 4590 | output1, output1_buffer_size, |
| 4591 | &function_output_length)); |
| 4592 | TEST_LE_U(function_output_length, |
| 4593 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4594 | alg, |
| 4595 | input->len - first_part_size)); |
| 4596 | TEST_LE_U(function_output_length, |
| 4597 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4598 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4599 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4600 | PSA_ASSERT(psa_cipher_finish(&operation1, |
| 4601 | output1 + output1_length, |
| 4602 | output1_buffer_size - output1_length, |
| 4603 | &function_output_length)); |
| 4604 | TEST_LE_U(function_output_length, |
| 4605 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4606 | TEST_LE_U(function_output_length, |
| 4607 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4608 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4609 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4610 | PSA_ASSERT(psa_cipher_abort(&operation1)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4611 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4612 | output2_buffer_size = output1_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4613 | TEST_LE_U(output2_buffer_size, |
| 4614 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4615 | TEST_LE_U(output2_buffer_size, |
| 4616 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
| 4617 | ASSERT_ALLOC(output2, output2_buffer_size); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4618 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4619 | if (iv_length > 0) { |
| 4620 | PSA_ASSERT(psa_cipher_set_iv(&operation2, |
| 4621 | iv, iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4622 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4623 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4624 | PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size, |
| 4625 | output2, output2_buffer_size, |
| 4626 | &function_output_length)); |
| 4627 | TEST_LE_U(function_output_length, |
| 4628 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4629 | TEST_LE_U(function_output_length, |
| 4630 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4631 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4632 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4633 | PSA_ASSERT(psa_cipher_update(&operation2, |
| 4634 | output1 + first_part_size, |
| 4635 | output1_length - first_part_size, |
| 4636 | output2, output2_buffer_size, |
| 4637 | &function_output_length)); |
| 4638 | TEST_LE_U(function_output_length, |
| 4639 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4640 | alg, |
| 4641 | output1_length - first_part_size)); |
| 4642 | TEST_LE_U(function_output_length, |
| 4643 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4644 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4645 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4646 | PSA_ASSERT(psa_cipher_finish(&operation2, |
| 4647 | output2 + output2_length, |
| 4648 | output2_buffer_size - output2_length, |
| 4649 | &function_output_length)); |
| 4650 | TEST_LE_U(function_output_length, |
| 4651 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4652 | TEST_LE_U(function_output_length, |
| 4653 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4654 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4655 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4656 | PSA_ASSERT(psa_cipher_abort(&operation2)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4657 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4658 | ASSERT_COMPARE(input->x, input->len, output2, output2_length); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4659 | |
| 4660 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4661 | psa_cipher_abort(&operation1); |
| 4662 | psa_cipher_abort(&operation2); |
| 4663 | mbedtls_free(output1); |
| 4664 | mbedtls_free(output2); |
| 4665 | psa_destroy_key(key); |
| 4666 | PSA_DONE(); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4667 | } |
| 4668 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 4669 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4670 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4671 | void aead_encrypt_decrypt(int key_type_arg, data_t *key_data, |
| 4672 | int alg_arg, |
| 4673 | data_t *nonce, |
| 4674 | data_t *additional_data, |
| 4675 | data_t *input_data, |
| 4676 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4677 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4678 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4679 | psa_key_type_t key_type = key_type_arg; |
| 4680 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4681 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4682 | unsigned char *output_data = NULL; |
| 4683 | size_t output_size = 0; |
| 4684 | size_t output_length = 0; |
| 4685 | unsigned char *output_data2 = NULL; |
| 4686 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4687 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4688 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4689 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4690 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4691 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4692 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4693 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4694 | psa_set_key_algorithm(&attributes, alg); |
| 4695 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4696 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4697 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4698 | &key)); |
| 4699 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4700 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4701 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4702 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4703 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4704 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4705 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4706 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4707 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
| 4708 | TEST_EQUAL(output_size, |
| 4709 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4710 | TEST_LE_U(output_size, |
| 4711 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4712 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4713 | ASSERT_ALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4714 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4715 | status = psa_aead_encrypt(key, alg, |
| 4716 | nonce->x, nonce->len, |
| 4717 | additional_data->x, |
| 4718 | additional_data->len, |
| 4719 | input_data->x, input_data->len, |
| 4720 | output_data, output_size, |
| 4721 | &output_length); |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4722 | |
| 4723 | /* If the operation is not supported, just skip and not fail in case the |
| 4724 | * encryption involves a common limitation of cryptography hardwares and |
| 4725 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4726 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4727 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4728 | 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] | 4729 | } |
| 4730 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4731 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4732 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4733 | if (PSA_SUCCESS == expected_result) { |
| 4734 | ASSERT_ALLOC(output_data2, output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4735 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4736 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4737 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4738 | TEST_EQUAL(input_data->len, |
| 4739 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length)); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4740 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4741 | TEST_LE_U(input_data->len, |
| 4742 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length)); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4743 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4744 | TEST_EQUAL(psa_aead_decrypt(key, alg, |
| 4745 | nonce->x, nonce->len, |
| 4746 | additional_data->x, |
| 4747 | additional_data->len, |
| 4748 | output_data, output_length, |
| 4749 | output_data2, output_length, |
| 4750 | &output_length2), |
| 4751 | expected_result); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4752 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4753 | ASSERT_COMPARE(input_data->x, input_data->len, |
| 4754 | output_data2, output_length2); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4755 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4756 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4757 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4758 | psa_destroy_key(key); |
| 4759 | mbedtls_free(output_data); |
| 4760 | mbedtls_free(output_data2); |
| 4761 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4762 | } |
| 4763 | /* END_CASE */ |
| 4764 | |
| 4765 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4766 | void aead_encrypt(int key_type_arg, data_t *key_data, |
| 4767 | int alg_arg, |
| 4768 | data_t *nonce, |
| 4769 | data_t *additional_data, |
| 4770 | data_t *input_data, |
| 4771 | data_t *expected_result) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4772 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4773 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4774 | psa_key_type_t key_type = key_type_arg; |
| 4775 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4776 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4777 | unsigned char *output_data = NULL; |
| 4778 | size_t output_size = 0; |
| 4779 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4780 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4781 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4782 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4783 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4784 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4785 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4786 | psa_set_key_algorithm(&attributes, alg); |
| 4787 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4788 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4789 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4790 | &key)); |
| 4791 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4792 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4793 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4794 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4795 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4796 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4797 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4798 | TEST_EQUAL(output_size, |
| 4799 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4800 | TEST_LE_U(output_size, |
| 4801 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
| 4802 | ASSERT_ALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4803 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4804 | status = psa_aead_encrypt(key, alg, |
| 4805 | nonce->x, nonce->len, |
| 4806 | additional_data->x, additional_data->len, |
| 4807 | input_data->x, input_data->len, |
| 4808 | output_data, output_size, |
| 4809 | &output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4810 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4811 | /* If the operation is not supported, just skip and not fail in case the |
| 4812 | * encryption involves a common limitation of cryptography hardwares and |
| 4813 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4814 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4815 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4816 | 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] | 4817 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4818 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4819 | PSA_ASSERT(status); |
| 4820 | ASSERT_COMPARE(expected_result->x, expected_result->len, |
| 4821 | output_data, output_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4822 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4823 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4824 | psa_destroy_key(key); |
| 4825 | mbedtls_free(output_data); |
| 4826 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4827 | } |
| 4828 | /* END_CASE */ |
| 4829 | |
| 4830 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4831 | void aead_decrypt(int key_type_arg, data_t *key_data, |
| 4832 | int alg_arg, |
| 4833 | data_t *nonce, |
| 4834 | data_t *additional_data, |
| 4835 | data_t *input_data, |
| 4836 | data_t *expected_data, |
| 4837 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4838 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4839 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4840 | psa_key_type_t key_type = key_type_arg; |
| 4841 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4842 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4843 | unsigned char *output_data = NULL; |
| 4844 | size_t output_size = 0; |
| 4845 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4846 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4847 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4848 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4849 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4850 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4851 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4852 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4853 | psa_set_key_algorithm(&attributes, alg); |
| 4854 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4855 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4856 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4857 | &key)); |
| 4858 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 4859 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4860 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4861 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 4862 | alg); |
| 4863 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4864 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4865 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4866 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4867 | TEST_EQUAL(output_size, |
| 4868 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 4869 | TEST_LE_U(output_size, |
| 4870 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4871 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4872 | ASSERT_ALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4873 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4874 | status = psa_aead_decrypt(key, alg, |
| 4875 | nonce->x, nonce->len, |
| 4876 | additional_data->x, |
| 4877 | additional_data->len, |
| 4878 | input_data->x, input_data->len, |
| 4879 | output_data, output_size, |
| 4880 | &output_length); |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4881 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4882 | /* If the operation is not supported, just skip and not fail in case the |
| 4883 | * decryption involves a common limitation of cryptography hardwares and |
| 4884 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4885 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 4886 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 4887 | 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] | 4888 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4889 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4890 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4891 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4892 | if (expected_result == PSA_SUCCESS) { |
| 4893 | ASSERT_COMPARE(expected_data->x, expected_data->len, |
| 4894 | output_data, output_length); |
| 4895 | } |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4896 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4897 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4898 | psa_destroy_key(key); |
| 4899 | mbedtls_free(output_data); |
| 4900 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4901 | } |
| 4902 | /* END_CASE */ |
| 4903 | |
| 4904 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4905 | void aead_multipart_encrypt(int key_type_arg, data_t *key_data, |
| 4906 | int alg_arg, |
| 4907 | data_t *nonce, |
| 4908 | data_t *additional_data, |
| 4909 | data_t *input_data, |
| 4910 | int do_set_lengths, |
| 4911 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4912 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4913 | size_t ad_part_len = 0; |
| 4914 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4915 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4916 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4917 | for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) { |
| 4918 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4919 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4920 | if (do_set_lengths) { |
| 4921 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4922 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4923 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4924 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4925 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4926 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4927 | |
| 4928 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4929 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4930 | alg_arg, nonce, |
| 4931 | additional_data, |
| 4932 | ad_part_len, |
| 4933 | input_data, -1, |
| 4934 | set_lengths_method, |
| 4935 | expected_output, |
| 4936 | 1, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4937 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4938 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4939 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4940 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 4941 | mbedtls_test_set_step(1000 + ad_part_len); |
| 4942 | |
| 4943 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4944 | alg_arg, nonce, |
| 4945 | additional_data, |
| 4946 | ad_part_len, |
| 4947 | input_data, -1, |
| 4948 | set_lengths_method, |
| 4949 | expected_output, |
| 4950 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4951 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4952 | } |
| 4953 | } |
| 4954 | |
| 4955 | for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) { |
| 4956 | /* Split data into length(data_part_len) parts. */ |
| 4957 | mbedtls_test_set_step(2000 + data_part_len); |
| 4958 | |
| 4959 | if (do_set_lengths) { |
| 4960 | if (data_part_len & 0x01) { |
| 4961 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4962 | } else { |
| 4963 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
| 4964 | } |
| 4965 | } |
| 4966 | |
| 4967 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4968 | alg_arg, nonce, |
| 4969 | additional_data, -1, |
| 4970 | input_data, data_part_len, |
| 4971 | set_lengths_method, |
| 4972 | expected_output, |
| 4973 | 1, 0)) { |
| 4974 | break; |
| 4975 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4976 | |
| 4977 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4978 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4979 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4980 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 4981 | alg_arg, nonce, |
| 4982 | additional_data, -1, |
| 4983 | input_data, data_part_len, |
| 4984 | set_lengths_method, |
| 4985 | expected_output, |
| 4986 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4987 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4988 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4989 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4990 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 4991 | /* Goto is required to silence warnings about unused labels, as we |
| 4992 | * don't actually do any test assertions in this function. */ |
| 4993 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4994 | } |
| 4995 | /* END_CASE */ |
| 4996 | |
| 4997 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4998 | void aead_multipart_decrypt(int key_type_arg, data_t *key_data, |
| 4999 | int alg_arg, |
| 5000 | data_t *nonce, |
| 5001 | data_t *additional_data, |
| 5002 | data_t *input_data, |
| 5003 | int do_set_lengths, |
| 5004 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5005 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5006 | size_t ad_part_len = 0; |
| 5007 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5008 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5009 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5010 | 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] | 5011 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5012 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5013 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5014 | if (do_set_lengths) { |
| 5015 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5016 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5017 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5018 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5019 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5020 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5021 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5022 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5023 | alg_arg, nonce, |
| 5024 | additional_data, |
| 5025 | ad_part_len, |
| 5026 | input_data, -1, |
| 5027 | set_lengths_method, |
| 5028 | expected_output, |
| 5029 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5030 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5031 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5032 | |
| 5033 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5034 | mbedtls_test_set_step(1000 + ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5035 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5036 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5037 | alg_arg, nonce, |
| 5038 | additional_data, |
| 5039 | ad_part_len, |
| 5040 | input_data, -1, |
| 5041 | set_lengths_method, |
| 5042 | expected_output, |
| 5043 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5044 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5045 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5046 | } |
| 5047 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5048 | 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] | 5049 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5050 | mbedtls_test_set_step(2000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5051 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5052 | if (do_set_lengths) { |
| 5053 | if (data_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5054 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5055 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5056 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5057 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5058 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5059 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5060 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5061 | alg_arg, nonce, |
| 5062 | additional_data, -1, |
| 5063 | input_data, data_part_len, |
| 5064 | set_lengths_method, |
| 5065 | expected_output, |
| 5066 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5067 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5068 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5069 | |
| 5070 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5071 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5072 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5073 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5074 | alg_arg, nonce, |
| 5075 | additional_data, -1, |
| 5076 | input_data, data_part_len, |
| 5077 | set_lengths_method, |
| 5078 | expected_output, |
| 5079 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5080 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5081 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5082 | } |
| 5083 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5084 | /* Goto is required to silence warnings about unused labels, as we |
| 5085 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5086 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5087 | } |
| 5088 | /* END_CASE */ |
| 5089 | |
| 5090 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5091 | void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data, |
| 5092 | int alg_arg, |
| 5093 | int nonce_length, |
| 5094 | int expected_nonce_length_arg, |
| 5095 | data_t *additional_data, |
| 5096 | data_t *input_data, |
| 5097 | int expected_status_arg) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5098 | { |
| 5099 | |
| 5100 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5101 | psa_key_type_t key_type = key_type_arg; |
| 5102 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5103 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5104 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5105 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5106 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5107 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5108 | size_t actual_nonce_length = 0; |
| 5109 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 5110 | unsigned char *output = NULL; |
| 5111 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5112 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5113 | size_t ciphertext_size = 0; |
| 5114 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5115 | size_t tag_length = 0; |
| 5116 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5117 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5118 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5119 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5120 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5121 | psa_set_key_algorithm(&attributes, alg); |
| 5122 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5123 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5124 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5125 | &key)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5126 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5127 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5128 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5129 | 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] | 5130 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5131 | ASSERT_ALLOC(output, output_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5132 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5133 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5135 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5136 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5137 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5138 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5139 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5140 | |
| 5141 | /* If the operation is not supported, just skip and not fail in case the |
| 5142 | * encryption involves a common limitation of cryptography hardwares and |
| 5143 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5144 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5145 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5146 | 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] | 5147 | } |
| 5148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5149 | PSA_ASSERT(status); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5151 | status = psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5152 | nonce_length, |
| 5153 | &actual_nonce_length); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5154 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5155 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5156 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5157 | TEST_EQUAL(actual_nonce_length, expected_nonce_length); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 5158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5159 | if (expected_status == PSA_SUCCESS) { |
| 5160 | TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type, |
| 5161 | alg)); |
| 5162 | } |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 5163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5164 | TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 5165 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5166 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5167 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5168 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5169 | input_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5170 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5171 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5172 | additional_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5173 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5174 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5175 | output, output_size, |
| 5176 | &ciphertext_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5177 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5178 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5179 | &ciphertext_length, tag_buffer, |
| 5180 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5181 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5182 | |
| 5183 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5184 | psa_destroy_key(key); |
| 5185 | mbedtls_free(output); |
| 5186 | mbedtls_free(ciphertext); |
| 5187 | psa_aead_abort(&operation); |
| 5188 | PSA_DONE(); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5189 | } |
| 5190 | /* END_CASE */ |
| 5191 | |
| 5192 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5193 | void aead_multipart_set_nonce(int key_type_arg, data_t *key_data, |
| 5194 | int alg_arg, |
| 5195 | int nonce_length_arg, |
| 5196 | int set_lengths_method_arg, |
| 5197 | data_t *additional_data, |
| 5198 | data_t *input_data, |
| 5199 | int expected_status_arg) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5200 | { |
| 5201 | |
| 5202 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5203 | psa_key_type_t key_type = key_type_arg; |
| 5204 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5205 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5206 | uint8_t *nonce_buffer = NULL; |
| 5207 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5208 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5209 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5210 | unsigned char *output = NULL; |
| 5211 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5212 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5213 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5214 | size_t ciphertext_size = 0; |
| 5215 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5216 | size_t tag_length = 0; |
| 5217 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5218 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5219 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5221 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5222 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5223 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5224 | psa_set_key_algorithm(&attributes, alg); |
| 5225 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5226 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5227 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5228 | &key)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5230 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5231 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5232 | 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] | 5233 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5234 | ASSERT_ALLOC(output, output_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5235 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5236 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5237 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5238 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
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 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
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 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5243 | |
| 5244 | /* If the operation is not supported, just skip and not fail in case the |
| 5245 | * encryption involves a common limitation of cryptography hardwares and |
| 5246 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5247 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5248 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5249 | 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] | 5250 | } |
| 5251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5252 | PSA_ASSERT(status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5253 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5254 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5255 | if (nonce_length_arg == -1) { |
| 5256 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
| 5257 | ASSERT_ALLOC(nonce_buffer, 4); |
| 5258 | nonce_length = 0; |
| 5259 | } else { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5260 | /* If length is zero, then this will return NULL. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5261 | nonce_length = (size_t) nonce_length_arg; |
| 5262 | ASSERT_ALLOC(nonce_buffer, nonce_length); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5263 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5264 | if (nonce_buffer) { |
| 5265 | for (index = 0; index < nonce_length - 1; ++index) { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5266 | nonce_buffer[index] = 'a' + index; |
| 5267 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5268 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5269 | } |
| 5270 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5271 | if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { |
| 5272 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5273 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5274 | } |
| 5275 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5276 | status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5277 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5278 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5279 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5280 | if (expected_status == PSA_SUCCESS) { |
| 5281 | if (set_lengths_method == SET_LENGTHS_AFTER_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 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5285 | 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] | 5286 | expected_status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5287 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5288 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5289 | /* 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] | 5290 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5291 | additional_data->len), |
| 5292 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5293 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5294 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5295 | output, output_size, |
| 5296 | &ciphertext_length), |
| 5297 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5298 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5299 | TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5300 | &ciphertext_length, tag_buffer, |
| 5301 | PSA_AEAD_TAG_MAX_SIZE, &tag_length), |
| 5302 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5303 | } |
| 5304 | |
| 5305 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5306 | psa_destroy_key(key); |
| 5307 | mbedtls_free(output); |
| 5308 | mbedtls_free(ciphertext); |
| 5309 | mbedtls_free(nonce_buffer); |
| 5310 | psa_aead_abort(&operation); |
| 5311 | PSA_DONE(); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5312 | } |
| 5313 | /* END_CASE */ |
| 5314 | |
| 5315 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5316 | 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] | 5317 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5318 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5319 | data_t *nonce, |
| 5320 | data_t *additional_data, |
| 5321 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5322 | int expected_status_arg) |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5323 | { |
| 5324 | |
| 5325 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5326 | psa_key_type_t key_type = key_type_arg; |
| 5327 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5328 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5329 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5330 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5331 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5332 | unsigned char *output = NULL; |
| 5333 | unsigned char *ciphertext = NULL; |
| 5334 | size_t output_size = output_size_arg; |
| 5335 | size_t ciphertext_size = 0; |
| 5336 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5337 | size_t tag_length = 0; |
| 5338 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5339 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5340 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5341 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5342 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5343 | psa_set_key_algorithm(&attributes, alg); |
| 5344 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5345 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5346 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5347 | &key)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5349 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5350 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5351 | ASSERT_ALLOC(output, output_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5353 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5354 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5355 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5356 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5357 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5358 | |
| 5359 | /* If the operation is not supported, just skip and not fail in case the |
| 5360 | * encryption involves a common limitation of cryptography hardwares and |
| 5361 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5362 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5363 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5364 | 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] | 5365 | } |
| 5366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5367 | PSA_ASSERT(status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5368 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5369 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5370 | input_data->len)); |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5371 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5372 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5374 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5375 | additional_data->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5377 | status = psa_aead_update(&operation, input_data->x, input_data->len, |
| 5378 | output, output_size, &ciphertext_length); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5379 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5380 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5381 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5382 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5383 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5384 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5385 | &ciphertext_length, tag_buffer, |
| 5386 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5387 | } |
| 5388 | |
| 5389 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5390 | psa_destroy_key(key); |
| 5391 | mbedtls_free(output); |
| 5392 | mbedtls_free(ciphertext); |
| 5393 | psa_aead_abort(&operation); |
| 5394 | PSA_DONE(); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5395 | } |
| 5396 | /* END_CASE */ |
| 5397 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5398 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5399 | void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data, |
| 5400 | int alg_arg, |
| 5401 | int finish_ciphertext_size_arg, |
| 5402 | int tag_size_arg, |
| 5403 | data_t *nonce, |
| 5404 | data_t *additional_data, |
| 5405 | data_t *input_data, |
| 5406 | int expected_status_arg) |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5407 | { |
| 5408 | |
| 5409 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5410 | psa_key_type_t key_type = key_type_arg; |
| 5411 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5412 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5413 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5414 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5415 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5416 | unsigned char *ciphertext = NULL; |
| 5417 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5418 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5419 | size_t ciphertext_size = 0; |
| 5420 | size_t ciphertext_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5421 | size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg; |
| 5422 | size_t tag_size = (size_t) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5423 | size_t tag_length = 0; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5424 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5425 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5426 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5427 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5428 | psa_set_key_algorithm(&attributes, alg); |
| 5429 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5430 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5431 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5432 | &key)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5433 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5434 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5435 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5436 | 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] | 5437 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5438 | ASSERT_ALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5439 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5440 | ASSERT_ALLOC(finish_ciphertext, finish_ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5441 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5442 | ASSERT_ALLOC(tag_buffer, tag_size); |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5444 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5445 | |
| 5446 | /* If the operation is not supported, just skip and not fail in case the |
| 5447 | * encryption involves a common limitation of cryptography hardwares and |
| 5448 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5449 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5450 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5451 | 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] | 5452 | } |
| 5453 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5454 | PSA_ASSERT(status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5455 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5456 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5457 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5458 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5459 | input_data->len)); |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5460 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5461 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5462 | additional_data->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5463 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5464 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5465 | ciphertext, ciphertext_size, &ciphertext_length)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5466 | |
| 5467 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5468 | status = psa_aead_finish(&operation, finish_ciphertext, |
| 5469 | finish_ciphertext_size, |
| 5470 | &ciphertext_length, tag_buffer, |
| 5471 | tag_size, &tag_length); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5472 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5473 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5474 | |
| 5475 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5476 | psa_destroy_key(key); |
| 5477 | mbedtls_free(ciphertext); |
| 5478 | mbedtls_free(finish_ciphertext); |
| 5479 | mbedtls_free(tag_buffer); |
| 5480 | psa_aead_abort(&operation); |
| 5481 | PSA_DONE(); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5482 | } |
| 5483 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5484 | |
| 5485 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5486 | void aead_multipart_verify(int key_type_arg, data_t *key_data, |
| 5487 | int alg_arg, |
| 5488 | data_t *nonce, |
| 5489 | data_t *additional_data, |
| 5490 | data_t *input_data, |
| 5491 | data_t *tag, |
| 5492 | int tag_usage_arg, |
| 5493 | int expected_setup_status_arg, |
| 5494 | int expected_status_arg) |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5495 | { |
| 5496 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5497 | psa_key_type_t key_type = key_type_arg; |
| 5498 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5499 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5500 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5501 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5502 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5503 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5504 | unsigned char *plaintext = NULL; |
| 5505 | unsigned char *finish_plaintext = NULL; |
| 5506 | size_t plaintext_size = 0; |
| 5507 | size_t plaintext_length = 0; |
| 5508 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5509 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5510 | unsigned char *tag_buffer = NULL; |
| 5511 | size_t tag_size = 0; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5512 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5513 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5514 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5515 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 5516 | psa_set_key_algorithm(&attributes, alg); |
| 5517 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5518 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5519 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5520 | &key)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5521 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5522 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5523 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5524 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 5525 | input_data->len); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5526 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5527 | ASSERT_ALLOC(plaintext, plaintext_size); |
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 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5530 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5531 | ASSERT_ALLOC(finish_plaintext, verify_plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5532 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5533 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5534 | |
| 5535 | /* If the operation is not supported, just skip and not fail in case the |
| 5536 | * encryption involves a common limitation of cryptography hardwares and |
| 5537 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5538 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5539 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5540 | 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] | 5541 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5542 | TEST_EQUAL(status, expected_setup_status); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5543 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5544 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5545 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5546 | } |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5547 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5548 | PSA_ASSERT(status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5549 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5550 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, 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 | status = psa_aead_set_lengths(&operation, additional_data->len, |
| 5553 | input_data->len); |
| 5554 | PSA_ASSERT(status); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5555 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5556 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5557 | additional_data->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5558 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5559 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 5560 | input_data->len, |
| 5561 | plaintext, plaintext_size, |
| 5562 | &plaintext_length)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5563 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5564 | if (tag_usage == USE_GIVEN_TAG) { |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5565 | tag_buffer = tag->x; |
| 5566 | tag_size = tag->len; |
| 5567 | } |
| 5568 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5569 | status = psa_aead_verify(&operation, finish_plaintext, |
| 5570 | verify_plaintext_size, |
| 5571 | &plaintext_length, |
| 5572 | tag_buffer, tag_size); |
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 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5575 | |
| 5576 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5577 | psa_destroy_key(key); |
| 5578 | mbedtls_free(plaintext); |
| 5579 | mbedtls_free(finish_plaintext); |
| 5580 | psa_aead_abort(&operation); |
| 5581 | PSA_DONE(); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5582 | } |
| 5583 | /* END_CASE */ |
| 5584 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5585 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5586 | void aead_multipart_setup(int key_type_arg, data_t *key_data, |
| 5587 | int alg_arg, int expected_status_arg) |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5588 | { |
| 5589 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5590 | psa_key_type_t key_type = key_type_arg; |
| 5591 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5592 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5593 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5594 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5595 | psa_status_t expected_status = expected_status_arg; |
| 5596 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5597 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5598 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5599 | psa_set_key_usage_flags(&attributes, |
| 5600 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5601 | psa_set_key_algorithm(&attributes, alg); |
| 5602 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5603 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5604 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5605 | &key)); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5606 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5607 | status = psa_aead_encrypt_setup(&operation, key, alg); |
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 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5610 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5611 | psa_aead_abort(&operation); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5612 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5613 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5614 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5615 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5616 | |
| 5617 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5618 | psa_destroy_key(key); |
| 5619 | psa_aead_abort(&operation); |
| 5620 | PSA_DONE(); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5621 | } |
| 5622 | /* END_CASE */ |
| 5623 | |
| 5624 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5625 | void aead_multipart_state_test(int key_type_arg, data_t *key_data, |
| 5626 | int alg_arg, |
| 5627 | data_t *nonce, |
| 5628 | data_t *additional_data, |
| 5629 | data_t *input_data) |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5630 | { |
| 5631 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5632 | psa_key_type_t key_type = key_type_arg; |
| 5633 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5634 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5635 | unsigned char *output_data = NULL; |
| 5636 | unsigned char *final_data = NULL; |
| 5637 | size_t output_size = 0; |
| 5638 | size_t finish_output_size = 0; |
| 5639 | size_t output_length = 0; |
| 5640 | size_t key_bits = 0; |
| 5641 | size_t tag_length = 0; |
| 5642 | size_t tag_size = 0; |
| 5643 | size_t nonce_length = 0; |
| 5644 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5645 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5646 | size_t output_part_length = 0; |
| 5647 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5648 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5649 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5650 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5651 | psa_set_key_usage_flags(&attributes, |
| 5652 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5653 | psa_set_key_algorithm(&attributes, alg); |
| 5654 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5655 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5656 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5657 | &key)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5658 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5659 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5660 | key_bits = psa_get_key_bits(&attributes); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5661 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5662 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5663 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5664 | TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE); |
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 | 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] | 5667 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5668 | ASSERT_ALLOC(output_data, output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5669 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5670 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
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 | TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
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 | ASSERT_ALLOC(final_data, finish_output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5675 | |
| 5676 | /* Test all operations error without calling setup first. */ |
| 5677 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5678 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5679 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5680 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5681 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5682 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5683 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5684 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5685 | &nonce_length), |
| 5686 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5687 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5688 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5689 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5690 | /* ------------------------------------------------------- */ |
| 5691 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5692 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 5693 | input_data->len), |
| 5694 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5695 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5696 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5697 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5698 | /* ------------------------------------------------------- */ |
| 5699 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5700 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5701 | additional_data->len), |
| 5702 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5703 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5704 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5705 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5706 | /* ------------------------------------------------------- */ |
| 5707 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5708 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 5709 | input_data->len, output_data, |
| 5710 | output_size, &output_length), |
| 5711 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5712 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5713 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5714 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5715 | /* ------------------------------------------------------- */ |
| 5716 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5717 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 5718 | finish_output_size, |
| 5719 | &output_part_length, |
| 5720 | tag_buffer, tag_length, |
| 5721 | &tag_size), |
| 5722 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5723 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5724 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5725 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5726 | /* ------------------------------------------------------- */ |
| 5727 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5728 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 5729 | finish_output_size, |
| 5730 | &output_part_length, |
| 5731 | tag_buffer, |
| 5732 | tag_length), |
| 5733 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5734 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5735 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5736 | |
| 5737 | /* Test for double setups. */ |
| 5738 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5739 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5740 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5741 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 5742 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5743 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5744 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5745 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5746 | /* ------------------------------------------------------- */ |
| 5747 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5748 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5749 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5750 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 5751 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5752 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5753 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5754 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5755 | /* ------------------------------------------------------- */ |
| 5756 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5757 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5758 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5759 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 5760 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5761 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5762 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5763 | |
| 5764 | /* ------------------------------------------------------- */ |
| 5765 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5766 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5767 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5768 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 5769 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5771 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5772 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5773 | /* Test for not setting a nonce. */ |
| 5774 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5775 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5776 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5777 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5778 | additional_data->len), |
| 5779 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5780 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5781 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5782 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5783 | /* ------------------------------------------------------- */ |
| 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 | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5786 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5787 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 5788 | input_data->len, output_data, |
| 5789 | output_size, &output_length), |
| 5790 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5791 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5792 | psa_aead_abort(&operation); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5793 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5794 | /* ------------------------------------------------------- */ |
| 5795 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5796 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5797 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5798 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 5799 | finish_output_size, |
| 5800 | &output_part_length, |
| 5801 | tag_buffer, tag_length, |
| 5802 | &tag_size), |
| 5803 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5804 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5805 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5806 | |
| 5807 | /* ------------------------------------------------------- */ |
| 5808 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5809 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5810 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5811 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 5812 | finish_output_size, |
| 5813 | &output_part_length, |
| 5814 | tag_buffer, |
| 5815 | tag_length), |
| 5816 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5817 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5818 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5819 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5820 | /* Test for double setting nonce. */ |
| 5821 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5822 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5823 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5824 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5825 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5826 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5827 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5828 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5829 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5830 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5831 | /* Test for double generating nonce. */ |
| 5832 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5833 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5834 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5835 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5836 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5837 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5838 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5839 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5840 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5841 | &nonce_length), |
| 5842 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5843 | |
| 5844 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5845 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5846 | |
| 5847 | /* Test for generate nonce then set and vice versa */ |
| 5848 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5849 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5850 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5851 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5852 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5853 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5854 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5855 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5856 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5857 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5858 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5859 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5860 | /* Test for generating nonce after calling set lengths */ |
| 5861 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5862 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5863 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5864 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5865 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5866 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5867 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5868 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5869 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5870 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5871 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5872 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5873 | /* 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] | 5874 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5875 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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 | if (operation.alg == PSA_ALG_CCM) { |
| 5878 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5879 | input_data->len), |
| 5880 | PSA_ERROR_INVALID_ARGUMENT); |
| 5881 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5882 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5883 | &nonce_length), |
| 5884 | PSA_ERROR_BAD_STATE); |
| 5885 | } else { |
| 5886 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5887 | input_data->len)); |
| 5888 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5889 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5890 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5891 | } |
| 5892 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5893 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5894 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5895 | /* 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] | 5896 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5897 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5898 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5899 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 5900 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5901 | input_data->len), |
| 5902 | PSA_ERROR_INVALID_ARGUMENT); |
| 5903 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5904 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5905 | &nonce_length), |
| 5906 | PSA_ERROR_BAD_STATE); |
| 5907 | } else { |
| 5908 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5909 | input_data->len)); |
| 5910 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5911 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5912 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5913 | } |
| 5914 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5915 | psa_aead_abort(&operation); |
Dave Rodgman | d26d744 | 2023-02-11 17:14:54 +0000 | [diff] [blame] | 5916 | #endif |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5917 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5918 | /* 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] | 5919 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5920 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5921 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5922 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5923 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5924 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5925 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5926 | if (operation.alg == PSA_ALG_CCM) { |
| 5927 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5928 | input_data->len), |
| 5929 | PSA_ERROR_INVALID_ARGUMENT); |
| 5930 | } else { |
| 5931 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5932 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5933 | } |
| 5934 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5935 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5936 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5937 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5938 | /* Test for setting nonce after calling set lengths */ |
| 5939 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5940 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5941 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5942 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5943 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5944 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5945 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5946 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5947 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5948 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5949 | /* 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] | 5950 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5951 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5952 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5953 | if (operation.alg == PSA_ALG_CCM) { |
| 5954 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5955 | input_data->len), |
| 5956 | PSA_ERROR_INVALID_ARGUMENT); |
| 5957 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5958 | PSA_ERROR_BAD_STATE); |
| 5959 | } else { |
| 5960 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5961 | input_data->len)); |
| 5962 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5963 | } |
| 5964 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5965 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5966 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5967 | /* 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] | 5968 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5969 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5970 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5971 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 5972 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5973 | input_data->len), |
| 5974 | PSA_ERROR_INVALID_ARGUMENT); |
| 5975 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 5976 | PSA_ERROR_BAD_STATE); |
| 5977 | } else { |
| 5978 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 5979 | input_data->len)); |
| 5980 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5981 | } |
| 5982 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5983 | psa_aead_abort(&operation); |
Dave Rodgman | a476363 | 2023-02-11 18:36:23 +0000 | [diff] [blame] | 5984 | #endif |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5985 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5986 | /* 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] | 5987 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5988 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5989 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 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 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5992 | if (operation.alg == PSA_ALG_CCM) { |
| 5993 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5994 | input_data->len), |
| 5995 | PSA_ERROR_INVALID_ARGUMENT); |
| 5996 | } else { |
| 5997 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 5998 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5999 | } |
| 6000 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6001 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6002 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6003 | /* 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] | 6004 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6005 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6006 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6007 | if (operation.alg == PSA_ALG_GCM) { |
| 6008 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6009 | SIZE_MAX), |
| 6010 | PSA_ERROR_INVALID_ARGUMENT); |
| 6011 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6012 | PSA_ERROR_BAD_STATE); |
| 6013 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6014 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6015 | SIZE_MAX)); |
| 6016 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6017 | } |
| 6018 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6019 | psa_aead_abort(&operation); |
Dave Rodgman | 91e8321 | 2023-02-11 20:07:43 +0000 | [diff] [blame] | 6020 | #endif |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6021 | |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 6022 | /* 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] | 6023 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6024 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6025 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 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 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6028 | if (operation.alg == PSA_ALG_GCM) { |
| 6029 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6030 | SIZE_MAX), |
| 6031 | PSA_ERROR_INVALID_ARGUMENT); |
| 6032 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6033 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6034 | SIZE_MAX)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6035 | } |
| 6036 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6037 | psa_aead_abort(&operation); |
Dave Rodgman | 641288b | 2023-02-11 22:02:04 +0000 | [diff] [blame] | 6038 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6039 | |
| 6040 | /* ------------------------------------------------------- */ |
| 6041 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6042 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6043 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6044 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6045 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6046 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6047 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6048 | &nonce_length), |
| 6049 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6050 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6051 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6052 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6053 | /* Test for generating nonce in decrypt setup. */ |
| 6054 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6055 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6056 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6057 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6058 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6059 | &nonce_length), |
| 6060 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6061 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6062 | psa_aead_abort(&operation); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6063 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6064 | /* Test for setting lengths twice. */ |
| 6065 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6066 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6067 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6068 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6069 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6070 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6071 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6072 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6073 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6074 | input_data->len), |
| 6075 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6076 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6077 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6078 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6079 | /* Test for setting lengths after setting nonce + already starting data. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6080 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6081 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6084 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6085 | if (operation.alg == PSA_ALG_CCM) { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6086 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6087 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6088 | additional_data->len), |
| 6089 | PSA_ERROR_BAD_STATE); |
| 6090 | } else { |
| 6091 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6092 | additional_data->len)); |
| 6093 | |
| 6094 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6095 | input_data->len), |
| 6096 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6097 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6098 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6099 | |
| 6100 | /* ------------------------------------------------------- */ |
| 6101 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6102 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6103 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6104 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6105 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6106 | if (operation.alg == PSA_ALG_CCM) { |
| 6107 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6108 | input_data->len, output_data, |
| 6109 | output_size, &output_length), |
| 6110 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6111 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6112 | } else { |
| 6113 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6114 | input_data->len, output_data, |
| 6115 | output_size, &output_length)); |
| 6116 | |
| 6117 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6118 | input_data->len), |
| 6119 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6120 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6121 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6122 | |
| 6123 | /* ------------------------------------------------------- */ |
| 6124 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6125 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6126 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6127 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6128 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6129 | if (operation.alg == PSA_ALG_CCM) { |
| 6130 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6131 | finish_output_size, |
| 6132 | &output_part_length, |
| 6133 | tag_buffer, tag_length, |
| 6134 | &tag_size)); |
| 6135 | } else { |
| 6136 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6137 | finish_output_size, |
| 6138 | &output_part_length, |
| 6139 | tag_buffer, tag_length, |
| 6140 | &tag_size)); |
| 6141 | |
| 6142 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6143 | input_data->len), |
| 6144 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6145 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6146 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6147 | |
| 6148 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 6149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6150 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6151 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6152 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6153 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6154 | &nonce_length)); |
| 6155 | if (operation.alg == PSA_ALG_CCM) { |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6156 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6157 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6158 | additional_data->len), |
| 6159 | PSA_ERROR_BAD_STATE); |
| 6160 | } else { |
| 6161 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6162 | additional_data->len)); |
| 6163 | |
| 6164 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6165 | input_data->len), |
| 6166 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6167 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6168 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6169 | |
| 6170 | /* ------------------------------------------------------- */ |
| 6171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6172 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6173 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6174 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6175 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6176 | &nonce_length)); |
| 6177 | if (operation.alg == PSA_ALG_CCM) { |
| 6178 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6179 | input_data->len, output_data, |
| 6180 | output_size, &output_length), |
| 6181 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6183 | } else { |
| 6184 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6185 | input_data->len, output_data, |
| 6186 | output_size, &output_length)); |
| 6187 | |
| 6188 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6189 | input_data->len), |
| 6190 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6191 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6192 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6193 | |
| 6194 | /* ------------------------------------------------------- */ |
| 6195 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6196 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6197 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6198 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6199 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6200 | &nonce_length)); |
| 6201 | if (operation.alg == PSA_ALG_CCM) { |
| 6202 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6203 | finish_output_size, |
| 6204 | &output_part_length, |
| 6205 | tag_buffer, tag_length, |
| 6206 | &tag_size)); |
| 6207 | } else { |
| 6208 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6209 | finish_output_size, |
| 6210 | &output_part_length, |
| 6211 | tag_buffer, tag_length, |
| 6212 | &tag_size)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6213 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6214 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6215 | input_data->len), |
| 6216 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6217 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6218 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6219 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6220 | /* Test for not sending any additional data or data after setting non zero |
| 6221 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6222 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6223 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6224 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6225 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6226 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6227 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6228 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6230 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6231 | finish_output_size, |
| 6232 | &output_part_length, |
| 6233 | tag_buffer, tag_length, |
| 6234 | &tag_size), |
| 6235 | PSA_ERROR_INVALID_ARGUMENT); |
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_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6238 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6239 | /* Test for not sending any additional data or data after setting non-zero |
| 6240 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6241 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6242 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6243 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6244 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6245 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6246 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6247 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6248 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6249 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6250 | finish_output_size, |
| 6251 | &output_part_length, |
| 6252 | tag_buffer, |
| 6253 | tag_length), |
| 6254 | PSA_ERROR_INVALID_ARGUMENT); |
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_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6257 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6258 | /* Test for not sending any additional data after setting a non-zero length |
| 6259 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6260 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6261 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6263 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6264 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6265 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6266 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6267 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6268 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6269 | input_data->len, output_data, |
| 6270 | output_size, &output_length), |
| 6271 | PSA_ERROR_INVALID_ARGUMENT); |
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_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6274 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6275 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 6276 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6277 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6278 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6279 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6280 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6281 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6282 | input_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6283 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6284 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6285 | additional_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6286 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6287 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6288 | finish_output_size, |
| 6289 | &output_part_length, |
| 6290 | tag_buffer, tag_length, |
| 6291 | &tag_size), |
| 6292 | PSA_ERROR_INVALID_ARGUMENT); |
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_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6295 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6296 | /* Test for sending too much additional data after setting lengths. */ |
| 6297 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6298 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6299 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6300 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6301 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6302 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6303 | |
| 6304 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6305 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6306 | additional_data->len), |
| 6307 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6308 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6309 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6310 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6311 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6312 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6313 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6314 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6315 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6316 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6317 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6318 | input_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6319 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6320 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6321 | additional_data->len)); |
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 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6324 | 1), |
| 6325 | PSA_ERROR_INVALID_ARGUMENT); |
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_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6328 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6329 | /* Test for sending too much data after setting lengths. */ |
| 6330 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6331 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6332 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6333 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6334 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6335 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6336 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6337 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6338 | input_data->len, output_data, |
| 6339 | output_size, &output_length), |
| 6340 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6341 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6342 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6343 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6344 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6345 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6346 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6347 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6348 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6349 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6350 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6351 | input_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6353 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6354 | additional_data->len)); |
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_update(&operation, input_data->x, |
| 6357 | input_data->len, output_data, |
| 6358 | output_size, &output_length)); |
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 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6361 | 1, output_data, |
| 6362 | output_size, &output_length), |
| 6363 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6364 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6365 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6366 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6367 | /* Test sending additional data after data. */ |
| 6368 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6369 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6371 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6372 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6373 | if (operation.alg != PSA_ALG_CCM) { |
| 6374 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6375 | input_data->len, output_data, |
| 6376 | output_size, &output_length)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6377 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6378 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6379 | additional_data->len), |
| 6380 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6381 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6382 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6383 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6384 | /* Test calling finish on decryption. */ |
| 6385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6386 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6387 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6388 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6389 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6390 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6391 | finish_output_size, |
| 6392 | &output_part_length, |
| 6393 | tag_buffer, tag_length, |
| 6394 | &tag_size), |
| 6395 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6396 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6397 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6398 | |
| 6399 | /* Test calling verify on encryption. */ |
| 6400 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6401 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6402 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6403 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6404 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6405 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6406 | finish_output_size, |
| 6407 | &output_part_length, |
| 6408 | tag_buffer, |
| 6409 | tag_length), |
| 6410 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6411 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6412 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6413 | |
| 6414 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6415 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6416 | psa_destroy_key(key); |
| 6417 | psa_aead_abort(&operation); |
| 6418 | mbedtls_free(output_data); |
| 6419 | mbedtls_free(final_data); |
| 6420 | PSA_DONE(); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6421 | } |
| 6422 | /* END_CASE */ |
| 6423 | |
| 6424 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6425 | void signature_size(int type_arg, |
| 6426 | int bits, |
| 6427 | int alg_arg, |
| 6428 | int expected_size_arg) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6429 | { |
| 6430 | psa_key_type_t type = type_arg; |
| 6431 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6432 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6433 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6434 | TEST_EQUAL(actual_size, (size_t) expected_size_arg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6435 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6436 | exit: |
| 6437 | ; |
| 6438 | } |
| 6439 | /* END_CASE */ |
| 6440 | |
| 6441 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6442 | void sign_hash_deterministic(int key_type_arg, data_t *key_data, |
| 6443 | int alg_arg, data_t *input_data, |
| 6444 | data_t *output_data) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6445 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6446 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6447 | psa_key_type_t key_type = key_type_arg; |
| 6448 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6449 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6450 | unsigned char *signature = NULL; |
| 6451 | size_t signature_size; |
| 6452 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6453 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6454 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6455 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6456 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6457 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6458 | psa_set_key_algorithm(&attributes, alg); |
| 6459 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6460 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6461 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6462 | &key)); |
| 6463 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6464 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6465 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6466 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6467 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6468 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6469 | key_bits, alg); |
| 6470 | TEST_ASSERT(signature_size != 0); |
| 6471 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6472 | ASSERT_ALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6473 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6474 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6475 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6476 | input_data->x, input_data->len, |
| 6477 | signature, signature_size, |
| 6478 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6479 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6480 | ASSERT_COMPARE(output_data->x, output_data->len, |
| 6481 | signature, signature_length); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6482 | |
| 6483 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6484 | /* |
| 6485 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6486 | * thus reset them as required. |
| 6487 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6488 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6489 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6490 | psa_destroy_key(key); |
| 6491 | mbedtls_free(signature); |
| 6492 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6493 | } |
| 6494 | /* END_CASE */ |
| 6495 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6496 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6497 | /** |
| 6498 | * sign_hash_interruptible() test intentions: |
| 6499 | * |
| 6500 | * Note: This test can currently only handle ECDSA. |
| 6501 | * |
| 6502 | * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 6503 | * and private keys / keypairs only). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6504 | * |
| 6505 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 6506 | * expected for different max_ops values. |
| 6507 | * |
| 6508 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 6509 | * and that each successful stage completes some ops (this is not mandated by |
| 6510 | * the PSA specification, but is currently the case). |
| 6511 | * |
| 6512 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 6513 | * complete() calls does not alter the number of ops returned. |
| 6514 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6515 | void sign_hash_interruptible(int key_type_arg, data_t *key_data, |
| 6516 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6517 | data_t *output_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6518 | { |
| 6519 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6520 | psa_key_type_t key_type = key_type_arg; |
| 6521 | psa_algorithm_t alg = alg_arg; |
| 6522 | size_t key_bits; |
| 6523 | unsigned char *signature = NULL; |
| 6524 | size_t signature_size; |
| 6525 | size_t signature_length = 0xdeadbeef; |
| 6526 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6527 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6528 | uint32_t num_ops = 0; |
| 6529 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6530 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6531 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6532 | size_t min_completes = 0; |
| 6533 | size_t max_completes = 0; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6534 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6535 | psa_sign_hash_interruptible_operation_t operation = |
| 6536 | psa_sign_hash_interruptible_operation_init(); |
| 6537 | |
| 6538 | PSA_ASSERT(psa_crypto_init()); |
| 6539 | |
| 6540 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6541 | psa_set_key_algorithm(&attributes, alg); |
| 6542 | psa_set_key_type(&attributes, key_type); |
| 6543 | |
| 6544 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6545 | &key)); |
| 6546 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6547 | key_bits = psa_get_key_bits(&attributes); |
| 6548 | |
| 6549 | /* Allocate a buffer which has the size advertised by the |
| 6550 | * library. */ |
| 6551 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6552 | key_bits, alg); |
| 6553 | TEST_ASSERT(signature_size != 0); |
| 6554 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6555 | ASSERT_ALLOC(signature, signature_size); |
| 6556 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6557 | psa_interruptible_set_max_ops(max_ops); |
| 6558 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6559 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6560 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6561 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6562 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6563 | TEST_ASSERT(num_ops_prior == 0); |
| 6564 | |
| 6565 | /* Start performing the signature. */ |
| 6566 | PSA_ASSERT(psa_sign_hash_start(&operation, key, alg, |
| 6567 | input_data->x, input_data->len)); |
| 6568 | |
| 6569 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6570 | TEST_ASSERT(num_ops_prior == 0); |
| 6571 | |
| 6572 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6573 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6574 | status = psa_sign_hash_complete(&operation, signature, signature_size, |
| 6575 | &signature_length); |
| 6576 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6577 | num_completes++; |
| 6578 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6579 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 6580 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6581 | /* We are asserting here that every complete makes progress |
| 6582 | * (completes some ops), which is true of the internal |
| 6583 | * implementation and probably any implementation, however this is |
| 6584 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6585 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6586 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6587 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 6588 | |
| 6589 | /* Ensure calling get_num_ops() twice still returns the same |
| 6590 | * number of ops as previously reported. */ |
| 6591 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6592 | |
| 6593 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6594 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6595 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6596 | |
| 6597 | TEST_ASSERT(status == PSA_SUCCESS); |
| 6598 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6599 | TEST_LE_U(min_completes, num_completes); |
| 6600 | TEST_LE_U(num_completes, max_completes); |
| 6601 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6602 | /* Verify that the signature is what is expected. */ |
| 6603 | ASSERT_COMPARE(output_data->x, output_data->len, |
| 6604 | signature, signature_length); |
| 6605 | |
| 6606 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6607 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6608 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6609 | TEST_ASSERT(num_ops == 0); |
| 6610 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6611 | exit: |
| 6612 | |
| 6613 | /* |
| 6614 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6615 | * thus reset them as required. |
| 6616 | */ |
| 6617 | psa_reset_key_attributes(&attributes); |
| 6618 | |
| 6619 | psa_destroy_key(key); |
| 6620 | mbedtls_free(signature); |
| 6621 | PSA_DONE(); |
| 6622 | } |
| 6623 | /* END_CASE */ |
| 6624 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6625 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6626 | void sign_hash_fail(int key_type_arg, data_t *key_data, |
| 6627 | int alg_arg, data_t *input_data, |
| 6628 | int signature_size_arg, int expected_status_arg) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6629 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6630 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6631 | psa_key_type_t key_type = key_type_arg; |
| 6632 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6633 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6634 | psa_status_t actual_status; |
| 6635 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 6636 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 6637 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6638 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6639 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6640 | ASSERT_ALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6642 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6643 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6644 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6645 | psa_set_key_algorithm(&attributes, alg); |
| 6646 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6647 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6648 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6649 | &key)); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6650 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6651 | actual_status = psa_sign_hash(key, alg, |
| 6652 | input_data->x, input_data->len, |
| 6653 | signature, signature_size, |
| 6654 | &signature_length); |
| 6655 | TEST_EQUAL(actual_status, expected_status); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6656 | /* The value of *signature_length is unspecified on error, but |
| 6657 | * whatever it is, it should be less than signature_size, so that |
| 6658 | * if the caller tries to read *signature_length bytes without |
| 6659 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6660 | TEST_LE_U(signature_length, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6661 | |
| 6662 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6663 | psa_reset_key_attributes(&attributes); |
| 6664 | psa_destroy_key(key); |
| 6665 | mbedtls_free(signature); |
| 6666 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6667 | } |
| 6668 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6669 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6670 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6671 | /** |
| 6672 | * sign_hash_fail_interruptible() test intentions: |
| 6673 | * |
| 6674 | * Note: This test can currently only handle ECDSA. |
| 6675 | * |
| 6676 | * 1. Test that various failure cases for interruptible sign hash fail with the |
| 6677 | * correct error codes, and at the correct point (at start or during |
| 6678 | * complete). |
| 6679 | * |
| 6680 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 6681 | * expected for different max_ops values. |
| 6682 | * |
| 6683 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 6684 | * and that each successful stage completes some ops (this is not mandated by |
| 6685 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 6686 | * |
| 6687 | * 4. Check that calling complete() when start() fails and complete() |
| 6688 | * after completion results in a BAD_STATE error. |
| 6689 | * |
| 6690 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 6691 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6692 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6693 | void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 6694 | int alg_arg, data_t *input_data, |
| 6695 | int signature_size_arg, |
| 6696 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6697 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6698 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6699 | { |
| 6700 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6701 | psa_key_type_t key_type = key_type_arg; |
| 6702 | psa_algorithm_t alg = alg_arg; |
| 6703 | size_t signature_size = signature_size_arg; |
| 6704 | psa_status_t actual_status; |
| 6705 | psa_status_t expected_start_status = expected_start_status_arg; |
| 6706 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 6707 | unsigned char *signature = NULL; |
| 6708 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6709 | uint32_t num_ops = 0; |
| 6710 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6711 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6712 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6713 | size_t min_completes = 0; |
| 6714 | size_t max_completes = 0; |
| 6715 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6716 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6717 | psa_sign_hash_interruptible_operation_t operation = |
| 6718 | psa_sign_hash_interruptible_operation_init(); |
| 6719 | |
| 6720 | ASSERT_ALLOC(signature, signature_size); |
| 6721 | |
| 6722 | PSA_ASSERT(psa_crypto_init()); |
| 6723 | |
| 6724 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6725 | psa_set_key_algorithm(&attributes, alg); |
| 6726 | psa_set_key_type(&attributes, key_type); |
| 6727 | |
| 6728 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6729 | &key)); |
| 6730 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6731 | psa_interruptible_set_max_ops(max_ops); |
| 6732 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6733 | interruptible_signverify_get_minmax_completes(max_ops, |
| 6734 | expected_complete_status, |
| 6735 | &min_completes, |
| 6736 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6737 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6738 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6739 | TEST_ASSERT(num_ops_prior == 0); |
| 6740 | |
| 6741 | /* Start performing the signature. */ |
| 6742 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 6743 | input_data->x, input_data->len); |
| 6744 | |
| 6745 | TEST_EQUAL(actual_status, expected_start_status); |
| 6746 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6747 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 6748 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 6749 | * start failed. */ |
| 6750 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6751 | signature_size, |
| 6752 | &signature_length); |
| 6753 | |
| 6754 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 6755 | |
| 6756 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6757 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 6758 | input_data->x, input_data->len); |
| 6759 | |
| 6760 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 6761 | } |
| 6762 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6763 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6764 | TEST_ASSERT(num_ops_prior == 0); |
| 6765 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6766 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6767 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6768 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6769 | signature_size, |
| 6770 | &signature_length); |
| 6771 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6772 | num_completes++; |
| 6773 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6774 | if (actual_status == PSA_SUCCESS || |
| 6775 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6776 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6777 | /* We are asserting here that every complete makes progress |
| 6778 | * (completes some ops), which is true of the internal |
| 6779 | * implementation and probably any implementation, however this is |
| 6780 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6781 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6782 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6783 | num_ops_prior = num_ops; |
| 6784 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6785 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6786 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6787 | TEST_EQUAL(actual_status, expected_complete_status); |
| 6788 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 6789 | /* Check that another complete returns BAD_STATE. */ |
| 6790 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 6791 | signature_size, |
| 6792 | &signature_length); |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 6793 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 6794 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 6795 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6796 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6797 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6798 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6799 | TEST_ASSERT(num_ops == 0); |
| 6800 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6801 | /* The value of *signature_length is unspecified on error, but |
| 6802 | * whatever it is, it should be less than signature_size, so that |
| 6803 | * if the caller tries to read *signature_length bytes without |
| 6804 | * checking the error code then they don't overflow a buffer. */ |
| 6805 | TEST_LE_U(signature_length, signature_size); |
| 6806 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6807 | TEST_LE_U(min_completes, num_completes); |
| 6808 | TEST_LE_U(num_completes, max_completes); |
| 6809 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 6810 | exit: |
| 6811 | psa_reset_key_attributes(&attributes); |
| 6812 | psa_destroy_key(key); |
| 6813 | mbedtls_free(signature); |
| 6814 | PSA_DONE(); |
| 6815 | } |
| 6816 | /* END_CASE */ |
| 6817 | |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6818 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6819 | void sign_verify_hash(int key_type_arg, data_t *key_data, |
| 6820 | int alg_arg, data_t *input_data) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6821 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6822 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6823 | psa_key_type_t key_type = key_type_arg; |
| 6824 | psa_algorithm_t alg = alg_arg; |
| 6825 | size_t key_bits; |
| 6826 | unsigned char *signature = NULL; |
| 6827 | size_t signature_size; |
| 6828 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6829 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6830 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6831 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6832 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6833 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 6834 | psa_set_key_algorithm(&attributes, alg); |
| 6835 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6836 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6837 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6838 | &key)); |
| 6839 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6840 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6841 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6842 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6843 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6844 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6845 | key_bits, alg); |
| 6846 | TEST_ASSERT(signature_size != 0); |
| 6847 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6848 | ASSERT_ALLOC(signature, signature_size); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6849 | |
| 6850 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6851 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6852 | input_data->x, input_data->len, |
| 6853 | signature, signature_size, |
| 6854 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6855 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6856 | TEST_LE_U(signature_length, signature_size); |
| 6857 | TEST_ASSERT(signature_length > 0); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6858 | |
| 6859 | /* Use the library to verify that the signature is correct. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6860 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 6861 | input_data->x, input_data->len, |
| 6862 | signature, signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6863 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6864 | if (input_data->len != 0) { |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6865 | /* Flip a bit in the input and verify that the signature is now |
| 6866 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6867 | * because ECDSA may ignore the last few bits of the input. */ |
| 6868 | input_data->x[0] ^= 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6869 | TEST_EQUAL(psa_verify_hash(key, alg, |
| 6870 | input_data->x, input_data->len, |
| 6871 | signature, signature_length), |
| 6872 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6873 | } |
| 6874 | |
| 6875 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6876 | /* |
| 6877 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6878 | * thus reset them as required. |
| 6879 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6880 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6881 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6882 | psa_destroy_key(key); |
| 6883 | mbedtls_free(signature); |
| 6884 | PSA_DONE(); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6885 | } |
| 6886 | /* END_CASE */ |
| 6887 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6888 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6889 | /** |
| 6890 | * sign_verify_hash_interruptible() test intentions: |
| 6891 | * |
| 6892 | * Note: This test can currently only handle ECDSA. |
| 6893 | * |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 6894 | * 1. Test that we can sign an input hash with the given keypair and then |
| 6895 | * afterwards verify that signature. This is currently the only way to test |
| 6896 | * non deterministic ECDSA, but this test can also handle deterministic. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6897 | * |
| 6898 | * 2. Test that after corrupting the hash, the verification detects an invalid |
| 6899 | * signature. |
| 6900 | * |
| 6901 | * 3. Test the number of calls to psa_sign_hash_complete() required are as |
| 6902 | * expected for different max_ops values. |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6903 | * |
| 6904 | * 4. Test that the number of ops done prior to starting signing and after abort |
| 6905 | * is zero and that each successful signing stage completes some ops (this is |
| 6906 | * not mandated by the PSA specification, but is currently the case). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6907 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6908 | 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] | 6909 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6910 | int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6911 | { |
| 6912 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6913 | psa_key_type_t key_type = key_type_arg; |
| 6914 | psa_algorithm_t alg = alg_arg; |
| 6915 | size_t key_bits; |
| 6916 | unsigned char *signature = NULL; |
| 6917 | size_t signature_size; |
| 6918 | size_t signature_length = 0xdeadbeef; |
| 6919 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6920 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6921 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6922 | uint32_t num_ops = 0; |
| 6923 | uint32_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6924 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6925 | size_t min_completes = 0; |
| 6926 | size_t max_completes = 0; |
| 6927 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6928 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 6929 | psa_sign_hash_interruptible_operation_init(); |
| 6930 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 6931 | psa_verify_hash_interruptible_operation_init(); |
| 6932 | |
| 6933 | PSA_ASSERT(psa_crypto_init()); |
| 6934 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6935 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 6936 | PSA_KEY_USAGE_VERIFY_HASH); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6937 | psa_set_key_algorithm(&attributes, alg); |
| 6938 | psa_set_key_type(&attributes, key_type); |
| 6939 | |
| 6940 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6941 | &key)); |
| 6942 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6943 | key_bits = psa_get_key_bits(&attributes); |
| 6944 | |
| 6945 | /* Allocate a buffer which has the size advertised by the |
| 6946 | * library. */ |
| 6947 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6948 | key_bits, alg); |
| 6949 | TEST_ASSERT(signature_size != 0); |
| 6950 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 6951 | ASSERT_ALLOC(signature, signature_size); |
| 6952 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6953 | psa_interruptible_set_max_ops(max_ops); |
| 6954 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6955 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6956 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6957 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6958 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); |
| 6959 | TEST_ASSERT(num_ops_prior == 0); |
| 6960 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6961 | /* Start performing the signature. */ |
| 6962 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 6963 | input_data->x, input_data->len)); |
| 6964 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6965 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); |
| 6966 | TEST_ASSERT(num_ops_prior == 0); |
| 6967 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6968 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6969 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6970 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6971 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 6972 | signature_size, |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6973 | &signature_length); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6974 | |
| 6975 | num_completes++; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6976 | |
| 6977 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 6978 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 6979 | /* We are asserting here that every complete makes progress |
| 6980 | * (completes some ops), which is true of the internal |
| 6981 | * implementation and probably any implementation, however this is |
| 6982 | * not mandated by the PSA specification. */ |
| 6983 | TEST_ASSERT(num_ops > num_ops_prior); |
| 6984 | |
| 6985 | num_ops_prior = num_ops; |
| 6986 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6987 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6988 | |
| 6989 | TEST_ASSERT(status == PSA_SUCCESS); |
| 6990 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6991 | TEST_LE_U(min_completes, num_completes); |
| 6992 | TEST_LE_U(num_completes, max_completes); |
| 6993 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6994 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 6995 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 6996 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 6997 | TEST_ASSERT(num_ops == 0); |
| 6998 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6999 | /* Check that the signature length looks sensible. */ |
| 7000 | TEST_LE_U(signature_length, signature_size); |
| 7001 | TEST_ASSERT(signature_length > 0); |
| 7002 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7003 | num_completes = 0; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7004 | |
| 7005 | /* Start verification. */ |
| 7006 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7007 | input_data->x, input_data->len, |
| 7008 | signature, signature_length)); |
| 7009 | |
| 7010 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7011 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7012 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7013 | |
| 7014 | num_completes++; |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7015 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7016 | |
| 7017 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7018 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7019 | TEST_LE_U(min_completes, num_completes); |
| 7020 | TEST_LE_U(num_completes, max_completes); |
| 7021 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7022 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7023 | |
| 7024 | verify_operation = psa_verify_hash_interruptible_operation_init(); |
| 7025 | |
| 7026 | if (input_data->len != 0) { |
| 7027 | /* Flip a bit in the input and verify that the signature is now |
| 7028 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 7029 | * because ECDSA may ignore the last few bits of the input. */ |
| 7030 | input_data->x[0] ^= 1; |
| 7031 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7032 | /* Start verification. */ |
| 7033 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7034 | input_data->x, input_data->len, |
| 7035 | signature, signature_length)); |
| 7036 | |
| 7037 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7038 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7039 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7040 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7041 | |
| 7042 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7043 | } |
| 7044 | |
| 7045 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7046 | |
| 7047 | exit: |
| 7048 | /* |
| 7049 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7050 | * thus reset them as required. |
| 7051 | */ |
| 7052 | psa_reset_key_attributes(&attributes); |
| 7053 | |
| 7054 | psa_destroy_key(key); |
| 7055 | mbedtls_free(signature); |
| 7056 | PSA_DONE(); |
| 7057 | } |
| 7058 | /* END_CASE */ |
| 7059 | |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7060 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7061 | void verify_hash(int key_type_arg, data_t *key_data, |
| 7062 | int alg_arg, data_t *hash_data, |
| 7063 | data_t *signature_data) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7064 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7065 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7066 | psa_key_type_t key_type = key_type_arg; |
| 7067 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7068 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7069 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7070 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 7071 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7072 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7073 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7074 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7075 | psa_set_key_algorithm(&attributes, alg); |
| 7076 | psa_set_key_type(&attributes, key_type); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7077 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7078 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7079 | &key)); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7080 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7081 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 7082 | hash_data->x, hash_data->len, |
| 7083 | signature_data->x, signature_data->len)); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 7084 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7085 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7086 | psa_reset_key_attributes(&attributes); |
| 7087 | psa_destroy_key(key); |
| 7088 | PSA_DONE(); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7089 | } |
| 7090 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7091 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7092 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7093 | /** |
| 7094 | * verify_hash_interruptible() test intentions: |
| 7095 | * |
| 7096 | * Note: This test can currently only handle ECDSA. |
| 7097 | * |
| 7098 | * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 7099 | * only). Given this test only does verification it can accept public keys as |
| 7100 | * well as private keys / keypairs. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7101 | * |
| 7102 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7103 | * expected for different max_ops values. |
| 7104 | * |
| 7105 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7106 | * and that each successful stage completes some ops (this is not mandated by |
| 7107 | * the PSA specification, but is currently the case). |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7108 | * |
| 7109 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 7110 | * complete() calls does not alter the number of ops returned. |
| 7111 | * |
| 7112 | * 5. Test that after corrupting the hash, the verification detects an invalid |
| 7113 | * signature. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7114 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7115 | void verify_hash_interruptible(int key_type_arg, data_t *key_data, |
| 7116 | int alg_arg, data_t *hash_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7117 | data_t *signature_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7118 | { |
| 7119 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7120 | psa_key_type_t key_type = key_type_arg; |
| 7121 | psa_algorithm_t alg = alg_arg; |
| 7122 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7123 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7124 | uint32_t num_ops = 0; |
| 7125 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7126 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7127 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7128 | size_t min_completes = 0; |
| 7129 | size_t max_completes = 0; |
| 7130 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7131 | psa_verify_hash_interruptible_operation_t operation = |
| 7132 | psa_verify_hash_interruptible_operation_init(); |
| 7133 | |
| 7134 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 7135 | |
| 7136 | PSA_ASSERT(psa_crypto_init()); |
| 7137 | |
| 7138 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7139 | psa_set_key_algorithm(&attributes, alg); |
| 7140 | psa_set_key_type(&attributes, key_type); |
| 7141 | |
| 7142 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7143 | &key)); |
| 7144 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7145 | psa_interruptible_set_max_ops(max_ops); |
| 7146 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7147 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 7148 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7149 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7150 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7151 | |
| 7152 | TEST_ASSERT(num_ops_prior == 0); |
| 7153 | |
| 7154 | /* Start verification. */ |
| 7155 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7156 | hash_data->x, hash_data->len, |
| 7157 | signature_data->x, signature_data->len) |
| 7158 | ); |
| 7159 | |
| 7160 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7161 | |
| 7162 | TEST_ASSERT(num_ops_prior == 0); |
| 7163 | |
| 7164 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7165 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7166 | status = psa_verify_hash_complete(&operation); |
| 7167 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7168 | num_completes++; |
| 7169 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7170 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 7171 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7172 | /* We are asserting here that every complete makes progress |
| 7173 | * (completes some ops), which is true of the internal |
| 7174 | * implementation and probably any implementation, however this is |
| 7175 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7176 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7177 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7178 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 7179 | |
| 7180 | /* Ensure calling get_num_ops() twice still returns the same |
| 7181 | * number of ops as previously reported. */ |
| 7182 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7183 | |
| 7184 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7185 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7186 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7187 | |
| 7188 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7189 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7190 | TEST_LE_U(min_completes, num_completes); |
| 7191 | TEST_LE_U(num_completes, max_completes); |
| 7192 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7193 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7194 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7195 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7196 | TEST_ASSERT(num_ops == 0); |
| 7197 | |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7198 | if (hash_data->len != 0) { |
| 7199 | /* Flip a bit in the hash and verify that the signature is now detected |
| 7200 | * as invalid. Flip a bit at the beginning, not at the end, because |
| 7201 | * ECDSA may ignore the last few bits of the input. */ |
| 7202 | hash_data->x[0] ^= 1; |
| 7203 | |
| 7204 | /* Start verification. */ |
| 7205 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7206 | hash_data->x, hash_data->len, |
| 7207 | signature_data->x, signature_data->len)); |
| 7208 | |
| 7209 | /* Continue performing the signature until complete. */ |
| 7210 | do { |
| 7211 | status = psa_verify_hash_complete(&operation); |
| 7212 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7213 | |
| 7214 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7215 | } |
| 7216 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7217 | exit: |
| 7218 | psa_reset_key_attributes(&attributes); |
| 7219 | psa_destroy_key(key); |
| 7220 | PSA_DONE(); |
| 7221 | } |
| 7222 | /* END_CASE */ |
| 7223 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7224 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7225 | void verify_hash_fail(int key_type_arg, data_t *key_data, |
| 7226 | int alg_arg, data_t *hash_data, |
| 7227 | data_t *signature_data, |
| 7228 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7229 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7230 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7231 | psa_key_type_t key_type = key_type_arg; |
| 7232 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7233 | psa_status_t actual_status; |
| 7234 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7235 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7236 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7237 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7238 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7239 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7240 | psa_set_key_algorithm(&attributes, alg); |
| 7241 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7242 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7243 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7244 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7245 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7246 | actual_status = psa_verify_hash(key, alg, |
| 7247 | hash_data->x, hash_data->len, |
| 7248 | signature_data->x, signature_data->len); |
| 7249 | TEST_EQUAL(actual_status, expected_status); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7250 | |
| 7251 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7252 | psa_reset_key_attributes(&attributes); |
| 7253 | psa_destroy_key(key); |
| 7254 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7255 | } |
| 7256 | /* END_CASE */ |
| 7257 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7258 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7259 | /** |
| 7260 | * verify_hash_fail_interruptible() test intentions: |
| 7261 | * |
| 7262 | * Note: This test can currently only handle ECDSA. |
| 7263 | * |
| 7264 | * 1. Test that various failure cases for interruptible verify hash fail with |
| 7265 | * the correct error codes, and at the correct point (at start or during |
| 7266 | * complete). |
| 7267 | * |
| 7268 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7269 | * expected for different max_ops values. |
| 7270 | * |
| 7271 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7272 | * and that each successful stage completes some ops (this is not mandated by |
| 7273 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7274 | * |
| 7275 | * 4. Check that calling complete() when start() fails and complete() |
| 7276 | * after completion results in a BAD_STATE error. |
| 7277 | * |
| 7278 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 7279 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7280 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7281 | void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 7282 | int alg_arg, data_t *hash_data, |
| 7283 | data_t *signature_data, |
| 7284 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7285 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7286 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7287 | { |
| 7288 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7289 | psa_key_type_t key_type = key_type_arg; |
| 7290 | psa_algorithm_t alg = alg_arg; |
| 7291 | psa_status_t actual_status; |
| 7292 | psa_status_t expected_start_status = expected_start_status_arg; |
| 7293 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 7294 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7295 | uint32_t num_ops = 0; |
| 7296 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7297 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7298 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7299 | size_t min_completes = 0; |
| 7300 | size_t max_completes = 0; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7301 | psa_verify_hash_interruptible_operation_t operation = |
| 7302 | psa_verify_hash_interruptible_operation_init(); |
| 7303 | |
| 7304 | PSA_ASSERT(psa_crypto_init()); |
| 7305 | |
| 7306 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7307 | psa_set_key_algorithm(&attributes, alg); |
| 7308 | psa_set_key_type(&attributes, key_type); |
| 7309 | |
| 7310 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7311 | &key)); |
| 7312 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7313 | psa_interruptible_set_max_ops(max_ops); |
| 7314 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7315 | interruptible_signverify_get_minmax_completes(max_ops, |
| 7316 | expected_complete_status, |
| 7317 | &min_completes, |
| 7318 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7319 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7320 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7321 | TEST_ASSERT(num_ops_prior == 0); |
| 7322 | |
| 7323 | /* Start verification. */ |
| 7324 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7325 | hash_data->x, hash_data->len, |
| 7326 | signature_data->x, |
| 7327 | signature_data->len); |
| 7328 | |
| 7329 | TEST_EQUAL(actual_status, expected_start_status); |
| 7330 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7331 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 7332 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7333 | * start failed. */ |
| 7334 | actual_status = psa_verify_hash_complete(&operation); |
| 7335 | |
| 7336 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7337 | |
| 7338 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7339 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7340 | hash_data->x, hash_data->len, |
| 7341 | signature_data->x, |
| 7342 | signature_data->len); |
| 7343 | |
| 7344 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7345 | } |
| 7346 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7347 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7348 | TEST_ASSERT(num_ops_prior == 0); |
| 7349 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7350 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7351 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7352 | actual_status = psa_verify_hash_complete(&operation); |
| 7353 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7354 | num_completes++; |
| 7355 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7356 | if (actual_status == PSA_SUCCESS || |
| 7357 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7358 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7359 | /* We are asserting here that every complete makes progress |
| 7360 | * (completes some ops), which is true of the internal |
| 7361 | * implementation and probably any implementation, however this is |
| 7362 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7363 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7364 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7365 | num_ops_prior = num_ops; |
| 7366 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7367 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7368 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7369 | TEST_EQUAL(actual_status, expected_complete_status); |
| 7370 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 7371 | /* Check that another complete returns BAD_STATE. */ |
| 7372 | actual_status = psa_verify_hash_complete(&operation); |
| 7373 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7374 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7375 | TEST_LE_U(min_completes, num_completes); |
| 7376 | TEST_LE_U(num_completes, max_completes); |
| 7377 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7378 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7379 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7380 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7381 | TEST_ASSERT(num_ops == 0); |
| 7382 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7383 | exit: |
| 7384 | psa_reset_key_attributes(&attributes); |
| 7385 | psa_destroy_key(key); |
| 7386 | PSA_DONE(); |
| 7387 | } |
| 7388 | /* END_CASE */ |
| 7389 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7390 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7391 | /** |
| 7392 | * interruptible_signverify_hash_state_test() test intentions: |
| 7393 | * |
| 7394 | * Note: This test can currently only handle ECDSA. |
| 7395 | * |
| 7396 | * 1. Test that calling the various interruptible sign and verify hash functions |
| 7397 | * in incorrect orders returns BAD_STATE errors. |
| 7398 | */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7399 | void interruptible_signverify_hash_state_test(int key_type_arg, |
| 7400 | data_t *key_data, int alg_arg, data_t *input_data) |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7401 | { |
| 7402 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7403 | psa_key_type_t key_type = key_type_arg; |
| 7404 | psa_algorithm_t alg = alg_arg; |
| 7405 | size_t key_bits; |
| 7406 | unsigned char *signature = NULL; |
| 7407 | size_t signature_size; |
| 7408 | size_t signature_length = 0xdeadbeef; |
| 7409 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7410 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7411 | psa_sign_hash_interruptible_operation_init(); |
| 7412 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7413 | psa_verify_hash_interruptible_operation_init(); |
| 7414 | |
| 7415 | PSA_ASSERT(psa_crypto_init()); |
| 7416 | |
| 7417 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7418 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7419 | psa_set_key_algorithm(&attributes, alg); |
| 7420 | psa_set_key_type(&attributes, key_type); |
| 7421 | |
| 7422 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7423 | &key)); |
| 7424 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7425 | key_bits = psa_get_key_bits(&attributes); |
| 7426 | |
| 7427 | /* Allocate a buffer which has the size advertised by the |
| 7428 | * library. */ |
| 7429 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7430 | key_bits, alg); |
| 7431 | TEST_ASSERT(signature_size != 0); |
| 7432 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7433 | ASSERT_ALLOC(signature, signature_size); |
| 7434 | |
| 7435 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7436 | |
| 7437 | /* --- Attempt completes prior to starts --- */ |
| 7438 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7439 | signature_size, |
| 7440 | &signature_length), |
| 7441 | PSA_ERROR_BAD_STATE); |
| 7442 | |
| 7443 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7444 | |
| 7445 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7446 | PSA_ERROR_BAD_STATE); |
| 7447 | |
| 7448 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7449 | |
| 7450 | /* --- Aborts in all other places. --- */ |
| 7451 | psa_sign_hash_abort(&sign_operation); |
| 7452 | |
| 7453 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7454 | input_data->x, input_data->len)); |
| 7455 | |
| 7456 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7457 | |
| 7458 | psa_interruptible_set_max_ops(1); |
| 7459 | |
| 7460 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7461 | input_data->x, input_data->len)); |
| 7462 | |
| 7463 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7464 | signature_size, |
| 7465 | &signature_length), |
| 7466 | PSA_OPERATION_INCOMPLETE); |
| 7467 | |
| 7468 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7469 | |
| 7470 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7471 | |
| 7472 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7473 | input_data->x, input_data->len)); |
| 7474 | |
| 7475 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7476 | signature_size, |
| 7477 | &signature_length)); |
| 7478 | |
| 7479 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7480 | |
| 7481 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7482 | |
| 7483 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7484 | input_data->x, input_data->len, |
| 7485 | signature, signature_length)); |
| 7486 | |
| 7487 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7488 | |
| 7489 | psa_interruptible_set_max_ops(1); |
| 7490 | |
| 7491 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7492 | input_data->x, input_data->len, |
| 7493 | signature, signature_length)); |
| 7494 | |
| 7495 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7496 | PSA_OPERATION_INCOMPLETE); |
| 7497 | |
| 7498 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7499 | |
| 7500 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7501 | |
| 7502 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7503 | input_data->x, input_data->len, |
| 7504 | signature, signature_length)); |
| 7505 | |
| 7506 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7507 | |
| 7508 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7509 | |
| 7510 | /* --- Attempt double starts. --- */ |
| 7511 | |
| 7512 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7513 | input_data->x, input_data->len)); |
| 7514 | |
| 7515 | TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg, |
| 7516 | input_data->x, input_data->len), |
| 7517 | PSA_ERROR_BAD_STATE); |
| 7518 | |
| 7519 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7520 | |
| 7521 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7522 | input_data->x, input_data->len, |
| 7523 | signature, signature_length)); |
| 7524 | |
| 7525 | TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg, |
| 7526 | input_data->x, input_data->len, |
| 7527 | signature, signature_length), |
| 7528 | PSA_ERROR_BAD_STATE); |
| 7529 | |
| 7530 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7531 | |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7532 | exit: |
| 7533 | /* |
| 7534 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7535 | * thus reset them as required. |
| 7536 | */ |
| 7537 | psa_reset_key_attributes(&attributes); |
| 7538 | |
| 7539 | psa_destroy_key(key); |
| 7540 | mbedtls_free(signature); |
| 7541 | PSA_DONE(); |
| 7542 | } |
| 7543 | /* END_CASE */ |
| 7544 | |
| 7545 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7546 | /** |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7547 | * interruptible_signverify_hash_edgecase_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7548 | * |
| 7549 | * Note: This test can currently only handle ECDSA. |
| 7550 | * |
| 7551 | * 1. Test various edge cases in the interruptible sign and verify hash |
| 7552 | * interfaces. |
| 7553 | */ |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7554 | void interruptible_signverify_hash_edgecase_tests(int key_type_arg, |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7555 | data_t *key_data, int alg_arg, data_t *input_data) |
| 7556 | { |
| 7557 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7558 | psa_key_type_t key_type = key_type_arg; |
| 7559 | psa_algorithm_t alg = alg_arg; |
| 7560 | size_t key_bits; |
| 7561 | unsigned char *signature = NULL; |
| 7562 | size_t signature_size; |
| 7563 | size_t signature_length = 0xdeadbeef; |
| 7564 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7565 | uint8_t *input_buffer = NULL; |
| 7566 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7567 | psa_sign_hash_interruptible_operation_init(); |
| 7568 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7569 | psa_verify_hash_interruptible_operation_init(); |
| 7570 | |
| 7571 | PSA_ASSERT(psa_crypto_init()); |
| 7572 | |
| 7573 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7574 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7575 | psa_set_key_algorithm(&attributes, alg); |
| 7576 | psa_set_key_type(&attributes, key_type); |
| 7577 | |
| 7578 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7579 | &key)); |
| 7580 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7581 | key_bits = psa_get_key_bits(&attributes); |
| 7582 | |
| 7583 | /* Allocate a buffer which has the size advertised by the |
| 7584 | * library. */ |
| 7585 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7586 | key_bits, alg); |
| 7587 | TEST_ASSERT(signature_size != 0); |
| 7588 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7589 | ASSERT_ALLOC(signature, signature_size); |
| 7590 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7591 | /* --- Change function inputs mid run, to cause an error (sign only, |
| 7592 | * verify passes all inputs to start. --- */ |
| 7593 | |
| 7594 | psa_interruptible_set_max_ops(1); |
| 7595 | |
| 7596 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7597 | input_data->x, input_data->len)); |
| 7598 | |
| 7599 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7600 | signature_size, |
| 7601 | &signature_length), |
| 7602 | PSA_OPERATION_INCOMPLETE); |
| 7603 | |
| 7604 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7605 | 0, |
| 7606 | &signature_length), |
| 7607 | PSA_ERROR_BUFFER_TOO_SMALL); |
| 7608 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7609 | /* And test that this invalidates the operation. */ |
| 7610 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7611 | 0, |
| 7612 | &signature_length), |
| 7613 | PSA_ERROR_BAD_STATE); |
| 7614 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7615 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7616 | |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7617 | /* Trash the hash buffer in between start and complete, to ensure |
| 7618 | * no reliance on external buffers. */ |
| 7619 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7620 | |
| 7621 | input_buffer = mbedtls_calloc(1, input_data->len); |
| 7622 | TEST_ASSERT(input_buffer != NULL); |
| 7623 | |
| 7624 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7625 | |
| 7626 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7627 | input_buffer, input_data->len)); |
| 7628 | |
| 7629 | memset(input_buffer, '!', input_data->len); |
| 7630 | mbedtls_free(input_buffer); |
| 7631 | input_buffer = NULL; |
| 7632 | |
| 7633 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7634 | signature_size, |
| 7635 | &signature_length)); |
| 7636 | |
| 7637 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7638 | |
| 7639 | input_buffer = mbedtls_calloc(1, input_data->len); |
| 7640 | TEST_ASSERT(input_buffer != NULL); |
| 7641 | |
| 7642 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7643 | |
| 7644 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7645 | input_buffer, input_data->len, |
| 7646 | signature, signature_length)); |
| 7647 | |
| 7648 | memset(input_buffer, '!', input_data->len); |
| 7649 | mbedtls_free(input_buffer); |
| 7650 | input_buffer = NULL; |
| 7651 | |
| 7652 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7653 | |
| 7654 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7655 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7656 | exit: |
| 7657 | /* |
| 7658 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7659 | * thus reset them as required. |
| 7660 | */ |
| 7661 | psa_reset_key_attributes(&attributes); |
| 7662 | |
| 7663 | psa_destroy_key(key); |
| 7664 | mbedtls_free(signature); |
| 7665 | PSA_DONE(); |
| 7666 | } |
| 7667 | /* END_CASE */ |
| 7668 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7669 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7670 | /** |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 7671 | * interruptible_signverify_hash_ops_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7672 | * |
| 7673 | * Note: This test can currently only handle ECDSA. |
| 7674 | * |
| 7675 | * 1. Test that setting max ops is reflected in both interruptible sign and |
| 7676 | * verify hash |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7677 | * 2. Test that changing the value of max_ops to unlimited during an operation |
| 7678 | * causes that operation to complete in the next call. |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7679 | * |
| 7680 | * 3. Test that calling get_num_ops() between complete calls gives the same |
| 7681 | * 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] | 7682 | */ |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 7683 | void interruptible_signverify_hash_ops_tests(int key_type_arg, |
| 7684 | data_t *key_data, int alg_arg, |
| 7685 | data_t *input_data) |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7686 | { |
| 7687 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7688 | psa_key_type_t key_type = key_type_arg; |
| 7689 | psa_algorithm_t alg = alg_arg; |
| 7690 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7691 | size_t key_bits; |
| 7692 | unsigned char *signature = NULL; |
| 7693 | size_t signature_size; |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7694 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7695 | uint32_t num_ops = 0; |
| 7696 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 7697 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7698 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7699 | psa_sign_hash_interruptible_operation_init(); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7700 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7701 | psa_verify_hash_interruptible_operation_init(); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7702 | |
| 7703 | PSA_ASSERT(psa_crypto_init()); |
| 7704 | |
| 7705 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7706 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7707 | psa_set_key_algorithm(&attributes, alg); |
| 7708 | psa_set_key_type(&attributes, key_type); |
| 7709 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7710 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key)); |
| 7711 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7712 | key_bits = psa_get_key_bits(&attributes); |
| 7713 | |
| 7714 | /* Allocate a buffer which has the size advertised by the |
| 7715 | * library. */ |
| 7716 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7717 | |
| 7718 | TEST_ASSERT(signature_size != 0); |
| 7719 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7720 | ASSERT_ALLOC(signature, signature_size); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7721 | |
| 7722 | /* Check that default max ops gets set if we don't set it. */ |
| 7723 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7724 | input_data->x, input_data->len)); |
| 7725 | |
| 7726 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 7727 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7728 | |
| 7729 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7730 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7731 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7732 | input_data->x, input_data->len, |
| 7733 | signature, signature_size)); |
| 7734 | |
| 7735 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 7736 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7737 | |
| 7738 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7739 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7740 | /* Check that max ops gets set properly. */ |
| 7741 | |
| 7742 | psa_interruptible_set_max_ops(0xbeef); |
| 7743 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7744 | TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7745 | |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 7746 | /* --- Ensure changing the max ops mid operation works (operation should |
| 7747 | * complete successfully after setting max ops to unlimited --- */ |
| 7748 | psa_interruptible_set_max_ops(1); |
| 7749 | |
| 7750 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7751 | input_data->x, input_data->len)); |
| 7752 | |
| 7753 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7754 | signature_size, |
| 7755 | &signature_length), |
| 7756 | PSA_OPERATION_INCOMPLETE); |
| 7757 | |
| 7758 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7759 | |
| 7760 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7761 | signature_size, |
| 7762 | &signature_length)); |
| 7763 | |
| 7764 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7765 | |
| 7766 | psa_interruptible_set_max_ops(1); |
| 7767 | |
| 7768 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7769 | input_data->x, input_data->len, |
| 7770 | signature, signature_length)); |
| 7771 | |
| 7772 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7773 | PSA_OPERATION_INCOMPLETE); |
| 7774 | |
| 7775 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7776 | |
| 7777 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7778 | |
| 7779 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7780 | |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 7781 | /* --- Test that not calling get_num_ops inbetween complete calls does not |
| 7782 | * result in lost ops. ---*/ |
| 7783 | |
| 7784 | psa_interruptible_set_max_ops(1); |
| 7785 | |
| 7786 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7787 | input_data->x, input_data->len)); |
| 7788 | |
| 7789 | /* Continue performing the signature until complete. */ |
| 7790 | do { |
| 7791 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7792 | signature_size, |
| 7793 | &signature_length); |
| 7794 | |
| 7795 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7796 | |
| 7797 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7798 | |
| 7799 | PSA_ASSERT(status); |
| 7800 | |
| 7801 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7802 | |
| 7803 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7804 | input_data->x, input_data->len)); |
| 7805 | |
| 7806 | /* Continue performing the signature until complete. */ |
| 7807 | do { |
| 7808 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7809 | signature_size, |
| 7810 | &signature_length); |
| 7811 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7812 | |
| 7813 | PSA_ASSERT(status); |
| 7814 | |
| 7815 | TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation)); |
| 7816 | |
| 7817 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7818 | |
| 7819 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7820 | input_data->x, input_data->len, |
| 7821 | signature, signature_length)); |
| 7822 | |
| 7823 | /* Continue performing the verification until complete. */ |
| 7824 | do { |
| 7825 | status = psa_verify_hash_complete(&verify_operation); |
| 7826 | |
| 7827 | num_ops = psa_verify_hash_get_num_ops(&verify_operation); |
| 7828 | |
| 7829 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7830 | |
| 7831 | PSA_ASSERT(status); |
| 7832 | |
| 7833 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7834 | |
| 7835 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7836 | input_data->x, input_data->len, |
| 7837 | signature, signature_length)); |
| 7838 | |
| 7839 | /* Continue performing the verification until complete. */ |
| 7840 | do { |
| 7841 | status = psa_verify_hash_complete(&verify_operation); |
| 7842 | |
| 7843 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7844 | |
| 7845 | PSA_ASSERT(status); |
| 7846 | |
| 7847 | TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation)); |
| 7848 | |
| 7849 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7850 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7851 | exit: |
| 7852 | /* |
| 7853 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7854 | * thus reset them as required. |
| 7855 | */ |
| 7856 | psa_reset_key_attributes(&attributes); |
| 7857 | |
| 7858 | psa_destroy_key(key); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 7859 | mbedtls_free(signature); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 7860 | PSA_DONE(); |
| 7861 | } |
| 7862 | /* END_CASE */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7863 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7864 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7865 | void sign_message_deterministic(int key_type_arg, |
| 7866 | data_t *key_data, |
| 7867 | int alg_arg, |
| 7868 | data_t *input_data, |
| 7869 | data_t *output_data) |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7870 | { |
| 7871 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7872 | psa_key_type_t key_type = key_type_arg; |
| 7873 | psa_algorithm_t alg = alg_arg; |
| 7874 | size_t key_bits; |
| 7875 | unsigned char *signature = NULL; |
| 7876 | size_t signature_size; |
| 7877 | size_t signature_length = 0xdeadbeef; |
| 7878 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7879 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7880 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7881 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7882 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 7883 | psa_set_key_algorithm(&attributes, alg); |
| 7884 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7885 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7886 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7887 | &key)); |
| 7888 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7889 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7890 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7891 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7892 | TEST_ASSERT(signature_size != 0); |
| 7893 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7894 | ASSERT_ALLOC(signature, signature_size); |
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_sign_message(key, alg, |
| 7897 | input_data->x, input_data->len, |
| 7898 | signature, signature_size, |
| 7899 | &signature_length)); |
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 | ASSERT_COMPARE(output_data->x, output_data->len, |
| 7902 | signature, signature_length); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7903 | |
| 7904 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7905 | psa_reset_key_attributes(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7906 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7907 | psa_destroy_key(key); |
| 7908 | mbedtls_free(signature); |
| 7909 | PSA_DONE(); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 7910 | |
| 7911 | } |
| 7912 | /* END_CASE */ |
| 7913 | |
| 7914 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7915 | void sign_message_fail(int key_type_arg, |
| 7916 | data_t *key_data, |
| 7917 | int alg_arg, |
| 7918 | data_t *input_data, |
| 7919 | int signature_size_arg, |
| 7920 | int expected_status_arg) |
| 7921 | { |
| 7922 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7923 | psa_key_type_t key_type = key_type_arg; |
| 7924 | psa_algorithm_t alg = alg_arg; |
| 7925 | size_t signature_size = signature_size_arg; |
| 7926 | psa_status_t actual_status; |
| 7927 | psa_status_t expected_status = expected_status_arg; |
| 7928 | unsigned char *signature = NULL; |
| 7929 | size_t signature_length = 0xdeadbeef; |
| 7930 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7931 | |
| 7932 | ASSERT_ALLOC(signature, signature_size); |
| 7933 | |
| 7934 | PSA_ASSERT(psa_crypto_init()); |
| 7935 | |
| 7936 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 7937 | psa_set_key_algorithm(&attributes, alg); |
| 7938 | psa_set_key_type(&attributes, key_type); |
| 7939 | |
| 7940 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7941 | &key)); |
| 7942 | |
| 7943 | actual_status = psa_sign_message(key, alg, |
| 7944 | input_data->x, input_data->len, |
| 7945 | signature, signature_size, |
| 7946 | &signature_length); |
| 7947 | TEST_EQUAL(actual_status, expected_status); |
| 7948 | /* The value of *signature_length is unspecified on error, but |
| 7949 | * whatever it is, it should be less than signature_size, so that |
| 7950 | * if the caller tries to read *signature_length bytes without |
| 7951 | * checking the error code then they don't overflow a buffer. */ |
| 7952 | TEST_LE_U(signature_length, signature_size); |
| 7953 | |
| 7954 | exit: |
| 7955 | psa_reset_key_attributes(&attributes); |
| 7956 | psa_destroy_key(key); |
| 7957 | mbedtls_free(signature); |
| 7958 | PSA_DONE(); |
| 7959 | } |
| 7960 | /* END_CASE */ |
| 7961 | |
| 7962 | /* BEGIN_CASE */ |
| 7963 | void sign_verify_message(int key_type_arg, |
| 7964 | data_t *key_data, |
| 7965 | int alg_arg, |
| 7966 | data_t *input_data) |
| 7967 | { |
| 7968 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7969 | psa_key_type_t key_type = key_type_arg; |
| 7970 | psa_algorithm_t alg = alg_arg; |
| 7971 | size_t key_bits; |
| 7972 | unsigned char *signature = NULL; |
| 7973 | size_t signature_size; |
| 7974 | size_t signature_length = 0xdeadbeef; |
| 7975 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7976 | |
| 7977 | PSA_ASSERT(psa_crypto_init()); |
| 7978 | |
| 7979 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 7980 | PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 7981 | psa_set_key_algorithm(&attributes, alg); |
| 7982 | psa_set_key_type(&attributes, key_type); |
| 7983 | |
| 7984 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7985 | &key)); |
| 7986 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7987 | key_bits = psa_get_key_bits(&attributes); |
| 7988 | |
| 7989 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 7990 | TEST_ASSERT(signature_size != 0); |
| 7991 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
| 7992 | ASSERT_ALLOC(signature, signature_size); |
| 7993 | |
| 7994 | PSA_ASSERT(psa_sign_message(key, alg, |
| 7995 | input_data->x, input_data->len, |
| 7996 | signature, signature_size, |
| 7997 | &signature_length)); |
| 7998 | TEST_LE_U(signature_length, signature_size); |
| 7999 | TEST_ASSERT(signature_length > 0); |
| 8000 | |
| 8001 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8002 | input_data->x, input_data->len, |
| 8003 | signature, signature_length)); |
| 8004 | |
| 8005 | if (input_data->len != 0) { |
| 8006 | /* Flip a bit in the input and verify that the signature is now |
| 8007 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 8008 | * because ECDSA may ignore the last few bits of the input. */ |
| 8009 | input_data->x[0] ^= 1; |
| 8010 | TEST_EQUAL(psa_verify_message(key, alg, |
| 8011 | input_data->x, input_data->len, |
| 8012 | signature, signature_length), |
| 8013 | PSA_ERROR_INVALID_SIGNATURE); |
| 8014 | } |
| 8015 | |
| 8016 | exit: |
| 8017 | psa_reset_key_attributes(&attributes); |
| 8018 | |
| 8019 | psa_destroy_key(key); |
| 8020 | mbedtls_free(signature); |
| 8021 | PSA_DONE(); |
| 8022 | } |
| 8023 | /* END_CASE */ |
| 8024 | |
| 8025 | /* BEGIN_CASE */ |
| 8026 | void verify_message(int key_type_arg, |
| 8027 | data_t *key_data, |
| 8028 | int alg_arg, |
| 8029 | data_t *input_data, |
| 8030 | data_t *signature_data) |
| 8031 | { |
| 8032 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8033 | psa_key_type_t key_type = key_type_arg; |
| 8034 | psa_algorithm_t alg = alg_arg; |
| 8035 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8036 | |
| 8037 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 8038 | |
| 8039 | PSA_ASSERT(psa_crypto_init()); |
| 8040 | |
| 8041 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8042 | psa_set_key_algorithm(&attributes, alg); |
| 8043 | psa_set_key_type(&attributes, key_type); |
| 8044 | |
| 8045 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8046 | &key)); |
| 8047 | |
| 8048 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8049 | input_data->x, input_data->len, |
| 8050 | signature_data->x, signature_data->len)); |
| 8051 | |
| 8052 | exit: |
| 8053 | psa_reset_key_attributes(&attributes); |
| 8054 | psa_destroy_key(key); |
| 8055 | PSA_DONE(); |
| 8056 | } |
| 8057 | /* END_CASE */ |
| 8058 | |
| 8059 | /* BEGIN_CASE */ |
| 8060 | void verify_message_fail(int key_type_arg, |
| 8061 | data_t *key_data, |
| 8062 | int alg_arg, |
| 8063 | data_t *hash_data, |
| 8064 | data_t *signature_data, |
| 8065 | int expected_status_arg) |
| 8066 | { |
| 8067 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8068 | psa_key_type_t key_type = key_type_arg; |
| 8069 | psa_algorithm_t alg = alg_arg; |
| 8070 | psa_status_t actual_status; |
| 8071 | psa_status_t expected_status = expected_status_arg; |
| 8072 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8073 | |
| 8074 | PSA_ASSERT(psa_crypto_init()); |
| 8075 | |
| 8076 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8077 | psa_set_key_algorithm(&attributes, alg); |
| 8078 | psa_set_key_type(&attributes, key_type); |
| 8079 | |
| 8080 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8081 | &key)); |
| 8082 | |
| 8083 | actual_status = psa_verify_message(key, alg, |
| 8084 | hash_data->x, hash_data->len, |
| 8085 | signature_data->x, |
| 8086 | signature_data->len); |
| 8087 | TEST_EQUAL(actual_status, expected_status); |
| 8088 | |
| 8089 | exit: |
| 8090 | psa_reset_key_attributes(&attributes); |
| 8091 | psa_destroy_key(key); |
| 8092 | PSA_DONE(); |
| 8093 | } |
| 8094 | /* END_CASE */ |
| 8095 | |
| 8096 | /* BEGIN_CASE */ |
| 8097 | void asymmetric_encrypt(int key_type_arg, |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8098 | data_t *key_data, |
| 8099 | int alg_arg, |
| 8100 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8101 | data_t *label, |
| 8102 | int expected_output_length_arg, |
| 8103 | int expected_status_arg) |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8104 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8105 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8106 | psa_key_type_t key_type = key_type_arg; |
| 8107 | psa_algorithm_t alg = alg_arg; |
| 8108 | size_t expected_output_length = expected_output_length_arg; |
| 8109 | size_t key_bits; |
| 8110 | unsigned char *output = NULL; |
| 8111 | size_t output_size; |
| 8112 | size_t output_length = ~0; |
| 8113 | psa_status_t actual_status; |
| 8114 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8115 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8116 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8117 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 8118 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8119 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8120 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 8121 | psa_set_key_algorithm(&attributes, alg); |
| 8122 | psa_set_key_type(&attributes, key_type); |
| 8123 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8124 | &key)); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8125 | |
| 8126 | /* Determine the maximum output length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8127 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8128 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8129 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8130 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8131 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
| 8132 | ASSERT_ALLOC(output, output_size); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8133 | |
| 8134 | /* Encrypt the input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8135 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8136 | input_data->x, input_data->len, |
| 8137 | label->x, label->len, |
| 8138 | output, output_size, |
| 8139 | &output_length); |
| 8140 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8141 | if (actual_status == PSA_SUCCESS) { |
| 8142 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8143 | } else { |
| 8144 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8145 | } |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8146 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8147 | /* If the label is empty, the test framework puts a non-null pointer |
| 8148 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8149 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8150 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8151 | if (output_size != 0) { |
| 8152 | memset(output, 0, output_size); |
| 8153 | } |
| 8154 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8155 | input_data->x, input_data->len, |
| 8156 | NULL, label->len, |
| 8157 | output, output_size, |
| 8158 | &output_length); |
| 8159 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8160 | if (actual_status == PSA_SUCCESS) { |
| 8161 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8162 | } else { |
| 8163 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8164 | } |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8165 | } |
| 8166 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8167 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8168 | /* |
| 8169 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8170 | * thus reset them as required. |
| 8171 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8172 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8173 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8174 | psa_destroy_key(key); |
| 8175 | mbedtls_free(output); |
| 8176 | PSA_DONE(); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8177 | } |
| 8178 | /* END_CASE */ |
| 8179 | |
| 8180 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8181 | void asymmetric_encrypt_decrypt(int key_type_arg, |
| 8182 | data_t *key_data, |
| 8183 | int alg_arg, |
| 8184 | data_t *input_data, |
| 8185 | data_t *label) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8186 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8187 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8188 | psa_key_type_t key_type = key_type_arg; |
| 8189 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8190 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8191 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8192 | size_t output_size; |
| 8193 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8194 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8195 | size_t output2_size; |
| 8196 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8197 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8198 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8199 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8200 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8201 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 8202 | psa_set_key_algorithm(&attributes, alg); |
| 8203 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8204 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8205 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8206 | &key)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8207 | |
| 8208 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8209 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8210 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8211 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8212 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8213 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
| 8214 | ASSERT_ALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8215 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8216 | output2_size = input_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8217 | TEST_LE_U(output2_size, |
| 8218 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)); |
| 8219 | TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
| 8220 | ASSERT_ALLOC(output2, output2_size); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8221 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 8222 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 8223 | * the original plaintext because of the non-optional random |
| 8224 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8225 | PSA_ASSERT(psa_asymmetric_encrypt(key, alg, |
| 8226 | input_data->x, input_data->len, |
| 8227 | label->x, label->len, |
| 8228 | output, output_size, |
| 8229 | &output_length)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8230 | /* We don't know what ciphertext length to expect, but check that |
| 8231 | * it looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8232 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8233 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8234 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8235 | output, output_length, |
| 8236 | label->x, label->len, |
| 8237 | output2, output2_size, |
| 8238 | &output2_length)); |
| 8239 | ASSERT_COMPARE(input_data->x, input_data->len, |
| 8240 | output2, output2_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8241 | |
| 8242 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8243 | /* |
| 8244 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8245 | * thus reset them as required. |
| 8246 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8247 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8248 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8249 | psa_destroy_key(key); |
| 8250 | mbedtls_free(output); |
| 8251 | mbedtls_free(output2); |
| 8252 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8253 | } |
| 8254 | /* END_CASE */ |
| 8255 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8256 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8257 | void asymmetric_decrypt(int key_type_arg, |
| 8258 | data_t *key_data, |
| 8259 | int alg_arg, |
| 8260 | data_t *input_data, |
| 8261 | data_t *label, |
| 8262 | data_t *expected_data) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8263 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8264 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8265 | psa_key_type_t key_type = key_type_arg; |
| 8266 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8267 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8268 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 8269 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8270 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8271 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8272 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8273 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8274 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8275 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8276 | psa_set_key_algorithm(&attributes, alg); |
| 8277 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8278 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8279 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8280 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8282 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8283 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8284 | |
| 8285 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8286 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8287 | TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
| 8288 | ASSERT_ALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8289 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8290 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8291 | input_data->x, input_data->len, |
| 8292 | label->x, label->len, |
| 8293 | output, |
| 8294 | output_size, |
| 8295 | &output_length)); |
| 8296 | ASSERT_COMPARE(expected_data->x, expected_data->len, |
| 8297 | output, output_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8298 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8299 | /* If the label is empty, the test framework puts a non-null pointer |
| 8300 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8301 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8302 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8303 | if (output_size != 0) { |
| 8304 | memset(output, 0, output_size); |
| 8305 | } |
| 8306 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8307 | input_data->x, input_data->len, |
| 8308 | NULL, label->len, |
| 8309 | output, |
| 8310 | output_size, |
| 8311 | &output_length)); |
| 8312 | ASSERT_COMPARE(expected_data->x, expected_data->len, |
| 8313 | output, output_length); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8314 | } |
| 8315 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8316 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8317 | psa_reset_key_attributes(&attributes); |
| 8318 | psa_destroy_key(key); |
| 8319 | mbedtls_free(output); |
| 8320 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8321 | } |
| 8322 | /* END_CASE */ |
| 8323 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8324 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8325 | void asymmetric_decrypt_fail(int key_type_arg, |
| 8326 | data_t *key_data, |
| 8327 | int alg_arg, |
| 8328 | data_t *input_data, |
| 8329 | data_t *label, |
| 8330 | int output_size_arg, |
| 8331 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8332 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8333 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8334 | psa_key_type_t key_type = key_type_arg; |
| 8335 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8336 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 8337 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8338 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8339 | psa_status_t actual_status; |
| 8340 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8341 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8342 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8343 | ASSERT_ALLOC(output, output_size); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8344 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8345 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8346 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8347 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8348 | psa_set_key_algorithm(&attributes, alg); |
| 8349 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8350 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8351 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8352 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8354 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8355 | input_data->x, input_data->len, |
| 8356 | label->x, label->len, |
| 8357 | output, output_size, |
| 8358 | &output_length); |
| 8359 | TEST_EQUAL(actual_status, expected_status); |
| 8360 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8361 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8362 | /* If the label is empty, the test framework puts a non-null pointer |
| 8363 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8364 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8365 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8366 | if (output_size != 0) { |
| 8367 | memset(output, 0, output_size); |
| 8368 | } |
| 8369 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8370 | input_data->x, input_data->len, |
| 8371 | NULL, label->len, |
| 8372 | output, output_size, |
| 8373 | &output_length); |
| 8374 | TEST_EQUAL(actual_status, expected_status); |
| 8375 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8376 | } |
| 8377 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8378 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8379 | psa_reset_key_attributes(&attributes); |
| 8380 | psa_destroy_key(key); |
| 8381 | mbedtls_free(output); |
| 8382 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8383 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8384 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8385 | |
| 8386 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8387 | void key_derivation_init() |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8388 | { |
| 8389 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 8390 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 8391 | * 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] | 8392 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8393 | size_t capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8394 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init(); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 8395 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8396 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8397 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8398 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8399 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8400 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8401 | TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity), |
| 8402 | PSA_ERROR_BAD_STATE); |
| 8403 | TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity), |
| 8404 | PSA_ERROR_BAD_STATE); |
| 8405 | TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity), |
| 8406 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8407 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8408 | /* A default operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8409 | PSA_ASSERT(psa_key_derivation_abort(&func)); |
| 8410 | PSA_ASSERT(psa_key_derivation_abort(&init)); |
| 8411 | PSA_ASSERT(psa_key_derivation_abort(&zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8412 | } |
| 8413 | /* END_CASE */ |
| 8414 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 8415 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8416 | void derive_setup(int alg_arg, int expected_status_arg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8417 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8418 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8419 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8420 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8421 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8422 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8423 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8424 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8425 | expected_status); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8426 | |
| 8427 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8428 | psa_key_derivation_abort(&operation); |
| 8429 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8430 | } |
| 8431 | /* END_CASE */ |
| 8432 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8433 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8434 | void derive_set_capacity(int alg_arg, int capacity_arg, |
| 8435 | int expected_status_arg) |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8436 | { |
| 8437 | psa_algorithm_t alg = alg_arg; |
| 8438 | size_t capacity = capacity_arg; |
| 8439 | psa_status_t expected_status = expected_status_arg; |
| 8440 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8441 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8442 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8444 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8445 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8446 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 8447 | expected_status); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8448 | |
| 8449 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8450 | psa_key_derivation_abort(&operation); |
| 8451 | PSA_DONE(); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8452 | } |
| 8453 | /* END_CASE */ |
| 8454 | |
| 8455 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8456 | void derive_input(int alg_arg, |
| 8457 | int step_arg1, int key_type_arg1, data_t *input1, |
| 8458 | int expected_status_arg1, |
| 8459 | int step_arg2, int key_type_arg2, data_t *input2, |
| 8460 | int expected_status_arg2, |
| 8461 | int step_arg3, int key_type_arg3, data_t *input3, |
| 8462 | int expected_status_arg3, |
| 8463 | int output_key_type_arg, int expected_output_status_arg) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8464 | { |
| 8465 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8466 | psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 }; |
| 8467 | psa_key_type_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 }; |
| 8468 | psa_status_t expected_statuses[] = { expected_status_arg1, |
| 8469 | expected_status_arg2, |
| 8470 | expected_status_arg3 }; |
| 8471 | data_t *inputs[] = { input1, input2, input3 }; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8472 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8473 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8474 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8475 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8476 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8477 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8478 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8479 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8480 | psa_status_t expected_output_status = expected_output_status_arg; |
| 8481 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8482 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8483 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8484 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8485 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8486 | psa_set_key_algorithm(&attributes, alg); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8487 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8488 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8489 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8490 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8491 | mbedtls_test_set_step(i); |
| 8492 | if (steps[i] == 0) { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 8493 | /* Skip this step */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8494 | } else if (key_types[i] != PSA_KEY_TYPE_NONE) { |
| 8495 | psa_set_key_type(&attributes, key_types[i]); |
| 8496 | PSA_ASSERT(psa_import_key(&attributes, |
| 8497 | inputs[i]->x, inputs[i]->len, |
| 8498 | &keys[i])); |
| 8499 | if (PSA_KEY_TYPE_IS_KEY_PAIR(key_types[i]) && |
| 8500 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8501 | // When taking a private key as secret input, use key agreement |
| 8502 | // to add the shared secret to the derivation |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8503 | TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self( |
| 8504 | &operation, keys[i]), |
| 8505 | expected_statuses[i]); |
| 8506 | } else { |
| 8507 | TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i], |
| 8508 | keys[i]), |
| 8509 | expected_statuses[i]); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8510 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8511 | } else { |
| 8512 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8513 | &operation, steps[i], |
| 8514 | inputs[i]->x, inputs[i]->len), |
| 8515 | expected_statuses[i]); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8516 | } |
| 8517 | } |
| 8518 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8519 | if (output_key_type != PSA_KEY_TYPE_NONE) { |
| 8520 | psa_reset_key_attributes(&attributes); |
| 8521 | psa_set_key_type(&attributes, output_key_type); |
| 8522 | psa_set_key_bits(&attributes, 8); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8523 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8524 | psa_key_derivation_output_key(&attributes, &operation, |
| 8525 | &output_key); |
| 8526 | } else { |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8527 | uint8_t buffer[1]; |
| 8528 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8529 | psa_key_derivation_output_bytes(&operation, |
| 8530 | buffer, sizeof(buffer)); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8531 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8532 | TEST_EQUAL(actual_output_status, expected_output_status); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8533 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8534 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8535 | psa_key_derivation_abort(&operation); |
| 8536 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8537 | psa_destroy_key(keys[i]); |
| 8538 | } |
| 8539 | psa_destroy_key(output_key); |
| 8540 | PSA_DONE(); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8541 | } |
| 8542 | /* END_CASE */ |
| 8543 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8544 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8545 | void derive_over_capacity(int alg_arg) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8546 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8547 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8548 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 8549 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8550 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8551 | unsigned char input1[] = "Input 1"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8552 | size_t input1_length = sizeof(input1); |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8553 | unsigned char input2[] = "Input 2"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8554 | size_t input2_length = sizeof(input2); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8555 | uint8_t buffer[42]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8556 | size_t capacity = sizeof(buffer); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 8557 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 8558 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8559 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8560 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8562 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8563 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8564 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8565 | psa_set_key_algorithm(&attributes, alg); |
| 8566 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8567 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8568 | PSA_ASSERT(psa_import_key(&attributes, |
| 8569 | key_data, sizeof(key_data), |
| 8570 | &key)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8571 | |
| 8572 | /* valid key derivation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8573 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8574 | input1, input1_length, |
| 8575 | input2, input2_length, |
| 8576 | capacity)) { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8577 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8578 | } |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8579 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8580 | /* state of operation shouldn't allow additional generation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8581 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8582 | PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8583 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8584 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8585 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8586 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity), |
| 8587 | PSA_ERROR_INSUFFICIENT_DATA); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8588 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8589 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8590 | psa_key_derivation_abort(&operation); |
| 8591 | psa_destroy_key(key); |
| 8592 | PSA_DONE(); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8593 | } |
| 8594 | /* END_CASE */ |
| 8595 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8596 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8597 | void derive_actions_without_setup() |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8598 | { |
| 8599 | uint8_t output_buffer[16]; |
| 8600 | size_t buffer_size = 16; |
| 8601 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8602 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8603 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8604 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8605 | output_buffer, buffer_size) |
| 8606 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8607 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8608 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8609 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8610 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8611 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8612 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8613 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8614 | output_buffer, buffer_size) |
| 8615 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8616 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8617 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8618 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8619 | |
| 8620 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8621 | psa_key_derivation_abort(&operation); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8622 | } |
| 8623 | /* END_CASE */ |
| 8624 | |
| 8625 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8626 | void derive_output(int alg_arg, |
| 8627 | int step1_arg, data_t *input1, int expected_status_arg1, |
| 8628 | int step2_arg, data_t *input2, int expected_status_arg2, |
| 8629 | int step3_arg, data_t *input3, int expected_status_arg3, |
| 8630 | int step4_arg, data_t *input4, int expected_status_arg4, |
| 8631 | data_t *key_agreement_peer_key, |
| 8632 | int requested_capacity_arg, |
| 8633 | data_t *expected_output1, |
| 8634 | data_t *expected_output2, |
| 8635 | int other_key_input_type, |
| 8636 | int key_input_type, |
| 8637 | int derive_type) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8638 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8639 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8640 | psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg }; |
| 8641 | data_t *inputs[] = { input1, input2, input3, input4 }; |
| 8642 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8643 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8644 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8645 | MBEDTLS_SVC_KEY_ID_INIT }; |
| 8646 | psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2, |
| 8647 | expected_status_arg3, expected_status_arg4 }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8648 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8649 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8650 | uint8_t *expected_outputs[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8651 | { expected_output1->x, expected_output2->x }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8652 | size_t output_sizes[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8653 | { expected_output1->len, expected_output2->len }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8654 | size_t output_buffer_size = 0; |
| 8655 | uint8_t *output_buffer = NULL; |
| 8656 | size_t expected_capacity; |
| 8657 | size_t current_capacity; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8658 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; |
| 8659 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; |
| 8660 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; |
| 8661 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8662 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8663 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8664 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8665 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8666 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
| 8667 | if (output_sizes[i] > output_buffer_size) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8668 | output_buffer_size = output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8669 | } |
| 8670 | if (output_sizes[i] == 0) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8671 | expected_outputs[i] = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8672 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8673 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8674 | ASSERT_ALLOC(output_buffer, output_buffer_size); |
| 8675 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8676 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8677 | /* Extraction phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8678 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8679 | PSA_ASSERT(psa_key_derivation_set_capacity(&operation, |
| 8680 | requested_capacity)); |
| 8681 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8682 | switch (steps[i]) { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8683 | case 0: |
| 8684 | break; |
| 8685 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8686 | switch (key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8687 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8688 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8689 | &operation, steps[i], |
| 8690 | inputs[i]->x, inputs[i]->len), |
| 8691 | statuses[i]); |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8692 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8693 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 8694 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8695 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8696 | break; |
| 8697 | case 1: // input key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8698 | psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE); |
| 8699 | psa_set_key_algorithm(&attributes1, alg); |
| 8700 | psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8701 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8702 | PSA_ASSERT(psa_import_key(&attributes1, |
| 8703 | inputs[i]->x, inputs[i]->len, |
| 8704 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8705 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8706 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 8707 | PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1)); |
| 8708 | TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)), |
| 8709 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8710 | } |
| 8711 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8712 | PSA_ASSERT(psa_key_derivation_input_key(&operation, |
| 8713 | steps[i], |
| 8714 | keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8715 | break; |
| 8716 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8717 | TEST_ASSERT(!"default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8718 | break; |
| 8719 | } |
| 8720 | break; |
| 8721 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8722 | switch (other_key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8723 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8724 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 8725 | steps[i], |
| 8726 | inputs[i]->x, |
| 8727 | inputs[i]->len), |
| 8728 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8729 | break; |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 8730 | case 1: // input key, type DERIVE |
| 8731 | case 11: // input key, type RAW |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8732 | psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE); |
| 8733 | psa_set_key_algorithm(&attributes2, alg); |
| 8734 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8735 | |
| 8736 | // other secret of type RAW_DATA passed with input_key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8737 | if (other_key_input_type == 11) { |
| 8738 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA); |
| 8739 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8740 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8741 | PSA_ASSERT(psa_import_key(&attributes2, |
| 8742 | inputs[i]->x, inputs[i]->len, |
| 8743 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8744 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8745 | TEST_EQUAL(psa_key_derivation_input_key(&operation, |
| 8746 | steps[i], |
| 8747 | keys[i]), |
| 8748 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8749 | break; |
| 8750 | case 2: // key agreement |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8751 | psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE); |
| 8752 | psa_set_key_algorithm(&attributes3, alg); |
| 8753 | psa_set_key_type(&attributes3, |
| 8754 | PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8755 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8756 | PSA_ASSERT(psa_import_key(&attributes3, |
| 8757 | inputs[i]->x, inputs[i]->len, |
| 8758 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8759 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8760 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 8761 | &operation, |
| 8762 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 8763 | keys[i], key_agreement_peer_key->x, |
| 8764 | key_agreement_peer_key->len), statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8765 | break; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8766 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8767 | TEST_ASSERT(!"default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8768 | break; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8769 | } |
| 8770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8771 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8772 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8773 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8774 | break; |
| 8775 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8776 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8777 | &operation, steps[i], |
| 8778 | inputs[i]->x, inputs[i]->len), statuses[i]); |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 8779 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8780 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 8781 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8782 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8783 | break; |
| 8784 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 8785 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 8786 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8787 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8788 | ¤t_capacity)); |
| 8789 | TEST_EQUAL(current_capacity, requested_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8790 | expected_capacity = requested_capacity; |
| 8791 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8792 | if (derive_type == 1) { // output key |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8793 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; |
| 8794 | |
| 8795 | /* For output key derivation secret must be provided using |
| 8796 | input key, otherwise operation is not permitted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8797 | if (key_input_type == 1) { |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 8798 | expected_status = PSA_SUCCESS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8799 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8800 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8801 | psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT); |
| 8802 | psa_set_key_algorithm(&attributes4, alg); |
| 8803 | psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE); |
| 8804 | psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8805 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8806 | TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation, |
| 8807 | &derived_key), expected_status); |
| 8808 | } else { // output bytes |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8809 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8810 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8811 | /* Read some bytes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8812 | status = psa_key_derivation_output_bytes(&operation, |
| 8813 | output_buffer, output_sizes[i]); |
| 8814 | if (expected_capacity == 0 && output_sizes[i] == 0) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8815 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8816 | TEST_ASSERT(status == PSA_SUCCESS || |
| 8817 | status == PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8818 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8819 | } else if (expected_capacity == 0 || |
| 8820 | output_sizes[i] > expected_capacity) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8821 | /* Capacity exceeded. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8822 | TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8823 | expected_capacity = 0; |
| 8824 | continue; |
| 8825 | } |
| 8826 | /* Success. Check the read data. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8827 | PSA_ASSERT(status); |
| 8828 | if (output_sizes[i] != 0) { |
| 8829 | ASSERT_COMPARE(output_buffer, output_sizes[i], |
| 8830 | expected_outputs[i], output_sizes[i]); |
| 8831 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 8832 | /* Check the operation status. */ |
| 8833 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8834 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8835 | ¤t_capacity)); |
| 8836 | TEST_EQUAL(expected_capacity, current_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8837 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8838 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8839 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8840 | |
| 8841 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8842 | mbedtls_free(output_buffer); |
| 8843 | psa_key_derivation_abort(&operation); |
| 8844 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8845 | psa_destroy_key(keys[i]); |
| 8846 | } |
| 8847 | psa_destroy_key(derived_key); |
| 8848 | PSA_DONE(); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 8849 | } |
| 8850 | /* END_CASE */ |
| 8851 | |
| 8852 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8853 | void derive_full(int alg_arg, |
| 8854 | data_t *key_data, |
| 8855 | data_t *input1, |
| 8856 | data_t *input2, |
| 8857 | int requested_capacity_arg) |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8858 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8859 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8860 | psa_algorithm_t alg = alg_arg; |
| 8861 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8862 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8863 | unsigned char output_buffer[16]; |
| 8864 | size_t expected_capacity = requested_capacity; |
| 8865 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8866 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8867 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8868 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8869 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8870 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8871 | psa_set_key_algorithm(&attributes, alg); |
| 8872 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8873 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8874 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8875 | &key)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8876 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8877 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8878 | input1->x, input1->len, |
| 8879 | input2->x, input2->len, |
| 8880 | requested_capacity)) { |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 8881 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8882 | } |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 8883 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8884 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8885 | ¤t_capacity)); |
| 8886 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8887 | |
| 8888 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8889 | while (current_capacity > 0) { |
| 8890 | size_t read_size = sizeof(output_buffer); |
| 8891 | if (read_size > current_capacity) { |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8892 | read_size = current_capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8893 | } |
| 8894 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8895 | output_buffer, |
| 8896 | read_size)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8897 | expected_capacity -= read_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8898 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 8899 | ¤t_capacity)); |
| 8900 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8901 | } |
| 8902 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8903 | /* Check that the operation refuses to go over capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8904 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1), |
| 8905 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8906 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8907 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8908 | |
| 8909 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8910 | psa_key_derivation_abort(&operation); |
| 8911 | psa_destroy_key(key); |
| 8912 | PSA_DONE(); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 8913 | } |
| 8914 | /* END_CASE */ |
| 8915 | |
Przemek Stekiel | 8258ea7 | 2022-10-19 12:17:19 +0200 | [diff] [blame] | 8916 | /* 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] | 8917 | void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg, |
| 8918 | int derivation_step, |
| 8919 | int capacity, int expected_capacity_status_arg, |
| 8920 | data_t *expected_output, |
| 8921 | int expected_output_status_arg) |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8922 | { |
| 8923 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 8924 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | d378504 | 2022-09-16 06:45:44 -0400 | [diff] [blame] | 8925 | 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] | 8926 | uint8_t *output_buffer = NULL; |
| 8927 | psa_status_t status; |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 8928 | psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg; |
| 8929 | psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg; |
| 8930 | 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] | 8931 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8932 | ASSERT_ALLOC(output_buffer, expected_output->len); |
| 8933 | PSA_ASSERT(psa_crypto_init()); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8934 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8935 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8936 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 8937 | expected_capacity_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8938 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8939 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 8940 | step, input->x, input->len), |
| 8941 | expected_input_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8942 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8943 | if (((psa_status_t) expected_input_status) != PSA_SUCCESS) { |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8944 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8945 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8946 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8947 | status = psa_key_derivation_output_bytes(&operation, output_buffer, |
| 8948 | expected_output->len); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8949 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8950 | TEST_EQUAL(status, expected_output_status); |
| 8951 | if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) { |
| 8952 | ASSERT_COMPARE(output_buffer, expected_output->len, expected_output->x, |
| 8953 | expected_output->len); |
| 8954 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8955 | |
| 8956 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8957 | mbedtls_free(output_buffer); |
| 8958 | psa_key_derivation_abort(&operation); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 8959 | PSA_DONE(); |
| 8960 | } |
| 8961 | /* END_CASE */ |
| 8962 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 8963 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8964 | void derive_key_exercise(int alg_arg, |
| 8965 | data_t *key_data, |
| 8966 | data_t *input1, |
| 8967 | data_t *input2, |
| 8968 | int derived_type_arg, |
| 8969 | int derived_bits_arg, |
| 8970 | int derived_usage_arg, |
| 8971 | int derived_alg_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8972 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8973 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8974 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8975 | psa_algorithm_t alg = alg_arg; |
| 8976 | psa_key_type_t derived_type = derived_type_arg; |
| 8977 | size_t derived_bits = derived_bits_arg; |
| 8978 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 8979 | psa_algorithm_t derived_alg = derived_alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8980 | size_t capacity = PSA_BITS_TO_BYTES(derived_bits); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8981 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8982 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8983 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8984 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8985 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8986 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8987 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8988 | psa_set_key_algorithm(&attributes, alg); |
| 8989 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
| 8990 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8991 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8992 | |
| 8993 | /* Derive a key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8994 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 8995 | input1->x, input1->len, |
| 8996 | input2->x, input2->len, |
| 8997 | capacity)) { |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 8998 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8999 | } |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9000 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9001 | psa_set_key_usage_flags(&attributes, derived_usage); |
| 9002 | psa_set_key_algorithm(&attributes, derived_alg); |
| 9003 | psa_set_key_type(&attributes, derived_type); |
| 9004 | psa_set_key_bits(&attributes, derived_bits); |
| 9005 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation, |
| 9006 | &derived_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9007 | |
| 9008 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9009 | PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes)); |
| 9010 | TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type); |
| 9011 | TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9012 | |
| 9013 | /* Exercise the derived key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9014 | if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) { |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9015 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9016 | } |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9017 | |
| 9018 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9019 | /* |
| 9020 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9021 | * thus reset them as required. |
| 9022 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9023 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9024 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9025 | psa_key_derivation_abort(&operation); |
| 9026 | psa_destroy_key(base_key); |
| 9027 | psa_destroy_key(derived_key); |
| 9028 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9029 | } |
| 9030 | /* END_CASE */ |
| 9031 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9032 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9033 | void derive_key_export(int alg_arg, |
| 9034 | data_t *key_data, |
| 9035 | data_t *input1, |
| 9036 | data_t *input2, |
| 9037 | int bytes1_arg, |
| 9038 | int bytes2_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9039 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9040 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9041 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9042 | psa_algorithm_t alg = alg_arg; |
| 9043 | size_t bytes1 = bytes1_arg; |
| 9044 | size_t bytes2 = bytes2_arg; |
| 9045 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9046 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9047 | uint8_t *output_buffer = NULL; |
| 9048 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9049 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9050 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9051 | size_t length; |
| 9052 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9053 | ASSERT_ALLOC(output_buffer, capacity); |
| 9054 | ASSERT_ALLOC(export_buffer, capacity); |
| 9055 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9056 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9057 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9058 | psa_set_key_algorithm(&base_attributes, alg); |
| 9059 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9060 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9061 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9062 | |
| 9063 | /* Derive some material and output it. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9064 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9065 | input1->x, input1->len, |
| 9066 | input2->x, input2->len, |
| 9067 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9068 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9069 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9070 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9071 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9072 | output_buffer, |
| 9073 | capacity)); |
| 9074 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9075 | |
| 9076 | /* 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] | 9077 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9078 | input1->x, input1->len, |
| 9079 | input2->x, input2->len, |
| 9080 | capacity)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9081 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9082 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9083 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9084 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9085 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9086 | psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA); |
| 9087 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1)); |
| 9088 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9089 | &derived_key)); |
| 9090 | PSA_ASSERT(psa_export_key(derived_key, |
| 9091 | export_buffer, bytes1, |
| 9092 | &length)); |
| 9093 | TEST_EQUAL(length, bytes1); |
| 9094 | PSA_ASSERT(psa_destroy_key(derived_key)); |
| 9095 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2)); |
| 9096 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9097 | &derived_key)); |
| 9098 | PSA_ASSERT(psa_export_key(derived_key, |
| 9099 | export_buffer + bytes1, bytes2, |
| 9100 | &length)); |
| 9101 | TEST_EQUAL(length, bytes2); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9102 | |
| 9103 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9104 | ASSERT_COMPARE(output_buffer, bytes1 + bytes2, |
| 9105 | export_buffer, capacity); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9106 | |
| 9107 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9108 | mbedtls_free(output_buffer); |
| 9109 | mbedtls_free(export_buffer); |
| 9110 | psa_key_derivation_abort(&operation); |
| 9111 | psa_destroy_key(base_key); |
| 9112 | psa_destroy_key(derived_key); |
| 9113 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9114 | } |
| 9115 | /* END_CASE */ |
| 9116 | |
| 9117 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9118 | void derive_key_type(int alg_arg, |
| 9119 | data_t *key_data, |
| 9120 | data_t *input1, |
| 9121 | data_t *input2, |
| 9122 | int key_type_arg, int bits_arg, |
| 9123 | data_t *expected_export) |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9124 | { |
| 9125 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9126 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9127 | const psa_algorithm_t alg = alg_arg; |
| 9128 | const psa_key_type_t key_type = key_type_arg; |
| 9129 | const size_t bits = bits_arg; |
| 9130 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9131 | const size_t export_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9132 | PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9133 | uint8_t *export_buffer = NULL; |
| 9134 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9135 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9136 | size_t export_length; |
| 9137 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9138 | ASSERT_ALLOC(export_buffer, export_buffer_size); |
| 9139 | PSA_ASSERT(psa_crypto_init()); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9140 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9141 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9142 | psa_set_key_algorithm(&base_attributes, alg); |
| 9143 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9144 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9145 | &base_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9146 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9147 | if (mbedtls_test_psa_setup_key_derivation_wrap( |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9148 | &operation, base_key, alg, |
| 9149 | input1->x, input1->len, |
| 9150 | input2->x, input2->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9151 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) { |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9152 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9153 | } |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9154 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9155 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9156 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9157 | psa_set_key_type(&derived_attributes, key_type); |
| 9158 | psa_set_key_bits(&derived_attributes, bits); |
| 9159 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9160 | &derived_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9161 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9162 | PSA_ASSERT(psa_export_key(derived_key, |
| 9163 | export_buffer, export_buffer_size, |
| 9164 | &export_length)); |
| 9165 | ASSERT_COMPARE(export_buffer, export_length, |
| 9166 | expected_export->x, expected_export->len); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9167 | |
| 9168 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9169 | mbedtls_free(export_buffer); |
| 9170 | psa_key_derivation_abort(&operation); |
| 9171 | psa_destroy_key(base_key); |
| 9172 | psa_destroy_key(derived_key); |
| 9173 | PSA_DONE(); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9174 | } |
| 9175 | /* END_CASE */ |
| 9176 | |
| 9177 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9178 | void derive_key(int alg_arg, |
| 9179 | data_t *key_data, data_t *input1, data_t *input2, |
| 9180 | int type_arg, int bits_arg, |
| 9181 | int expected_status_arg, |
| 9182 | int is_large_output) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9183 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9184 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9185 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9186 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 9187 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9188 | size_t bits = bits_arg; |
| 9189 | psa_status_t expected_status = expected_status_arg; |
| 9190 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9191 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9192 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9193 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9194 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9195 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9196 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9197 | psa_set_key_algorithm(&base_attributes, alg); |
| 9198 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9199 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9200 | &base_key)); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9201 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9202 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9203 | input1->x, input1->len, |
| 9204 | input2->x, input2->len, |
| 9205 | SIZE_MAX)) { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9206 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9207 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9208 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9209 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9210 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9211 | psa_set_key_type(&derived_attributes, type); |
| 9212 | psa_set_key_bits(&derived_attributes, bits); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9213 | |
| 9214 | psa_status_t status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9215 | psa_key_derivation_output_key(&derived_attributes, |
| 9216 | &operation, |
| 9217 | &derived_key); |
| 9218 | if (is_large_output > 0) { |
| 9219 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9220 | } |
| 9221 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9222 | |
| 9223 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9224 | psa_key_derivation_abort(&operation); |
| 9225 | psa_destroy_key(base_key); |
| 9226 | psa_destroy_key(derived_key); |
| 9227 | PSA_DONE(); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9228 | } |
| 9229 | /* END_CASE */ |
| 9230 | |
| 9231 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9232 | void key_agreement_setup(int alg_arg, |
| 9233 | int our_key_type_arg, int our_key_alg_arg, |
| 9234 | data_t *our_key_data, data_t *peer_key_data, |
| 9235 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9236 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9237 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9238 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 9239 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9240 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9241 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9242 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9243 | psa_status_t expected_status = expected_status_arg; |
| 9244 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9245 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9246 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9247 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9248 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9249 | psa_set_key_algorithm(&attributes, our_key_alg); |
| 9250 | psa_set_key_type(&attributes, our_key_type); |
| 9251 | PSA_ASSERT(psa_import_key(&attributes, |
| 9252 | our_key_data->x, our_key_data->len, |
| 9253 | &our_key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9254 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9255 | /* The tests currently include inputs that should fail at either step. |
| 9256 | * Test cases that fail at the setup step should be changed to call |
| 9257 | * key_derivation_setup instead, and this function should be renamed |
| 9258 | * to key_agreement_fail. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9259 | status = psa_key_derivation_setup(&operation, alg); |
| 9260 | if (status == PSA_SUCCESS) { |
| 9261 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 9262 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 9263 | our_key, |
| 9264 | peer_key_data->x, peer_key_data->len), |
| 9265 | expected_status); |
| 9266 | } else { |
| 9267 | TEST_ASSERT(status == expected_status); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9268 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9269 | |
| 9270 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9271 | psa_key_derivation_abort(&operation); |
| 9272 | psa_destroy_key(our_key); |
| 9273 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9274 | } |
| 9275 | /* END_CASE */ |
| 9276 | |
| 9277 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9278 | void raw_key_agreement(int alg_arg, |
| 9279 | int our_key_type_arg, data_t *our_key_data, |
| 9280 | data_t *peer_key_data, |
| 9281 | data_t *expected_output) |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9282 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9283 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9284 | psa_algorithm_t alg = alg_arg; |
| 9285 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9286 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9287 | unsigned char *output = NULL; |
| 9288 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9289 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9290 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9291 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9292 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9293 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9294 | psa_set_key_algorithm(&attributes, alg); |
| 9295 | psa_set_key_type(&attributes, our_key_type); |
| 9296 | PSA_ASSERT(psa_import_key(&attributes, |
| 9297 | our_key_data->x, our_key_data->len, |
| 9298 | &our_key)); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9299 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9300 | PSA_ASSERT(psa_get_key_attributes(our_key, &attributes)); |
| 9301 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9302 | |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9303 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9304 | TEST_LE_U(expected_output->len, |
| 9305 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits)); |
| 9306 | TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits), |
| 9307 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9308 | |
| 9309 | /* Good case with exact output size */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9310 | ASSERT_ALLOC(output, expected_output->len); |
| 9311 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9312 | peer_key_data->x, peer_key_data->len, |
| 9313 | output, expected_output->len, |
| 9314 | &output_length)); |
| 9315 | ASSERT_COMPARE(output, output_length, |
| 9316 | expected_output->x, expected_output->len); |
| 9317 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9318 | output = NULL; |
| 9319 | output_length = ~0; |
| 9320 | |
| 9321 | /* Larger buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9322 | ASSERT_ALLOC(output, expected_output->len + 1); |
| 9323 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9324 | peer_key_data->x, peer_key_data->len, |
| 9325 | output, expected_output->len + 1, |
| 9326 | &output_length)); |
| 9327 | ASSERT_COMPARE(output, output_length, |
| 9328 | expected_output->x, expected_output->len); |
| 9329 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9330 | output = NULL; |
| 9331 | output_length = ~0; |
| 9332 | |
| 9333 | /* Buffer too small */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9334 | ASSERT_ALLOC(output, expected_output->len - 1); |
| 9335 | TEST_EQUAL(psa_raw_key_agreement(alg, our_key, |
| 9336 | peer_key_data->x, peer_key_data->len, |
| 9337 | output, expected_output->len - 1, |
| 9338 | &output_length), |
| 9339 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9340 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9341 | TEST_LE_U(output_length, expected_output->len - 1); |
| 9342 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9343 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9344 | |
| 9345 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9346 | mbedtls_free(output); |
| 9347 | psa_destroy_key(our_key); |
| 9348 | PSA_DONE(); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9349 | } |
| 9350 | /* END_CASE */ |
| 9351 | |
| 9352 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9353 | void key_agreement_capacity(int alg_arg, |
| 9354 | int our_key_type_arg, data_t *our_key_data, |
| 9355 | data_t *peer_key_data, |
| 9356 | int expected_capacity_arg) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9357 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9358 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9359 | psa_algorithm_t alg = alg_arg; |
| 9360 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9361 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9362 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9363 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9364 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9365 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9366 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9367 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9368 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9369 | psa_set_key_algorithm(&attributes, alg); |
| 9370 | psa_set_key_type(&attributes, our_key_type); |
| 9371 | PSA_ASSERT(psa_import_key(&attributes, |
| 9372 | our_key_data->x, our_key_data->len, |
| 9373 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9374 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9375 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9376 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9377 | &operation, |
| 9378 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9379 | peer_key_data->x, peer_key_data->len)); |
| 9380 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9381 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9382 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9383 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9384 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9385 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9386 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 9387 | /* Test the advertised capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9388 | PSA_ASSERT(psa_key_derivation_get_capacity( |
| 9389 | &operation, &actual_capacity)); |
| 9390 | TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9391 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9392 | /* Test the actual capacity by reading the output. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9393 | while (actual_capacity > sizeof(output)) { |
| 9394 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9395 | output, sizeof(output))); |
| 9396 | actual_capacity -= sizeof(output); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9397 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9398 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9399 | output, actual_capacity)); |
| 9400 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1), |
| 9401 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9402 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9403 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9404 | psa_key_derivation_abort(&operation); |
| 9405 | psa_destroy_key(our_key); |
| 9406 | PSA_DONE(); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9407 | } |
| 9408 | /* END_CASE */ |
| 9409 | |
| 9410 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9411 | void key_agreement_output(int alg_arg, |
| 9412 | int our_key_type_arg, data_t *our_key_data, |
| 9413 | data_t *peer_key_data, |
| 9414 | data_t *expected_output1, data_t *expected_output2) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9415 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9416 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9417 | psa_algorithm_t alg = alg_arg; |
| 9418 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9419 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9420 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9421 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9422 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9423 | ASSERT_ALLOC(actual_output, MAX(expected_output1->len, |
| 9424 | expected_output2->len)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9425 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9426 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9427 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9428 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9429 | psa_set_key_algorithm(&attributes, alg); |
| 9430 | psa_set_key_type(&attributes, our_key_type); |
| 9431 | PSA_ASSERT(psa_import_key(&attributes, |
| 9432 | our_key_data->x, our_key_data->len, |
| 9433 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9434 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9435 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9436 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9437 | &operation, |
| 9438 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9439 | peer_key_data->x, peer_key_data->len)); |
| 9440 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9441 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9442 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9443 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9444 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9445 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9446 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9447 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9448 | actual_output, |
| 9449 | expected_output1->len)); |
| 9450 | ASSERT_COMPARE(actual_output, expected_output1->len, |
| 9451 | expected_output1->x, expected_output1->len); |
| 9452 | if (expected_output2->len != 0) { |
| 9453 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9454 | actual_output, |
| 9455 | expected_output2->len)); |
| 9456 | ASSERT_COMPARE(actual_output, expected_output2->len, |
| 9457 | expected_output2->x, expected_output2->len); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9458 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9459 | |
| 9460 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9461 | psa_key_derivation_abort(&operation); |
| 9462 | psa_destroy_key(our_key); |
| 9463 | PSA_DONE(); |
| 9464 | mbedtls_free(actual_output); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9465 | } |
| 9466 | /* END_CASE */ |
| 9467 | |
| 9468 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9469 | void generate_random(int bytes_arg) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9470 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9471 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9472 | unsigned char *output = NULL; |
| 9473 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9474 | size_t i; |
| 9475 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9476 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9477 | TEST_ASSERT(bytes_arg >= 0); |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 9478 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9479 | ASSERT_ALLOC(output, bytes); |
| 9480 | ASSERT_ALLOC(changed, bytes); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9481 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9482 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9483 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9484 | /* Run several times, to ensure that every output byte will be |
| 9485 | * nonzero at least once with overwhelming probability |
| 9486 | * (2^(-8*number_of_runs)). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9487 | for (run = 0; run < 10; run++) { |
| 9488 | if (bytes != 0) { |
| 9489 | memset(output, 0, bytes); |
| 9490 | } |
| 9491 | PSA_ASSERT(psa_generate_random(output, bytes)); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9492 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9493 | for (i = 0; i < bytes; i++) { |
| 9494 | if (output[i] != 0) { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9495 | ++changed[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9496 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9497 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9498 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9499 | |
| 9500 | /* Check that every byte was changed to nonzero at least once. This |
| 9501 | * validates that psa_generate_random is overwriting every byte of |
| 9502 | * the output buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9503 | for (i = 0; i < bytes; i++) { |
| 9504 | TEST_ASSERT(changed[i] != 0); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9505 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9506 | |
| 9507 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9508 | PSA_DONE(); |
| 9509 | mbedtls_free(output); |
| 9510 | mbedtls_free(changed); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9511 | } |
| 9512 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9513 | |
| 9514 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9515 | void generate_key(int type_arg, |
| 9516 | int bits_arg, |
| 9517 | int usage_arg, |
| 9518 | int alg_arg, |
| 9519 | int expected_status_arg, |
| 9520 | int is_large_key) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9521 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9522 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9523 | psa_key_type_t type = type_arg; |
| 9524 | psa_key_usage_t usage = usage_arg; |
| 9525 | size_t bits = bits_arg; |
| 9526 | psa_algorithm_t alg = alg_arg; |
| 9527 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9528 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 9529 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9530 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9531 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9532 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9533 | psa_set_key_usage_flags(&attributes, usage); |
| 9534 | psa_set_key_algorithm(&attributes, alg); |
| 9535 | psa_set_key_type(&attributes, type); |
| 9536 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9537 | |
| 9538 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9539 | psa_status_t status = psa_generate_key(&attributes, &key); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9540 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9541 | if (is_large_key > 0) { |
| 9542 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9543 | } |
| 9544 | TEST_EQUAL(status, expected_status); |
| 9545 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9546 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9547 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9548 | |
| 9549 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9550 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 9551 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 9552 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9553 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 9554 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9555 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +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 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9560 | /* |
| 9561 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9562 | * thus reset them as required. |
| 9563 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9564 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9565 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9566 | psa_destroy_key(key); |
| 9567 | PSA_DONE(); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 9568 | } |
| 9569 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 9570 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 9571 | /* 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] | 9572 | void generate_key_rsa(int bits_arg, |
| 9573 | data_t *e_arg, |
| 9574 | int expected_status_arg) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9575 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9576 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 9577 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9578 | size_t bits = bits_arg; |
| 9579 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 9580 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 9581 | psa_status_t expected_status = expected_status_arg; |
| 9582 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9583 | uint8_t *exported = NULL; |
| 9584 | size_t exported_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9585 | PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9586 | size_t exported_length = SIZE_MAX; |
| 9587 | uint8_t *e_read_buffer = NULL; |
| 9588 | int is_default_public_exponent = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9589 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9590 | size_t e_read_length = SIZE_MAX; |
| 9591 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9592 | if (e_arg->len == 0 || |
| 9593 | (e_arg->len == 3 && |
| 9594 | 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] | 9595 | is_default_public_exponent = 1; |
| 9596 | e_read_size = 0; |
| 9597 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9598 | ASSERT_ALLOC(e_read_buffer, e_read_size); |
| 9599 | ASSERT_ALLOC(exported, exported_size); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9600 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9601 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9602 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9603 | psa_set_key_usage_flags(&attributes, usage); |
| 9604 | psa_set_key_algorithm(&attributes, alg); |
| 9605 | PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type, |
| 9606 | e_arg->x, e_arg->len)); |
| 9607 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9608 | |
| 9609 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9610 | TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status); |
| 9611 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9612 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9613 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9614 | |
| 9615 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9616 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 9617 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 9618 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 9619 | PSA_ASSERT(psa_get_key_domain_parameters(&attributes, |
| 9620 | e_read_buffer, e_read_size, |
| 9621 | &e_read_length)); |
| 9622 | if (is_default_public_exponent) { |
| 9623 | TEST_EQUAL(e_read_length, 0); |
| 9624 | } else { |
| 9625 | ASSERT_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len); |
| 9626 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9627 | |
| 9628 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9629 | if (!mbedtls_test_psa_exercise_key(key, usage, alg)) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9630 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9631 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9632 | |
| 9633 | /* Export the key and check the public exponent. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9634 | PSA_ASSERT(psa_export_public_key(key, |
| 9635 | exported, exported_size, |
| 9636 | &exported_length)); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9637 | { |
| 9638 | uint8_t *p = exported; |
| 9639 | uint8_t *end = exported + exported_length; |
| 9640 | size_t len; |
| 9641 | /* RSAPublicKey ::= SEQUENCE { |
| 9642 | * modulus INTEGER, -- n |
| 9643 | * publicExponent INTEGER } -- e |
| 9644 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9645 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 9646 | MBEDTLS_ASN1_SEQUENCE | |
| 9647 | MBEDTLS_ASN1_CONSTRUCTED)); |
| 9648 | TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1)); |
| 9649 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 9650 | MBEDTLS_ASN1_INTEGER)); |
| 9651 | if (len >= 1 && p[0] == 0) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9652 | ++p; |
| 9653 | --len; |
| 9654 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9655 | if (e_arg->len == 0) { |
| 9656 | TEST_EQUAL(len, 3); |
| 9657 | TEST_EQUAL(p[0], 1); |
| 9658 | TEST_EQUAL(p[1], 0); |
| 9659 | TEST_EQUAL(p[2], 1); |
| 9660 | } else { |
| 9661 | ASSERT_COMPARE(p, len, e_arg->x, e_arg->len); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9662 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9663 | } |
| 9664 | |
| 9665 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9666 | /* |
| 9667 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 9668 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 9669 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9670 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9671 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9672 | psa_destroy_key(key); |
| 9673 | PSA_DONE(); |
| 9674 | mbedtls_free(e_read_buffer); |
| 9675 | mbedtls_free(exported); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 9676 | } |
| 9677 | /* END_CASE */ |
| 9678 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9679 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9680 | void persistent_key_load_key_from_storage(data_t *data, |
| 9681 | int type_arg, int bits_arg, |
| 9682 | int usage_flags_arg, int alg_arg, |
| 9683 | int generation_method) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9684 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9685 | 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] | 9686 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9687 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9688 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9689 | psa_key_type_t type = type_arg; |
| 9690 | size_t bits = bits_arg; |
| 9691 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 9692 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9693 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9694 | unsigned char *first_export = NULL; |
| 9695 | unsigned char *second_export = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9696 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9697 | size_t first_exported_length; |
| 9698 | size_t second_exported_length; |
| 9699 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9700 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9701 | ASSERT_ALLOC(first_export, export_size); |
| 9702 | ASSERT_ALLOC(second_export, export_size); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9703 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9704 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9705 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9706 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9707 | psa_set_key_id(&attributes, key_id); |
| 9708 | psa_set_key_usage_flags(&attributes, usage_flags); |
| 9709 | psa_set_key_algorithm(&attributes, alg); |
| 9710 | psa_set_key_type(&attributes, type); |
| 9711 | psa_set_key_bits(&attributes, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9712 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9713 | switch (generation_method) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9714 | case IMPORT_KEY: |
| 9715 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9716 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, |
| 9717 | &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9718 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9719 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9720 | case GENERATE_KEY: |
| 9721 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9722 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9723 | break; |
| 9724 | |
| 9725 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 9726 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9727 | { |
| 9728 | /* Create base key */ |
| 9729 | psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256); |
| 9730 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9731 | psa_set_key_usage_flags(&base_attributes, |
| 9732 | PSA_KEY_USAGE_DERIVE); |
| 9733 | psa_set_key_algorithm(&base_attributes, derive_alg); |
| 9734 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9735 | PSA_ASSERT(psa_import_key(&base_attributes, |
| 9736 | data->x, data->len, |
| 9737 | &base_key)); |
| 9738 | /* Derive a key. */ |
| 9739 | PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg)); |
| 9740 | PSA_ASSERT(psa_key_derivation_input_key( |
| 9741 | &operation, |
| 9742 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key)); |
| 9743 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 9744 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
| 9745 | NULL, 0)); |
| 9746 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, |
| 9747 | &operation, |
| 9748 | &key)); |
| 9749 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
| 9750 | PSA_ASSERT(psa_destroy_key(base_key)); |
| 9751 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9752 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9753 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9754 | TEST_ASSUME(!"KDF not supported in this configuration"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9755 | #endif |
| 9756 | break; |
| 9757 | |
| 9758 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9759 | TEST_ASSERT(!"generation_method not implemented in test"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 9760 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9761 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9762 | psa_reset_key_attributes(&attributes); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9763 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9764 | /* Export the key if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9765 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9766 | PSA_ASSERT(psa_export_key(key, |
| 9767 | first_export, export_size, |
| 9768 | &first_exported_length)); |
| 9769 | if (generation_method == IMPORT_KEY) { |
| 9770 | ASSERT_COMPARE(data->x, data->len, |
| 9771 | first_export, first_exported_length); |
| 9772 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9773 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9774 | |
| 9775 | /* Shutdown and restart */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9776 | PSA_ASSERT(psa_purge_key(key)); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 9777 | PSA_DONE(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9778 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9779 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9780 | /* Check key slot still contains key data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9781 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 9782 | TEST_ASSERT(mbedtls_svc_key_id_equal( |
| 9783 | psa_get_key_id(&attributes), key_id)); |
| 9784 | TEST_EQUAL(psa_get_key_lifetime(&attributes), |
| 9785 | PSA_KEY_LIFETIME_PERSISTENT); |
| 9786 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 9787 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 9788 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 9789 | mbedtls_test_update_key_usage_flags(usage_flags)); |
| 9790 | TEST_EQUAL(psa_get_key_algorithm(&attributes), alg); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9791 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 9792 | /* Export the key again if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9793 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 9794 | PSA_ASSERT(psa_export_key(key, |
| 9795 | second_export, export_size, |
| 9796 | &second_exported_length)); |
| 9797 | ASSERT_COMPARE(first_export, first_exported_length, |
| 9798 | second_export, second_exported_length); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9799 | } |
| 9800 | |
| 9801 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9802 | if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 9803 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9804 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9805 | |
| 9806 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9807 | /* |
| 9808 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9809 | * thus reset them as required. |
| 9810 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9811 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9812 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9813 | mbedtls_free(first_export); |
| 9814 | mbedtls_free(second_export); |
| 9815 | psa_key_derivation_abort(&operation); |
| 9816 | psa_destroy_key(base_key); |
| 9817 | psa_destroy_key(key); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 9818 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 9819 | } |
| 9820 | /* END_CASE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9821 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 9822 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9823 | void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg, |
| 9824 | int primitive_arg, int hash_arg, int role_arg, |
| 9825 | int test_input, data_t *pw_data, |
| 9826 | int inj_err_type_arg, |
| 9827 | int expected_error_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9828 | { |
| 9829 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 9830 | psa_pake_operation_t operation = psa_pake_operation_init(); |
| 9831 | psa_algorithm_t alg = alg_arg; |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9832 | psa_pake_primitive_t primitive = primitive_arg; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 9833 | psa_key_type_t key_type_pw = key_type_pw_arg; |
| 9834 | psa_key_usage_t key_usage_pw = key_usage_pw_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9835 | psa_algorithm_t hash_alg = hash_arg; |
| 9836 | psa_pake_role_t role = role_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9837 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9838 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9839 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
| 9840 | psa_status_t expected_error = expected_error_arg; |
| 9841 | psa_status_t status; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9842 | unsigned char *output_buffer = NULL; |
| 9843 | size_t output_len = 0; |
| 9844 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9845 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9846 | |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9847 | size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9848 | PSA_PAKE_STEP_KEY_SHARE); |
| 9849 | ASSERT_ALLOC(output_buffer, buf_size); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9850 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9851 | if (pw_data->len > 0) { |
| 9852 | psa_set_key_usage_flags(&attributes, key_usage_pw); |
| 9853 | psa_set_key_algorithm(&attributes, alg); |
| 9854 | psa_set_key_type(&attributes, key_type_pw); |
| 9855 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 9856 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9857 | } |
| 9858 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9859 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 9860 | psa_pake_cs_set_primitive(&cipher_suite, primitive); |
| 9861 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9862 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9863 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 9864 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9865 | if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) { |
| 9866 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 9867 | expected_error); |
| 9868 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9869 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 9870 | expected_error); |
| 9871 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9872 | TEST_EQUAL(psa_pake_set_password_key(&operation, key), |
| 9873 | expected_error); |
| 9874 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9875 | TEST_EQUAL(psa_pake_set_role(&operation, role), |
| 9876 | expected_error); |
| 9877 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9878 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 9879 | NULL, 0, NULL), |
| 9880 | expected_error); |
| 9881 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 9882 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0), |
| 9883 | expected_error); |
| 9884 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9885 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9886 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9887 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9888 | status = psa_pake_setup(&operation, &cipher_suite); |
| 9889 | if (status != PSA_SUCCESS) { |
| 9890 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9891 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9892 | } |
| 9893 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9894 | if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) { |
| 9895 | TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite), |
| 9896 | expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9897 | goto exit; |
| 9898 | } |
| 9899 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9900 | status = psa_pake_set_role(&operation, role); |
| 9901 | if (status != PSA_SUCCESS) { |
| 9902 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9903 | goto exit; |
| 9904 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9905 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9906 | if (pw_data->len > 0) { |
| 9907 | status = psa_pake_set_password_key(&operation, key); |
| 9908 | if (status != PSA_SUCCESS) { |
| 9909 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9910 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9911 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9912 | } |
| 9913 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9914 | if (inj_err_type == INJECT_ERR_INVALID_USER) { |
| 9915 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 9916 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9917 | goto exit; |
| 9918 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9919 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9920 | if (inj_err_type == INJECT_ERR_INVALID_PEER) { |
| 9921 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 9922 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9923 | goto exit; |
| 9924 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9925 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9926 | if (inj_err_type == INJECT_ERR_SET_USER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9927 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9928 | TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4), |
| 9929 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9930 | goto exit; |
| 9931 | } |
| 9932 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9933 | if (inj_err_type == INJECT_ERR_SET_PEER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9934 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9935 | TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4), |
| 9936 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9937 | goto exit; |
| 9938 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 9939 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9940 | const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 9941 | PSA_PAKE_STEP_KEY_SHARE); |
| 9942 | const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 9943 | PSA_PAKE_STEP_ZK_PUBLIC); |
| 9944 | const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 9945 | PSA_PAKE_STEP_ZK_PROOF); |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 9946 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9947 | if (test_input) { |
| 9948 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 9949 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0), |
| 9950 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9951 | goto exit; |
| 9952 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 9953 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9954 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 9955 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 9956 | output_buffer, size_zk_proof), |
| 9957 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9958 | goto exit; |
| 9959 | } |
| 9960 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9961 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 9962 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 9963 | output_buffer, size_zk_proof), |
| 9964 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9965 | goto exit; |
| 9966 | } |
| 9967 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9968 | status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 9969 | output_buffer, size_key_share); |
| 9970 | if (status != PSA_SUCCESS) { |
| 9971 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9972 | goto exit; |
| 9973 | } |
| 9974 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9975 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 9976 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9977 | output_buffer, size_zk_public + 1), |
| 9978 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9979 | goto exit; |
| 9980 | } |
| 9981 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9982 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9983 | // 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] | 9984 | psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9985 | output_buffer, size_zk_public + 1); |
| 9986 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9987 | output_buffer, size_zk_public), |
| 9988 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9989 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 9990 | } |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9991 | } else { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9992 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 9993 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 9994 | NULL, 0, NULL), |
| 9995 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 9996 | goto exit; |
| 9997 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 9998 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9999 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 10000 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 10001 | output_buffer, buf_size, &output_len), |
| 10002 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10003 | goto exit; |
| 10004 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10005 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10006 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 10007 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10008 | output_buffer, buf_size, &output_len), |
| 10009 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10010 | goto exit; |
| 10011 | } |
| 10012 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10013 | status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10014 | output_buffer, buf_size, &output_len); |
| 10015 | if (status != PSA_SUCCESS) { |
| 10016 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10017 | goto exit; |
| 10018 | } |
| 10019 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10020 | TEST_ASSERT(output_len > 0); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10021 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10022 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 10023 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10024 | output_buffer, size_zk_public - 1, &output_len), |
| 10025 | PSA_ERROR_BUFFER_TOO_SMALL); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10026 | goto exit; |
| 10027 | } |
| 10028 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10029 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10030 | // 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] | 10031 | psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10032 | output_buffer, size_zk_public - 1, &output_len); |
| 10033 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10034 | output_buffer, buf_size, &output_len), |
| 10035 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10036 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10037 | } |
| 10038 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10039 | |
| 10040 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10041 | PSA_ASSERT(psa_destroy_key(key)); |
| 10042 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10043 | mbedtls_free(output_buffer); |
| 10044 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10045 | } |
| 10046 | /* END_CASE */ |
| 10047 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 10048 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10049 | void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg, |
| 10050 | int client_input_first, int inject_error, |
| 10051 | data_t *pw_data) |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10052 | { |
| 10053 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10054 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10055 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10056 | psa_algorithm_t alg = alg_arg; |
| 10057 | psa_algorithm_t hash_alg = hash_arg; |
| 10058 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10059 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10060 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10061 | PSA_INIT(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10062 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10063 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10064 | psa_set_key_algorithm(&attributes, alg); |
| 10065 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10066 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10067 | &key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10068 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10069 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10070 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10071 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10072 | |
| 10073 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10074 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10075 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10076 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10077 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10078 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10079 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10080 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10081 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10082 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10083 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10084 | client_input_first, 1, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10085 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10086 | if (inject_error == 1 || inject_error == 2) { |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10087 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10088 | } |
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 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10091 | client_input_first, 2, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10092 | |
| 10093 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10094 | psa_destroy_key(key); |
| 10095 | psa_pake_abort(&server); |
| 10096 | psa_pake_abort(&client); |
| 10097 | PSA_DONE(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10098 | } |
| 10099 | /* END_CASE */ |
| 10100 | |
| 10101 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10102 | void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg, |
| 10103 | int derive_alg_arg, data_t *pw_data, |
| 10104 | int client_input_first, int inj_err_type_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10105 | { |
| 10106 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10107 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10108 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10109 | psa_algorithm_t alg = alg_arg; |
| 10110 | psa_algorithm_t hash_alg = hash_arg; |
| 10111 | psa_algorithm_t derive_alg = derive_alg_arg; |
| 10112 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10113 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10114 | psa_key_derivation_operation_t server_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10115 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10116 | psa_key_derivation_operation_t client_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10117 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10118 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10119 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10120 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10121 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10122 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10123 | psa_set_key_algorithm(&attributes, alg); |
| 10124 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10125 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10126 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10127 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10128 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10129 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10130 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10131 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10132 | /* Get shared key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10133 | PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg)); |
| 10134 | PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10135 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10136 | if (PSA_ALG_IS_TLS12_PRF(derive_alg) || |
| 10137 | PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) { |
| 10138 | PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive, |
| 10139 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10140 | (const uint8_t *) "", 0)); |
| 10141 | PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive, |
| 10142 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10143 | (const uint8_t *) "", 0)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10144 | } |
| 10145 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10146 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10147 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10149 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10150 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10151 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10152 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10153 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10154 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10155 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) { |
| 10156 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10157 | PSA_ERROR_BAD_STATE); |
| 10158 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10159 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10160 | goto exit; |
| 10161 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10162 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10163 | /* First round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10164 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10165 | client_input_first, 1, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10166 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10167 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) { |
| 10168 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10169 | PSA_ERROR_BAD_STATE); |
| 10170 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10171 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10172 | goto exit; |
| 10173 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10174 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10175 | /* Second round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10176 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10177 | client_input_first, 2, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10178 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10179 | PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive)); |
| 10180 | PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10181 | |
| 10182 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10183 | psa_key_derivation_abort(&server_derive); |
| 10184 | psa_key_derivation_abort(&client_derive); |
| 10185 | psa_destroy_key(key); |
| 10186 | psa_pake_abort(&server); |
| 10187 | psa_pake_abort(&client); |
| 10188 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10189 | } |
| 10190 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10191 | |
| 10192 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10193 | void ecjpake_size_macros() |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10194 | { |
| 10195 | const psa_algorithm_t alg = PSA_ALG_JPAKE; |
| 10196 | const size_t bits = 256; |
| 10197 | const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10198 | 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] | 10199 | 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] | 10200 | PSA_ECC_FAMILY_SECP_R1); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10201 | |
| 10202 | // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types |
| 10203 | /* 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] | 10204 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10205 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
| 10206 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10207 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10208 | /* The output for ZK_PROOF is the same bitsize as the curve */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10209 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10210 | PSA_BITS_TO_BYTES(bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10211 | |
| 10212 | /* Input sizes are the same as output sizes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10213 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10214 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE)); |
| 10215 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10216 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC)); |
| 10217 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10218 | 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] | 10219 | |
| 10220 | /* These inequalities will always hold even when other PAKEs are added */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10221 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10222 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10223 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10224 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10225 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10226 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10227 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10228 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10229 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10230 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10231 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10232 | PSA_PAKE_INPUT_MAX_SIZE); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10233 | } |
| 10234 | /* END_CASE */ |